From 6ce181bcfe291d8eac5e628600d27dc7e777dcee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Wed, 28 Oct 2015 12:08:27 +0100 Subject: [PATCH] add extractor 'nhentai' --- gallery_dl/extractor/__init__.py | 1 + gallery_dl/extractor/nhentai.py | 70 ++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 gallery_dl/extractor/nhentai.py diff --git a/gallery_dl/extractor/__init__.py b/gallery_dl/extractor/__init__.py index 88509734..a80569f5 100644 --- a/gallery_dl/extractor/__init__.py +++ b/gallery_dl/extractor/__init__.py @@ -26,6 +26,7 @@ modules = [ "imgchili", "imgur", "mangareader", + "nhentai", "nijie", "powermanga", "redhawkscans", diff --git a/gallery_dl/extractor/nhentai.py b/gallery_dl/extractor/nhentai.py new file mode 100644 index 00000000..e462b701 --- /dev/null +++ b/gallery_dl/extractor/nhentai.py @@ -0,0 +1,70 @@ +# -*- coding: utf-8 -*- + +# Copyright 2015 Mike Fährmann +# +# 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. + +"""Extract images from http://nhentai.net/""" + +from .common import Extractor, Message +from .. import text +import json + +info = { + "category": "nhentai", + "extractor": "NhentaiExtractor", + "directory": ["{category}", "{gallery-id} {title}"], + "filename": "{category}_{gallery-id}_{num:>03}.{extension}", + "pattern": [ + r"(?:https?://)?(?:www\.)?nhentai\.net/g/(\d+)", + ], +} + +class NhentaiExtractor(Extractor): + + def __init__(self, match): + Extractor.__init__(self) + self.gid = match.group(1) + + def items(self): + ginfo = self.get_gallery_info() + data = self.get_job_metadata(ginfo) + urlbase = "http:{}galleries/{}/".format(ginfo["media_url"], data["media-id"]) + extdict = {"j": "jpg", "p": "png", "g": "gif"} + yield Message.Version, 1 + yield Message.Directory, data + for num, image in enumerate(ginfo["images"]["pages"], 1): + ext = extdict.get(image["t"], "jpg") + data["num"] = num + data["width"] = image["w"] + data["height"] = image["h"] + data["extension"] = ext + yield Message.Url, "{}{}.{}".format(urlbase, num, ext), data + + def get_gallery_info(self): + """Extract and return gallery-info""" + page = self.request("http://nhentai.net/g/" + self.gid + "/1/").text + media_url, pos = text.extract(page, "nhentai.reader({\n\t\t\tmedia_url: '", "'") + json_data, pos = text.extract(page, "gallery: ", ",\n", pos) + json_dict = json.loads(json_data) + json_dict["media_url"] = media_url + return json_dict + + def get_job_metadata(self, ginfo): + """Collect metadata for extractor-job""" + title_en = ginfo["title"].get("english", "") + title_ja = ginfo["title"].get("japanese", "") + return { + "category": info["category"], + "gallery-id": self.gid, + "upload-date": ginfo["upload_date"], + "media-id": ginfo["media_id"], + "favorites": ginfo["num_favorites"], + "scanlator": ginfo["scanlator"], + "count": ginfo["num_pages"], + "title": title_en or title_ja, + "title-en": title_en, + "title-ja": title_ja, + }