From c155c7b94b21fa0d35138ce733a511607326332b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Wed, 5 Oct 2016 09:19:09 +0200 Subject: [PATCH] [hentai2read] put some common code in a base class --- gallery_dl/extractor/hentai2read.py | 55 +++++++---------------------- gallery_dl/extractor/hentaicdn.py | 53 +++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 42 deletions(-) create mode 100644 gallery_dl/extractor/hentaicdn.py diff --git a/gallery_dl/extractor/hentai2read.py b/gallery_dl/extractor/hentai2read.py index b0d93dca..8c01cfa2 100644 --- a/gallery_dl/extractor/hentai2read.py +++ b/gallery_dl/extractor/hentai2read.py @@ -6,17 +6,15 @@ # it under the terms of the GNU General Public License version 2 as # published by the Free Software Foundation. -"""Extract images from https://hentai2read.com/""" +"""Extract hentaimanga from https://hentai2read.com/""" -from .common import Extractor, Message from .. import text -import json +from . import hentaicdn import re -class Hentai2readMangaExtractor(Extractor): +class Hentai2readMangaExtractor(hentaicdn.HentaicdnMangaExtractor): """Extractor for mangas from hentai2read.com""" category = "hentai2read" - subcategory = "manga" pattern = [r"(?:https?://)?(?:www\.)?hentai2read\.com/([^/]+)/?$"] test = [ ("http://hentai2read.com/amazon_elixir/", { @@ -28,29 +26,20 @@ class Hentai2readMangaExtractor(Extractor): ] def __init__(self, match): - Extractor.__init__(self) + hentaicdn.HentaicdnMangaExtractor.__init__(self) self.url_title = match.group(1) - def items(self): - yield Message.Version, 1 - for chapter in self.get_chapters(): - yield Message.Queue, chapter - def get_chapters(self): - """Return a list of all chapter urls""" - page = self.request("http://hentai2read.com/" + self.url_title).text - page = text.extract(page, '\n')[0] - needle = '
  • \n', '\n' + )[0] + return text.extract_iter(page, '
  • \n", "")[0] match = re.match(r"Reading (?:(.+) dj - )?(.+) Hentai - \d+: ", title) return { @@ -86,9 +63,3 @@ class Hentai2readChapterExtractor(Extractor): "lang": "en", "language": "English", } - - @staticmethod - def get_image_urls(page): - """Extract and return a list of all image-urls""" - images = text.extract(page, "var rff_imageList = ", ";")[0] - return json.loads(images) diff --git a/gallery_dl/extractor/hentaicdn.py b/gallery_dl/extractor/hentaicdn.py new file mode 100644 index 00000000..d4e3f127 --- /dev/null +++ b/gallery_dl/extractor/hentaicdn.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- + +# Copyright 2016 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. + +"""Base classes for extractors from sites based on hentaicdn""" + +from .common import Extractor, Message +from .. import text +import json + +class HentaicdnMangaExtractor(Extractor): + """Base class for extractors for mangas""" + subcategory = "manga" + + def items(self): + yield Message.Version, 1 + for chapter in reversed(list(self.get_chapters())): + yield Message.Queue, chapter + + def get_chapters(self): + """Return a list of all chapter urls""" + + +class HentaicdnChapterExtractor(Extractor): + """Base class for extractors for a single manga chapter""" + subcategory = "chapter" + directory_fmt = ["{category}", "{gallery-id} {title}"] + filename_fmt = "{category}_{gallery-id}_{chapter:>02}_{num:>03}.{extension}" + url = "" + + def items(self): + page = self.request(self.url).text + images = self.get_image_urls(page) + data = self.get_job_metadata(page, images) + yield Message.Version, 1 + yield Message.Directory, data + for num, part in enumerate(images, 1): + data["num"] = num + url = "http://hentaicdn.com/hentai" + part + yield Message.Url, url, text.nameext_from_url(url, data) + + def get_job_metadata(self, page, images): + """Collect metadata for extractor-job""" + + @staticmethod + def get_image_urls(page): + """Extract and return a list of all image-urls""" + images = text.extract(page, "var rff_imageList = ", ";")[0] + return json.loads(images)