2015-04-11 16:12:28 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-11-20 20:08:55 +01:00
|
|
|
|
2015-04-11 16:12:28 +02:00
|
|
|
# Copyright 2014, 2015 Mike Fährmann
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License version 2 as
|
|
|
|
# published by the Free Software Foundation.
|
|
|
|
|
|
|
|
"""Extract images from galleries at http://imgbox.com/"""
|
|
|
|
|
2015-12-05 19:30:27 +01:00
|
|
|
from .common import Extractor, AsynchronousExtractor, Message
|
2015-10-03 15:43:02 +02:00
|
|
|
from .. import text
|
2015-04-11 16:12:28 +02:00
|
|
|
import re
|
|
|
|
|
2015-12-05 19:30:27 +01:00
|
|
|
class ImgboxGalleryExtractor(AsynchronousExtractor):
|
2016-09-12 10:20:57 +02:00
|
|
|
"""Extractor for image galleries from imgbox.com"""
|
2015-11-21 04:26:30 +01:00
|
|
|
category = "imgbox"
|
2016-09-12 10:20:57 +02:00
|
|
|
subcategory = "gallery"
|
2015-11-21 04:26:30 +01:00
|
|
|
directory_fmt = ["{category}", "{title} - {gallery-key}"]
|
|
|
|
filename_fmt = "{num:>03}-{name}"
|
2015-12-05 19:30:27 +01:00
|
|
|
pattern = [r"(?:https?://)?(?:www\.)?imgbox\.com/g/([A-Za-z0-9]{10})"]
|
2016-09-13 08:00:08 +02:00
|
|
|
test = [("http://imgbox.com/g/JaX5V5HX7g", {
|
|
|
|
"url": "c7c3466dde31d4308833816961104c7d1100368d",
|
|
|
|
"keyword": "23deb783d3afee090f61472b495e797c8f262b93",
|
|
|
|
"content": "d20307dc8511ac24d688859c55abf2e2cc2dd3cc",
|
|
|
|
})]
|
2015-04-11 16:12:28 +02:00
|
|
|
url_base = "http://imgbox.com"
|
2014-11-20 20:08:55 +01:00
|
|
|
|
2015-10-05 15:35:48 +02:00
|
|
|
def __init__(self, match):
|
|
|
|
AsynchronousExtractor.__init__(self)
|
2015-04-11 16:12:28 +02:00
|
|
|
self.key = match.group(1)
|
|
|
|
self.metadata = {}
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
page = self.request(self.url_base + "/g/" + self.key).text
|
|
|
|
self.metadata = self.get_job_metadata(page)
|
|
|
|
yield Message.Version, 1
|
|
|
|
yield Message.Directory, self.metadata
|
|
|
|
for match in re.finditer(r'<a href="([^"]+)"><img alt="', page):
|
2015-10-03 15:43:02 +02:00
|
|
|
imgpage = self.request(self.url_base + match.group(1)).text
|
2016-09-13 08:00:08 +02:00
|
|
|
data = self.get_file_metadata(imgpage)
|
|
|
|
data = text.nameext_from_url(data["filename"], data)
|
|
|
|
yield Message.Url, self.get_file_url(imgpage), data
|
2015-04-11 16:12:28 +02:00
|
|
|
|
|
|
|
def get_job_metadata(self, page):
|
|
|
|
"""Collect metadata for extractor-job"""
|
2016-09-13 08:00:08 +02:00
|
|
|
title = text.extract(page, "<h1>", "</h1>")[0]
|
|
|
|
parts = title.rsplit(" - ", maxsplit=1)
|
2015-04-11 16:12:28 +02:00
|
|
|
return {
|
2015-11-21 04:26:30 +01:00
|
|
|
"category": self.category,
|
2015-04-11 16:12:28 +02:00
|
|
|
"gallery-key": self.key,
|
2016-09-13 08:00:08 +02:00
|
|
|
"title": text.unescape(parts[0]),
|
|
|
|
"count": parts[1][:-7],
|
2015-04-11 16:12:28 +02:00
|
|
|
}
|
|
|
|
|
2015-10-03 15:43:02 +02:00
|
|
|
def get_file_metadata(self, page):
|
2015-04-11 16:12:28 +02:00
|
|
|
"""Collect metadata for a downloadable file"""
|
2015-12-05 19:30:27 +01:00
|
|
|
return text.extract_all(page, (
|
2015-11-03 00:19:04 +01:00
|
|
|
("num" , '</a> ', ' of '),
|
2016-09-13 08:00:08 +02:00
|
|
|
(None , 'class="image-container"', ''),
|
|
|
|
("image-key", 'alt="', '"'),
|
|
|
|
("filename" , ' title="', '"'),
|
2015-12-05 19:30:27 +01:00
|
|
|
), values=self.metadata.copy())[0]
|
2015-04-11 16:12:28 +02:00
|
|
|
|
2015-11-21 04:26:30 +01:00
|
|
|
@staticmethod
|
|
|
|
def get_file_url(page):
|
2015-04-11 16:12:28 +02:00
|
|
|
"""Extract download-url"""
|
|
|
|
base = "http://i.imgbox.com/"
|
2015-12-05 19:30:27 +01:00
|
|
|
path = text.extract(page, base, '"')[0]
|
2015-04-11 16:12:28 +02:00
|
|
|
return base + path
|
2015-12-05 19:30:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ImgboxImageExtractor(Extractor):
|
2016-09-12 10:20:57 +02:00
|
|
|
"""Extractor for single images from imgbox.com"""
|
2015-12-05 19:30:27 +01:00
|
|
|
category = "imgbox"
|
2016-09-12 10:20:57 +02:00
|
|
|
subcategory = "image"
|
2015-12-05 19:30:27 +01:00
|
|
|
directory_fmt = ["{category}"]
|
|
|
|
filename_fmt = "{filename}"
|
|
|
|
pattern = [r"(?:https?://)?(?:www\.)?imgbox\.com/([A-Za-z0-9]{8})"]
|
2016-09-13 08:00:08 +02:00
|
|
|
test = [("http://imgbox.com/qHhw7lpG", {
|
|
|
|
"url": "d96990ea12223895287d139695077b70dfa0abe4",
|
|
|
|
"keyword": "c5e87be93fec3122151edf85b6424d1871279590",
|
|
|
|
"content": "0c8768055e4e20e7c7259608b67799171b691140",
|
|
|
|
})]
|
2015-12-05 19:30:27 +01:00
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
Extractor.__init__(self)
|
|
|
|
self.key = match.group(1)
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
page = self.request("http://imgbox.com/" + self.key).text
|
|
|
|
url , pos = text.extract(page, 'src="http://i.', '"')
|
|
|
|
filename, pos = text.extract(page, ' title="', '"', pos)
|
|
|
|
data = {"category": self.category, "image-key": self.key}
|
|
|
|
text.nameext_from_url(filename, data)
|
|
|
|
yield Message.Version, 1
|
|
|
|
yield Message.Directory, data
|
|
|
|
yield Message.Url, "http://i." + url, data
|