# -*- coding: utf-8 -*- # 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, Message from .. import text class ImagebamExtractor(AsynchronousExtractor): category = "imagebam" directory_fmt = ["{category}", "{title} - {gallery-key}"] filename_fmt = "{num:>03}-{filename}" pattern = [r"(?:https?://)?(?:www\.)?imagebam\.com/gallery/([^/]+).*"] url_base = "http://www.imagebam.com" def __init__(self, match): AsynchronousExtractor.__init__(self) self.gkey = match.group(1) def items(self): data = self.get_job_metadata() data["num"] = 0 yield Message.Version, 1 yield Message.Directory, data for image_url, image_id in self.get_images(data["first-url"]): data["id"] = image_id data["num"] += 1 text.nameext_from_url(image_url, data) yield Message.Url, image_url, data.copy() def get_job_metadata(self): """Collect metadata for extractor-job""" response = self.request(self.url_base + "/gallery/" + self.gkey) response.encoding = "utf-8" page = response.text data = { "category": self.category, "gallery-key": self.gkey, } data, _ = text.extract_all(page, ( (None , " ", " <"), ("count" , "'>", " images"), ("first-url", "', '') if pos == 0: done = True else: url, pos = text.extract(page, ' href="', '"', pos-70) image_id , pos = text.extract(page, '