| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Extractors for https://www.smugmug.com/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message |
| 12 |
from .. import text, oauth |
| 13 |
|
| 14 |
BASE_PATTERN = ( |
| 15 |
r"(?:smugmug:(?!album:)(?:https?://)?([^/]+)|" |
| 16 |
r"(?:https?://)?([\w-]+)\.smugmug\.com)") |
| 17 |
|
| 18 |
|
| 19 |
class SmugmugExtractor(Extractor): |
| 20 |
"""Base class for smugmug extractors""" |
| 21 |
category = "smugmug" |
| 22 |
filename_fmt = ("{category}_{User[NickName]:?/_/}" |
| 23 |
"{Image[UploadKey]}_{Image[ImageKey]}.{extension}") |
| 24 |
empty_user = { |
| 25 |
"Uri": "", |
| 26 |
"ResponseLevel": "Public", |
| 27 |
"Name": "", |
| 28 |
"NickName": "", |
| 29 |
"QuickShare": False, |
| 30 |
"RefTag": "", |
| 31 |
"ViewPassHint": "", |
| 32 |
"WebUri": "", |
| 33 |
"Uris": None, |
| 34 |
} |
| 35 |
|
| 36 |
def _init(self): |
| 37 |
self.api = SmugmugAPI(self) |
| 38 |
self.videos = self.config("videos", True) |
| 39 |
self.session = self.api.session |
| 40 |
|
| 41 |
def _select_format(self, image): |
| 42 |
details = image["Uris"]["ImageSizeDetails"] |
| 43 |
media = None |
| 44 |
|
| 45 |
if self.videos and image["IsVideo"]: |
| 46 |
fltr = "VideoSize" |
| 47 |
elif "ImageSizeOriginal" in details: |
| 48 |
media = details["ImageSizeOriginal"] |
| 49 |
else: |
| 50 |
fltr = "ImageSize" |
| 51 |
|
| 52 |
if not media: |
| 53 |
sizes = filter(lambda s: s[0].startswith(fltr), details.items()) |
| 54 |
media = max(sizes, key=lambda s: s[1]["Width"])[1] |
| 55 |
del image["Uris"] |
| 56 |
|
| 57 |
for key in ("Url", "Width", "Height", "MD5", "Size", "Watermarked", |
| 58 |
"Bitrate", "Duration"): |
| 59 |
if key in media: |
| 60 |
image[key] = media[key] |
| 61 |
return image["Url"] |
| 62 |
|
| 63 |
|
| 64 |
class SmugmugAlbumExtractor(SmugmugExtractor): |
| 65 |
"""Extractor for smugmug albums""" |
| 66 |
subcategory = "album" |
| 67 |
directory_fmt = ("{category}", "{User[NickName]}", "{Album[Name]}") |
| 68 |
archive_fmt = "a_{Album[AlbumKey]}_{Image[ImageKey]}" |
| 69 |
pattern = r"smugmug:album:([^:]+)$" |
| 70 |
example = "smugmug:album:ID" |
| 71 |
|
| 72 |
def __init__(self, match): |
| 73 |
SmugmugExtractor.__init__(self, match) |
| 74 |
self.album_id = match[1] |
| 75 |
|
| 76 |
def items(self): |
| 77 |
album = self.api.album(self.album_id, "User") |
| 78 |
user = album["Uris"].get("User") or self.empty_user.copy() |
| 79 |
|
| 80 |
del user["Uris"] |
| 81 |
del album["Uris"] |
| 82 |
data = {"Album": album, "User": user} |
| 83 |
|
| 84 |
yield Message.Directory, "", data |
| 85 |
|
| 86 |
for image in self.api.album_images(self.album_id, "ImageSizeDetails"): |
| 87 |
url = self._select_format(image) |
| 88 |
data["Image"] = image |
| 89 |
yield Message.Url, url, text.nameext_from_url(url, data) |
| 90 |
|
| 91 |
|
| 92 |
class SmugmugImageExtractor(SmugmugExtractor): |
| 93 |
"""Extractor for individual smugmug images""" |
| 94 |
subcategory = "image" |
| 95 |
archive_fmt = "{Image[ImageKey]}" |
| 96 |
pattern = BASE_PATTERN + r"(?:/[^/?#]+)+/i-([^/?#-]+)" |
| 97 |
example = "https://USER.smugmug.com/PATH/i-ID" |
| 98 |
|
| 99 |
def __init__(self, match): |
| 100 |
SmugmugExtractor.__init__(self, match) |
| 101 |
self.image_id = match[3] |
| 102 |
|
| 103 |
def items(self): |
| 104 |
image = self.api.image(self.image_id, "ImageSizeDetails") |
| 105 |
url = self._select_format(image) |
| 106 |
|
| 107 |
data = {"Image": image} |
| 108 |
text.nameext_from_url(url, data) |
| 109 |
|
| 110 |
yield Message.Directory, "", data |
| 111 |
yield Message.Url, url, data |
| 112 |
|
| 113 |
|
| 114 |
class SmugmugPathExtractor(SmugmugExtractor): |
| 115 |
"""Extractor for smugmug albums from URL paths and users""" |
| 116 |
subcategory = "path" |
| 117 |
pattern = BASE_PATTERN + r"((?:/[^/?#a-fh-mo-z][^/?#]*)*)/?$" |
| 118 |
example = "https://USER.smugmug.com/PATH" |
| 119 |
|
| 120 |
def __init__(self, match): |
| 121 |
SmugmugExtractor.__init__(self, match) |
| 122 |
self.domain, self.user, self.path = match.groups() |
| 123 |
|
| 124 |
def items(self): |
| 125 |
if not self.user: |
| 126 |
self.user = self.api.site_user(self.domain)["NickName"] |
| 127 |
|
| 128 |
if self.path: |
| 129 |
if self.path.startswith("/gallery/n-"): |
| 130 |
node = self.api.node(self.path[11:]) |
| 131 |
else: |
| 132 |
data = self.api.user_urlpathlookup(self.user, self.path) |
| 133 |
node = data["Uris"]["Node"] |
| 134 |
|
| 135 |
if node["Type"] == "Album": |
| 136 |
nodes = (node,) |
| 137 |
elif node["Type"] == "Folder": |
| 138 |
nodes = self.album_nodes(node) |
| 139 |
else: |
| 140 |
nodes = () |
| 141 |
|
| 142 |
for node in nodes: |
| 143 |
album_id = node["Uris"]["Album"].rpartition("/")[2] |
| 144 |
node["_extractor"] = SmugmugAlbumExtractor |
| 145 |
yield Message.Queue, "smugmug:album:" + album_id, node |
| 146 |
|
| 147 |
else: |
| 148 |
for album in self.api.user_albums(self.user): |
| 149 |
uri = "smugmug:album:" + album["AlbumKey"] |
| 150 |
album["_extractor"] = SmugmugAlbumExtractor |
| 151 |
yield Message.Queue, uri, album |
| 152 |
|
| 153 |
def album_nodes(self, root): |
| 154 |
"""Yield all descendant album nodes of 'root'""" |
| 155 |
for node in self.api.node_children(root["NodeID"]): |
| 156 |
if node["Type"] == "Album": |
| 157 |
yield node |
| 158 |
elif node["Type"] == "Folder": |
| 159 |
yield from self.album_nodes(node) |
| 160 |
|
| 161 |
|
| 162 |
class SmugmugAPI(oauth.OAuth1API): |
| 163 |
"""Minimal interface for the smugmug API v2""" |
| 164 |
API_DOMAIN = "api.smugmug.com" |
| 165 |
API_KEY = "RCVHDGjcbc4Fhzq4qzqLdZmvwmwB6LM2" |
| 166 |
API_SECRET = ("jGrdndvJqhTx8XSNs7TFTSSthhZHq92d" |
| 167 |
"dMpbpDpkDVNM7TDgnvLFMtfB5Mg5kH73") |
| 168 |
HEADERS = {"Accept": "application/json"} |
| 169 |
|
| 170 |
def album(self, album_id, expands=None): |
| 171 |
return self._expansion("album/" + album_id, expands) |
| 172 |
|
| 173 |
def image(self, image_id, expands=None): |
| 174 |
return self._expansion("image/" + image_id, expands) |
| 175 |
|
| 176 |
def node(self, node_id, expands=None): |
| 177 |
return self._expansion("node/" + node_id, expands) |
| 178 |
|
| 179 |
def user(self, username, expands=None): |
| 180 |
return self._expansion("user/" + username, expands) |
| 181 |
|
| 182 |
def album_images(self, album_id, expands=None): |
| 183 |
return self._pagination("album/" + album_id + "!images", expands) |
| 184 |
|
| 185 |
def node_children(self, node_id, expands=None): |
| 186 |
return self._pagination("node/" + node_id + "!children", expands) |
| 187 |
|
| 188 |
def user_albums(self, username, expands=None): |
| 189 |
return self._pagination("user/" + username + "!albums", expands) |
| 190 |
|
| 191 |
def site_user(self, domain): |
| 192 |
return self._call("!siteuser", domain=domain)["Response"]["User"] |
| 193 |
|
| 194 |
def user_urlpathlookup(self, username, path): |
| 195 |
endpoint = "user/" + username + "!urlpathlookup" |
| 196 |
params = {"urlpath": path} |
| 197 |
return self._expansion(endpoint, "Node", params) |
| 198 |
|
| 199 |
def _call(self, endpoint, params=None, domain=API_DOMAIN): |
| 200 |
url = f"https://{domain}/api/v2/{endpoint}" |
| 201 |
params = params or {} |
| 202 |
if self.api_key: |
| 203 |
params["APIKey"] = self.api_key |
| 204 |
params["_verbosity"] = "1" |
| 205 |
|
| 206 |
response = self.request(url, params=params, headers=self.HEADERS) |
| 207 |
try: |
| 208 |
data = response.json() |
| 209 |
except ValueError: |
| 210 |
raise self.exc.NotFoundError(self.extractor.__class__.subcategory) |
| 211 |
|
| 212 |
if 200 <= data["Code"] < 400: |
| 213 |
return data |
| 214 |
if data["Code"] == 404: |
| 215 |
raise self.exc.NotFoundError(self.extractor.__class__.subcategory) |
| 216 |
if data["Code"] == 429: |
| 217 |
raise self.exc.AbortExtraction("Rate limit reached") |
| 218 |
self.log.debug(data) |
| 219 |
raise self.exc.AbortExtraction("API request failed") |
| 220 |
|
| 221 |
def _expansion(self, endpoint, expands, params=None): |
| 222 |
endpoint = self._extend(endpoint, expands) |
| 223 |
result = self._apply_expansions(self._call(endpoint, params), expands) |
| 224 |
if not result: |
| 225 |
raise self.exc.NotFoundError() |
| 226 |
return result[0] |
| 227 |
|
| 228 |
def _pagination(self, endpoint, expands=None): |
| 229 |
endpoint = self._extend(endpoint, expands) |
| 230 |
params = {"start": 1, "count": 100} |
| 231 |
|
| 232 |
while True: |
| 233 |
data = self._call(endpoint, params) |
| 234 |
yield from self._apply_expansions(data, expands) |
| 235 |
|
| 236 |
if "NextPage" not in data["Response"]["Pages"]: |
| 237 |
return |
| 238 |
params["start"] += params["count"] |
| 239 |
|
| 240 |
def _extend(self, endpoint, expands): |
| 241 |
if expands: |
| 242 |
endpoint += "?_expand=" + expands |
| 243 |
return endpoint |
| 244 |
|
| 245 |
def _apply_expansions(self, data, expands): |
| 246 |
|
| 247 |
def unwrap(response): |
| 248 |
locator = response["Locator"] |
| 249 |
return response[locator] if locator in response else [] |
| 250 |
|
| 251 |
objs = unwrap(data["Response"]) |
| 252 |
if not isinstance(objs, list): |
| 253 |
objs = (objs,) |
| 254 |
|
| 255 |
if "Expansions" in data: |
| 256 |
expansions = data["Expansions"] |
| 257 |
expands = expands.split(",") |
| 258 |
|
| 259 |
for obj in objs: |
| 260 |
uris = obj["Uris"] |
| 261 |
|
| 262 |
for name in expands: |
| 263 |
if name in uris: |
| 264 |
uri = uris[name] |
| 265 |
uris[name] = unwrap(expansions[uri]) |
| 266 |
|
| 267 |
return objs |