aboutsummaryrefslogtreecommitdiffstats
path: root/gallery_dl/extractor/mangafire.py
blob: 8db91b3290b61c379347599816f6718757181fed (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# -*- coding: utf-8 -*-

# Copyright 2025 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://mangafire.to/"""

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

BASE_PATTERN = r"(?:https?://)?(?:www\.)?mangafire\.to"


class MangafireBase():
    """Base class for mangafire extractors"""
    category = "mangafire"
    root = "https://mangafire.to"


class MangafireChapterExtractor(MangafireBase, ChapterExtractor):
    """Extractor for mangafire manga chapters"""
    directory_fmt = (
        "{category}", "{manga}",
        "{volume:?v/ />02}{chapter:?c//>03}{chapter_minor:?//}{title:?: //}")
    filename_fmt = (
        "{manga}{volume:?_v//>02}{chapter:?_c//>03}{chapter_minor:?//}_"
        "{page:>03}.{extension}")
    archive_fmt = (
        "{manga_id}_{chapter_id}_{page}")
    pattern = (BASE_PATTERN + r"/read/([\w-]+\.(\w+))/([\w-]+)"
               r"/((chapter|volume)-\d+(?:\D.*)?)")
    example = "https://mangafire.to/read/MANGA.ID/LANG/chapter-123"

    def metadata(self, _):
        manga_path, manga_id, lang, chapter_info, self.type = self.groups

        try:
            chapters = _manga_chapters(self, (manga_id, self.type, lang))
            anchor = chapters[chapter_info]
        except KeyError:
            raise exception.NotFoundError("chapter")
        self.chapter_id = text.extr(anchor, 'data-id="', '"')

        return {
            **_manga_info(self, manga_path),
            **_chapter_info(anchor),
        }

    def images(self, page):
        url = f"{self.root}/ajax/read/{self.type}/{self.chapter_id}"
        headers = {"x-requested-with": "XMLHttpRequest"}
        data = self.request_json(url, headers=headers)

        return [
            (image[0], None)
            for image in data["result"]["images"]
        ]


class MangafireMangaExtractor(MangafireBase, MangaExtractor):
    """Extractor for mangafire manga"""
    chapterclass = MangafireChapterExtractor
    pattern = BASE_PATTERN + r"/manga/([\w-]+)\.(\w+)"
    example = "https://mangafire.to/manga/MANGA.ID"

    def chapters(self, page):
        manga_slug, manga_id = self.groups
        lang = self.config("lang") or "en"

        manga = _manga_info(self, f"{manga_slug}.{manga_id}")
        chapters = _manga_chapters(self, (manga_id, "chapter", lang))

        return [
            (self.root + text.extr(anchor, 'href="', '"'), {
                **manga,
                **_chapter_info(anchor),
            })
            for anchor in chapters.values()
        ]


@memcache(keyarg=1)
def _manga_info(self, manga_path, page=None):
    if page is None:
        url = f"{self.root}/manga/{manga_path}"
        page = self.request(url).text
    slug, _, mid = manga_path.rpartition(".")

    extr = text.extract_from(page)
    manga = {
        "cover": text.extr(extr(
            'class="poster">', '</div>'), 'src="', '"'),
        "status": extr("<p>", "<").replace("_", " ").title(),
        "manga"     : text.unescape(extr(
            'itemprop="name">', "<")),
        "manga_id": mid,
        "manga_slug": slug,
        "manga_titles": text.unescape(extr(
            "<h6>", "<")).split("; "),
        "type": text.remove_html(extr(
            'class="min-info">', "</a>")),
        "author": text.unescape(text.remove_html(extr(
            "<span>Author:</span>", "</div>"))).split(" , "),
        "published": text.remove_html(extr(
            "<span>Published:</span>", "</div>")),
        "tags": text.split_html(extr(
            "<span>Genres:</span>", "</div>"))[::2],
        "publisher": text.unescape(text.remove_html(extr(
            "<span>Mangazines:</span>", "</div>"))).split(" , "),
        "score": text.parse_float(text.remove_html(extr(
            'class="score">', " / "))),
        "description": text.remove_html(extr(
            'id="synopsis">', "<script>")),
    }

    if len(lst := manga["author"]) == 1 and not lst[0]:
        manga["author"] = ()
    if len(lst := manga["publisher"]) == 1 and not lst[0]:
        manga["publisher"] = ()

    return manga


@memcache(keyarg=1)
def _manga_chapters(self, manga_info):
    manga_id, type, lang = manga_info
    url = f"{self.root}/ajax/read/{manga_id}/{type}/{lang}"
    headers = {"x-requested-with": "XMLHttpRequest"}
    data = self.request_json(url, headers=headers)

    needle = f"{manga_id}/{lang}/"
    return {
        text.extr(anchor, needle, '"'): anchor
        for anchor in text.extract_iter(data["result"]["html"], "<a ", ">")
    }


@memcache(keyarg=0)
def _chapter_info(info):
    _, lang, chapter_info = text.extr(info, 'href="', '"').rsplit("/", 2)

    if chapter_info.startswith("vol"):
        volume = text.extr(info, 'data-number="', '"')
        volume_id = text.parse_int(text.extr(info, 'data-id="', '"'))
        return {
            "volume"        : text.parse_int(volume),
            "volume_id"     : volume_id,
            "chapter"       : 0,
            "chapter_minor" : "",
            "chapter_string": chapter_info,
            "chapter_id"    : volume_id,
            "title"         : text.unescape(text.extr(info, 'title="', '"')),
            "lang"          : lang,
        }

    chapter, sep, minor = text.extr(info, 'data-number="', '"').partition(".")
    return {
        "chapter"       : text.parse_int(chapter),
        "chapter_minor" : sep + minor,
        "chapter_string": chapter_info,
        "chapter_id"    : text.parse_int(text.extr(info, 'data-id="', '"')),
        "title"         : text.unescape(text.extr(info, 'title="', '"')),
        "lang"          : lang,
    }