[imgur] fix image url retrieval
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
# Copyright 2015 Mike Fährmann
|
# Copyright 2015, 2016 Mike Fährmann
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify
|
# 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
|
# it under the terms of the GNU General Public License version 2 as
|
||||||
@@ -9,20 +9,18 @@
|
|||||||
"""Extract images from albums at https://imgur.com/"""
|
"""Extract images from albums at https://imgur.com/"""
|
||||||
|
|
||||||
from .common import Extractor, Message
|
from .common import Extractor, Message
|
||||||
from .. import text
|
from .. import text, exception
|
||||||
from urllib.parse import urljoin
|
|
||||||
import os.path
|
|
||||||
|
|
||||||
class ImgurAlbumExtractor(Extractor):
|
class ImgurAlbumExtractor(Extractor):
|
||||||
"""Extractor for image albums from imgur.com"""
|
"""Extractor for image albums from imgur.com"""
|
||||||
category = "imgur"
|
category = "imgur"
|
||||||
subcategory = "album"
|
subcategory = "album"
|
||||||
directory_fmt = ["{category}", "{album-key} - {title}"]
|
directory_fmt = ["{category}", "{album-key} - {title}"]
|
||||||
filename_fmt = "{category}_{album-key}_{num:>03}_{name}.{extension}"
|
filename_fmt = "{category}_{album-key}_{num:>03}_{hash}{ext}"
|
||||||
pattern = [r"(?:https?://)?(?:www\.)?imgur\.com/(?:a|gallery)/([^/?&#]+)"]
|
pattern = [r"(?:https?://)?(?:www\.)?imgur\.com/(?:a|gallery)/([^/?&#]+)"]
|
||||||
test = [("https://imgur.com/a/TcBmP", {
|
test = [("https://imgur.com/a/TcBmP", {
|
||||||
"url": "ce3552f550a5b5316bd9c7ae02e21e39f30c0563",
|
"url": "ce3552f550a5b5316bd9c7ae02e21e39f30c0563",
|
||||||
"keyword": "c76bbf86f8f114cdaadab396c0ea4acf47aa44eb",
|
"keyword": "8301572a22c139b5e0704ccaf2bcf49a111e2384",
|
||||||
})]
|
})]
|
||||||
|
|
||||||
def __init__(self, match):
|
def __init__(self, match):
|
||||||
@@ -30,15 +28,15 @@ class ImgurAlbumExtractor(Extractor):
|
|||||||
self.album = match.group(1)
|
self.album = match.group(1)
|
||||||
|
|
||||||
def items(self):
|
def items(self):
|
||||||
|
imgs = self.get_images()
|
||||||
data = self.get_job_metadata()
|
data = self.get_job_metadata()
|
||||||
yield Message.Version, 1
|
yield Message.Version, 1
|
||||||
yield Message.Directory, data
|
yield Message.Directory, data
|
||||||
for num, url in enumerate(self.get_image_urls(), 1):
|
for num, image in enumerate(imgs, 1):
|
||||||
name, ext = os.path.splitext(url[20:])
|
image["num"] = num
|
||||||
data["num"] = num
|
image.update(data)
|
||||||
data["name"] = name
|
url = "https://i.imgur.com/" + image["hash"] + image["ext"]
|
||||||
data["extension"] = ext[1:]
|
yield Message.Url, url, image
|
||||||
yield Message.Url, url, data
|
|
||||||
|
|
||||||
def get_job_metadata(self):
|
def get_job_metadata(self):
|
||||||
"""Collect metadata for extractor-job"""
|
"""Collect metadata for extractor-job"""
|
||||||
@@ -46,24 +44,15 @@ class ImgurAlbumExtractor(Extractor):
|
|||||||
data = text.extract_all(page, (
|
data = text.extract_all(page, (
|
||||||
('title', '<meta property="og:title" content="', '"'),
|
('title', '<meta property="og:title" content="', '"'),
|
||||||
('count', '"num_images":"', '"'),
|
('count', '"num_images":"', '"'),
|
||||||
('date' , '"datetime":"', ' '),
|
|
||||||
('time' , '', '"'),
|
|
||||||
), values={"album-key": self.album})[0]
|
), values={"album-key": self.album})[0]
|
||||||
data["title"] = text.unescape(data["title"])
|
data["title"] = text.unescape(data["title"])
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def get_image_urls(self):
|
def get_images(self):
|
||||||
"""Yield urls of all images in this album"""
|
"""Return a list of all images in this album"""
|
||||||
num = 0
|
url = ("https://imgur.com/ajaxalbums/getimages/"
|
||||||
while True:
|
+ self.album + "/hit.json")
|
||||||
url = "https://imgur.com/a/{}/all/page/{}?scrolled".format(self.album, num)
|
data = self.request(url).json()["data"]
|
||||||
page = self.request(url).text
|
if not data:
|
||||||
pos = begin = text.extract(page, '<div class="posts">', '')[1]
|
raise exception.NotFoundError("album")
|
||||||
while True:
|
return data["images"]
|
||||||
url, pos = text.extract(page, '<a href="', '"', pos)
|
|
||||||
if not url:
|
|
||||||
break
|
|
||||||
yield urljoin("https:", url)
|
|
||||||
if pos == begin:
|
|
||||||
return
|
|
||||||
num += 1
|
|
||||||
|
|||||||
Reference in New Issue
Block a user