2018-03-05 18:37:21 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-01-03 11:52:00 +01:00
|
|
|
# Copyright 2018-2019 Mike Fährmann
|
2018-03-05 18:37:21 +01:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
"""Extract manga-chapters and entire manga from https://mangadex.org/"""
|
|
|
|
|
2018-08-10 16:26:10 +02:00
|
|
|
from .common import Extractor, Message
|
2018-08-08 18:08:26 +02:00
|
|
|
from .. import text, util
|
2018-03-05 18:37:21 +01:00
|
|
|
|
|
|
|
|
2018-08-10 16:26:10 +02:00
|
|
|
class MangadexExtractor(Extractor):
|
2018-03-05 18:37:21 +01:00
|
|
|
"""Base class for mangadex extractors"""
|
|
|
|
category = "mangadex"
|
|
|
|
root = "https://mangadex.org"
|
|
|
|
|
2018-08-08 18:08:26 +02:00
|
|
|
# mangadex-to-iso639-1 codes
|
|
|
|
iso639_map = {
|
|
|
|
"br": "pt",
|
|
|
|
"ct": "ca",
|
|
|
|
"gb": "en",
|
|
|
|
"vn": "vi",
|
|
|
|
}
|
|
|
|
|
2018-08-10 16:26:10 +02:00
|
|
|
def chapter_data(self, chapter_id):
|
|
|
|
"""Request API results for 'chapter_id'"""
|
|
|
|
url = "{}/api/chapter/{}".format(self.root, chapter_id)
|
|
|
|
return self.request(url).json()
|
2018-03-05 18:37:21 +01:00
|
|
|
|
2018-08-10 16:26:10 +02:00
|
|
|
def manga_data(self, manga_id, *, cache={}):
|
|
|
|
"""Request API results for 'manga_id'"""
|
|
|
|
if manga_id not in cache:
|
|
|
|
url = "{}/api/manga/{}".format(self.root, manga_id)
|
|
|
|
cache[manga_id] = self.request(url).json()
|
|
|
|
return cache[manga_id]
|
|
|
|
|
|
|
|
|
|
|
|
class MangadexChapterExtractor(MangadexExtractor):
|
2018-03-05 18:37:21 +01:00
|
|
|
"""Extractor for manga-chapters from mangadex.org"""
|
2018-08-10 16:26:10 +02:00
|
|
|
subcategory = "chapter"
|
2019-02-08 13:45:40 +01:00
|
|
|
directory_fmt = (
|
2018-08-10 16:26:10 +02:00
|
|
|
"{category}", "{manga}",
|
2019-02-08 13:45:40 +01:00
|
|
|
"{volume:?v/ />02}c{chapter:>03}{chapter_minor}{title:?: //}")
|
2018-08-10 16:26:10 +02:00
|
|
|
filename_fmt = (
|
|
|
|
"{manga}_c{chapter:>03}{chapter_minor}_{page:>03}.{extension}")
|
2018-03-06 14:15:15 +01:00
|
|
|
archive_fmt = "{chapter_id}_{page}"
|
2019-02-08 13:45:40 +01:00
|
|
|
pattern = r"(?:https?://)?(?:www\.)?mangadex\.(?:org|com)/chapter/(\d+)"
|
|
|
|
test = (
|
2018-03-05 18:37:21 +01:00
|
|
|
("https://mangadex.org/chapter/122094", {
|
2018-08-10 16:26:10 +02:00
|
|
|
"keyword": "7bd7f82ab9d3f06976c4b68afe78d0040851ac3c",
|
2018-03-05 18:37:21 +01:00
|
|
|
"content": "7ab3bef5caccb62b881f8e6e70359d3c7be8137f",
|
|
|
|
}),
|
|
|
|
# oneshot
|
|
|
|
("https://mangadex.org/chapter/138086", {
|
|
|
|
"count": 64,
|
2018-08-10 16:26:10 +02:00
|
|
|
"keyword": "435e157dc5529d152458ba751ffe5bfbaf4850fb",
|
2018-05-23 18:37:12 +02:00
|
|
|
}),
|
2019-02-08 13:45:40 +01:00
|
|
|
)
|
2018-03-05 18:37:21 +01:00
|
|
|
|
|
|
|
def __init__(self, match):
|
2019-02-11 13:31:10 +01:00
|
|
|
MangadexExtractor.__init__(self, match)
|
2018-08-10 16:26:10 +02:00
|
|
|
self.chapter_id = match.group(1)
|
2018-05-23 18:37:12 +02:00
|
|
|
self.data = None
|
2018-03-05 18:37:21 +01:00
|
|
|
|
2018-08-10 16:26:10 +02:00
|
|
|
def items(self):
|
2019-02-11 18:38:47 +01:00
|
|
|
data = self.metadata()
|
|
|
|
imgs = self.images()
|
2018-08-10 16:26:10 +02:00
|
|
|
data["count"] = len(imgs)
|
|
|
|
|
|
|
|
yield Message.Version, 1
|
|
|
|
yield Message.Directory, data
|
|
|
|
for data["page"], url in enumerate(imgs, 1):
|
|
|
|
yield Message.Url, url, text.nameext_from_url(url, data)
|
2018-04-21 16:18:03 +02:00
|
|
|
|
2019-02-11 18:38:47 +01:00
|
|
|
def metadata(self):
|
2018-08-10 16:26:10 +02:00
|
|
|
"""Return a dict with general metadata"""
|
|
|
|
cdata = self.chapter_data(self.chapter_id)
|
|
|
|
mdata = self.manga_data(cdata["manga_id"])
|
|
|
|
self.data = cdata
|
2018-03-05 18:37:21 +01:00
|
|
|
|
2018-08-10 16:26:10 +02:00
|
|
|
chapter, sep, minor = cdata["chapter"].partition(".")
|
2018-03-05 18:37:21 +01:00
|
|
|
return {
|
2018-08-08 18:08:26 +02:00
|
|
|
"manga": mdata["manga"]["title"],
|
2018-08-10 16:26:10 +02:00
|
|
|
"manga_id": cdata["manga_id"],
|
|
|
|
"artist": mdata["manga"]["artist"],
|
|
|
|
"author": mdata["manga"]["author"],
|
|
|
|
"title": text.unescape(cdata["title"]),
|
|
|
|
"volume": text.parse_int(cdata["volume"]),
|
2018-05-23 18:37:12 +02:00
|
|
|
"chapter": text.parse_int(chapter),
|
2018-08-08 18:08:26 +02:00
|
|
|
"chapter_minor": sep + minor,
|
2018-08-10 16:26:10 +02:00
|
|
|
"chapter_id": cdata["id"],
|
|
|
|
"group": mdata["chapter"][self.chapter_id]["group_name"],
|
|
|
|
"date": cdata["timestamp"],
|
|
|
|
"lang": util.language_to_code(cdata["lang_name"]),
|
|
|
|
"language": cdata["lang_name"],
|
2018-03-05 18:37:21 +01:00
|
|
|
}
|
|
|
|
|
2019-02-11 18:38:47 +01:00
|
|
|
def images(self):
|
2018-08-10 16:26:10 +02:00
|
|
|
"""Return a list of all image URLs"""
|
2018-08-08 18:08:26 +02:00
|
|
|
base = self.data["server"] + self.data["hash"] + "/"
|
2018-08-25 11:06:28 +02:00
|
|
|
if base.startswith("/"):
|
|
|
|
base = text.urljoin(self.root, base)
|
2018-08-10 16:26:10 +02:00
|
|
|
return [base + page for page in self.data["page_array"]]
|
2018-03-05 18:37:21 +01:00
|
|
|
|
|
|
|
|
2018-08-10 16:26:10 +02:00
|
|
|
class MangadexMangaExtractor(MangadexExtractor):
|
2018-03-05 18:37:21 +01:00
|
|
|
"""Extractor for manga from mangadex.org"""
|
2018-08-10 16:26:10 +02:00
|
|
|
subcategory = "manga"
|
|
|
|
categorytransfer = True
|
2019-02-08 13:45:40 +01:00
|
|
|
pattern = (r"(?:https?://)?(?:www\.)?mangadex\.(?:org|com)"
|
|
|
|
r"/(?:title|manga)/(\d+)")
|
|
|
|
test = (
|
2018-03-05 18:37:21 +01:00
|
|
|
("https://mangadex.org/manga/2946/souten-no-koumori", {
|
2019-01-03 11:52:00 +01:00
|
|
|
"pattern": r"https://mangadex.org/chapter/\d+",
|
2018-03-05 18:37:21 +01:00
|
|
|
"keywords": {
|
|
|
|
"manga": "Souten no Koumori",
|
|
|
|
"manga_id": 2946,
|
|
|
|
"title": "Oneshot",
|
2018-03-06 14:15:15 +01:00
|
|
|
"volume": 0,
|
|
|
|
"chapter": 0,
|
|
|
|
"chapter_minor": "",
|
2018-03-05 18:37:21 +01:00
|
|
|
"chapter_id": int,
|
|
|
|
"group": str,
|
2018-08-08 18:08:26 +02:00
|
|
|
"date": int,
|
2018-03-05 18:37:21 +01:00
|
|
|
"lang": str,
|
|
|
|
"language": str,
|
|
|
|
},
|
|
|
|
}),
|
2018-05-06 17:43:50 +02:00
|
|
|
("https://mangadex.org/manga/13318/dagashi-kashi/chapters/2/", {
|
|
|
|
"count": ">= 100",
|
|
|
|
}),
|
2019-01-03 11:52:00 +01:00
|
|
|
("https://mangadex.org/title/13004/yorumori-no-kuni-no-sora-ni", {
|
|
|
|
"count": 0,
|
|
|
|
}),
|
2019-02-08 13:45:40 +01:00
|
|
|
("https://mangadex.org/title/2946/souten-no-koumori"),
|
|
|
|
)
|
2018-08-08 18:08:26 +02:00
|
|
|
|
|
|
|
def __init__(self, match):
|
2019-02-11 13:31:10 +01:00
|
|
|
MangadexExtractor.__init__(self, match)
|
2018-08-10 16:26:10 +02:00
|
|
|
self.manga_id = text.parse_int(match.group(1))
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
yield Message.Version, 1
|
|
|
|
for data in self.chapters():
|
|
|
|
url = "{}/chapter/{}".format(self.root, data["chapter_id"])
|
|
|
|
yield Message.Queue, url, data
|
|
|
|
|
|
|
|
def chapters(self):
|
|
|
|
"""Return a sorted list of chapter-metadata dicts"""
|
|
|
|
data = self.manga_data(self.manga_id)
|
2019-01-03 11:52:00 +01:00
|
|
|
if "chapter" not in data:
|
|
|
|
return ()
|
2018-08-08 18:08:26 +02:00
|
|
|
manga = data["manga"]
|
|
|
|
|
2018-03-05 18:37:21 +01:00
|
|
|
results = []
|
2018-08-08 18:08:26 +02:00
|
|
|
for chid, info in data["chapter"].items():
|
|
|
|
chapter, sep, minor = info["chapter"].partition(".")
|
|
|
|
lang = self.iso639_map.get(info["lang_code"], info["lang_code"])
|
2018-08-10 16:26:10 +02:00
|
|
|
results.append({
|
2018-08-08 18:08:26 +02:00
|
|
|
"manga": manga["title"],
|
2018-08-10 16:26:10 +02:00
|
|
|
"manga_id": self.manga_id,
|
2018-08-08 18:08:26 +02:00
|
|
|
"artist": manga["artist"],
|
|
|
|
"author": manga["author"],
|
|
|
|
"title": text.unescape(info["title"]),
|
|
|
|
"volume": text.parse_int(info["volume"]),
|
|
|
|
"chapter": text.parse_int(chapter),
|
|
|
|
"chapter_minor": sep + minor,
|
|
|
|
"chapter_id": text.parse_int(chid),
|
|
|
|
"group": text.unescape(info["group_name"]),
|
|
|
|
"date": info["timestamp"],
|
|
|
|
"lang": lang,
|
|
|
|
"language": util.code_to_language(lang),
|
2018-08-10 16:26:10 +02:00
|
|
|
})
|
2018-08-08 18:08:26 +02:00
|
|
|
|
2018-08-10 16:26:10 +02:00
|
|
|
results.sort(key=lambda x: (x["chapter"], x["chapter_minor"]))
|
2018-08-08 18:08:26 +02:00
|
|
|
return results
|