summaryrefslogtreecommitdiffstats
path: root/gallery_dl/extractor/danbooru.py
blob: e8d3abf9c7b3efc24d48c2b7caa5c8df05b8d396 (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
# -*- coding: utf-8 -*-

# Copyright 2014-2019 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.

"""Extract images from https://danbooru.donmai.us/"""

from . import booru


BASE_PATTERN = (
    r"(?:https?://)?"
    r"(?P<subdomain>danbooru|hijiribe|sonohara|safebooru)"
    r"\.donmai\.us")


class DanbooruExtractor(booru.DanbooruPageMixin, booru.BooruExtractor):
    """Base class for danbooru extractors"""
    category = "danbooru"
    page_limit = 1000

    def __init__(self, match):
        super().__init__(match)
        self.subdomain = match.group("subdomain")
        self.scheme = "https" if self.subdomain == "danbooru" else "http"
        self.api_url = "{scheme}://{subdomain}.donmai.us/posts.json".format(
            scheme=self.scheme, subdomain=self.subdomain)
        self.ugoira = self.config("ugoira", True)

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


class DanbooruTagExtractor(booru.TagMixin, DanbooruExtractor):
    """Extractor for images from danbooru based on search-tags"""
    pattern = BASE_PATTERN + r"/posts\?(?:[^&#]*&)*tags=(?P<tags>[^&#]+)"
    test = (
        ("https://danbooru.donmai.us/posts?tags=bonocho", {
            "content": "b196fb9f1668109d7774a0a82efea3ffdda07746",
        }),
        # test page transitions
        ("https://danbooru.donmai.us/posts?tags=canvas_%28cocktail_soft%29", {
            "count": ">= 50",
        }),
        ("https://hijiribe.donmai.us/posts?tags=bonocho"),
        ("https://sonohara.donmai.us/posts?tags=bonocho"),
        ("https://safebooru.donmai.us/posts?tags=bonocho"),
    )


class DanbooruPoolExtractor(booru.PoolMixin, DanbooruExtractor):
    """Extractor for image-pools from danbooru"""
    pattern = BASE_PATTERN + r"/pools/(?P<pool>\d+)"
    test = ("https://danbooru.donmai.us/pools/7659", {
        "content": "b16bab12bea5f7ea9e0a836bf8045f280e113d99",
    })


class DanbooruPostExtractor(booru.PostMixin, DanbooruExtractor):
    """Extractor for single images from danbooru"""
    pattern = BASE_PATTERN + r"/posts/(?P<post>\d+)"
    test = (
        ("https://danbooru.donmai.us/posts/294929", {
            "content": "5e255713cbf0a8e0801dc423563c34d896bb9229",
        }),
        ("https://danbooru.donmai.us/posts/3613024", {
            "pattern": r"https?://.+\.webm$",
            "options": (("ugoira", False),)
        })
    )


class DanbooruPopularExtractor(booru.PopularMixin, DanbooruExtractor):
    """Extractor for popular images from danbooru"""
    pattern = BASE_PATTERN + r"/explore/posts/popular(?:\?(?P<query>[^#]*))?"
    test = (
        ("https://danbooru.donmai.us/explore/posts/popular"),
        (("https://danbooru.donmai.us/explore/posts/popular"
          "?date=2013-06-06+03%3A34%3A22+-0400&scale=week"), {
            "count": ">= 1",
        }),
    )

    def __init__(self, match):
        super().__init__(match)
        urlfmt = "{scheme}://{subdomain}.donmai.us/explore/posts/popular.json"
        self.api_url = urlfmt.format(
            scheme=self.scheme, subdomain=self.subdomain)