Debian Codegraph @

gallery_dl/extractor/myhentaigallery.py

raw
1 # -*- coding: utf-8 -*-
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License version 2 as
5 # published by the Free Software Foundation.
6
7 """Extractors for https://myhentaigallery.com/"""
8
9 from .common import Extractor, GalleryExtractor, Message
10 from .. import text
11
12 BASE_PATTERN = r"(?:https?://)?myhentaigallery\.com"
13
14
15 class MyhentaigalleryBase():
16 category = "myhentaigallery"
17 root = "https://myhentaigallery.com"
18
19
20 class MyhentaigalleryGalleryExtractor(MyhentaigalleryBase, GalleryExtractor):
21 """Extractor for image galleries from myhentaigallery.com"""
22 directory_fmt = ("{category}", "{gallery_id} {artist:?[/] /J, }{title}")
23 pattern = BASE_PATTERN + r"/(?:a|g(?:allery/(?:thumbnails|show))?)/(\d+)"
24 example = "https://myhentaigallery.com/a/12345"
25
26 def __init__(self, match):
27 self.gallery_id = match[1]
28 url = f"{self.root}/a/{self.gallery_id}"
29 GalleryExtractor.__init__(self, match, url)
30
31 def _init(self):
32 self.session.headers["Referer"] = self.page_url
33
34 def metadata(self, page):
35 extr = text.extract_from(page)
36 split = text.split_html
37
38 title = extr('<div class="comic-description">\n', '</h1>').lstrip()
39 if title.startswith("<h1>"):
40 title = title[4:]
41
42 if not title:
43 raise self.exc.NotFoundError("gallery")
44
45 return {
46 "title" : text.unescape(title),
47 "gallery_id": text.parse_int(self.gallery_id),
48 "tags" : split(extr(" Categories:", "</div>")),
49 "artist" : split(extr(" Artists:" , "</div>")),
50 "group" : split(extr(" Groups:" , "</div>")),
51 "parodies" : split(extr(" Parodies:" , "</div>")),
52 }
53
54 def images(self, page):
55 return [
56 (text.unescape(text.extr(url, 'src="', '"')).replace(
57 "/thumbnail/", "/original/"), None)
58 for url in text.extract_iter(page, 'class="comic-thumb"', '</div>')
59 ]
60
61
62 class MyhentaigalleryTagExtractor(MyhentaigalleryBase, Extractor):
63 """Extractor for myhentaigallery tag searches"""
64 subcategory = "tag"
65 pattern = BASE_PATTERN + r"/[ag]/((artist|category|group|parody)/(\d+).*)"
66 example = "https://myhentaigallery.com/a/category/123"
67
68 def items(self):
69 data = {"_extractor": MyhentaigalleryGalleryExtractor}
70 for url in self.galleries():
71 yield Message.Queue, url, data
72
73 def galleries(self):
74 root = self.root
75 url = f"{root}/a/{self.groups[0]}"
76
77 while True:
78 page = self.request(url).text
79
80 for inner in text.extract_iter(
81 page, '<div class="comic-inner">', "<div"):
82 yield root + text.extr(inner, 'href="', '"')
83
84 try:
85 pos = page.index(">Next<")
86 except ValueError:
87 return
88 url = root + text.rextr(page, 'href="', '"', pos)