2019-01-20 16:19:13 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2023-09-11 16:30:55 +02:00
|
|
|
# Copyright 2019-2023 Mike Fährmann
|
2019-01-20 16:19:13 +01: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.
|
|
|
|
|
2023-09-11 16:30:55 +02:00
|
|
|
"""Extractors for https://photobucket.com/"""
|
2019-01-20 16:19:13 +01:00
|
|
|
|
|
|
|
from .common import Extractor, Message
|
2019-01-22 17:24:43 +01:00
|
|
|
from .. import text, exception
|
2023-03-02 18:25:47 +01:00
|
|
|
import binascii
|
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"
|
2019-02-08 13:45:40 +01:00
|
|
|
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}"
|
2021-12-29 22:39:29 +01:00
|
|
|
pattern = (r"(?:https?://)?((?:[\w-]+\.)?photobucket\.com)"
|
|
|
|
r"/user/[^/?&#]+/library(?:/[^?&#]*)?")
|
2023-09-11 16:30:55 +02:00
|
|
|
example = "https://s123.photobucket.com/user/USER/library"
|
2019-01-20 16:19:13 +01:00
|
|
|
|
|
|
|
def __init__(self, match):
|
2019-04-03 18:30:45 +02:00
|
|
|
self.root = "https://" + match.group(1)
|
2023-07-25 20:09:44 +02:00
|
|
|
Extractor.__init__(self, match)
|
|
|
|
|
|
|
|
def _init(self):
|
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
|
|
|
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():
|
2019-02-12 21:26:41 +01:00
|
|
|
album["_extractor"] = PhotobucketAlbumExtractor
|
2019-01-21 19:55:05 +01:00
|
|
|
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
|
2019-01-27 20:37:22 +01:00
|
|
|
json_data = text.extract(page, "collectionData:", ",\n")[0]
|
|
|
|
if not json_data:
|
2022-11-04 23:39:38 +01:00
|
|
|
msg = text.extr(page, 'libraryPrivacyBlock">', "</div>")
|
2019-01-27 20:37:22 +01:00
|
|
|
msg = ' ("{}")'.format(text.remove_html(msg)) if msg else ""
|
|
|
|
self.log.error("Unable to get JSON data%s", msg)
|
|
|
|
return
|
|
|
|
data = json.loads(json_data)
|
2019-01-20 16:19:13 +01:00
|
|
|
|
|
|
|
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):
|
2019-01-22 21:35:09 +01:00
|
|
|
"""Return all subalbum objects"""
|
2019-01-21 19:55:05 +01:00
|
|
|
url = self.root + "/component/Albums-SubalbumList"
|
2019-01-22 21:35:09 +01:00
|
|
|
params = {
|
|
|
|
"albumPath": self.album_path,
|
|
|
|
"fetchSubAlbumsOnly": "true",
|
|
|
|
"deferCollapsed": "true",
|
|
|
|
"json": "1",
|
|
|
|
}
|
2019-01-21 19:55:05 +01:00
|
|
|
|
|
|
|
data = self.request(url, params=params).json()
|
2019-01-27 20:37:22 +01:00
|
|
|
return data["body"].get("subAlbums", ())
|
2019-01-22 17:24:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
class PhotobucketImageExtractor(Extractor):
|
|
|
|
"""Extractor for individual images from photobucket.com"""
|
|
|
|
category = "photobucket"
|
|
|
|
subcategory = "image"
|
2019-02-08 13:45:40 +01:00
|
|
|
directory_fmt = ("{category}", "{username}")
|
2019-01-22 17:24:43 +01:00
|
|
|
filename_fmt = "{pictureId:?/_/}{titleOrFilename}.{extension}"
|
|
|
|
archive_fmt = "{username}_{id}"
|
2021-12-29 22:39:29 +01:00
|
|
|
pattern = (r"(?:https?://)?(?:[\w-]+\.)?photobucket\.com"
|
|
|
|
r"(?:/gallery/user/([^/?&#]+)/media/([^/?&#]+)"
|
|
|
|
r"|/user/([^/?&#]+)/media/[^?&#]+\.html)")
|
2023-09-11 16:30:55 +02:00
|
|
|
example = "https://s123.photobucket.com/user/USER/media/NAME.EXT.html"
|
2019-01-22 17:24:43 +01:00
|
|
|
|
|
|
|
def __init__(self, match):
|
2019-02-11 13:31:10 +01:00
|
|
|
Extractor.__init__(self, match)
|
2019-01-22 17:24:43 +01:00
|
|
|
self.user = match.group(1) or match.group(3)
|
|
|
|
self.media_id = match.group(2)
|
2023-07-25 20:09:44 +02:00
|
|
|
|
|
|
|
def _init(self):
|
2019-01-22 17:24:43 +01:00
|
|
|
self.session.headers["Referer"] = self.url
|
|
|
|
|
|
|
|
def items(self):
|
2019-04-03 18:30:45 +02:00
|
|
|
url = "https://photobucket.com/galleryd/search.php"
|
2019-01-22 17:24:43 +01:00
|
|
|
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
|
2019-10-28 16:06:36 +01:00
|
|
|
self.log.debug(image["message"])
|
2019-01-22 17:24:43 +01:00
|
|
|
else:
|
2019-10-28 16:06:36 +01:00
|
|
|
raise exception.StopExtraction(image["message"])
|
2019-01-22 17:24:43 +01:00
|
|
|
|
|
|
|
# adjust metadata entries to be at least somewhat similar
|
2019-04-03 18:30:45 +02:00
|
|
|
# to what the 'album' extractor provides
|
2019-01-22 17:24:43 +01:00
|
|
|
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", [])
|
|
|
|
|
2023-03-02 18:25:47 +01:00
|
|
|
mtype, _, mid = binascii.a2b_base64(image["id"]).partition(b":")
|
2019-01-22 17:24:43 +01:00
|
|
|
image["pictureId"] = mid.decode() if mtype == b"mediaId" else ""
|
|
|
|
|
|
|
|
yield Message.Directory, image
|
|
|
|
yield Message.Url, image["fileUrl"], image
|