Debian Codegraph @

gallery_dl/extractor/gelbooru_v01.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2021-2025 Mike Fährmann
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License version 2 as
7 # published by the Free Software Foundation.
8
9 """Extractors for Gelbooru Beta 0.1.11 sites"""
10
11 from . import booru
12 from .. import text
13
14
15 class GelbooruV01Extractor(booru.BooruExtractor):
16 basecategory = "gelbooru_v01"
17 per_page = 20
18
19 def _parse_post(self, post_id):
20 url = f"{self.root}/index.php?page=post&s=view&id={post_id}"
21 extr = text.extract_from(self.request(url).text)
22
23 post = {
24 "id" : post_id,
25 "created_at": extr('Posted: ', ' <'),
26 "uploader" : extr('By: ', ' <'),
27 "width" : extr('Size: ', 'x'),
28 "height" : extr('', ' <'),
29 "source" : extr('Source: ', ' <'),
30 "rating" : (extr('Rating: ', '<') or "?")[0].lower(),
31 "score" : extr('Score: ', ' <'),
32 "file_url" : extr('<img alt="img" src="', '"'),
33 "tags" : text.unescape(extr(
34 'id="tags" name="tags" cols="40" rows="5">', '<')),
35 }
36
37 post["md5"] = post["file_url"].rpartition("/")[2].partition(".")[0]
38 post["date"] = self.parse_datetime_iso(post["created_at"])
39
40 return post
41
42 def skip_files(self, num):
43 self.page_start += num
44 return num
45
46 def _pagination(self, url, begin, end):
47 pid = self.page_start
48
49 while True:
50 page = self.request(url + str(pid)).text
51
52 cnt = 0
53 for post_id in text.extract_iter(page, begin, end):
54 yield self._parse_post(post_id)
55 cnt += 1
56
57 if cnt < self.per_page:
58 return
59 pid += self.per_page
60
61
62 BASE_PATTERN = GelbooruV01Extractor.update({
63 "thecollection": {
64 "root": "https://the-collection.booru.org",
65 "pattern": r"the-collection\.booru\.org",
66 },
67 "illusioncardsbooru": {
68 "root": "https://illusioncards.booru.org",
69 "pattern": r"illusioncards\.booru\.org",
70 },
71 "allgirlbooru": {
72 "root": "https://allgirl.booru.org",
73 "pattern": r"allgirl\.booru\.org",
74 },
75 "drawfriends": {
76 "root": "https://drawfriends.booru.org",
77 "pattern": r"drawfriends\.booru\.org",
78 },
79 "vidyart2": {
80 "root": "https://vidyart2.booru.org",
81 "pattern": r"vidyart2\.booru\.org",
82 },
83 })
84
85
86 class GelbooruV01TagExtractor(GelbooruV01Extractor):
87 subcategory = "tag"
88 directory_fmt = ("{category}", "{search_tags}")
89 archive_fmt = "t_{search_tags}_{id}"
90 pattern = BASE_PATTERN + r"/index\.php\?page=post&s=list&tags=([^&#]+)"
91 example = "https://allgirl.booru.org/index.php?page=post&s=list&tags=TAG"
92
93 def metadata(self):
94 self.tags = tags = self.groups[-1]
95 return {"search_tags": text.unquote(tags.replace("+", " "))}
96
97 def posts(self):
98 url = f"{self.root}/index.php?page=post&s=list&tags={self.tags}&pid="
99 return self._pagination(url, 'class="thumb"><a id="p', '"')
100
101
102 class GelbooruV01FavoriteExtractor(GelbooruV01Extractor):
103 subcategory = "favorite"
104 directory_fmt = ("{category}", "favorites", "{favorite_id}")
105 archive_fmt = "f_{favorite_id}_{id}"
106 per_page = 50
107 pattern = BASE_PATTERN + r"/index\.php\?page=favorites&s=view&id=(\d+)"
108 example = "https://allgirl.booru.org/index.php?page=favorites&s=view&id=1"
109
110 def metadata(self):
111 self.favorite_id = fav_id = self.groups[-1]
112 return {"favorite_id": text.parse_int(fav_id)}
113
114 def posts(self):
115 url = (f"{self.root}/index.php"
116 f"?page=favorites&s=view&id={self.favorite_id}&pid=")
117 return self._pagination(url, "posts[", "]")
118
119
120 class GelbooruV01PostExtractor(GelbooruV01Extractor):
121 subcategory = "post"
122 archive_fmt = "{id}"
123 pattern = BASE_PATTERN + r"/index\.php\?page=post&s=view&id=(\d+)"
124 example = "https://allgirl.booru.org/index.php?page=post&s=view&id=12345"
125
126 def posts(self):
127 return (self._parse_post(self.groups[-1]),)