You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gallery-dl/gallery_dl/extractor/imgur.py

66 lines
2.2 KiB

# -*- 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):
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)/([^/?&#]+)"]
def __init__(self, match):
Extractor.__init__(self)
self.album = match.group(1)
def items(self):
9 years ago
data = self.get_job_metadata()
yield Message.Version, 1
yield Message.Directory, data
9 years ago
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
9 years ago
def get_job_metadata(self):
"""Collect metadata for extractor-job"""
9 years ago
page = self.request("https://imgur.com/a/" + self.album).text
data = {
"category": self.category,
"album-key": self.album,
}
9 years ago
return text.extract_all(page, (
('title', '<meta property="og:title" content="', '"'),
('count', '"num_images":"', '"'),
('date' , '"datetime":"', ' '),
('time' , '', '"'),
9 years ago
), values=data)[0]
9 years ago
def get_image_urls(self):
"""Yield urls of all images in this album"""
num = 0
while True:
9 years ago
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
num += 1