Debian Codegraph @

gallery_dl/extractor/shimmie2.py

raw
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2023-2026 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 Shimmie2 instances"""
10
11 from .common import BaseExtractor, Message
12 from .. import text
13
14
15 class Shimmie2Extractor(BaseExtractor):
16 """Base class for shimmie2 extractors"""
17 basecategory = "shimmie2"
18 filename_fmt = "{category}_{id}{md5:?_//}.{extension}"
19 archive_fmt = "{id}"
20
21 def _init(self):
22 if cookies := self.config_instance("cookies"):
23 domain = self.root.rpartition("/")[2]
24 self.cookies_update_dict(cookies, domain=domain)
25
26 if file_url := self.config_instance("file_url"):
27 self.file_url_fmt = file_url
28 if quote := self.config_instance("quote"):
29 self._quote_type = lambda _: quote
30
31 def items(self):
32 data = self.metadata()
33
34 for post in self.posts():
35
36 post["id"] = text.parse_int(post["id"])
37 post["width"] = text.parse_int(post["width"])
38 post["height"] = text.parse_int(post["height"])
39 post["tags"] = text.unquote(post["tags"])
40 post.update(data)
41
42 url = post["file_url"]
43 if "/index.php?" in url:
44 post["filename"], _, post["extension"] = \
45 url.rpartition("/")[2].rpartition(".")
46 else:
47 text.nameext_from_url(url, post)
48
49 yield Message.Directory, "", post
50 yield Message.Url, url, post
51
52 def metadata(self):
53 """Return general metadata"""
54 return ()
55
56 def posts(self):
57 """Return an iterable containing data of all relevant posts"""
58 return ()
59
60 def _quote_type(self, page):
61 """Return quoting character used in 'page' (' or ")"""
62 try:
63 return page[page.index("<link rel=")+10]
64 except Exception:
65 return "'"
66
67
68 BASE_PATTERN = Shimmie2Extractor.update({
69 "cavemanon": {
70 "root": "https://booru.cavemanon.xyz",
71 "pattern": r"booru\.cavemanon\.xyz",
72 "file_url": "{0}/index.php?q=image/{2}.{4}",
73 },
74 "rule34hentai": {
75 "root": "https://rule34hentai.net",
76 "pattern": r"rule34hentai\.net",
77 },
78 "vidyapics": {
79 "root": "https://vidya.pics",
80 "pattern": r"vidya\.pics",
81 },
82 "nozrip": {
83 "root": "https://noz.rip/booru",
84 "pattern": r"noz\.rip/booru",
85 },
86 "thecollectionS": {
87 "root": "https://co.llection.pics",
88 "pattern": r"co\.llection\.pics",
89 },
90 "prequelfanart": {
91 "root": "https://www.prequeladventure.com/fanartbooru",
92 "pattern": r"(?:www\.)?prequeladventure\.com/fanartbooru",
93 },
94 }) + r"/(?:index\.php\?q=/?)?"
95
96
97 class Shimmie2TagExtractor(Shimmie2Extractor):
98 """Extractor for shimmie2 posts by tag search"""
99 subcategory = "tag"
100 directory_fmt = ("{category}", "{search_tags}")
101 file_url_fmt = "{}/_images/{}/{}%20-%20{}.{}"
102 pattern = BASE_PATTERN + r"post/list/([^/?#]+)(?:/(\d+))?"
103 example = "https://vidya.pics/post/list/TAG/1"
104
105 def metadata(self):
106 self.tags = text.unquote(self.groups[-2])
107 return {"search_tags": self.tags}
108
109 def posts(self):
110 pnum = text.parse_int(self.groups[-1], 1)
111 file_url_fmt = self.file_url_fmt.format
112
113 init = True
114 mime = ""
115
116 while True:
117 url = f"{self.root}/post/list/{self.tags}/{pnum}"
118 page = self.request(url).text
119 extr = text.extract_from(page)
120
121 if init:
122 init = False
123 quote = self._quote_type(page)
124 quote_alt = "'" if quote == '"' else '"'
125 has_mime = (" data-mime=" in page)
126 has_pid = (" data-post-id=" in page)
127
128 while True:
129 if has_mime:
130 mime = extr(" data-mime="+quote, quote)
131 if has_pid:
132 pid = extr(" data-post-id="+quote, quote)
133 else:
134 pid = extr(" href='/post/view/", quote)
135
136 if not pid:
137 break
138
139 data = extr("title="+quote, quote).split(" // ")
140 tags = data[0]
141
142 try:
143 width, _, height = data[1].partition("x")
144 size = data[2]
145 except Exception:
146 width = height = 0
147 size = ""
148
149 md5 = extr("/_thumbs/", "/")
150
151 yield {
152 "file_url": file_url_fmt(
153 self.root, md5, pid, text.quote(tags),
154 mime.rpartition("/")[2] if mime else "jpg"),
155 "id": pid,
156 "md5": md5,
157 "tags": tags,
158 "width": width,
159 "height": height,
160 "size": text.parse_bytes(size[:-1]),
161 }
162
163 pnum += 1
164 if not extr(f"/{pnum}{quote}>Next</", ">") and \
165 not extr(f"/{pnum}{quote_alt}>Next</", ">"):
166 return
167
168
169 class Shimmie2PostExtractor(Shimmie2Extractor):
170 """Extractor for single shimmie2 posts"""
171 subcategory = "post"
172 pattern = BASE_PATTERN + r"post/view/(\d+)"
173 example = "https://vidya.pics/post/view/12345"
174
175 def posts(self):
176 post_id = self.groups[-1]
177 root = self.root
178 base = root if (pos := root.find("/", 8)) < 0 else root[:pos]
179
180 url = f"{root}/post/view/{post_id}"
181 page = self.request(url).text
182 extr = text.extract_from(page)
183 qt = self._quote_type(page)
184
185 post = {
186 "id" : post_id,
187 "tags": extr(": ", "<").partition(" - ")[0].rstrip(")"),
188 "md5" : extr("/_thumbs/", "/"),
189 "" : (extr(f"id={qt}main_image{qt}", ">") or
190 extr("<source ", ">")),
191 "source": text.extr(
192 extr(">Source Link<", "</tr>"), "href="+qt, qt),
193 "parent_id": text.parse_int(text.remove_html(
194 extr("Parent</th>", "</tr>")), None),
195 "rating": text.remove_html(
196 extr(">Rating</th>", "</tr>")),
197 "size": text.parse_bytes(
198 extr(">Info</th>", "B</").rpartition(" // ")[2]),
199 }
200
201 file = post.pop("")
202 post["file_url"] = base + text.extr(file, "src="+qt, qt).lstrip(".")
203 post["width"] = text.extr(file, "data-width="+qt, qt)
204 post["height"] = text.extr(
205 file, "data-height="+qt, qt).partition(" ")[0]
206
207 if not post["md5"]:
208 post["md5"] = text.extr(post["file_url"], "/_images/", "/")
209
210 return (post,)