| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright 2017-2026 Mike Fährmann |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License version 2 as |
| 7 |
# published by the Free Software Foundation. |
| 8 |
|
| 9 |
"""Extractors for https://www.flickr.com/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message |
| 12 |
from .. import text, oauth, util |
| 13 |
|
| 14 |
BASE_PATTERN = r"(?:https?://)?(?:www\.|secure\.|m\.)?flickr\.com" |
| 15 |
|
| 16 |
|
| 17 |
class FlickrExtractor(Extractor): |
| 18 |
"""Base class for flickr extractors""" |
| 19 |
category = "flickr" |
| 20 |
root = "https://www.flickr.com" |
| 21 |
filename_fmt = "{category}_{id}.{extension}" |
| 22 |
directory_fmt = ("{category}", "{user[username]}") |
| 23 |
archive_fmt = "{id}" |
| 24 |
request_interval = (1.0, 2.0) |
| 25 |
request_interval_min = 0.5 |
| 26 |
|
| 27 |
def _init(self): |
| 28 |
self.user = None |
| 29 |
self.item_id = self.groups[0] |
| 30 |
|
| 31 |
if self.config("urls") == "download": |
| 32 |
self._file_url = self._file_url_dl |
| 33 |
|
| 34 |
def items(self): |
| 35 |
self.api = FlickrAPI(self) |
| 36 |
|
| 37 |
data = self.metadata() |
| 38 |
extract = self.api._extract_format |
| 39 |
for photo in self.photos(): |
| 40 |
try: |
| 41 |
photo = extract(photo) |
| 42 |
except Exception as exc: |
| 43 |
self.log.warning( |
| 44 |
"Skipping photo %s (%s: %s)", |
| 45 |
photo["id"], exc.__class__.__name__, exc) |
| 46 |
self.log.traceback(exc) |
| 47 |
else: |
| 48 |
photo.update(data) |
| 49 |
url = self._file_url(photo) |
| 50 |
yield Message.Directory, "", photo |
| 51 |
yield Message.Url, url, text.nameext_from_url(url, photo) |
| 52 |
|
| 53 |
def metadata(self): |
| 54 |
"""Return general metadata""" |
| 55 |
self.user = self.api.urls_lookupUser(self.item_id) |
| 56 |
if self.config("profile", False): |
| 57 |
self.user.update(self.api.people_getInfo(self.user["nsid"])) |
| 58 |
return {"user": self.user} |
| 59 |
|
| 60 |
def photos(self): |
| 61 |
"""Return an iterable with all relevant photo objects""" |
| 62 |
|
| 63 |
def _file_url(self, photo): |
| 64 |
return photo["url"] |
| 65 |
|
| 66 |
def _file_url_dl(self, photo): |
| 67 |
url = photo["url"] |
| 68 |
|
| 69 |
if "/video/" in url: |
| 70 |
return url |
| 71 |
|
| 72 |
path, _, ext = url.rpartition(".") |
| 73 |
return f"{path}_d.{ext}" |
| 74 |
|
| 75 |
|
| 76 |
class FlickrImageExtractor(FlickrExtractor): |
| 77 |
"""Extractor for individual images from flickr.com""" |
| 78 |
subcategory = "image" |
| 79 |
pattern = (r"(?:https?://)?(?:" |
| 80 |
r"(?:(?:www\.|secure\.|m\.)?flickr\.com/photos/[^/?#]+/" |
| 81 |
r"|[\w-]+\.static\.?flickr\.com/(?:\d+/)+)(\d+)" |
| 82 |
r"|flic\.kr/p/([A-Za-z1-9]+))") |
| 83 |
example = "https://www.flickr.com/photos/USER/12345" |
| 84 |
|
| 85 |
def items(self): |
| 86 |
self.api = FlickrAPI(self) |
| 87 |
|
| 88 |
item_id, enc_id = self.groups |
| 89 |
if enc_id is not None: |
| 90 |
alphabet = ("123456789abcdefghijkmnopqrstu" |
| 91 |
"vwxyzABCDEFGHJKLMNPQRSTUVWXYZ") |
| 92 |
item_id = util.bdecode(enc_id, alphabet) |
| 93 |
|
| 94 |
photo = self.api.photos_getInfo(item_id) |
| 95 |
|
| 96 |
self.api._extract_metadata(photo, False) |
| 97 |
if photo["media"] == "video" and self.api.videos: |
| 98 |
self.api._extract_video(photo) |
| 99 |
else: |
| 100 |
self.api._extract_photo(photo) |
| 101 |
|
| 102 |
if self.config("profile", False): |
| 103 |
photo["user"] = self.api.people_getInfo(photo["owner"]["nsid"]) |
| 104 |
else: |
| 105 |
photo["user"] = photo["owner"] |
| 106 |
|
| 107 |
photo["title"] = photo["title"]["_content"] |
| 108 |
photo["comments"] = text.parse_int(photo["comments"]["_content"]) |
| 109 |
photo["description"] = photo["description"]["_content"] |
| 110 |
photo["tags"] = [t["raw"] for t in photo["tags"]["tag"]] |
| 111 |
photo["date"] = self.parse_timestamp(photo["dateuploaded"]) |
| 112 |
photo["views"] = text.parse_int(photo["views"]) |
| 113 |
photo["id"] = text.parse_int(photo["id"]) |
| 114 |
|
| 115 |
if "location" in photo: |
| 116 |
location = photo["location"] |
| 117 |
for key, value in location.items(): |
| 118 |
if isinstance(value, dict): |
| 119 |
location[key] = value["_content"] |
| 120 |
|
| 121 |
url = self._file_url(photo) |
| 122 |
yield Message.Directory, "", photo |
| 123 |
yield Message.Url, url, text.nameext_from_url(url, photo) |
| 124 |
|
| 125 |
|
| 126 |
class FlickrAlbumExtractor(FlickrExtractor): |
| 127 |
"""Extractor for photo albums from flickr.com""" |
| 128 |
subcategory = "album" |
| 129 |
directory_fmt = ("{category}", "{user[username]}", |
| 130 |
"Albums", "{album[id]} {album[title]}") |
| 131 |
archive_fmt = "a_{album[id]}_{id}" |
| 132 |
pattern = BASE_PATTERN + r"/photos/([^/?#]+)/(?:album|set)s(?:/(\d+))?" |
| 133 |
example = "https://www.flickr.com/photos/USER/albums/12345" |
| 134 |
|
| 135 |
def items(self): |
| 136 |
self.album_id = self.groups[1] |
| 137 |
if self.album_id: |
| 138 |
return FlickrExtractor.items(self) |
| 139 |
return self._album_items() |
| 140 |
|
| 141 |
def _album_items(self): |
| 142 |
self.api = FlickrAPI(self) |
| 143 |
|
| 144 |
data = FlickrExtractor.metadata(self) |
| 145 |
data["_extractor"] = FlickrAlbumExtractor |
| 146 |
|
| 147 |
for album in self.api.photosets_getList(self.user["nsid"]): |
| 148 |
self.api._clean_info(album).update(data) |
| 149 |
url = (f"https://www.flickr.com/photos/{self.user['path_alias']}" |
| 150 |
f"/albums/{album['id']}") |
| 151 |
yield Message.Queue, url, album |
| 152 |
|
| 153 |
def metadata(self): |
| 154 |
data = FlickrExtractor.metadata(self) |
| 155 |
try: |
| 156 |
data["album"] = self.api.photosets_getInfo( |
| 157 |
self.album_id, self.user["nsid"]) |
| 158 |
except Exception: |
| 159 |
data["album"] = {} |
| 160 |
self.log.warning("%s: Unable to retrieve album metadata", |
| 161 |
self.album_id) |
| 162 |
return data |
| 163 |
|
| 164 |
def photos(self): |
| 165 |
return self.api.photosets_getPhotos(self.album_id) |
| 166 |
|
| 167 |
|
| 168 |
class FlickrGalleryExtractor(FlickrExtractor): |
| 169 |
"""Extractor for photo galleries from flickr.com""" |
| 170 |
subcategory = "gallery" |
| 171 |
directory_fmt = ("{category}", "{user[username]}", |
| 172 |
"Galleries", "{gallery[gallery_id]} {gallery[title]}") |
| 173 |
archive_fmt = "g_{gallery[id]}_{id}" |
| 174 |
pattern = BASE_PATTERN + r"/photos/([^/?#]+)/galleries/(\d+)" |
| 175 |
example = "https://www.flickr.com/photos/USER/galleries/12345/" |
| 176 |
|
| 177 |
def metadata(self): |
| 178 |
data = FlickrExtractor.metadata(self) |
| 179 |
self.gallery_id = self.groups[1] |
| 180 |
data["gallery"] = self.api.galleries_getInfo(self.gallery_id) |
| 181 |
return data |
| 182 |
|
| 183 |
def photos(self): |
| 184 |
return self.api.galleries_getPhotos(self.gallery_id) |
| 185 |
|
| 186 |
|
| 187 |
class FlickrGroupExtractor(FlickrExtractor): |
| 188 |
"""Extractor for group pools from flickr.com""" |
| 189 |
subcategory = "group" |
| 190 |
directory_fmt = ("{category}", "Groups", "{group[groupname]}") |
| 191 |
archive_fmt = "G_{group[nsid]}_{id}" |
| 192 |
pattern = BASE_PATTERN + r"/groups/([^/?#]+)" |
| 193 |
example = "https://www.flickr.com/groups/NAME/" |
| 194 |
|
| 195 |
def metadata(self): |
| 196 |
self.group = self.api.urls_lookupGroup(self.item_id) |
| 197 |
return {"group": self.group} |
| 198 |
|
| 199 |
def photos(self): |
| 200 |
return self.api.groups_pools_getPhotos(self.group["nsid"]) |
| 201 |
|
| 202 |
|
| 203 |
class FlickrUserExtractor(FlickrExtractor): |
| 204 |
"""Extractor for the photostream of a flickr user""" |
| 205 |
subcategory = "user" |
| 206 |
archive_fmt = "u_{user[nsid]}_{id}" |
| 207 |
pattern = BASE_PATTERN + r"/photos/([^/?#]+)/?$" |
| 208 |
example = "https://www.flickr.com/photos/USER/" |
| 209 |
|
| 210 |
def photos(self): |
| 211 |
return self.api.people_getPhotos(self.user["nsid"]) |
| 212 |
|
| 213 |
|
| 214 |
class FlickrFavoriteExtractor(FlickrExtractor): |
| 215 |
"""Extractor for favorite photos of a flickr user""" |
| 216 |
subcategory = "favorite" |
| 217 |
directory_fmt = ("{category}", "{user[username]}", "Favorites") |
| 218 |
archive_fmt = "f_{user[nsid]}_{id}" |
| 219 |
pattern = BASE_PATTERN + r"/photos/([^/?#]+)/favorites" |
| 220 |
example = "https://www.flickr.com/photos/USER/favorites" |
| 221 |
|
| 222 |
def photos(self): |
| 223 |
return self.api.favorites_getList(self.user["nsid"]) |
| 224 |
|
| 225 |
|
| 226 |
class FlickrSearchExtractor(FlickrExtractor): |
| 227 |
"""Extractor for flickr photos based on search results""" |
| 228 |
subcategory = "search" |
| 229 |
directory_fmt = ("{category}", "Search", "{search[text]}") |
| 230 |
archive_fmt = "s_{search}_{id}" |
| 231 |
pattern = BASE_PATTERN + r"/search/?\?([^#]+)" |
| 232 |
example = "https://flickr.com/search/?text=QUERY" |
| 233 |
|
| 234 |
def metadata(self): |
| 235 |
self.search = text.parse_query(self.groups[0]) |
| 236 |
if "text" not in self.search: |
| 237 |
self.search["text"] = "" |
| 238 |
return {"search": self.search} |
| 239 |
|
| 240 |
def photos(self): |
| 241 |
return self.api.photos_search(self.search) |
| 242 |
|
| 243 |
|
| 244 |
class FlickrAPI(oauth.OAuth1API): |
| 245 |
"""Minimal interface for the flickr API |
| 246 |
|
| 247 |
https://www.flickr.com/services/api/ |
| 248 |
""" |
| 249 |
|
| 250 |
API_URL = "https://api.flickr.com/services/rest/" |
| 251 |
# API_KEY = "" |
| 252 |
API_SECRET = "" |
| 253 |
FORMATS = [ |
| 254 |
("o" , "Original" , None), |
| 255 |
("6k", "X-Large 6K" , 6144), |
| 256 |
("5k", "X-Large 5K" , 5120), |
| 257 |
("4k", "X-Large 4K" , 4096), |
| 258 |
("3k", "X-Large 3K" , 3072), |
| 259 |
("k" , "Large 2048" , 2048), |
| 260 |
("h" , "Large 1600" , 1600), |
| 261 |
("l" , "Large" , 1024), |
| 262 |
("c" , "Medium 800" , 800), |
| 263 |
("z" , "Medium 640" , 640), |
| 264 |
("m" , "Medium" , 500), |
| 265 |
("n" , "Small 320" , 320), |
| 266 |
("s" , "Small" , 240), |
| 267 |
("q" , "Large Square", 150), |
| 268 |
("t" , "Thumbnail" , 100), |
| 269 |
("s" , "Square" , 75), |
| 270 |
] |
| 271 |
VIDEO_FORMATS = { |
| 272 |
"orig" : 9, |
| 273 |
"1080p" : 8, |
| 274 |
"720p" : 7, |
| 275 |
"360p" : 6, |
| 276 |
"288p" : 5, |
| 277 |
"700" : 4, |
| 278 |
"300" : 3, |
| 279 |
"100" : 2, |
| 280 |
"appletv" : 1, |
| 281 |
"iphone_wifi": 0, |
| 282 |
} |
| 283 |
LICENSES = { |
| 284 |
"0": "All Rights Reserved", |
| 285 |
"1": "Attribution-NonCommercial-ShareAlike License", |
| 286 |
"2": "Attribution-NonCommercial License", |
| 287 |
"3": "Attribution-NonCommercial-NoDerivs License", |
| 288 |
"4": "Attribution License", |
| 289 |
"5": "Attribution-ShareAlike License", |
| 290 |
"6": "Attribution-NoDerivs License", |
| 291 |
"7": "No known copyright restrictions", |
| 292 |
"8": "United States Government Work", |
| 293 |
"9": "Public Domain Dedication (CC0)", |
| 294 |
"10": "Public Domain Mark", |
| 295 |
} |
| 296 |
|
| 297 |
@property |
| 298 |
def API_KEY(self): |
| 299 |
return self.extractor.cache(self._extract_apikey, _key=None, _exp=3600) |
| 300 |
|
| 301 |
def __init__(self, extractor): |
| 302 |
oauth.OAuth1API.__init__(self, extractor) |
| 303 |
|
| 304 |
self.videos = extractor.config("videos", True) |
| 305 |
self.meta_exif = extractor.config("exif", False) |
| 306 |
self.meta_info = extractor.config("info", False) |
| 307 |
self.meta_contexts = extractor.config("contexts", False) |
| 308 |
|
| 309 |
self.maxsize = extractor.config("size-max") |
| 310 |
if isinstance(self.maxsize, str): |
| 311 |
for fmt, fmtname, fmtwidth in self.FORMATS: |
| 312 |
if self.maxsize == fmt or self.maxsize == fmtname: |
| 313 |
self.maxsize = fmtwidth |
| 314 |
break |
| 315 |
else: |
| 316 |
self.maxsize = None |
| 317 |
extractor.log.warning( |
| 318 |
"Could not match '%s' to any format", self.maxsize) |
| 319 |
if self.maxsize: |
| 320 |
self.formats = [fmt for fmt in self.FORMATS |
| 321 |
if not fmt[2] or fmt[2] <= self.maxsize] |
| 322 |
else: |
| 323 |
self.formats = self.FORMATS |
| 324 |
self.formats = self.formats[:8] |
| 325 |
|
| 326 |
def favorites_getList(self, user_id): |
| 327 |
"""Returns a list of the user's favorite photos.""" |
| 328 |
params = {"user_id": user_id} |
| 329 |
return self._pagination("favorites.getList", params) |
| 330 |
|
| 331 |
def galleries_getInfo(self, gallery_id): |
| 332 |
"""Gets information about a gallery.""" |
| 333 |
params = {"gallery_id": gallery_id} |
| 334 |
gallery = self._call("galleries.getInfo", params)["gallery"] |
| 335 |
return self._clean_info(gallery) |
| 336 |
|
| 337 |
def galleries_getPhotos(self, gallery_id): |
| 338 |
"""Return the list of photos for a gallery.""" |
| 339 |
params = {"gallery_id": gallery_id} |
| 340 |
return self._pagination("galleries.getPhotos", params) |
| 341 |
|
| 342 |
def groups_pools_getPhotos(self, group_id): |
| 343 |
"""Returns a list of pool photos for a given group.""" |
| 344 |
params = {"group_id": group_id} |
| 345 |
return self._pagination("groups.pools.getPhotos", params) |
| 346 |
|
| 347 |
def people_getInfo(self, user_id): |
| 348 |
"""Get information about a user.""" |
| 349 |
params = {"user_id": user_id} |
| 350 |
user = self._call("people.getInfo", params) |
| 351 |
|
| 352 |
try: |
| 353 |
user = user["person"] |
| 354 |
for key in ("description", "username", "realname", "location", |
| 355 |
"profileurl", "photosurl", "mobileurl"): |
| 356 |
if isinstance(user.get(key), dict): |
| 357 |
user[key] = user[key]["_content"] |
| 358 |
photos = user["photos"] |
| 359 |
for key in ("count", "firstdate", "firstdatetaken"): |
| 360 |
if isinstance(photos.get(key), dict): |
| 361 |
photos[key] = photos[key]["_content"] |
| 362 |
except Exception: |
| 363 |
pass |
| 364 |
|
| 365 |
return user |
| 366 |
|
| 367 |
def people_getPhotos(self, user_id): |
| 368 |
"""Return photos from the given user's photostream.""" |
| 369 |
params = {"user_id": user_id} |
| 370 |
return self._pagination("people.getPhotos", params) |
| 371 |
|
| 372 |
def photos_getAllContexts(self, photo_id): |
| 373 |
"""Returns all visible sets and pools the photo belongs to.""" |
| 374 |
params = {"photo_id": photo_id} |
| 375 |
data = self._call("photos.getAllContexts", params) |
| 376 |
del data["stat"] |
| 377 |
return data |
| 378 |
|
| 379 |
def photos_getExif(self, photo_id): |
| 380 |
"""Retrieves a list of EXIF/TIFF/GPS tags for a given photo.""" |
| 381 |
params = {"photo_id": photo_id} |
| 382 |
return self._call("photos.getExif", params)["photo"] |
| 383 |
|
| 384 |
def photos_getInfo(self, photo_id): |
| 385 |
"""Get information about a photo.""" |
| 386 |
params = {"photo_id": photo_id} |
| 387 |
return self._call("photos.getInfo", params)["photo"] |
| 388 |
|
| 389 |
def photos_getSizes(self, photo_id): |
| 390 |
"""Returns the available sizes for a photo.""" |
| 391 |
params = {"photo_id": photo_id} |
| 392 |
sizes = self._call("photos.getSizes", params)["sizes"]["size"] |
| 393 |
if self.maxsize: |
| 394 |
for index, size in enumerate(sizes): |
| 395 |
if index > 0 and (int(size["width"]) > self.maxsize or |
| 396 |
int(size["height"]) > self.maxsize): |
| 397 |
del sizes[index:] |
| 398 |
break |
| 399 |
return sizes |
| 400 |
|
| 401 |
def photos_search(self, params): |
| 402 |
"""Return a list of photos matching some criteria.""" |
| 403 |
return self._pagination("photos.search", params.copy()) |
| 404 |
|
| 405 |
def photosets_getInfo(self, photoset_id, user_id): |
| 406 |
"""Gets information about a photoset.""" |
| 407 |
params = {"photoset_id": photoset_id, "user_id": user_id} |
| 408 |
photoset = self._call("photosets.getInfo", params)["photoset"] |
| 409 |
return self._clean_info(photoset) |
| 410 |
|
| 411 |
def photosets_getList(self, user_id): |
| 412 |
"""Returns the photosets belonging to the specified user.""" |
| 413 |
params = {"user_id": user_id} |
| 414 |
return self._pagination_sets("photosets.getList", params) |
| 415 |
|
| 416 |
def photosets_getPhotos(self, photoset_id): |
| 417 |
"""Get the list of photos in a set.""" |
| 418 |
params = {"photoset_id": photoset_id} |
| 419 |
return self._pagination("photosets.getPhotos", params, "photoset") |
| 420 |
|
| 421 |
def urls_lookupGroup(self, groupname): |
| 422 |
"""Returns a group NSID, given the url to a group's page.""" |
| 423 |
params = {"url": "https://www.flickr.com/groups/" + groupname} |
| 424 |
group = self._call("urls.lookupGroup", params)["group"] |
| 425 |
return {"nsid": group["id"], |
| 426 |
"path_alias": groupname, |
| 427 |
"groupname": group["groupname"]["_content"]} |
| 428 |
|
| 429 |
def urls_lookupUser(self, username): |
| 430 |
"""Returns a user NSID, given the url to a user's photos or profile.""" |
| 431 |
params = {"url": "https://www.flickr.com/photos/" + username} |
| 432 |
user = self._call("urls.lookupUser", params)["user"] |
| 433 |
return { |
| 434 |
"nsid" : user["id"], |
| 435 |
"username" : user["username"]["_content"], |
| 436 |
"path_alias": username, |
| 437 |
} |
| 438 |
|
| 439 |
def video_getStreamInfo(self, video_id, secret=None): |
| 440 |
"""Returns all available video streams""" |
| 441 |
params = {"photo_id": video_id} |
| 442 |
if not secret: |
| 443 |
secret = self._call("photos.getInfo", params)["photo"]["secret"] |
| 444 |
params["secret"] = secret |
| 445 |
stream = self._call("video.getStreamInfo", params)["streams"]["stream"] |
| 446 |
return max(stream, key=lambda s: self.VIDEO_FORMATS.get(s["type"], 0)) |
| 447 |
|
| 448 |
def _call(self, method, params): |
| 449 |
params["method"] = "flickr." + method |
| 450 |
params["format"] = "json" |
| 451 |
params["nojsoncallback"] = "1" |
| 452 |
if self.api_key: |
| 453 |
params["api_key"] = self.api_key |
| 454 |
response = self.request(self.API_URL, params=params) |
| 455 |
try: |
| 456 |
data = response.json() |
| 457 |
except ValueError: |
| 458 |
data = {"code": -1, "message": response.content} |
| 459 |
if "code" in data: |
| 460 |
msg = data.get("message", "") |
| 461 |
self.log.debug("Server response: %s", data) |
| 462 |
if data["code"] == 1: |
| 463 |
raise self.exc.NotFoundError(self.extractor.subcategory) |
| 464 |
elif data["code"] == 2: |
| 465 |
raise self.exc.AuthorizationError(msg) |
| 466 |
elif data["code"] == 98: |
| 467 |
raise self.exc.AuthenticationError(msg) |
| 468 |
elif data["code"] == 99: |
| 469 |
raise self.exc.AuthorizationError(msg) |
| 470 |
raise self.exc.AbortExtraction("API request failed: " + msg) |
| 471 |
return data |
| 472 |
|
| 473 |
def _pagination(self, method, params, key="photos"): |
| 474 |
extras = ("description,date_upload,tags,views,media," |
| 475 |
"path_alias,owner_name,") |
| 476 |
if includes := self.extractor.config("metadata"): |
| 477 |
if isinstance(includes, (list, tuple)): |
| 478 |
includes = ",".join(includes) |
| 479 |
elif not isinstance(includes, str): |
| 480 |
includes = ("license,date_taken,original_format,last_update," |
| 481 |
"geo,machine_tags,o_dims") |
| 482 |
extras = extras + includes + "," |
| 483 |
extras += ",".join("url_" + fmt[0] for fmt in self.formats) |
| 484 |
|
| 485 |
params["extras"] = extras |
| 486 |
params["page"] = 1 |
| 487 |
|
| 488 |
while True: |
| 489 |
data = self._call(method, params)[key] |
| 490 |
yield from data["photo"] |
| 491 |
if params["page"] >= data["pages"]: |
| 492 |
return |
| 493 |
params["page"] += 1 |
| 494 |
|
| 495 |
def _pagination_sets(self, method, params): |
| 496 |
params["page"] = 1 |
| 497 |
|
| 498 |
while True: |
| 499 |
data = self._call(method, params)["photosets"] |
| 500 |
yield from data["photoset"] |
| 501 |
if params["page"] >= data["pages"]: |
| 502 |
return |
| 503 |
params["page"] += 1 |
| 504 |
|
| 505 |
def _extract_apikey(self): |
| 506 |
extr = self.extractor |
| 507 |
extr.log.info("Retrieving public API key") |
| 508 |
page = extr.request(extr.root + "/photos/").text |
| 509 |
return text.extr(page, '.flickr.api.site_key = "', '"') |
| 510 |
|
| 511 |
def _extract_format(self, photo): |
| 512 |
photo["description"] = photo["description"]["_content"].strip() |
| 513 |
photo["views"] = text.parse_int(photo["views"]) |
| 514 |
photo["date"] = self.extractor.parse_timestamp(photo["dateupload"]) |
| 515 |
photo["tags"] = photo["tags"].split() |
| 516 |
|
| 517 |
self._extract_metadata(photo) |
| 518 |
photo["id"] = text.parse_int(photo["id"]) |
| 519 |
|
| 520 |
if "owner" not in photo: |
| 521 |
photo["owner"] = self.extractor.user |
| 522 |
elif not self.meta_info: |
| 523 |
photo["owner"] = { |
| 524 |
"nsid" : photo["owner"], |
| 525 |
"username" : photo["ownername"], |
| 526 |
"path_alias": photo["pathalias"], |
| 527 |
} |
| 528 |
|
| 529 |
del photo["pathalias"] |
| 530 |
del photo["ownername"] |
| 531 |
|
| 532 |
if photo["media"] == "video" and self.videos: |
| 533 |
return self._extract_video(photo) |
| 534 |
|
| 535 |
for fmt, fmtname, fmtwidth in self.formats: |
| 536 |
key = "url_" + fmt |
| 537 |
if key in photo: |
| 538 |
photo["width"] = text.parse_int(photo["width_" + fmt]) |
| 539 |
photo["height"] = text.parse_int(photo["height_" + fmt]) |
| 540 |
if self.maxsize and (photo["width"] > self.maxsize or |
| 541 |
photo["height"] > self.maxsize): |
| 542 |
continue |
| 543 |
photo["url"] = photo[key] |
| 544 |
photo["label"] = fmtname |
| 545 |
|
| 546 |
# remove excess data |
| 547 |
keys = [ |
| 548 |
key for key in photo |
| 549 |
if key.startswith(("url_", "width_", "height_")) |
| 550 |
] |
| 551 |
for key in keys: |
| 552 |
del photo[key] |
| 553 |
break |
| 554 |
else: |
| 555 |
self._extract_photo(photo) |
| 556 |
|
| 557 |
return photo |
| 558 |
|
| 559 |
def _extract_photo(self, photo): |
| 560 |
size = self.photos_getSizes(photo["id"])[-1] |
| 561 |
photo["url"] = size["source"] |
| 562 |
photo["label"] = size["label"] |
| 563 |
photo["width"] = text.parse_int(size["width"]) |
| 564 |
photo["height"] = text.parse_int(size["height"]) |
| 565 |
return photo |
| 566 |
|
| 567 |
def _extract_video(self, photo): |
| 568 |
stream = self.video_getStreamInfo(photo["id"], photo.get("secret")) |
| 569 |
photo["url"] = stream["_content"] |
| 570 |
photo["label"] = stream["type"] |
| 571 |
photo["width"] = photo["height"] = 0 |
| 572 |
return photo |
| 573 |
|
| 574 |
def _extract_metadata(self, photo, info=True): |
| 575 |
if info and self.meta_info: |
| 576 |
try: |
| 577 |
photo.update(self.photos_getInfo(photo["id"])) |
| 578 |
photo["title"] = photo["title"]["_content"] |
| 579 |
photo["comments"] = text.parse_int( |
| 580 |
photo["comments"]["_content"]) |
| 581 |
photo["description"] = photo["description"]["_content"] |
| 582 |
photo["tags"] = [t["raw"] for t in photo["tags"]["tag"]] |
| 583 |
photo["views"] = text.parse_int(photo["views"]) |
| 584 |
photo["id"] = text.parse_int(photo["id"]) |
| 585 |
except Exception as exc: |
| 586 |
self.log.warning( |
| 587 |
"Unable to retrieve 'info' data for %s (%s: %s)", |
| 588 |
photo["id"], exc.__class__.__name__, exc) |
| 589 |
|
| 590 |
if self.meta_exif: |
| 591 |
try: |
| 592 |
photo.update(self.photos_getExif(photo["id"])) |
| 593 |
except Exception as exc: |
| 594 |
self.log.warning( |
| 595 |
"Unable to retrieve 'exif' data for %s (%s: %s)", |
| 596 |
photo["id"], exc.__class__.__name__, exc) |
| 597 |
|
| 598 |
if self.meta_contexts: |
| 599 |
try: |
| 600 |
photo.update(self.photos_getAllContexts(photo["id"])) |
| 601 |
except Exception as exc: |
| 602 |
self.log.warning( |
| 603 |
"Unable to retrieve 'contexts' data for %s (%s: %s)", |
| 604 |
photo["id"], exc.__class__.__name__, exc) |
| 605 |
|
| 606 |
if "license" in photo: |
| 607 |
photo["license_name"] = self.LICENSES.get(photo["license"]) |
| 608 |
|
| 609 |
def _clean_info(self, info): |
| 610 |
info["title"] = info["title"]["_content"] |
| 611 |
info["description"] = info["description"]["_content"] |
| 612 |
return info |