Debian Codegraph @

gallery_dl/extractor/lightroom.py

raw symbols SCIP
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"]
63 for res in data["resources"]:
64 img_url, img_size = None, 0
65 for key, value in res["asset"]["links"].items():
66 if not key.startswith("/rels/rendition_type/"):
67 continue
68 size = text.parse_int(key.split("/")[-1])
69 if size > img_size:
70 img_size = size
71 img_url = value["href"]
72
73 if img_url:
74 img = {
75 "id": res["asset"]["id"],
76 "num": num,
77 "url": base_url + img_url,
78 }
79 img.update(album_md)
80 yield img
81 num += 1
82 try:
83 next_url = data["links"]["next"]["href"]
84 except KeyError:
85 next_url = None