aboutsummaryrefslogtreecommitdiffstats
path: root/gallery_dl/extractor/lensdump.py
blob: 12e8860c177488adc2b654eaddd73cf7756861b2 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# -*- 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://lensdump.com/"""

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

BASE_PATTERN = r"(?:https?://)?lensdump\.com"


class LensdumpBase():
    """Base class for lensdump extractors"""
    category = "lensdump"
    root = "https://lensdump.com"

    def nodes(self, page=None):
        if page is None:
            page = self.request(self.url).text

        # go through all pages starting from the oldest
        page_url = text.urljoin(self.root, text.extr(
            text.extr(page, ' id="list-most-oldest-link"', '>'),
            'href="', '"'))
        while page_url is not None:
            if page_url == self.url:
                current_page = page
            else:
                current_page = self.request(page_url).text

            for node in text.extract_iter(
                    current_page, ' class="list-item ', '>'):
                yield node

            # find url of next page
            page_url = text.extr(
                text.extr(current_page, ' data-pagination="next"', '>'),
                'href="', '"')
            if page_url is not None and len(page_url) > 0:
                page_url = text.urljoin(self.root, page_url)
            else:
                page_url = None


class LensdumpAlbumExtractor(LensdumpBase, GalleryExtractor):
    subcategory = "album"
    pattern = BASE_PATTERN + r"/(?:((?!\w+/albums|a/|i/)\w+)|a/(\w+))"
    example = "https://lensdump.com/a/ID"

    def __init__(self, match):
        GalleryExtractor.__init__(self, match, match.string)
        self.gallery_id = match.group(1) or match.group(2)

    def metadata(self, page):
        return {
            "gallery_id": self.gallery_id,
            "title": text.unescape(text.extr(
                page, 'property="og:title" content="', '"').strip())
        }

    def images(self, page):
        for node in self.nodes(page):
            # get urls and filenames of images in current page
            json_data = util.json_loads(text.unquote(
                text.extr(node, "data-object='", "'") or
                text.extr(node, 'data-object="', '"')))
            image_id = json_data.get('name')
            image_url = json_data.get('url')
            image_title = json_data.get('title')
            if image_title is not None:
                image_title = text.unescape(image_title)
            yield (image_url, {
                'id': image_id,
                'url': image_url,
                'title': image_title,
                'name': json_data.get('filename'),
                'filename': image_id,
                'extension': json_data.get('extension'),
                'height': text.parse_int(json_data.get('height')),
                'width': text.parse_int(json_data.get('width')),
            })


class LensdumpAlbumsExtractor(LensdumpBase, Extractor):
    """Extractor for album list from lensdump.com"""
    subcategory = "albums"
    pattern = BASE_PATTERN + r"/\w+/albums"
    example = "https://lensdump.com/USER/albums"

    def items(self):
        for node in self.nodes():
            album_url = text.urljoin(self.root, text.extr(
                node, 'data-url-short="', '"'))
            yield Message.Queue, album_url, {
                "_extractor": LensdumpAlbumExtractor}


class LensdumpImageExtractor(LensdumpBase, Extractor):
    """Extractor for individual images on lensdump.com"""
    subcategory = "image"
    filename_fmt = "{category}_{id}{title:?_//}.{extension}"
    directory_fmt = ("{category}",)
    archive_fmt = "{id}"
    pattern = r"(?:https?://)?(?:(?:i\d?\.)?lensdump\.com|\w\.l3n\.co)/i/(\w+)"
    example = "https://lensdump.com/i/ID"

    def __init__(self, match):
        Extractor.__init__(self, match)
        self.key = match.group(1)

    def items(self):
        url = "{}/i/{}".format(self.root, self.key)
        extr = text.extract_from(self.request(url).text)

        data = {
            "id"    : self.key,
            "title" : text.unescape(extr(
                'property="og:title" content="', '"')),
            "url"   : extr(
                'property="og:image" content="', '"'),
            "width" : text.parse_int(extr(
                'property="image:width" content="', '"')),
            "height": text.parse_int(extr(
                'property="image:height" content="', '"')),
            "date"  : text.parse_datetime(extr(
                '<span title="', '"'), "%Y-%m-%d %H:%M:%S"),
        }

        text.nameext_from_url(data["url"], data)
        yield Message.Directory, data
        yield Message.Url, data["url"], data