aboutsummaryrefslogtreecommitdiffstats
path: root/gallery_dl/extractor/lightroom.py
blob: b557149c4a64b57742b18b0cf5cfe9ea18a7aca9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# -*- coding: utf-8 -*-

# 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.

"""Extractors for https://lightroom.adobe.com/"""

from .common import Extractor, Message
from .. import text, util


class LightroomGalleryExtractor(Extractor):
    """Extractor for an image gallery on lightroom.adobe.com"""
    category = "lightroom"
    subcategory = "gallery"
    directory_fmt = ("{category}", "{user}", "{title}")
    filename_fmt = "{num:>04}_{id}.{extension}"
    archive_fmt = "{id}"
    pattern = r"(?:https?://)?lightroom\.adobe\.com/shares/([0-9a-f]+)"
    example = "https://lightroom.adobe.com/shares/0123456789abcdef"

    def __init__(self, match):
        Extractor.__init__(self, match)
        self.href = match[1]

    def items(self):
        # Get config
        url = "https://lightroom.adobe.com/shares/" + self.href
        response = self.request(url)
        album = util.json_loads(
            text.extr(response.text, "albumAttributes: ", "\n")
        )

        images = self.images(album)
        for img in images:
            url = img["url"]
            yield Message.Directory, img
            yield Message.Url, url, text.nameext_from_url(url, img)

    def metadata(self, album):
        payload = album["payload"]
        story = payload.get("story") or {}
        return {
            "gallery_id": self.href,
            "user": story.get("author", ""),
            "title": story.get("title", payload["name"]),
        }

    def images(self, album):
        album_md = self.metadata(album)
        base_url = album["base"]
        next_url = album["links"]["/rels/space_album_images_videos"]["href"]
        num = 1

        while next_url:
            url = base_url + next_url
            page = self.request(url).text
            # skip 1st line as it's a JS loop
            data = util.json_loads(page[page.index("\n") + 1:])

            base_url = data["base"]
            for res in data["resources"]:
                img_url, img_size = None, 0
                for key, value in res["asset"]["links"].items():
                    if not key.startswith("/rels/rendition_type/"):
                        continue
                    size = text.parse_int(key.split("/")[-1])
                    if size > img_size:
                        img_size = size
                        img_url = value["href"]

                if img_url:
                    img = {
                        "id": res["asset"]["id"],
                        "num": num,
                        "url": base_url + img_url,
                    }
                    img.update(album_md)
                    yield img
                    num += 1
            try:
                next_url = data["links"]["next"]["href"]
            except KeyError:
                next_url = None