Debian Codegraph @

gallery_dl/extractor/mangafox.py

raw
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2017-2025 Mike Fährmann
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License version 2 as
7 # published by the Free Software Foundation.
8
9 """Extractors for https://fanfox.net/"""
10
11 from .common import ChapterExtractor, MangaExtractor
12 from .. import text
13
14 BASE_PATTERN = r"(?:https?://)?(?:www\.|m\.)?(?:fanfox\.net|mangafox\.me)"
15
16
17 class MangafoxChapterExtractor(ChapterExtractor):
18 """Extractor for manga chapters from fanfox.net"""
19 category = "mangafox"
20 root = "https://m.fanfox.net"
21 pattern = BASE_PATTERN + \
22 r"(/manga/[^/?#]+/((?:v([^/?#]+)/)?c(\d+)([^/?#]*)))"
23 example = "https://fanfox.net/manga/TITLE/v01/c001/1.html"
24
25 def __init__(self, match):
26 base, self.cstr, self.volume, self.chapter, self.minor = match.groups()
27 self.urlbase = self.root + base
28 ChapterExtractor.__init__(self, match, self.urlbase + "/1.html")
29
30 def metadata(self, page):
31 manga, pos = text.extract(page, "<title>", "</title>")
32 count, pos = text.extract(
33 page, ">", "<", page.find("</select>", pos) - 40)
34 sid , pos = text.extract(page, "var series_id =", ";", pos)
35 cid , pos = text.extract(page, "var chapter_id =", ";", pos)
36
37 return {
38 "manga" : text.unescape(manga),
39 "volume" : text.parse_int(self.volume),
40 "chapter" : text.parse_int(self.chapter),
41 "chapter_minor" : self.minor or "",
42 "chapter_string": self.cstr,
43 "count" : text.parse_int(count),
44 "sid" : text.parse_int(sid),
45 "cid" : text.parse_int(cid),
46 }
47
48 def images(self, page):
49 pnum = 1
50 while True:
51 url, pos = text.extract(page, '<img src="', '"')
52 yield text.ensure_http_scheme(text.unescape(url)), None
53 url, pos = text.extract(page, ' src="', '"', pos)
54 yield text.ensure_http_scheme(text.unescape(url)), None
55
56 pnum += 2
57 page = self.request(f"{self.urlbase}/{pnum}.html").text
58
59
60 class MangafoxMangaExtractor(MangaExtractor):
61 """Extractor for manga from fanfox.net"""
62 category = "mangafox"
63 root = "https://m.fanfox.net"
64 chapterclass = MangafoxChapterExtractor
65 pattern = BASE_PATTERN + r"(/manga/[^/?#]+)/?$"
66 example = "https://fanfox.net/manga/TITLE"
67
68 def chapters(self, page):
69 results = []
70 chapter_match = MangafoxChapterExtractor.pattern.match
71
72 extr = text.extract_from(page)
73 manga = extr('<p class="title">', '</p>')
74 author = extr('<p>Author(s):', '</p>')
75 extr('<dd class="chlist">', '')
76
77 genres, _, summary = text.extr(
78 page, '<div class="manga-genres">', '</section>'
79 ).partition('<div class="manga-summary">')
80
81 data = {
82 "manga" : text.unescape(manga),
83 "author" : text.remove_html(author),
84 "description": text.unescape(text.remove_html(summary)),
85 "tags" : text.split_html(genres),
86 "lang" : "en",
87 "language" : "English",
88 }
89
90 while True:
91 url = "https://" + extr('<a href="//', '"')
92 match = chapter_match(url)
93 if not match:
94 return results
95 _, cstr, volume, chapter, minor = match.groups()
96
97 chapter = {
98 "volume" : text.parse_int(volume),
99 "chapter" : text.parse_int(chapter),
100 "chapter_minor" : minor or "",
101 "chapter_string": cstr,
102 "date" : self.parse_datetime(
103 extr('right">', '</span>'), "%b %d, %Y"),
104 }
105 chapter.update(data)
106 results.append((url, chapter))