|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# This program is free software; you can redistribute it and/or modify |
| 4 | +# it under the terms of the GNU General Public License version 2 as |
| 5 | +# published by the Free Software Foundation. |
| 6 | + |
| 7 | +"""Extractors for https://kaliscan.me/""" |
| 8 | + |
| 9 | +from .common import ChapterExtractor, MangaExtractor |
| 10 | +from .. import text |
| 11 | +from ..cache import memcache |
| 12 | + |
| 13 | +BASE_PATTERN = r"(?:https?://)?kaliscan\.me" |
| 14 | + |
| 15 | + |
| 16 | +class KaliscanBase(): |
| 17 | + """Base class for kaliscan extractors""" |
| 18 | + category = "kaliscan" |
| 19 | + root = "https://kaliscan.me" |
| 20 | + |
| 21 | + @memcache(keyarg=1) |
| 22 | + def manga_data(self, manga_slug, page=None): |
| 23 | + if page is None: |
| 24 | + url = f"{self.root}/manga/{manga_slug}" |
| 25 | + page = self.request(url).text |
| 26 | + extr = text.extract_from(page) |
| 27 | + |
| 28 | + manga_id = text.parse_int(extr("bookId =", ";")) |
| 29 | + title = text.unescape(extr("<h1>", "<")) |
| 30 | + if alt_titles := extr("<h2>", "<"): |
| 31 | + alt_titles = [t.strip() for t in alt_titles.split(",")] |
| 32 | + else: |
| 33 | + alt_titles = () |
| 34 | + |
| 35 | + author = text.remove_html(extr( |
| 36 | + "Authors :</strong>", "</p>")) |
| 37 | + status = text.remove_html(extr( |
| 38 | + "Status :</strong>", "</p>")) |
| 39 | + genres = [g.strip(" ,") for g in text.split_html(extr( |
| 40 | + "Genres :</strong>", "</p>"))] |
| 41 | + |
| 42 | + if descr := extr('class="content"', '<div class="readmore"'): |
| 43 | + descr = text.remove_html(descr[descr.find(">")+1:]).strip() |
| 44 | + else: |
| 45 | + descr = "" |
| 46 | + |
| 47 | + return { |
| 48 | + "manga" : title, |
| 49 | + "manga_id" : manga_id, |
| 50 | + "manga_slug" : manga_slug, |
| 51 | + "manga_titles": alt_titles, |
| 52 | + "author" : author, |
| 53 | + "status" : status, |
| 54 | + "genres" : genres, |
| 55 | + "description" : descr, |
| 56 | + "lang" : "en", |
| 57 | + "language" : "English", |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | +class KaliscanChapterExtractor(KaliscanBase, ChapterExtractor): |
| 62 | + """Extractor for kaliscan manga chapters""" |
| 63 | + pattern = BASE_PATTERN + r"(/manga/([\w-]+)/chapter-([\d.]+))" |
| 64 | + example = "https://kaliscan.me/manga/ID-MANGA/chapter-1" |
| 65 | + |
| 66 | + def metadata(self, page): |
| 67 | + extr = text.extract_from(page) |
| 68 | + |
| 69 | + manga_id = text.parse_int(extr("bookId =", ";")) |
| 70 | + extr("bookSlug =", ";") |
| 71 | + chapter_id = text.parse_int(extr("chapterId =", ";")) |
| 72 | + extr("chapterSlug =", ";") |
| 73 | + chapter_number = extr("chapterNumber =", ";").strip(' "\'') |
| 74 | + |
| 75 | + chapter, sep, minor = chapter_number.partition(".") |
| 76 | + |
| 77 | + data = { |
| 78 | + "chapter" : text.parse_int(chapter), |
| 79 | + "chapter_minor": sep + minor, |
| 80 | + "chapter_id" : chapter_id, |
| 81 | + **self.manga_data(self.groups[1]), |
| 82 | + } |
| 83 | + if manga_id and not data["manga_id"]: |
| 84 | + data["manga_id"] = manga_id |
| 85 | + return data |
| 86 | + |
| 87 | + def images(self, page): |
| 88 | + images_str = text.extr(page, 'var chapImages = "', '"') |
| 89 | + if not images_str: |
| 90 | + return () |
| 91 | + return [ |
| 92 | + (url, None) |
| 93 | + for url in (u.strip() for u in images_str.split(",")) |
| 94 | + if url |
| 95 | + ] |
| 96 | + |
| 97 | + |
| 98 | +class KaliscanMangaExtractor(KaliscanBase, MangaExtractor): |
| 99 | + """Extractor for kaliscan manga""" |
| 100 | + chapterclass = KaliscanChapterExtractor |
| 101 | + pattern = BASE_PATTERN + r"(/manga/([\w-]+))/?$" |
| 102 | + example = "https://kaliscan.me/manga/ID-MANGA" |
| 103 | + |
| 104 | + def chapters(self, page): |
| 105 | + data = self.manga_data(self.groups[1], page) |
| 106 | + |
| 107 | + chapter_list = text.extr(page, 'id="chapter-list">', '</ul>') |
| 108 | + if not chapter_list: |
| 109 | + return () |
| 110 | + |
| 111 | + results = [] |
| 112 | + for li in text.extract_iter(chapter_list, "<li", "</li>"): |
| 113 | + url = text.extr(li, 'href="', '"') |
| 114 | + if not url: |
| 115 | + continue |
| 116 | + if url[0] == "/": |
| 117 | + url = self.root + url |
| 118 | + |
| 119 | + chapter, sep, minor = url.rpartition( |
| 120 | + "/chapter-")[2].partition(".") |
| 121 | + |
| 122 | + results.append((url, { |
| 123 | + "chapter" : text.parse_int(chapter), |
| 124 | + "chapter_minor": sep + minor, |
| 125 | + **data, |
| 126 | + })) |
| 127 | + return results |
0 commit comments