aboutsummaryrefslogtreecommitdiffstats
path: root/gallery_dl/extractor/mangareader.py
blob: fd9c7ace9fcf6b1023ac1d8975a14d29776f8051 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# -*- coding: utf-8 -*-

# Copyright 2015-2020 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.

"""Extractors for https://www.mangareader.net/"""

from .common import ChapterExtractor, MangaExtractor
from .. import text
from ..cache import memcache
import json


class MangareaderBase():
    """Base class for mangareader extractors"""
    category = "mangareader"
    root = "https://www.mangareader.net"

    @memcache(keyarg=1)
    def _manga_info(self, path, page=None):
        if not page:
            page = self.request(self.root + path).text
        extr = text.extract_from(page)
        data = {
            "manga"   : text.unescape(extr('class="name">', '<')),
            "release" : text.unescape(extr('Year of Release :</td><td>', '<')),
            "author"  : text.unescape(text.unescape(extr(
                'Author :</td><td>', '<'))),
            "artist"  : text.unescape(text.unescape(extr(
                'Artist :</td><td>', '<'))),
            "lang"    : "en",
            "language": "English",
        }

        extr('<table', '>')
        chapters = []
        while True:
            url = extr('</i> <a href="', '"')
            if not url:
                return chapters
            chapter = {
                "chapter": text.parse_int(url.rpartition("/")[2]),
                "title"  : text.unescape(extr("</a> : ", "<")),
                "date"   : extr("<td>", "<"),
            }
            chapter.update(data)
            chapters.append((self.root + url, chapter))


class MangareaderChapterExtractor(MangareaderBase, ChapterExtractor):
    """Extractor for manga-chapters from mangareader.net"""
    archive_fmt = "{manga}_{chapter}_{page}"
    pattern = r"(?:https?://)?(?:www\.)?mangareader\.net((/[^/?&#]+)/(\d+))"
    test = (("https://www.mangareader.net"
             "/karate-shoukoushi-kohinata-minoru/11"), {
        "url": "45ece5668d1e9f65cf2225237d78de58660b54e4",
        "keyword": "133e3e2f7c0529a35bbb16149e34c40546f8dfd6",
    })

    def __init__(self, match):
        ChapterExtractor.__init__(self, match)
        _, self.path, self.chapter = match.groups()

    def metadata(self, page):
        chapter = text.parse_int(self.chapter)
        return self._manga_info(self.path)[chapter-1][1]

    def images(self, page):
        data = json.loads(text.extract(
            page, 'document["mj"]=', '</script>')[0])
        return [
            (text.ensure_http_scheme(img["u"]), {
                "width" : text.parse_int(img["w"]),
                "height": text.parse_int(img["h"]),
            })
            for img in data["im"]
        ]


class MangareaderMangaExtractor(MangareaderBase, MangaExtractor):
    """Extractor for manga from mangareader.net"""
    chapterclass = MangareaderChapterExtractor
    reverse = False
    pattern = r"(?:https?://)?(?:www\.)?mangareader\.net(/[^/?&#]+)/?$"
    test = ("https://www.mangareader.net/mushishi", {
        "url": "bc203b858b4ad76e5d77e39118a7be0350e357da",
        "keyword": "031b3ea085921c552de017ecbb9b906e462229c9",
    })

    def chapters(self, page):
        path = self.manga_url[len(self.root):]
        return self._manga_info(path, page)