aboutsummaryrefslogtreecommitdiffstats
path: root/gallery_dl/extractor/vk.py
blob: ea42265158111f3fc6b841019b83bc20b97d34a2 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# -*- coding: utf-8 -*-

# Copyright 2021-2025 Mike Fährmann
#
# 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://vk.com/"""

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

BASE_PATTERN = r"(?:https://)?(?:www\.|m\.)?vk\.com"


class VkExtractor(Extractor):
    """Base class for vk extractors"""
    category = "vk"
    directory_fmt = ("{category}", "{user[name]|user[id]}")
    filename_fmt = "{id}.{extension}"
    archive_fmt = "{user[id]}_{id}"
    root = "https://vk.com"
    request_interval = (0.5, 1.5)

    def _init(self):
        self.offset = text.parse_int(self.config("offset"))

    def finalize(self):
        if self.offset:
            self.log.info("Use '-o offset=%s' to continue downloading "
                          "from the current position", self.offset)

    def skip(self, num):
        self.offset += num
        return num

    def items(self):
        subn = text.re(r"/imp[fg]/").subn
        sizes = "wzyxrqpo"

        data = self.metadata()
        yield Message.Directory, "", data

        for photo in self.photos():

            for size in sizes:
                size += "_"
                if size in photo:
                    break
            else:
                self.log.warning("no photo URL found (%s)", photo.get("id"))
                continue

            try:
                url = photo[size + "src"]
            except KeyError:
                self.log.warning("no photo URL found (%s)", photo.get("id"))
                continue

            url_sub, count = subn("/", url.partition("?")[0])
            if count:
                photo["_fallback"] = (url,)
                photo["url"] = url = url_sub
            else:
                photo["url"] = url

            try:
                _, photo["width"], photo["height"] = photo[size]
            except ValueError:
                # photo without width/height entries (#2535)
                photo["width"] = photo["height"] = 0

            photo["id"] = photo["id"].rpartition("_")[2]
            photo["date"] = self.parse_timestamp(text.extr(
                photo["date"], 'data-date="', '"'))
            photo["description"] = text.unescape(text.extr(
                photo.get("desc", ""), ">", "<"))
            photo.update(data)

            text.nameext_from_url(url, photo)
            yield Message.Url, url, photo

    def _pagination(self, photos_id):
        url = self.root + "/al_photos.php"
        headers = {
            "X-Requested-With": "XMLHttpRequest",
            "Origin"          : self.root,
            "Referer"         : self.root + "/" + photos_id,
        }
        data = {
            "act"      : "show",
            "al"       : "1",
            "direction": "1",
            "list"     : photos_id,
            "offset"   : self.offset,
        }

        while True:
            response = self.request(
                url, method="POST", headers=headers, data=data)
            if response.history and "/challenge.html" in response.url:
                raise exception.AbortExtraction(
                    "HTTP redirect to 'challenge' page:\n" + response.url)

            payload = response.json()["payload"][1]
            if len(payload) < 4:
                self.log.debug(payload)
                raise exception.AuthorizationError(
                    text.unescape(payload[0]) if payload[0] else None)

            total = payload[1]
            photos = payload[3]

            for i in range(len(photos)):
                photos[i]["num"] = self.offset + i + 1
                photos[i]["count"] = total

            offset_next = self.offset + len(photos)
            if offset_next >= total:
                # the last chunk of photos also contains the first few photos
                # again if 'total' is not a multiple of 10
                if extra := total - offset_next:
                    del photos[extra:]

                yield from photos
                self.offset = 0
                return

            yield from photos
            data["offset"] = self.offset = offset_next


