| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Extractors for https://nhentai.net/""" |
| 10 |
|
| 11 |
from .common import GalleryExtractor, Extractor, Message |
| 12 |
from .. import text, util |
| 13 |
import collections |
| 14 |
import random |
| 15 |
|
| 16 |
|
| 17 |
class NhentaiGalleryExtractor(GalleryExtractor): |
| 18 |
"""Extractor for image galleries from nhentai.net""" |
| 19 |
category = "nhentai" |
| 20 |
root = "https://nhentai.net" |
| 21 |
pattern = r"(?:https?://)?nhentai\.net/g/(\d+)" |
| 22 |
example = "https://nhentai.net/g/12345/" |
| 23 |
|
| 24 |
def metadata(self, _): |
| 25 |
url = f"{self.root}/api/v2/galleries/{self.groups[0]}" |
| 26 |
self.data = data = self.request_json(url) |
| 27 |
|
| 28 |
title = data["title"] |
| 29 |
title_en = title.get("english", "") |
| 30 |
title_ja = title.get("japanese", "") |
| 31 |
|
| 32 |
info = collections.defaultdict(list) |
| 33 |
for tag in data["tags"]: |
| 34 |
info[tag["type"]].append(tag["name"]) |
| 35 |
|
| 36 |
language = "" |
| 37 |
for language in info["language"]: |
| 38 |
if language != "translated": |
| 39 |
language = language.capitalize() |
| 40 |
break |
| 41 |
|
| 42 |
return { |
| 43 |
"title" : title_en or title_ja, |
| 44 |
"title_en" : title_en, |
| 45 |
"title_ja" : title_ja, |
| 46 |
"title_pretty": title.get("pretty", ""), |
| 47 |
"gallery_id": data["id"], |
| 48 |
"media_id" : text.parse_int(data["media_id"]), |
| 49 |
"date" : self.parse_timestamp(data["upload_date"]), |
| 50 |
"scanlator" : info.get("scanlator", ""), |
| 51 |
"artist" : info["artist"], |
| 52 |
"group" : info["group"], |
| 53 |
"parody" : info["parody"], |
| 54 |
"characters": info["character"], |
| 55 |
"tags" : info["tag"], |
| 56 |
"type" : info["category"][0] if "category" in info else "", |
| 57 |
"lang" : util.language_to_code(language), |
| 58 |
"language" : language, |
| 59 |
} |
| 60 |
|
| 61 |
def images(self, _): |
| 62 |
data = self.data |
| 63 |
return [ |
| 64 |
(f"https://i{random.randint(1, 4)}.nhentai.net/{page['path']}", { |
| 65 |
"width" : page["width"], |
| 66 |
"height": page["height"], |
| 67 |
}) |
| 68 |
for page in data["pages"] |
| 69 |
] |
| 70 |
|
| 71 |
|
| 72 |
class NhentaiExtractor(Extractor): |
| 73 |
"""Base class for nhentai extractors""" |
| 74 |
category = "nhentai" |
| 75 |
root = "https://nhentai.net" |
| 76 |
|
| 77 |
def __init__(self, match): |
| 78 |
Extractor.__init__(self, match) |
| 79 |
self.path, self.query = match.groups() |
| 80 |
|
| 81 |
def items(self): |
| 82 |
data = {"_extractor": NhentaiGalleryExtractor} |
| 83 |
for gallery_id in self._pagination(): |
| 84 |
url = f"{self.root}/g/{gallery_id}/" |
| 85 |
data["gallery_id"] = text.parse_int(gallery_id) |
| 86 |
yield Message.Queue, url, data |
| 87 |
|
| 88 |
def _pagination(self): |
| 89 |
url = self.root + self.path |
| 90 |
params = text.parse_query(self.query) |
| 91 |
params["page"] = text.parse_int(params.get("page"), 1) |
| 92 |
|
| 93 |
while True: |
| 94 |
page = self.request(url, params=params).text |
| 95 |
yield from text.extract_iter(page, 'href="/g/', '/') |
| 96 |
if 'class="next' not in page: |
| 97 |
return |
| 98 |
params["page"] += 1 |
| 99 |
|
| 100 |
|
| 101 |
class NhentaiTagExtractor(NhentaiExtractor): |
| 102 |
"""Extractor for nhentai tag searches""" |
| 103 |
subcategory = "tag" |
| 104 |
pattern = (r"(?:https?://)?nhentai\.net(" |
| 105 |
r"/(?:artist|category|character|group|language|parody|tag)" |
| 106 |
r"/[^/?#]+(?:/popular[^/?#]*)?/?)(?:\?([^#]+))?") |
| 107 |
example = "https://nhentai.net/tag/TAG/" |
| 108 |
|
| 109 |
|
| 110 |
class NhentaiSearchExtractor(NhentaiExtractor): |
| 111 |
"""Extractor for nhentai search results""" |
| 112 |
subcategory = "search" |
| 113 |
pattern = r"(?:https?://)?nhentai\.net(/search/?)\?([^#]+)" |
| 114 |
example = "https://nhentai.net/search/?q=QUERY" |
| 115 |
|
| 116 |
|
| 117 |
class NhentaiFavoriteExtractor(NhentaiExtractor): |
| 118 |
"""Extractor for nhentai favorites""" |
| 119 |
subcategory = "favorite" |
| 120 |
pattern = r"(?:https?://)?nhentai\.net(/favorites/?)(?:\?([^#]+))?" |
| 121 |
example = "https://nhentai.net/favorites/" |