2015-10-11 16:22:38 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright 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 albums at https://imgur.com/"""
|
|
|
|
|
|
|
|
from .common import Extractor, Message
|
|
|
|
from .. import text
|
|
|
|
import os.path
|
|
|
|
|
|
|
|
info = {
|
|
|
|
"category": "imgur",
|
|
|
|
"extractor": "ImgurExtractor",
|
|
|
|
"directory": ["{category}", "{album-key} - {title}"],
|
|
|
|
"filename": "{category}_{album-key}_{num:>03}_{name}.{extension}",
|
|
|
|
"pattern": [
|
|
|
|
r"(?:https?://)?(?:www\.)?imgur\.com/(?:a|gallery)/([^/?&#]+)",
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
class ImgurExtractor(Extractor):
|
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
Extractor.__init__(self)
|
|
|
|
self.album = match.group(1)
|
|
|
|
|
|
|
|
def items(self):
|
2015-11-12 00:18:56 +01:00
|
|
|
data = self.get_job_metadata()
|
2015-10-11 16:22:38 +02:00
|
|
|
yield Message.Version, 1
|
|
|
|
yield Message.Directory, data
|
2015-11-12 00:18:56 +01:00
|
|
|
for num, url in enumerate(self.get_image_urls(), 1):
|
|
|
|
name, ext = os.path.splitext(url[20:])
|
|
|
|
data["num"] = num
|
|
|
|
data["name"] = name
|
|
|
|
data["extension"] = ext[1:]
|
|
|
|
yield Message.Url, url, data
|
2015-10-11 16:22:38 +02:00
|
|
|
|
2015-11-12 00:18:56 +01:00
|
|
|
def get_job_metadata(self):
|
2015-10-11 16:22:38 +02:00
|
|
|
"""Collect metadata for extractor-job"""
|
2015-11-12 00:18:56 +01:00
|
|
|
page = self.request("https://imgur.com/a/" + self.album).text
|
|
|
|
data = {
|
2015-10-11 16:22:38 +02:00
|
|
|
"category": info["category"],
|
|
|
|
"album-key": self.album,
|
|
|
|
}
|
2015-11-12 00:18:56 +01:00
|
|
|
return text.extract_all(page, (
|
|
|
|
('title', '<meta property="og:title" content="', '"'),
|
|
|
|
('date' , '"create_datetime":"', '"'),
|
|
|
|
('count', '"num_images":', ','),
|
|
|
|
), values=data)[0]
|
2015-10-11 16:22:38 +02:00
|
|
|
|
2015-11-12 00:18:56 +01:00
|
|
|
def get_image_urls(self):
|
|
|
|
"""Yield urls of all images in this album"""
|
2015-10-11 16:22:38 +02:00
|
|
|
num = 0
|
|
|
|
while True:
|
2015-11-12 00:18:56 +01:00
|
|
|
url = "https://imgur.com/a/{}/all/page/{}?scrolled".format(self.album, num)
|
|
|
|
page = self.request(url).text
|
|
|
|
pos = begin = text.extract(page, '<div class="posts">', '')[1]
|
|
|
|
while True:
|
|
|
|
url, pos = text.extract(page, '<a href="', '"', pos)
|
|
|
|
if not url:
|
|
|
|
break
|
|
|
|
yield "https:" + url
|
|
|
|
if pos == begin:
|
|
|
|
return
|
2015-10-11 16:22:38 +02:00
|
|
|
num += 1
|