Debian Codegraph @

gallery_dl/extractor/imhentai.py

raw
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2025-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 https://imhentai.xxx/ and mirror sites"""
10
11 from .common import GalleryExtractor, BaseExtractor, Message
12 from .. import text, util
13
14
15 class ImhentaiExtractor(BaseExtractor):
16 basecategory = "IMHentai"
17
18 def _pagination(self, url):
19 prev = None
20 base = self.root + "/gallery/"
21 data = {"_extractor": ImhentaiGalleryExtractor}
22
23 while True:
24 page = self.request(url).text
25
26 pos = page.find('class="ranking_list"')
27 if pos >= 0:
28 page = page[:pos]
29
30 extr = text.extract_from(page)
31
32 while True:
33 gallery_id = extr('href="/gallery/', '"')
34 if gallery_id == prev:
35 continue
36 if not gallery_id:
37 break
38 yield Message.Queue, base + gallery_id, data
39 prev = gallery_id
40
41 href = text.rextr(page, "class='page-link' href='", "'")
42 if not href or href == "#":
43 return
44 if href[0] == "/":
45 if href[1] == "/":
46 href = "https:" + href
47 else:
48 href = self.root + href
49 url = href
50
51
52 BASE_PATTERN = ImhentaiExtractor.update({
53 "imhentai": {
54 "root": "https://imhentai.xxx",
55 "pattern": r"(?:www\.)?imhentai\.xxx",
56 },
57 "hentaiera": {
58 "root": "https://hentaiera.com",
59 "pattern": r"(?:www\.)?hentaiera\.com",
60 },
61 "hentairox": {
62 "root": "https://hentairox.com",
63 "pattern": r"(?:www\.)?hentairox\.com",
64 },
65 "hentaifox": {
66 "root": "https://hentaifox.com",
67 "pattern": r"(?:www\.)?hentaifox\.com",
68 },
69 "hentaienvy": {
70 "root": "https://hentaienvy.com",
71 "pattern": r"(?:www\.)?hentaienvy\.com",
72 },
73 "hentaizap": {
74 "root": "https://hentaizap.com",
75 "pattern": r"(?:www\.)?hentaizap\.com",
76 },
77 })
78
79
80 class ImhentaiGalleryExtractor(ImhentaiExtractor, GalleryExtractor):
81 """Extractor for imhentai galleries"""
82 pattern = BASE_PATTERN + r"/(?:gallery|view)/(\d+)"
83 example = "https://imhentai.xxx/gallery/12345/"
84
85 def __init__(self, match):
86 ImhentaiExtractor.__init__(self, match)
87 self.gallery_id = self.groups[-1]
88 self.page_url = f"{self.root}/gallery/{self.gallery_id}/"
89
90 def metadata(self, page):
91 extr = text.extract_from(page)
92 title = extr("<h1>", "<")
93 title_alt = extr('class="subtitle">', "<")
94 end = "</li>" if extr('<ul class="galleries_info', ">") else "</ul>"
95
96 data = {
97 "gallery_id": text.parse_int(self.gallery_id),
98 "title" : text.unescape(title),
99 "title_alt" : text.unescape(title_alt),
100 "parody" : self._split(extr(">Parodies", end)),
101 "character" : self._split(extr(">Characters", end)),
102 "tags" : self._split(extr(">Tags", end)),
103 "artist" : self._split(extr(">Artists", end)),
104 "group" : self._split(extr(">Groups", end)),
105 "language" : self._split(extr(">Languages", end)),
106 "type" : extr("href='/category/", "/"),
107 }
108
109 if data["language"]:
110 data["lang"] = util.language_to_code(data["language"][0])
111
112 return data
113
114 def _split(self, html):
115 results = []
116 for tag in text.extract_iter(html, ">", "</a>"):
117 badge = ("badge'>" in tag or "class='badge" in tag)
118 tag = text.remove_html(tag)
119 if badge:
120 tag = tag.rpartition(" ")[0]
121 results.append(tag)
122 results.sort()
123 return results
124
125 def images(self, page):
126 base = text.extr(page, 'data-src="', '"').rpartition("/")[0] + "/"
127 exts = {"j": "jpg", "p": "png", "g": "gif", "w": "webp", "a": "avif"}
128
129 try:
130 data = util.json_loads(text.extr(page, "$.parseJSON('", "'"))
131 except Exception:
132 data = None
133
134 if data is None:
135 self.log.warning("%s: Missing image data", self.gallery_id)
136
137 def _fallback_exts(i):
138 for ext in util.advance(exts.values(), 1):
139 yield f"{base}{i}.{ext}"
140 cnt = text.parse_int(text.extr(
141 page, 'id="load_pages" value="', '"'))
142 return [(f"{base}{i}.jpg", {"_fallback": _fallback_exts(i)})
143 for i in range(1, cnt+1)]
144
145 results = []
146 for i in map(str, range(1, len(data)+1)):
147 ext, width, height = data[i].split(",")
148 url = f"{base}{i}.{exts[ext]}"
149 results.append((url, {
150 "width" : text.parse_int(width),
151 "height": text.parse_int(height),
152 }))
153 return results
154
155
156 class ImhentaiTagExtractor(ImhentaiExtractor):
157 """Extractor for imhentai tag searches"""
158 subcategory = "tag"
159 pattern = (BASE_PATTERN + r"(/(?:"
160 r"artist|category|character|group|language|parody|tag"
161 r")/([^/?#]+))")
162 example = "https://imhentai.xxx/tag/TAG/"
163
164 def items(self):
165 url = self.root + self.groups[-2] + "/"
166 return self._pagination(url)
167
168
169 class ImhentaiSearchExtractor(ImhentaiExtractor):
170 """Extractor for imhentai search results"""
171 subcategory = "search"
172 pattern = BASE_PATTERN + r"(/(?:advanced-)?search/?\?[^#]+|/[^/?#]+/?)"
173 example = "https://imhentai.xxx/search/?key=QUERY"
174
175 def items(self):
176 return self._pagination(self.root + self.groups[-1])