2019-01-20 16:19:13 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright 2019 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://photobucket.com/"""
|
|
|
|
|
|
|
|
from .common import Extractor, Message
|
2019-01-22 17:24:43 +01:00
|
|
|
from .. import text, exception
|
|
|
|
import base64
|
2019-01-20 16:19:13 +01:00
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
|
|
class PhotobucketAlbumExtractor(Extractor):
|
2019-01-21 19:55:05 +01:00
|
|
|
"""Extractor for albums on photobucket.com"""
|
2019-01-20 16:19:13 +01:00
|
|
|
category = "photobucket"
|
|
|
|
subcategory = "album"
|
|
|
|
directory_fmt = ["{category}", "{username}", "{location}"]
|
2019-01-21 19:55:05 +01:00
|
|
|
filename_fmt = "{offset:>03}{pictureId:?_//}_{titleOrFilename}.{extension}"
|
2019-01-20 16:19:13 +01:00
|
|
|
archive_fmt = "{id}"
|
2019-01-21 19:55:05 +01:00
|
|
|
pattern = [r"(?:https?://)?((?:[^.]+\.)?photobucket\.com)"
|
2019-01-20 16:19:13 +01:00
|
|
|
r"/user/[^/?&#]+/library/[^?&#]*"]
|
|
|
|
test = [
|
|
|
|
("http://s258.photobucket.com/user/focolandia/library/", {
|
|
|
|
"pattern": r"http://i\d+.photobucket.com/albums/hh280/focolandia",
|
|
|
|
"count": ">= 39"
|
|
|
|
}),
|
2019-01-21 19:55:05 +01:00
|
|
|
("http://s271.photobucket.com/user/lakerfanryan/library/", {
|
|
|
|
"options": (("image-filter", "False"),),
|
|
|
|
"pattern": "http://s271.photobucket.com/user/lakerfanryan/library",
|
|
|
|
"count": ">= 22",
|
|
|
|
}),
|
2019-01-20 16:19:13 +01:00
|
|
|
("http://s1110.photobucket.com/user/chndrmhn100/library/"
|
|
|
|
"Chandu%20is%20the%20King?sort=3&page=1", None),
|
|
|
|
]
|
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
Extractor.__init__(self)
|
2019-01-21 19:55:05 +01:00
|
|
|
self.album_path = ""
|
2019-01-20 16:19:13 +01:00
|
|
|
self.url = match.group(0)
|
2019-01-21 19:55:05 +01:00
|
|
|
self.root = "http://" + match.group(1)
|
2019-01-20 16:19:13 +01:00
|
|
|
self.session.headers["Referer"] = self.url
|
|
|
|
|
2019-01-21 19:55:05 +01:00
|
|
|
def items(self):
|
2019-01-20 16:19:13 +01:00
|
|
|
yield Message.Version, 1
|
|
|
|
for image in self.images():
|
2019-01-21 19:55:05 +01:00
|
|
|
image["titleOrFilename"] = text.unescape(image["titleOrFilename"])
|
2019-01-20 16:19:13 +01:00
|
|
|
image["title"] = text.unescape(image["title"])
|
|
|
|
image["extension"] = image["ext"]
|
|
|
|
yield Message.Directory, image
|
|
|
|
yield Message.Url, image["fullsizeUrl"], image
|
|
|
|
|
2019-01-21 19:55:05 +01:00
|
|
|
if self.config("subalbums", True):
|
|
|
|
for album in self.subalbums():
|
|
|
|
yield Message.Queue, album["url"], album
|
|
|
|
|
2019-01-20 16:19:13 +01:00
|
|
|
def images(self):
|
2019-01-21 19:55:05 +01:00
|
|
|
"""Yield all images of the current album"""
|
|
|
|
url = self.url
|
2019-01-20 16:19:13 +01:00
|
|
|
params = {"sort": "3", "page": 1}
|
|
|
|
|
|
|
|
while True:
|
|
|
|
page = self.request(url, params=params).text
|
|
|
|
data = json.loads(text.extract(page, "collectionData:", ",\n")[0])
|
|
|
|
|
|
|
|
yield from data["items"]["objects"]
|
|
|
|
|
|
|
|
if data["total"] <= data["offset"] + data["pageSize"]:
|
2019-01-21 19:55:05 +01:00
|
|
|
self.album_path = data["currentAlbumPath"]
|
2019-01-20 16:19:13 +01:00
|
|
|
return
|
|
|
|
params["page"] += 1
|
2019-01-21 19:55:05 +01:00
|
|
|
|
|
|
|
def subalbums(self):
|
|
|
|
"""Yield all subalbum URLs"""
|
|
|
|
url = self.root + "/component/Albums-SubalbumList"
|
|
|
|
params = {"albumPath": self.album_path, "json": "1"}
|
|
|
|
|
|
|
|
data = self.request(url, params=params).json()
|
|
|
|
albums = data["body"]["subAlbums"]
|
|
|
|
albums.reverse()
|
|
|
|
|
|
|
|
while albums:
|
|
|
|
album = albums.pop()
|
|
|
|
|
|
|
|
subs = album.pop("subAlbums")
|
|
|
|
if subs:
|
|
|
|
subs.reverse()
|
|
|
|
albums.extend(subs)
|
|
|
|
|
|
|
|
yield album
|
2019-01-22 17:24:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
class PhotobucketImageExtractor(Extractor):
|
|
|
|
"""Extractor for individual images from photobucket.com"""
|
|
|
|
category = "photobucket"
|
|
|
|
subcategory = "image"
|
|
|
|
directory_fmt = ["{category}", "{username}"]
|
|
|
|
filename_fmt = "{pictureId:?/_/}{titleOrFilename}.{extension}"
|
|
|
|
archive_fmt = "{username}_{id}"
|
|
|
|
pattern = [r"(?:https?://)?(?:[^.]+\.)?photobucket\.com"
|
|
|
|
r"(?:/gallery/user/([^/?&#]+)/media/([^/?&#]+)"
|
|
|
|
r"|/user/([^/?&#]+)/media/[^?&#]+\.html)"]
|
|
|
|
test = [
|
|
|
|
(("http://s271.photobucket.com/user/lakerfanryan"
|
|
|
|
"/media/Untitled-3-1.jpg.html"), {
|
|
|
|
"url": "256fe63bee84762f92337e963ec0baa27bba87e2",
|
|
|
|
"keyword": "81fbe6f5f821a2d20dabb931726ab9e7565ba96d",
|
|
|
|
}),
|
|
|
|
(("http://s271.photobucket.com/user/lakerfanryan"
|
|
|
|
"/media/IsotopeswBros.jpg.html?sort=3&o=2"), {
|
|
|
|
"url": "44e644e29a564398fcb2fd8edce738696afe7208",
|
|
|
|
"keyword": "6addb30d6db6d7c3222761ade37c0bded67e5783",
|
|
|
|
}),
|
|
|
|
]
|
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
Extractor.__init__(self)
|
|
|
|
self.url = match.group(0)
|
|
|
|
self.user = match.group(1) or match.group(3)
|
|
|
|
self.media_id = match.group(2)
|
|
|
|
self.session.headers["Referer"] = self.url
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
url = "http://photobucket.com/galleryd/search.php"
|
|
|
|
params = {"userName": self.user, "searchTerm": "", "ref": ""}
|
|
|
|
|
|
|
|
if self.media_id:
|
|
|
|
params["mediaId"] = self.media_id
|
|
|
|
else:
|
|
|
|
params["url"] = self.url
|
|
|
|
|
|
|
|
# retry API call up to 5 times, since it can randomly fail
|
|
|
|
tries = 0
|
|
|
|
while tries < 5:
|
|
|
|
data = self.request(url, method="POST", params=params).json()
|
|
|
|
image = data["mediaDocuments"]
|
|
|
|
if "message" not in image:
|
|
|
|
break # success
|
|
|
|
tries += 1
|
|
|
|
self.log.debug("'%s'", image["message"])
|
|
|
|
else:
|
|
|
|
self.log.error("photobucket says: '%s'", image["message"])
|
|
|
|
raise exception.StopExtraction()
|
|
|
|
|
|
|
|
# adjust metadata entries to be at least somewhat similar
|
|
|
|
# to the 'album' extractor
|
|
|
|
if "media" in image:
|
|
|
|
image = image["media"][image["mediaIndex"]]
|
|
|
|
image["albumView"] = data["mediaDocuments"]["albumView"]
|
|
|
|
image["username"] = image["ownerId"]
|
|
|
|
else:
|
|
|
|
image["fileUrl"] = image.pop("imageUrl")
|
|
|
|
|
|
|
|
image.setdefault("title", "")
|
|
|
|
image.setdefault("description", "")
|
|
|
|
name, _, ext = image["fileUrl"].rpartition("/")[2].rpartition(".")
|
|
|
|
image["ext"] = image["extension"] = ext
|
|
|
|
image["titleOrFilename"] = image["title"] or name
|
|
|
|
image["tags"] = image.pop("clarifaiTagList", [])
|
|
|
|
|
|
|
|
mtype, _, mid = base64.b64decode(image["id"]).partition(b":")
|
|
|
|
image["pictureId"] = mid.decode() if mtype == b"mediaId" else ""
|
|
|
|
|
|
|
|
yield Message.Version, 1
|
|
|
|
yield Message.Directory, image
|
|
|
|
yield Message.Url, image["fileUrl"], image
|