class VkPhotosExtractor(VkExtractor):
    """Extractor for photos from a vk user"""
    subcategory = "photos"
    pattern = (BASE_PATTERN + r"/(?:"
               r"(?:albums|photos|id)(-?\d+)"
               r"|(?!(?:album|tag|wall)-?\d+_?)([^/?#]+))")
    example = "https://vk.com/id12345"

    def __init__(self, match):
        VkExtractor.__init__(self, match)
        self.user_id, self.user_name = match.groups()

    def photos(self):
        return self._pagination("photos" + self.user_id)

    def metadata(self):
        if self.user_id:
            user_id = self.user_id
            prefix = "public" if user_id[0] == "-" else "id"
            url = f"{self.root}/{prefix}{user_id.lstrip('-')}"
            data = self._extract_profile(url)
        else:
            url = f"{self.root}/{self.user_name}"
            data = self._extract_profile(url)
            self.user_id = data["user"]["id"]
        return data

    def _extract_profile(self, url):
        page = self.request(url).text
        extr = text.extract_from(page)

        user = {
            "id"  : extr('property="og:url" content="https://vk.com/id', '"'),
            "nick": text.unescape(extr(
                "<title>", " | VK</title>")),
            "info": text.unescape(extr(
                ',"activity":"', '","')).replace("\\/", "/"),
            "name": extr('href="https://m.vk.com/', '"'),
        }

        if user["id"]:
            user["group"] = False
        else:
            user["group"] = True
            user["id"] = extr('data-from-id="', '"')

        return {"user": user}


class VkAlbumExtractor(VkExtractor):
    """Extractor for a vk album"""
    subcategory = "album"
    directory_fmt = ("{category}", "{user[id]}", "{album[id]}")
    pattern = BASE_PATTERN + r"/album(-?\d+)_(\d+)$"
    example = "https://vk.com/album12345_00"

    def photos(self):
        user_id, album_id = self.groups
        return self._pagination(f"album{user_id}_{album_id}")

    def metadata(self):
        user_id, album_id = self.groups

        url = f"{self.root}/album{user_id}_{album_id}"
        page = self.request(url).text
        desc = text.extr(page, 'name="og:description" value="', '"')
        try:
            album_name, user_name, photos = desc.rsplit(" - ", 2)
        except ValueError:
            if msg := text.extr(
                    page, '<div class="message_page_title">Error</div>',
                    "</div>"):
                msg = f" ('{text.remove_html(msg)[:-5]}')"
            self.log.warning("%s_%s: Failed to extract metadata%s",
                             user_id, album_id, msg)
            return {"user": {"id": user_id}, "album": {"id": album_id}}

        return {
            "user": {
                "id"   : user_id,
                "nick" : text.unescape(user_name),
                "name" : text.unescape(text.extr(
                    page, 'class="ui_crumb" href="/', '"')),
                "group": user_id[0] == "-",
            },
            "album": {
                "id"   : album_id,
                "name" : text.unescape(album_name),
                "count": text.parse_int(photos[:-7])
            },
        }


class VkTaggedExtractor(VkExtractor):
    """Extractor for a vk tagged photos"""
    subcategory = "tagged"
    directory_fmt = ("{category}", "{user[id]}", "tags")
    pattern = BASE_PATTERN + r"/tag(-?\d+)$"
    example = "https://vk.com/tag12345"

    def __init__(self, match):
        VkExtractor.__init__(self, match)
        self.user_id = match[1]

    def photos(self):
        return self._pagination("tag" + self.user_id)

    def metadata(self):
        return {"user": {"id": self.user_id}}


class VkWallPostExtractor(VkExtractor):
    """Extractor for a vk wall post"""
    subcategory = "wall-post"
    directory_fmt = ("{category}", "{user[id]}", "wall")
    filename_fmt = "{wall[id]}_{num}.{extension}"
    pattern = BASE_PATTERN + r"/wall(-?\d+)_(\d+)"
    example = "https://vk.com/wall12345_123"

    def photos(self):
        user_id, wall_id = self.groups
        return self._pagination(f"wall{user_id}_{wall_id}")

    def metadata(self):
        user_id, wall_id = self.groups

        url = f"{self.root}/wall{user_id}_{wall_id}"
        page = self.request(url).text
        desc = text.unescape(
            text.extr(page, 'data-testid="post_description">', "</div>") or
            text.extr(page, 'name="description" content="', '"'))

        return {
            "user": {
                "id": user_id,
            },
            "wall": {
                "id": wall_id,
                "description": desc,
            },
        }