aboutsummaryrefslogtreecommitdiffstats
path: root/gallery_dl/extractor/danbooru.py
blob: 588d94b54c01c5b19d324b2cdfd9157bfc9dcc58 (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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# -*- coding: utf-8 -*-

# Copyright 2014-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://danbooru.donmai.us/ and other Danbooru instances"""

from .common import BaseExtractor, Message
from .. import text, util, dt


class DanbooruExtractor(BaseExtractor):
    """Base class for danbooru extractors"""
    basecategory = "Danbooru"
    filename_fmt = "{category}_{id}_{filename}.{extension}"
    page_limit = 1000
    page_start = None
    per_page = 200
    useragent = util.USERAGENT
    request_interval = (0.5, 1.5)

    def _init(self):
        self.ugoira = self.config("ugoira", False)
        self.external = self.config("external", False)
        self.includes = False

        threshold = self.config("threshold")
        if isinstance(threshold, int):
            self.threshold = 1 if threshold < 1 else threshold
        else:
            self.threshold = self.per_page - 20

        username, api_key = self._get_auth_info()
        if username:
            self.log.debug("Using HTTP Basic Auth for user '%s'", username)
            self.session.auth = util.HTTPBasicAuth(username, api_key)

    def skip(self, num):
        pages = num // self.per_page
        if pages >= self.page_limit:
            pages = self.page_limit - 1
        self.page_start = pages + 1
        return pages * self.per_page

    def items(self):
        # 'includes' initialization must be done here and not in '_init()'
        # or it'll cause an exception with e621 when 'metadata' is enabled
        if includes := self.config("metadata"):
            if isinstance(includes, (list, tuple)):
                includes = ",".join(includes)
            elif not isinstance(includes, str):
                includes = "artist_commentary,children,notes,parent,uploader"
            self.includes = includes + ",id"

        data = self.metadata()
        for post in self.posts():

            try:
                url = post["file_url"]
            except KeyError:
                if self.external and post["source"]:
                    post.update(data)
                    yield Message.Directory, "", post
                    yield Message.Queue, post["source"], post
                continue

            text.nameext_from_url(url, post)
            post["date"] = dt.parse_iso(post["created_at"])

            post["tags"] = (
                post["tag_string"].split(" ")
                if post["tag_string"] else ())
            post["tags_artist"] = (
                post["tag_string_artist"].split(" ")
                if post["tag_string_artist"] else ())
            post["tags_character"] = (
                post["tag_string_character"].split(" ")
                if post["tag_string_character"] else ())
            post["tags_copyright"] = (
                post["tag_string_copyright"].split(" ")
                if post["tag_string_copyright"] else ())
            post["tags_general"] = (
                post["tag_string_general"].split(" ")
                if post["tag_string_general"] else ())
            post["tags_meta"] = (
                post["tag_string_meta"].split(" ")
                if post["tag_string_meta"] else ())

            if post["extension"] == "zip":
                if self.ugoira:
                    post["_ugoira_original"] = False
                    post["_ugoira_frame_data"] = post["frames"] = \
                        self._ugoira_frames(post)
                    post["_http_adjust_extension"] = False
                else:
                    url = post["large_file_url"]
                    post["extension"] = "webm"

            if url[0] == "/":
                if url[1] == "/":
                    url = "https:" + url
                else:
                    url = self.root + url

            post.update(data)
            yield Message.Directory, "", post
            yield Message.Url, url, post

    def items_artists(self):
        for artist in self.artists():
            artist["_extractor"] = DanbooruTagExtractor
            url = f"{self.root}/posts?tags={text.quote(artist['name'])}"
            yield Message.Queue, url, artist

    def metadata(self):
        return ()

    def posts(self):
        return ()

    def _pagination(self, endpoint, params, prefix=None):
        url = self.root + endpoint
        params["limit"] = self.per_page
        params["page"] = self.page_start

        first = True
        while True:
            posts = self.request_json(url, params=params)
            if isinstance(posts, dict):
                posts = posts["posts"]

            if posts:
                if self.includes:
                    params_meta = {
                        "only" : self.includes,
                        "limit": len(posts),
                        "tags" : "id:" + ",".join(str(p["id"]) for p in posts),
                    }
                    data = {
                        meta["id"]: meta
                        for meta in self.request_json(url, params=params_meta)
                    }
                    for post in posts:
                        post.update(data[post["id"]])

                if prefix == "a" and not first:
                    posts.reverse()

                yield from posts

            if len(posts) < self.threshold:
                return

            if prefix:
                params["page"] = prefix + str(posts[-1]["id"])
            elif params["page"]:
                params["page"] += 1
            else:
                params["page"] = 2
            first = False

    def _ugoira_frames(self, post):
        data = self.request_json(
            f"{self.root}/posts/{post['id']}.json?only=media_metadata"
        )["media_metadata"]["metadata"]

        if "Ugoira:FrameMimeType" in data:
            ext = data["Ugoira:FrameMimeType"].rpartition("/")[2]
            if ext == "jpeg":
                ext = "jpg"
        else:
            ext = data["ZIP:ZipFileName"].rpartition(".")[2]

        delays = data["Ugoira:FrameDelays"]
        return [{"file": f"{index:>06}.{ext}", "delay": delay}
                for index, delay in enumerate(delays)]

    def _collection_posts(self, cid, ctype):
        reverse = prefix = None

        order = self.config("order-posts")
        if not order or order in {"asc", "pool", "pool_asc", "asc_pool"}:
            params = {"tags": f"ord{ctype}:{cid}"}
        elif order in {"id", "desc_id", "id_desc"}:
            params = {"tags": f"{ctype}:{cid}"}
            prefix = "b"
        elif order in {"desc", "desc_pool", "pool_desc"}:
            params = {"tags": f"ord{ctype}:{cid}"}
            reverse = True
        elif order in {"asc_id", "id_asc"}:
            params = {"tags": f"{ctype}:{cid}"}
            reverse = True

        posts = self._pagination("/posts.json", params, prefix)
        if reverse:
            self.log.info("Collecting posts of %s %s", ctype, cid)
            return self._collection_enumerate_reverse(posts)
        else:
            return self._collection_enumerate(posts)

    def _collection_metadata(self, cid, ctype, cname=None):
        url = f"{self.root}/{cname or ctype}s/{cid}.json"
        collection = self.request_json(url)
        collection["name"] = collection["name"].replace("_", " ")
        self.post_ids = collection.pop("post_ids", ())
        return {ctype: collection}

    def _collection_enumerate(self, posts):
        pid_to_num = {pid: num for num, pid in enumerate(self.post_ids, 1)}
        for post in posts:
            post["num"] = pid_to_num[post["id"]]
            yield post

    def _collection_enumerate_reverse(self, posts):
        posts = list(posts)
        posts.reverse()

        pid_to_num = {pid: num for num, pid in enumerate(self.post_ids, 1)}
        for post in posts:
            post["num"] = pid_to_num[post["id"]]
        return posts


BASE_PATTERN = DanbooruExtractor.update({
    "danbooru": {
        "root": None,
        "pattern": r"(?:(?:danbooru|hijiribe|sonohara|safebooru)\.donmai\.us"
                   r"|donmai\.moe)",
    },
    "atfbooru": {
        "root": "https://booru.allthefallen.moe",
        "pattern": r"booru\.allthefallen\.moe",
    },
    "aibooru": {
        "root": None,
        "pattern": r"(?:safe\.|general\.)?aibooru\.(?:online|download)",
    },
    "booruvar": {
        "root": "https://booru.borvar.art",
        "pattern": r"booru\.borvar\.art",
    },
})


class DanbooruTagExtractor(DanbooruExtractor):
    """Extractor for danbooru posts from tag searches"""
    subcategory = "tag"
    directory_fmt = ("{category}", "{search_tags}")
    archive_fmt = "t_{search_tags}_{id}"
    pattern = BASE_PATTERN + r"/posts\?(?:[^&#]*&)*tags=([^&#]*)"
    example = "https://danbooru.donmai.us/posts?tags=TAG"

    def metadata(self):
        self.tags = text.unquote(self.groups[-1].replace("+", " "))
        return {"search_tags": self.tags}

    def posts(self):
        prefix = "b"
        for tag in self.tags.split():
            if tag.startswith("order:"):
                if tag == "order:id" or tag == "order:id_asc":
                    prefix = "a"
                elif tag == "order:id_desc":
                    prefix = "b"
                else:
                    prefix = None
            elif tag.startswith(
                    ("id:", "md5:", "ordfav:", "ordfavgroup:", "ordpool:")):
                prefix = None
                break

        return self._pagination("/posts.json", {"tags": self.tags}, prefix)


class DanbooruRandomExtractor(DanbooruTagExtractor):
    """Extractor for a random danbooru post"""
    subcategory = "random"
    pattern = BASE_PATTERN + r"/posts/random(?:\?(?:[^&#]*&)*tags=([^&#]*))?"
    example = "https://danbooru.donmai.us/posts/random?tags=TAG"

    def metadata(self):
        tags = self.groups[-1] or ""
        self.tags = text.unquote(tags.replace("+", " "))
        return {"search_tags": self.tags}

    def posts(self):
        posts = self.request_json(self.root + "/posts/random.json",
                                  params={"tags": self.tags or None})
        return (posts,) if isinstance(posts, dict) else posts


class DanbooruPoolExtractor(DanbooruExtractor):
    """Extractor for Danbooru pools"""
    subcategory = "pool"
    directory_fmt = ("{category}", "pool", "{pool[id]} {pool[name]}")
    filename_fmt = "{num:>04}_{id}_{filename}.{extension}"
    archive_fmt = "p_{pool[id]}_{id}"
    pattern = BASE_PATTERN + r"/pool(?:s|/show)/(\d+)"
    example = "https://danbooru.donmai.us/pools/12345"

    def metadata(self):
        self.pool_id = self.groups[-1]
        return self._collection_metadata(self.pool_id, "pool")

    def posts(self):
        return self._collection_posts(self.pool_id, "pool")


class DanbooruFavgroupExtractor(DanbooruExtractor):
    """Extractor for Danbooru favorite groups"""
    subcategory = "favgroup"
    directory_fmt = ("{category}", "Favorite Groups",
                     "{favgroup[id]} {favgroup[name]}")
    filename_fmt = "{num:>04}_{id}_{filename}.{extension}"
    archive_fmt = "fg_{favgroup[id]}_{id}"
    pattern = BASE_PATTERN + r"/favorite_group(?:s|/show)/(\d+)"
    example = "https://danbooru.donmai.us/favorite_groups/12345"

    def metadata(self):
        return self._collection_metadata(
            self.groups[-1], "favgroup", "favorite_group")

    def posts(self):
        return self._collection_posts(self.groups[-1], "favgroup")


class DanbooruPostExtractor(DanbooruExtractor):
    """Extractor for single danbooru posts"""
    subcategory = "post"
    archive_fmt = "{id}"
    pattern = BASE_PATTERN + r"/post(?:s|/show)/(\d+)"
    example = "https://danbooru.donmai.us/posts/12345"

    def posts(self):
        url = f"{self.root}/posts/{self.groups[-1]}.json"
        post = self.request_json(url)
        if self.includes:
            params = {"only": self.includes}
            post.update(self.request_json(url, params=params))
        return (post,)


class DanbooruMediaassetExtractor(DanbooruExtractor):
    """Extractor for a danbooru media asset"""
    subcategory = "media-asset"
    filename_fmt = "{category}_ma{id}_{filename}.{extension}"
    archive_fmt = "m{id}"
    pattern = BASE_PATTERN + r"/media_assets/(\d+)"
    example = "https://danbooru.donmai.us/media_assets/12345"

    def posts(self):
        url = f"{self.root}/media_assets/{self.groups[-1]}.json"
        asset = self.request_json(url)

        asset["file_url"] = asset["variants"][-1]["url"]
        asset["tag_string"] = \
            asset["tag_string_artist"] = \
            asset["tag_string_character"] = \
            asset["tag_string_copyright"] = \
            asset["tag_string_general"] = \
            asset["tag_string_meta"] = ""

        if self.includes:
            params = {"only": self.includes}
            asset.update(self.request_json(url, params=params))
        return (asset,)


class DanbooruPopularExtractor(DanbooruExtractor):
    """Extractor for popular images from danbooru"""
    subcategory = "popular"
    directory_fmt = ("{category}", "popular", "{scale}", "{date}")
    archive_fmt = "P_{scale[0]}_{date}_{id}"
    pattern = BASE_PATTERN + r"/(?:explore/posts/)?popular(?:\?([^#]*))?"
    example = "https://danbooru.donmai.us/explore/posts/popular"

    def metadata(self):
        self.params = params = text.parse_query(self.groups[-1])
        scale = params.get("scale", "day")
        date = params.get("date") or dt.date.today().isoformat()

        if scale == "week":
            date = dt.date.fromisoformat(date)
            date = (date - dt.timedelta(days=date.weekday())).isoformat()
        elif scale == "month":
            date = date[:-3]

        return {"date": date, "scale": scale}

    def posts(self):
        return self._pagination("/explore/posts/popular.json", self.params)


class DanbooruArtistExtractor(DanbooruExtractor):
    """Extractor for danbooru artists"""
    subcategory = "artist"
    pattern = BASE_PATTERN + r"/artists/(\d+)"
    example = "https://danbooru.donmai.us/artists/12345"

    items = DanbooruExtractor.items_artists

    def artists(self):
        url = f"{self.root}/artists/{self.groups[-1]}.json"
        return (self.request_json(url),)


class DanbooruArtistSearchExtractor(DanbooruExtractor):
    """Extractor for danbooru artist searches"""
    subcategory = "artist-search"
    pattern = BASE_PATTERN + r"/artists/?\?([^#]+)"
    example = "https://danbooru.donmai.us/artists?QUERY"

    items = DanbooruExtractor.items_artists

    def artists(self):
        url = self.root + "/artists.json"
        params = text.parse_query(self.groups[-1])
        params["page"] = text.parse_int(params.get("page"), 1)

        while True:
            artists = self.request_json(url, params=params)

            yield from artists

            if len(artists) < 20:
                return
            params["page"] += 1