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
|
|
|
|
|
|
|
|
class ImgurExtractor(Extractor):
|
|
|
|
|
2015-11-21 04:26:30 +01:00
|
|
|
category = "imgur"
|
|
|
|
directory_fmt = ["{category}", "{album-key} - {title}"]
|
|
|
|
filename_fmt = "{category}_{album-key}_{num:>03}_{name}.{extension}"
|
|
|
|
pattern = [r"(?:https?://)?(?:www\.)?imgur\.com/(?:a|gallery)/([^/?&#]+)"]
|
2015-12-13 04:36:44 +01:00
|
|
|
test = [("https://imgur.com/a/TcBmP", {
|
|
|
|
"url": "ce3552f550a5b5316bd9c7ae02e21e39f30c0563",
|
|
|
|
"keyword": "5c96eee4df5938ed37f1f95f5c4ef64444bddeb4",
|
|
|
|
})]
|
2015-11-21 04:26:30 +01:00
|
|
|
|
2015-10-11 16:22:38 +02:00
|
|
|
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-11-21 04:26:30 +01:00
|
|
|
"category": self.category,
|
2015-10-11 16:22:38 +02:00
|
|
|
"album-key": self.album,
|
|
|
|
}
|
2015-11-12 00:18:56 +01:00
|
|
|
return text.extract_all(page, (
|
|
|
|
('title', '<meta property="og:title" content="', '"'),
|
2015-12-13 03:35:55 +01:00
|
|
|
('count', '"num_images":"', '"'),
|
|
|
|
('date' , '"datetime":"', ' '),
|
|
|
|
('time' , '', '"'),
|
2015-11-12 00:18:56 +01:00
|
|
|
), 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
|