2015-04-11 16:12:28 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-11-20 20:08:55 +01:00
|
|
|
|
2017-09-10 22:20:47 +02:00
|
|
|
# Copyright 2014-2017 Mike Fährmann
|
2015-04-11 16:12:28 +02:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2016-10-12 15:21:38 +02:00
|
|
|
"""Extract images from galleries at https://imgbox.com/"""
|
2015-04-11 16:12:28 +02:00
|
|
|
|
2015-12-05 19:30:27 +01:00
|
|
|
from .common import Extractor, AsynchronousExtractor, Message
|
2016-11-07 08:39:03 +01:00
|
|
|
from .. import text, exception
|
2015-04-11 16:12:28 +02:00
|
|
|
import re
|
|
|
|
|
2017-02-01 00:53:19 +01:00
|
|
|
|
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"
|
2017-09-10 22:20:47 +02:00
|
|
|
directory_fmt = ["{category}", "{title} - {gallery_key}"]
|
2016-10-12 15:21:38 +02:00
|
|
|
filename_fmt = "{num:>03}-{filename}"
|
2015-12-05 19:30:27 +01:00
|
|
|
pattern = [r"(?:https?://)?(?:www\.)?imgbox\.com/g/([A-Za-z0-9]{10})"]
|
2016-12-31 00:51:06 +01:00
|
|
|
test = [
|
|
|
|
("https://imgbox.com/g/JaX5V5HX7g", {
|
|
|
|
"url": "6eafdeebaf0774238dddc9227e2ba315e40e9b7c",
|
2017-09-10 22:20:47 +02:00
|
|
|
"keyword": "abe510221e1dc8c804296be25adf1498fb93f892",
|
2016-12-31 00:51:06 +01:00
|
|
|
"content": "d20307dc8511ac24d688859c55abf2e2cc2dd3cc",
|
|
|
|
}),
|
|
|
|
("https://imgbox.com/g/JaX5V5HX7h", {
|
|
|
|
"exception": exception.NotFoundError,
|
|
|
|
}),
|
|
|
|
]
|
2016-10-12 15:21:38 +02:00
|
|
|
url_base = "https://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
|
2016-11-07 08:39:03 +01:00
|
|
|
if "The specified gallery could not be found." in page:
|
|
|
|
raise exception.NotFoundError("gallery")
|
2015-04-11 16:12:28 +02:00
|
|
|
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)
|
2016-11-07 08:39:03 +01:00
|
|
|
if data["filename"]:
|
|
|
|
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]
|
2017-08-21 18:29:50 +02:00
|
|
|
title, _, count = title.rpartition(" - ")
|
2015-04-11 16:12:28 +02:00
|
|
|
return {
|
2017-09-10 22:20:47 +02:00
|
|
|
"gallery_key": self.key,
|
2017-08-21 18:29:50 +02:00
|
|
|
"title": text.unescape(title),
|
|
|
|
"count": count[:-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"', ''),
|
2017-09-10 22:20:47 +02:00
|
|
|
("image_key", 'alt="', '"'),
|
2016-09-13 08:00:08 +02:00
|
|
|
("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"""
|
2016-10-12 15:21:38 +02:00
|
|
|
base = "https://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
|
|
|
pattern = [r"(?:https?://)?(?:www\.)?imgbox\.com/([A-Za-z0-9]{8})"]
|
2016-12-31 00:51:06 +01:00
|
|
|
test = [
|
|
|
|
("https://imgbox.com/qHhw7lpG", {
|
|
|
|
"url": "b9556dc307edf88e016fbced6d354702bc236070",
|
2017-09-10 22:20:47 +02:00
|
|
|
"keyword": "a5cdcdf6e784bb186ed65a0cd7978ae2d0e17a12",
|
2016-12-31 00:51:06 +01:00
|
|
|
"content": "0c8768055e4e20e7c7259608b67799171b691140",
|
|
|
|
}),
|
|
|
|
("https://imgbox.com/qHhw7lpH", {
|
|
|
|
"exception": exception.NotFoundError,
|
|
|
|
}),
|
|
|
|
]
|
2015-12-05 19:30:27 +01:00
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
Extractor.__init__(self)
|
|
|
|
self.key = match.group(1)
|
|
|
|
|
|
|
|
def items(self):
|
2016-10-12 15:21:38 +02:00
|
|
|
page = self.request("https://imgbox.com/" + self.key).text
|
2016-11-07 08:39:03 +01:00
|
|
|
url , pos = text.extract(page, 'src="https://i.', '"')
|
|
|
|
if not url:
|
|
|
|
raise exception.NotFoundError("image")
|
2015-12-05 19:30:27 +01:00
|
|
|
filename, pos = text.extract(page, ' title="', '"', pos)
|
2017-09-10 22:20:47 +02:00
|
|
|
data = text.nameext_from_url(filename, {"image_key": self.key})
|
2015-12-05 19:30:27 +01:00
|
|
|
yield Message.Version, 1
|
|
|
|
yield Message.Directory, data
|
2016-10-12 15:21:38 +02:00
|
|
|
yield Message.Url, "https://i." + url, data
|