Debian Codegraph @

gallery_dl/extractor/hentainexus.py

raw
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2019-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://hentainexus.com/"""
10
11 from .common import GalleryExtractor, Extractor, Message
12 from .. import text, util
13
14
15 class HentainexusGalleryExtractor(GalleryExtractor):
16 """Extractor for hentainexus galleries"""
17 category = "hentainexus"
18 root = "https://hentainexus.com"
19 pattern = (r"(?i)(?:https?://)?(?:www\.)?hentainexus\.com"
20 r"/(?:view|read)/(\d+)")
21 example = "https://hentainexus.com/view/12345"
22
23 def __init__(self, match):
24 self.gallery_id = match[1]
25 url = f"{self.root}/view/{self.gallery_id}"
26 GalleryExtractor.__init__(self, match, url)
27
28 def metadata(self, page):
29 rmve = text.remove_html
30 extr = text.extract_from(page)
31 data = {
32 "gallery_id": text.parse_int(self.gallery_id),
33 "cover" : extr('"og:image" content="', '"'),
34 "title" : extr('<h1 class="title">', '</h1>'),
35 }
36
37 for key in ("Artist", "Book", "Circle", "Event", "Language",
38 "Magazine", "Parody", "Publisher", "Description"):
39 value = rmve(extr('viewcolumn">' + key + '</td>', '</td>'))
40 value, sep, rest = value.rpartition(" (")
41 data[key.lower()] = value if sep else rest
42
43 data["tags"] = tags = []
44 for k in text.extract_iter(page, '<a href="/?q=tag:', '"'):
45 tags.append(text.unquote(k).strip('"').replace("+", " "))
46
47 if not data["language"]:
48 data["language"] = "English"
49 data["lang"] = util.language_to_code(data["language"])
50
51 if "doujin" in data["tags"]:
52 data["type"] = "Doujinshi"
53 elif "illustration" in data["tags"]:
54 data["type"] = "Illustration"
55 else:
56 data["type"] = "Manga"
57 data["title_conventional"] = self._join_title(data)
58 return data
59
60 def images(self, _):
61 url = f"{self.root}/read/{self.gallery_id}"
62 page = self.request(url).text
63 imgs = util.json_loads(self._decode(text.extr(
64 page, 'initReader("', '"')))
65
66 headers = None
67 if not self.config("original", True):
68 headers = {"Accept": "image/webp,*/*"}
69 for img in imgs:
70 img["_http_headers"] = headers
71
72 results = []
73 for img in imgs:
74 try:
75 results.append(
76 (img.get("image_source") or img["image_fallback"], img))
77 except KeyError:
78 pass
79 return results
80
81 def _decode(self, data):
82 # https://hentainexus.com/static/js/reader.min.js?r=22
83 hostname = "hentainexus.com"
84 primes = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53)
85 blob = list(util.b64rdecode(data))
86 for i in range(0, len(hostname)):
87 blob[i] = blob[i] ^ ord(hostname[i])
88
89 key = blob[0:64]
90
91 C = 0
92 for k in key:
93 C = C ^ k
94 for _ in range(8):
95 if C & 1:
96 C = C >> 1 ^ 0xc
97 else:
98 C = C >> 1
99 k = primes[C & 0x7]
100
101 x = 0
102 L = len(key)
103 S = list(range(256))
104 for i in range(256):
105 x = (x + S[i] + key[i % L]) & 255
106 S[i], S[x] = S[x], S[i]
107
108 result = []
109 a = c = m = x = 0
110 for n in range(64, len(blob)):
111 a = (a + k) & 255
112 x = (c + S[(x + S[a]) & 255]) & 255
113 c = (c + a + S[a]) & 255
114
115 S[a], S[x] = S[x], S[a]
116 m = S[(x + S[(a + S[(m + c) & 255]) & 255]) & 255]
117 result.append(chr(blob[n] ^ m))
118 return "".join(result)
119
120 def _join_title(self, data):
121 event = data['event']
122 artist = data['artist']
123 circle = data['circle']
124 title = data['title']
125 parody = data['parody']
126 book = data['book']
127 magazine = data['magazine']
128
129 # a few galleries have a large number of artists or parodies,
130 # which get replaced with "Various" in the title string
131 if artist.count(',') >= 3:
132 artist = 'Various'
133 if parody.count(',') >= 3:
134 parody = 'Various'
135
136 jt = ''
137 if event:
138 jt += f'({event}) '
139 if circle:
140 jt += f'[{circle} ({artist})] '
141 else:
142 jt += f'[{artist}] '
143 jt += title
144 if parody.lower() != 'original work':
145 jt += f' ({parody})'
146 if book:
147 jt += f' ({book})'
148 if magazine:
149 jt += f' ({magazine})'
150 return jt
151
152
153 class HentainexusSearchExtractor(Extractor):
154 """Extractor for hentainexus search results"""
155 category = "hentainexus"
156 subcategory = "search"
157 root = "https://hentainexus.com"
158 pattern = (r"(?i)(?:https?://)?(?:www\.)?hentainexus\.com"
159 r"(?:/page/\d+)?/?(?:\?(q=[^/?#]+))?$")
160 example = "https://hentainexus.com/?q=QUERY"
161
162 def items(self):
163 params = text.parse_query(self.groups[0])
164 data = {"_extractor": HentainexusGalleryExtractor}
165 path = "/"
166
167 while path:
168 page = self.request(self.root + path, params=params).text
169 extr = text.extract_from(page)
170
171 while True:
172 gallery_id = extr('<a href="/view/', '"')
173 if not gallery_id:
174 break
175 yield Message.Queue, self.root + "/view/" + gallery_id, data
176
177 path = extr('class="pagination-next" href="', '"')