2015-04-11 14:05:36 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-11-20 21:27:57 +01:00
|
|
|
|
2016-09-02 08:25:29 +02:00
|
|
|
# Copyright 2014-2016 Mike Fährmann
|
2015-04-11 14:05:36 +02:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2016-09-02 08:25:29 +02:00
|
|
|
"""Extract images from http://www.imagebam.com/"""
|
2015-04-11 14:05:36 +02:00
|
|
|
|
2016-09-02 08:25:29 +02:00
|
|
|
from .common import Extractor, AsynchronousExtractor, Message
|
2015-10-03 15:43:02 +02:00
|
|
|
from .. import text
|
2015-04-11 14:05:36 +02:00
|
|
|
|
2016-09-02 08:25:29 +02:00
|
|
|
class ImagebamGalleryExtractor(AsynchronousExtractor):
|
2016-09-12 10:20:57 +02:00
|
|
|
"""Extractor for image galleries from imagebam.com"""
|
2015-11-21 04:26:30 +01:00
|
|
|
category = "imagebam"
|
2016-09-02 08:25:29 +02:00
|
|
|
subcategory = "gallery"
|
2015-11-21 04:26:30 +01:00
|
|
|
directory_fmt = ["{category}", "{title} - {gallery-key}"]
|
|
|
|
filename_fmt = "{num:>03}-{filename}"
|
|
|
|
pattern = [r"(?:https?://)?(?:www\.)?imagebam\.com/gallery/([^/]+).*"]
|
2016-09-02 08:25:29 +02:00
|
|
|
test = [("http://www.imagebam.com/gallery/adz2y0f9574bjpmonaismyrhtjgvey4o", {
|
|
|
|
"url": "d7a4483b6d5ebba81950a349aad58ae034c60eda",
|
2016-09-25 17:28:46 +02:00
|
|
|
"keyword": "e4a9395dbd06d4af3172a6a61c90601bc47ee18c",
|
2016-09-02 08:25:29 +02:00
|
|
|
"content": "596e6bfa157f2c7169805d50075c2986549973a8",
|
|
|
|
})]
|
2015-04-11 14:05:36 +02:00
|
|
|
url_base = "http://www.imagebam.com"
|
2014-11-20 21:27:57 +01:00
|
|
|
|
2015-10-05 15:35:48 +02:00
|
|
|
def __init__(self, match):
|
|
|
|
AsynchronousExtractor.__init__(self)
|
2015-11-04 00:03:48 +01:00
|
|
|
self.gkey = match.group(1)
|
2015-04-11 14:05:36 +02:00
|
|
|
|
|
|
|
def items(self):
|
2015-11-04 00:03:48 +01:00
|
|
|
data = self.get_job_metadata()
|
|
|
|
data["num"] = 0
|
2015-04-11 14:05:36 +02:00
|
|
|
yield Message.Version, 1
|
2015-11-04 00:03:48 +01:00
|
|
|
yield Message.Directory, data
|
|
|
|
for image_url, image_id in self.get_images(data["first-url"]):
|
|
|
|
data["id"] = image_id
|
|
|
|
data["num"] += 1
|
2015-11-16 17:32:26 +01:00
|
|
|
text.nameext_from_url(image_url, data)
|
2015-11-04 00:03:48 +01:00
|
|
|
yield Message.Url, image_url, data.copy()
|
2015-04-11 14:05:36 +02:00
|
|
|
|
|
|
|
def get_job_metadata(self):
|
|
|
|
"""Collect metadata for extractor-job"""
|
2016-07-12 12:06:17 +02:00
|
|
|
url = self.url_base + "/gallery/" + self.gkey
|
|
|
|
page = self.request(url, encoding="utf-8").text
|
2016-09-25 14:22:07 +02:00
|
|
|
return text.extract_all(page, (
|
2015-11-04 00:03:48 +01:00
|
|
|
(None , "<img src='/img/icons/photos.png'", ""),
|
|
|
|
("title" , "'> ", " <"),
|
|
|
|
("count" , "'>", " images"),
|
|
|
|
("first-url", "<a href='http://www.imagebam.com", "'"),
|
2016-09-25 14:22:07 +02:00
|
|
|
), values={"gallery-key": self.gkey})[0]
|
2015-11-04 00:03:48 +01:00
|
|
|
|
|
|
|
def get_images(self, url):
|
2016-09-12 10:20:57 +02:00
|
|
|
"""Yield all image-urls and -ids for a gallery"""
|
2015-11-04 00:03:48 +01:00
|
|
|
done = False
|
|
|
|
while not done:
|
|
|
|
page = self.request(self.url_base + url).text
|
|
|
|
_ , pos = text.extract(page, 'class="btn btn-default" title="Next">', '')
|
|
|
|
if pos == 0:
|
|
|
|
done = True
|
|
|
|
else:
|
|
|
|
url, pos = text.extract(page, ' href="', '"', pos-70)
|
|
|
|
image_id , pos = text.extract(page, '<img class="image" id="', '"', pos)
|
|
|
|
image_url, pos = text.extract(page, ' src="', '"', pos)
|
|
|
|
yield image_url, image_id
|
2016-09-02 08:25:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ImagebamImageExtractor(Extractor):
|
2016-09-12 10:20:57 +02:00
|
|
|
"""Extractor for single images from imagebam.com"""
|
2016-09-02 08:25:29 +02:00
|
|
|
category = "imagebam"
|
|
|
|
subcategory = "image"
|
|
|
|
directory_fmt = ["{category}"]
|
|
|
|
filename_fmt = "{filename}"
|
|
|
|
pattern = [r"(?:https?://)?(?:www\.)?imagebam\.com/image/([0-9a-f]{15})"]
|
|
|
|
test = [("http://www.imagebam.com/image/94d56c502511890", {
|
|
|
|
"url": "94add9417c685d113a91bcdda4916e9538b5f8a9",
|
2016-09-25 17:28:46 +02:00
|
|
|
"keyword": "fd99b2f45b761d0b639af46740aacd976f5dfcc7",
|
2016-09-02 08:25:29 +02:00
|
|
|
"content": "0c8768055e4e20e7c7259608b67799171b691140",
|
|
|
|
})]
|
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
Extractor.__init__(self)
|
|
|
|
self.token = match.group(1)
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
page = self.request("http://www.imagebam.com/image/" + self.token).text
|
2016-09-25 14:22:07 +02:00
|
|
|
url = text.extract(page, 'property="og:image" content="', '"')[0]
|
|
|
|
data = text.nameext_from_url(url, {"token": self.token})
|
2016-09-02 08:25:29 +02:00
|
|
|
yield Message.Version, 1
|
|
|
|
yield Message.Directory, data
|
|
|
|
yield Message.Url, url, data
|