2015-10-28 12:08:27 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-01-14 07:51:05 +01:00
|
|
|
# Copyright 2015-2019 Mike Fährmann
|
2015-10-28 12:08:27 +01:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License version 2 as
|
|
|
|
# published by the Free Software Foundation.
|
|
|
|
|
2017-04-20 13:20:41 +02:00
|
|
|
"""Extract images from https://nhentai.net/"""
|
2015-10-28 12:08:27 +01:00
|
|
|
|
|
|
|
from .common import Extractor, Message
|
2018-04-20 14:53:21 +02:00
|
|
|
from .. import text
|
2019-01-14 07:51:05 +01:00
|
|
|
import json
|
2015-10-28 12:08:27 +01:00
|
|
|
|
2017-02-01 00:53:19 +01:00
|
|
|
|
2018-03-28 17:21:44 +02:00
|
|
|
class NHentaiExtractor(Extractor):
|
|
|
|
"""Base class for nhentai extractors"""
|
2015-11-21 04:26:30 +01:00
|
|
|
category = "nhentai"
|
2018-03-28 17:21:44 +02:00
|
|
|
root = "https://nhentai.net"
|
|
|
|
media_url = "https://i.nhentai.net"
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def transform_to_metadata(ginfo):
|
|
|
|
"""Transform an nhentai API response into compatible metadata"""
|
|
|
|
title_en = ginfo["title"].get("english", "")
|
|
|
|
title_ja = ginfo["title"].get("japanese", "")
|
|
|
|
return {
|
|
|
|
"gallery_id": ginfo["id"],
|
|
|
|
"upload_date": ginfo["upload_date"],
|
|
|
|
"media_id": ginfo["media_id"],
|
|
|
|
"scanlator": ginfo["scanlator"],
|
|
|
|
"count": ginfo["num_pages"],
|
|
|
|
"title": title_en or title_ja,
|
|
|
|
"title_en": title_en,
|
|
|
|
"title_ja": title_ja,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class NhentaiGalleryExtractor(NHentaiExtractor):
|
|
|
|
"""Extractor for image galleries from nhentai.net"""
|
2016-10-16 13:40:14 +02:00
|
|
|
subcategory = "gallery"
|
2017-09-10 22:20:47 +02:00
|
|
|
directory_fmt = ["{category}", "{gallery_id} {title}"]
|
|
|
|
filename_fmt = "{category}_{gallery_id}_{num:>03}.{extension}"
|
2018-01-30 22:49:16 +01:00
|
|
|
archive_fmt = "{gallery_id}_{num}"
|
2018-03-28 17:21:44 +02:00
|
|
|
pattern = [r"(?:https?://)?nhentai\.net/g/(\d+)"]
|
2018-03-21 12:37:46 +01:00
|
|
|
test = [("https://nhentai.net/g/147850/", {
|
2017-01-27 22:43:50 +01:00
|
|
|
"url": "5179dbf0f96af44005a0ff705a0ad64ac26547d0",
|
2018-03-21 12:37:46 +01:00
|
|
|
"keyword": "2f94976e657f3043a89997e22f4de8e1b22d9175",
|
2015-12-13 04:36:44 +01:00
|
|
|
})]
|
2015-11-21 04:26:30 +01:00
|
|
|
|
2015-10-28 12:08:27 +01:00
|
|
|
def __init__(self, match):
|
2018-03-28 17:21:44 +02:00
|
|
|
NHentaiExtractor.__init__(self)
|
2015-10-28 12:08:27 +01:00
|
|
|
self.gid = match.group(1)
|
|
|
|
|
|
|
|
def items(self):
|
2018-03-28 17:21:44 +02:00
|
|
|
ginfo = self.get_gallery_info(self.gid)
|
|
|
|
data = self.transform_to_metadata(ginfo)
|
|
|
|
urlfmt = "{}/galleries/{}/{{}}.{{}}".format(
|
|
|
|
self.media_url, data["media_id"])
|
2015-10-28 12:08:27 +01:00
|
|
|
extdict = {"j": "jpg", "p": "png", "g": "gif"}
|
|
|
|
yield Message.Version, 1
|
|
|
|
yield Message.Directory, data
|
2017-01-27 22:43:50 +01:00
|
|
|
for data["num"], image in enumerate(ginfo["images"]["pages"], 1):
|
2015-10-28 12:08:27 +01:00
|
|
|
ext = extdict.get(image["t"], "jpg")
|
|
|
|
data["width"] = image["w"]
|
|
|
|
data["height"] = image["h"]
|
|
|
|
data["extension"] = ext
|
2017-01-27 22:43:50 +01:00
|
|
|
yield Message.Url, urlfmt.format(data["num"], ext), data
|
2015-10-28 12:08:27 +01:00
|
|
|
|
2018-03-28 17:21:44 +02:00
|
|
|
def get_gallery_info(self, gallery_id):
|
|
|
|
"""Extract and return info about a gallery by ID"""
|
2019-01-14 07:51:05 +01:00
|
|
|
url = "{}/g/{}".format(self.root, gallery_id)
|
|
|
|
page = self.request(url).text
|
|
|
|
return json.loads(text.extract(page, "N.gallery(", ");")[0])
|
2015-10-28 12:08:27 +01:00
|
|
|
|
2018-03-28 17:21:44 +02:00
|
|
|
|
|
|
|
class NhentaiSearchExtractor(NHentaiExtractor):
|
|
|
|
"""Extractor for nhentai search results"""
|
|
|
|
category = "nhentai"
|
|
|
|
subcategory = "search"
|
2019-01-14 07:51:05 +01:00
|
|
|
pattern = [r"(?:https?://)?nhentai\.net/search/?\?([^#]+)"]
|
|
|
|
test = [("https://nhentai.net/search/?q=touhou", {
|
|
|
|
"pattern": NhentaiGalleryExtractor.pattern[0],
|
|
|
|
"count": 30,
|
|
|
|
"range": "1-30",
|
|
|
|
})]
|
2018-03-28 17:21:44 +02:00
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
NHentaiExtractor.__init__(self)
|
|
|
|
self.params = text.parse_query(match.group(1))
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
yield Message.Version, 1
|
2019-01-14 07:51:05 +01:00
|
|
|
for gid in self._pagination(self.params):
|
|
|
|
url = "{}/g/{}/".format(self.root, gid)
|
|
|
|
yield Message.Queue, url, {}
|
2018-03-28 17:21:44 +02:00
|
|
|
|
2019-01-14 07:51:05 +01:00
|
|
|
def _pagination(self, params):
|
|
|
|
url = "{}/search/".format(self.root)
|
2018-04-20 14:53:21 +02:00
|
|
|
params["page"] = text.parse_int(params.get("page"), 1)
|
2018-03-28 17:21:44 +02:00
|
|
|
|
|
|
|
while True:
|
2019-01-14 07:51:05 +01:00
|
|
|
page = self.request(url, params=params).text
|
2018-03-28 17:21:44 +02:00
|
|
|
|
2019-01-14 07:51:05 +01:00
|
|
|
yield from text.extract_iter(page, 'href="/g/', '/')
|
2018-03-28 17:21:44 +02:00
|
|
|
|
2019-01-14 07:51:05 +01:00
|
|
|
if 'class="next"' not in page:
|
2018-03-28 17:21:44 +02:00
|
|
|
return
|
|
|
|
params["page"] += 1
|