[imagebam] update to new extractor interface

This commit is contained in:
Mike Fährmann
2015-04-11 14:05:36 +02:00
parent 758fe00441
commit e41768d969

View File

@@ -1,20 +1,48 @@
from .common import AsyncExtractor # -*- coding: utf-8 -*-
class Extractor(AsyncExtractor): # Copyright 2014, 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 galleries at http://www.imagebam.com/"""
from .common import AsynchronousExtractor
from .common import Message
from .common import filename_from_url
info = {
"category": "imagebam",
"extractor": "ImagebamExtractor",
"directory": ["{category}", "{title} - {key}"],
"filename": "{num:>03}-{name}",
"pattern": [
r"(?:https?://)?(?:www\.)?imagebam\.com/(gallery)/([^/]+).*",
],
}
class ImagebamExtractor(AsynchronousExtractor):
url_base = "http://www.imagebam.com"
def __init__(self, match, config): def __init__(self, match, config):
AsyncExtractor.__init__(self, config) AsynchronousExtractor.__init__(self, config)
title, key = self.get_title(match) self.match = match
self.category = "imagebam" self.num = 0
self.directory = title + " - " + key self.metadata = {}
def images(self): def items(self):
next_url = self.url self.num = 0
num = 1 self.metadata = self.get_job_metadata()
yield Message.Version, 1
yield Message.Directory, self.metadata
next_url = self.metadata["first-url"]
done = False done = False
while not done: while not done:
# get current page # get current page
text = self.request("http://www.imagebam.com" + next_url).text text = self.request(self.url_base + next_url).text
# get url for next page # get url for next page
next_url, pos = self.extract(text, "<a class='buttonblue' href='", "'") next_url, pos = self.extract(text, "<a class='buttonblue' href='", "'")
@@ -24,24 +52,30 @@ class Extractor(AsyncExtractor):
done = True done = True
# get image url # get image url
img_url , pos = self.extract(text, 'onclick="scale(this);" src="', '"', pos) img_url, pos = self.extract(text, 'onclick="scale(this);" src="', '"', pos)
# extract filename from image url yield Message.Url, img_url, self.get_file_metadata(img_url)
name = img_url[img_url.rindex("/")+1:]
yield img_url, "{:>03}-{}".format(num, name) def get_job_metadata(self):
num += 1 """Collect metadata for extractor-job"""
gallery_key = self.match.group(2)
text = self.request(self.url_base + "/gallery/" + gallery_key).text
_ , pos = self.extract(text, "<img src='/img/icons/photos.png'", "")
title, pos = self.extract(text, "'> ", " <", pos)
count, pos = self.extract(text, "'>", " images", pos)
url , pos = self.extract(text, "<a href='http://www.imagebam.com", "'", pos)
return {
"category": info["category"],
"key": gallery_key,
"title": title,
"count": count,
"first-url": url,
}
def get_title(self, match): def get_file_metadata(self, url):
if match.group(1) == "image": """Collect metadata for a downloadable file"""
text = self.request(match.group(0)).text self.num += 1
gallery_url, _ = self.extract(text, "class='gallery_title'><a href='", "'") data = self.metadata.copy()
gallery_key = gallery_url.split("/")[-2] data["num"] = self.num
else: data["name"] = filename_from_url(url)
gallery_key = match.group(2) return data
text = self.request("http://www.imagebam.com/gallery/" + gallery_key).text
_ , pos = self.extract(text, "<img src='/img/icons/photos.png'", "")
title , pos = self.extract(text, "'> ", " <", pos)
self.url, pos = self.extract(text, "<a href='http://www.imagebam.com", "'", pos)
return title, gallery_key