1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-23 03:02:50 +01:00
gallery-dl/gallery_dl/extractor/batoto.py

132 lines
4.4 KiB
Python
Raw Normal View History

2023-12-27 04:33:33 +01:00
# -*- coding: utf-8 -*-
# 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.
2024-01-06 00:51:52 +01:00
"""Extractors for https://bato.to/"""
2023-12-27 04:33:33 +01:00
2024-01-06 00:51:52 +01:00
from .common import Extractor, ChapterExtractor, MangaExtractor
2023-12-27 04:33:33 +01:00
from .. import text, exception
import re
BASE_PATTERN = (r"(?:https?://)?(?:"
r"(?:ba|d|h|m|w)to\.to|"
r"(?:(?:manga|read)toto|batocomic|[xz]bato)\.(?:com|net|org)|"
r"comiko\.(?:net|org)|"
r"bat(?:otoo|o?two)\.com)")
2023-12-27 04:33:33 +01:00
2023-12-27 05:41:37 +01:00
class BatotoBase():
"""Base class for batoto extractors"""
category = "batoto"
2023-12-27 04:33:33 +01:00
root = "https://bato.to"
2024-01-06 00:51:52 +01:00
def request(self, url, **kwargs):
kwargs["encoding"] = "utf-8"
return Extractor.request(self, url, **kwargs)
2023-12-27 05:41:37 +01:00
class BatotoChapterExtractor(BatotoBase, ChapterExtractor):
2024-01-06 00:51:52 +01:00
"""Extractor for bato.to manga chapters"""
pattern = BASE_PATTERN + r"/(?:title/[^/?#]+|chapter)/(\d+)"
example = "https://bato.to/title/12345-MANGA/54321"
2023-12-27 04:33:33 +01:00
def __init__(self, match):
2024-01-06 00:51:52 +01:00
self.root = text.root_from_url(match.group(0))
self.chapter_id = match.group(1)
url = "{}/title/0/{}".format(self.root, self.chapter_id)
ChapterExtractor.__init__(self, match, url)
2023-12-27 04:33:33 +01:00
def metadata(self, page):
2024-01-06 00:51:52 +01:00
extr = text.extract_from(page)
try:
manga, info, _ = extr("<title>", "<").rsplit(" - ", 3)
except ValueError:
manga = info = None
manga_id = text.extr(
extr('rel="canonical" href="', '"'), "/title/", "/")
2023-12-27 04:33:33 +01:00
if not manga:
manga = extr('link-hover">', "<")
info = text.remove_html(extr('link-hover">', "</"))
2023-12-27 04:33:33 +01:00
match = re.match(
2024-01-06 00:51:52 +01:00
r"(?:Volume\s+(\d+) )?"
r"\w+\s+(\d+)(.*)", info)
if match:
volume, chapter, minor = match.groups()
title = text.remove_html(extr(
"selected>", "</option")).partition(" : ")[2]
else:
volume = chapter = 0
minor = ""
title = info
2023-12-27 04:33:33 +01:00
return {
"manga" : text.unescape(manga),
2024-01-06 00:51:52 +01:00
"manga_id" : text.parse_int(manga_id),
2023-12-27 04:33:33 +01:00
"title" : text.unescape(title),
"volume" : text.parse_int(volume),
"chapter" : text.parse_int(chapter),
2024-01-06 00:51:52 +01:00
"chapter_minor": minor,
"chapter_id" : text.parse_int(self.chapter_id),
"date" : text.parse_timestamp(extr(' time="', '"')[:-3]),
2023-12-27 04:33:33 +01:00
}
def images(self, page):
images_container = text.extr(page, 'pageOpts', ':[0,0]}"')
images_container = text.unescape(images_container)
2023-12-27 05:41:37 +01:00
return [
(url, None)
for url in text.extract_iter(images_container, r"\"", r"\"")
]
2023-12-27 04:33:33 +01:00
class BatotoMangaExtractor(BatotoBase, MangaExtractor):
2024-01-06 00:51:52 +01:00
"""Extractor for bato.to manga"""
2023-12-27 04:33:33 +01:00
reverse = False
chapterclass = BatotoChapterExtractor
pattern = (BASE_PATTERN +
r"/(?:title/(\d+)[^/?#]*|series/(\d+)(?:/[^/?#]*)?)/?$")
2024-01-06 00:51:52 +01:00
example = "https://bato.to/title/12345-MANGA/"
def __init__(self, match):
self.root = text.root_from_url(match.group(0))
self.manga_id = match.group(1) or match.group(2)
2024-01-06 00:51:52 +01:00
url = "{}/title/{}".format(self.root, self.manga_id)
MangaExtractor.__init__(self, match, url)
2023-12-27 04:33:33 +01:00
def chapters(self, page):
2024-01-06 00:51:52 +01:00
extr = text.extract_from(page)
2023-12-27 05:41:37 +01:00
2024-01-06 00:51:52 +01:00
warning = extr(' class="alert alert-warning">', "</div><")
if warning:
raise exception.StopExtraction("'%s'", text.remove_html(warning))
data = {
"manga_id": text.parse_int(self.manga_id),
"manga" : text.unescape(extr(
"<title>", "<").rpartition(" - ")[0]),
}
extr('<div data-hk="0-0-0-0"', "")
2023-12-27 04:33:33 +01:00
results = []
2024-01-06 00:51:52 +01:00
while True:
href = extr('<a href="/title/', '"')
if not href:
break
chapter = href.rpartition("-ch_")[2]
chapter, sep, minor = chapter.partition(".")
data["chapter"] = text.parse_int(chapter)
data["chapter_minor"] = sep + minor
data["date"] = text.parse_datetime(
extr('time="', '"'), "%Y-%m-%dT%H:%M:%S.%fZ")
url = "{}/title/{}".format(self.root, href)
2023-12-27 04:33:33 +01:00
results.append((url, data.copy()))
2023-12-27 05:41:37 +01:00
return results