# -*- coding: utf-8 -*- # Copyright 2014-2016 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 http://www.imagebam.com/""" from .common import Extractor, AsynchronousExtractor, Message from .. import text class ImagebamGalleryExtractor(AsynchronousExtractor): """Extractor for image galleries from imagebam.com""" category = "imagebam" subcategory = "gallery" directory_fmt = ["{category}", "{title} - {gallery-key}"] filename_fmt = "{num:>03}-{filename}" pattern = [r"(?:https?://)?(?:www\.)?imagebam\.com/gallery/([^/]+).*"] test = [("http://www.imagebam.com/gallery/adz2y0f9574bjpmonaismyrhtjgvey4o", { "url": "d7a4483b6d5ebba81950a349aad58ae034c60eda", "keyword": "9f54ab808d77f2517444411dfbf8686189c20b43", "content": "596e6bfa157f2c7169805d50075c2986549973a8", })] 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""" url = self.url_base + "/gallery/" + self.gkey page = self.request(url, encoding="utf-8").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, '