| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Extractors for https://mangataro.org/""" |
| 10 |
|
| 11 |
from .common import ChapterExtractor, MangaExtractor |
| 12 |
from .. import text |
| 13 |
import hashlib |
| 14 |
import time |
| 15 |
|
| 16 |
BASE_PATTERN = r"(?:https?://)?mangataro\.org" |
| 17 |
|
| 18 |
|
| 19 |
class MangataroBase(): |
| 20 |
"""Base class for mangataro extractors""" |
| 21 |
category = "mangataro" |
| 22 |
root = "https://mangataro.org" |
| 23 |
|
| 24 |
def _manga_info(self, slug): |
| 25 |
url = f"{self.root}/manga/{slug}" |
| 26 |
page = self.request(url).text |
| 27 |
manga = self._extract_jsonld(page) |
| 28 |
|
| 29 |
return { |
| 30 |
"manga" : manga["name"].rpartition( |
| 31 |
" | ")[0].rpartition(" ")[0], |
| 32 |
"manga_id" : text.extr(page, 'data-manga-id="', '"'), |
| 33 |
"manga_url" : manga["url"], |
| 34 |
"cover" : manga["image"], |
| 35 |
"author" : manga["author"]["name"].split(", "), |
| 36 |
"genre" : manga["genre"], |
| 37 |
"status" : manga["status"], |
| 38 |
"description": text.unescape(text.extr( |
| 39 |
page, 'id="description-content-tab">', "</div></div>")), |
| 40 |
"tags" : text.split_html(text.extr( |
| 41 |
page, ">Genres</h4>", "</div>")), |
| 42 |
"publisher" : text.remove_html(text.extr( |
| 43 |
page, '>Serialization</h4>', "</div>")), |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
class MangataroChapterExtractor(MangataroBase, ChapterExtractor): |
| 48 |
"""Extractor for mangataro manga chapters""" |
| 49 |
pattern = BASE_PATTERN + r"(/read/([^/?#]+)/(?:[^/?#]*-)?(\d+))" |
| 50 |
example = "https://mangataro.org/read/MANGA/ch123-12345" |
| 51 |
|
| 52 |
def metadata(self, page): |
| 53 |
_, slug, chapter_id = self.groups |
| 54 |
comic = self._extract_jsonld(page)["@graph"][0] |
| 55 |
chapter = comic["position"] |
| 56 |
minor = chapter - int(chapter) |
| 57 |
desc = comic["description"].split(" - ", 3) |
| 58 |
|
| 59 |
return { |
| 60 |
**self.cache(self._manga_info, slug), |
| 61 |
"title" : desc[1] if len(desc) > 3 else "", |
| 62 |
"chapter" : int(chapter), |
| 63 |
"chapter_minor": str(round(minor, 5))[1:] if minor else "", |
| 64 |
"chapter_id" : text.parse_int(chapter_id), |
| 65 |
"chapter_url" : comic["url"], |
| 66 |
"date" : self.parse_datetime_iso(comic["datePublished"]), |
| 67 |
"date_updated" : self.parse_datetime_iso(comic["dateModified"]), |
| 68 |
} |
| 69 |
|
| 70 |
def images(self, page): |
| 71 |
url = f"{self.root}/auth/chapter-content?chapter_id={self.groups[2]}" |
| 72 |
data = self.request_json(url) |
| 73 |
return [(url, None) for url in data["images"]] |
| 74 |
|
| 75 |
|
| 76 |
class MangataroMangaExtractor(MangataroBase, MangaExtractor): |
| 77 |
"""Extractor for mangataro manga""" |
| 78 |
chapterclass = MangataroChapterExtractor |
| 79 |
pattern = BASE_PATTERN + r"/manga/([^/?#]+)" |
| 80 |
example = "https://mangataro.org/manga/MANGA" |
| 81 |
|
| 82 |
def chapters(self, _): |
| 83 |
manga = self.cache(self._manga_info, self.groups[0]) |
| 84 |
|
| 85 |
url = self.root + "/auth/manga-chapters" |
| 86 |
params = { |
| 87 |
"manga_id": manga["manga_id"], |
| 88 |
"offset" : 0, |
| 89 |
"limit" : 500, |
| 90 |
"order" : "DESC", |
| 91 |
} |
| 92 |
headers = { |
| 93 |
"Referer" : manga["manga_url"], |
| 94 |
} |
| 95 |
|
| 96 |
results = [] |
| 97 |
while True: |
| 98 |
self._update_params(params) |
| 99 |
data = self.request_json(url, params=params, headers=headers) |
| 100 |
|
| 101 |
for ch in data["chapters"]: |
| 102 |
chapter, sep, minor = ch["chapter"].partition(".") |
| 103 |
results.append((ch["url"], { |
| 104 |
**manga, |
| 105 |
"chapter_id" : text.parse_int(ch.pop("id", None)), |
| 106 |
**ch, |
| 107 |
"chapter" : text.parse_int(chapter), |
| 108 |
"chapter_minor": "." + minor if sep else "", |
| 109 |
})) |
| 110 |
|
| 111 |
if not data.get("has_more"): |
| 112 |
break |
| 113 |
params["offset"] += (data.get("limit") or params["limit"]) |
| 114 |
return results |
| 115 |
|
| 116 |
def _update_params(self, params): |
| 117 |
|
| 118 |
|
| 119 |
if (ts := int(time.time())) == params.get("_ts"): |
| 120 |
return |
| 121 |
Y, m, d, H, _, _, _, _, _ = time.gmtime(ts) |
| 122 |
secret = f"{ts}mng_ch_{Y:>04}{m:>02}{d:>02}{H:>02}" |
| 123 |
params["_t"] = hashlib.md5(secret.encode()).hexdigest()[:16] |
| 124 |
params["_ts"] = ts |