| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# This program is free software; you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License version 2 as |
| 5 |
# published by the Free Software Foundation. |
| 6 |
|
| 7 |
"""Extractors for https://lightroom.adobe.com/""" |
| 8 |
|
| 9 |
from .common import Extractor, Message |
| 10 |
from .. import text, util |
| 11 |
|
| 12 |
|
| 13 |
class LightroomGalleryExtractor(Extractor): |
| 14 |
"""Extractor for an image gallery on lightroom.adobe.com""" |
| 15 |
category = "lightroom" |
| 16 |
subcategory = "gallery" |
| 17 |
directory_fmt = ("{category}", "{user}", "{title}") |
| 18 |
filename_fmt = "{num:>04}_{id}.{extension}" |
| 19 |
archive_fmt = "{id}" |
| 20 |
pattern = r"(?:https?://)?lightroom\.adobe\.com/shares/([0-9a-f]+)" |
| 21 |
example = "https://lightroom.adobe.com/shares/0123456789abcdef" |
| 22 |
|
| 23 |
def __init__(self, match): |
| 24 |
Extractor.__init__(self, match) |
| 25 |
self.href = match[1] |
| 26 |
|
| 27 |
def items(self): |
| 28 |
# Get config |
| 29 |
url = "https://lightroom.adobe.com/shares/" + self.href |
| 30 |
response = self.request(url) |
| 31 |
album = util.json_loads( |
| 32 |
text.extr(response.text, "albumAttributes: ", "\n") |
| 33 |
) |
| 34 |
|
| 35 |
images = self.images(album) |
| 36 |
for img in images: |
| 37 |
url = img["url"] |
| 38 |
yield Message.Directory, "", img |
| 39 |
yield Message.Url, url, text.nameext_from_url(url, img) |
| 40 |
|
| 41 |
def metadata(self, album): |
| 42 |
payload = album["payload"] |
| 43 |
story = payload.get("story") or {} |
| 44 |
return { |
| 45 |
"gallery_id": self.href, |
| 46 |
"user": story.get("author", ""), |
| 47 |
"title": story.get("title", payload["name"]), |
| 48 |
} |
| 49 |
|
| 50 |
def images(self, album): |
| 51 |
album_md = self.metadata(album) |
| 52 |
base_url = album["base"] |
| 53 |
next_url = album["links"]["/rels/space_album_images_videos"]["href"] |
| 54 |
num = 1 |
| 55 |
|
| 56 |
while next_url: |
| 57 |
url = base_url + next_url |
| 58 |
page = self.request(url).text |
| 59 |
# skip 1st line as it's a JS loop |
| 60 |
data = util.json_loads(page[page.index("\n") + 1:]) |
| 61 |
|
| 62 |
base_url = data["base"].replace( |
| 63 |
"//photos.adobe.io/v2/", "//lightroom.adobe.com/v2c/", 1) |
| 64 |
for res in data["resources"]: |
| 65 |
img_url, img_size = None, 0 |
| 66 |
for key, value in res["asset"]["links"].items(): |
| 67 |
if not key.startswith("/rels/rendition_type/"): |
| 68 |
continue |
| 69 |
size = text.parse_int(key.split("/")[-1]) |
| 70 |
if size > img_size: |
| 71 |
img_size = size |
| 72 |
img_url = value["href"] |
| 73 |
|
| 74 |
if img_url: |
| 75 |
img = { |
| 76 |
"id": res["asset"]["id"], |
| 77 |
"num": num, |
| 78 |
"url": base_url + img_url, |
| 79 |
} |
| 80 |
img.update(album_md) |
| 81 |
yield img |
| 82 |
num += 1 |
| 83 |
try: |
| 84 |
next_url = data["links"]["next"]["href"] |
| 85 |
except KeyError: |
| 86 |
next_url = None |