1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-23 11:12:40 +01:00
gallery-dl/gallery_dl/extractor/imgbox.py

111 lines
4.0 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2014-11-20 20:08:55 +01:00
# Copyright 2014-2017 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.
2016-10-12 15:21:38 +02:00
"""Extract images from galleries at https://imgbox.com/"""
2015-12-05 19:30:27 +01:00
from .common import Extractor, AsynchronousExtractor, Message
from .. import text, exception
import re
2017-02-01 00:53:19 +01:00
2015-12-05 19:30:27 +01:00
class ImgboxGalleryExtractor(AsynchronousExtractor):
"""Extractor for image galleries from imgbox.com"""
2015-11-21 04:26:30 +01:00
category = "imgbox"
subcategory = "gallery"
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",
"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
def __init__(self, match):
AsynchronousExtractor.__init__(self)
self.key = match.group(1)
self.metadata = {}
def items(self):
page = self.request(self.url_base + "/g/" + self.key).text
if "The specified gallery could not be found." in page:
raise exception.NotFoundError("gallery")
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)
if data["filename"]:
data = text.nameext_from_url(data["filename"], data)
yield Message.Url, self.get_file_url(imgpage), data
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]
title, _, count = title.rpartition(" - ")
return {
"gallery_key": self.key,
"title": text.unescape(title),
"count": count[:-7],
}
2015-10-03 15:43:02 +02:00
def get_file_metadata(self, page):
"""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> &nbsp; ', ' of '),
2016-09-13 08:00:08 +02:00
(None , 'class="image-container"', ''),
("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-11-21 04:26:30 +01:00
@staticmethod
def get_file_url(page):
"""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]
return base + path
2015-12-05 19:30:27 +01:00
class ImgboxImageExtractor(Extractor):
"""Extractor for single images from imgbox.com"""
2015-12-05 19:30:27 +01:00
category = "imgbox"
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",
"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
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)
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