2015-11-07 02:32:59 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-02-07 11:22:47 +01:00
|
|
|
# Copyright 2015-2018 Mike Fährmann
|
2015-11-07 02:32:59 +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.
|
|
|
|
|
2015-11-28 22:21:35 +01:00
|
|
|
"""Extract manga-chapters and entire manga from http://kissmanga.com/"""
|
2015-11-07 02:32:59 +01:00
|
|
|
|
2018-02-07 11:22:47 +01:00
|
|
|
from .common import ChapterExtractor, MangaExtractor
|
2018-04-20 14:53:21 +02:00
|
|
|
from .. import text, cloudflare, aes, exception
|
2017-04-21 20:36:47 +02:00
|
|
|
from ..cache import cache
|
2015-11-07 02:32:59 +01:00
|
|
|
import re
|
2017-04-09 17:15:38 +02:00
|
|
|
import hashlib
|
2017-04-21 20:36:47 +02:00
|
|
|
import ast
|
2015-11-07 02:32:59 +01:00
|
|
|
|
2017-04-05 12:16:23 +02:00
|
|
|
IV = [
|
2017-04-21 20:36:47 +02:00
|
|
|
0xa5, 0xe8, 0xe2, 0xe9, 0xc2, 0x72, 0x1b, 0xe0,
|
|
|
|
0xa8, 0x4a, 0xd6, 0x60, 0xc4, 0x72, 0xc1, 0xf3
|
2017-04-05 12:16:23 +02:00
|
|
|
]
|
|
|
|
|
2017-02-01 00:53:19 +01:00
|
|
|
|
2018-02-07 11:22:47 +01:00
|
|
|
class KissmangaBase():
|
2015-11-28 22:21:35 +01:00
|
|
|
"""Base class for kissmanga extractors"""
|
2015-11-27 16:19:21 +01:00
|
|
|
category = "kissmanga"
|
2018-02-12 23:09:34 +01:00
|
|
|
archive_fmt = "{chapter_id}_{page}"
|
2018-12-30 13:19:35 +01:00
|
|
|
root = "https://kissmanga.com"
|
2015-11-27 16:19:21 +01:00
|
|
|
|
2017-12-01 15:22:50 +01:00
|
|
|
def request(self, url):
|
|
|
|
response = cloudflare.request_func(self, url)
|
|
|
|
if response.history and "/Message/AreYouHuman?" in response.url:
|
|
|
|
self.log.error("Requesting too many pages caused a redirect to %s."
|
|
|
|
" Try visiting this URL in your browser and solving"
|
|
|
|
" the CAPTCHA to continue.", response.url)
|
|
|
|
raise exception.StopExtraction()
|
|
|
|
return response
|
2015-11-27 16:19:21 +01:00
|
|
|
|
2017-09-19 16:23:13 +02:00
|
|
|
@staticmethod
|
2017-09-24 15:59:25 +02:00
|
|
|
def parse_chapter_string(data):
|
|
|
|
"""Parse 'chapter_string' value contained in 'data'"""
|
2017-09-19 16:23:13 +02:00
|
|
|
data["chapter_string"] = text.unescape(data["chapter_string"])
|
|
|
|
|
|
|
|
match = re.match((
|
|
|
|
r"(?:[Vv]ol\.0*(\d+) )?"
|
|
|
|
r"(?:[Cc]h\.)?0*(\d+)"
|
|
|
|
r"(?:[.:]0*(\d+))?"
|
|
|
|
r"(?: *[:-]? *(.+))?"
|
|
|
|
), data["chapter_string"])
|
|
|
|
|
|
|
|
if not match:
|
2017-12-24 22:53:10 +01:00
|
|
|
match = re.match((
|
|
|
|
r".+?(?: -)? ()"
|
|
|
|
r"0*(\d+)(?:[Vv.]0*(\d+))?"
|
|
|
|
r"(?: *[:-]? *(.+))?"
|
|
|
|
), data["chapter_string"])
|
2017-09-19 16:23:13 +02:00
|
|
|
|
2018-12-14 16:08:36 +01:00
|
|
|
if match:
|
|
|
|
volume, chapter, minor, title = match.groups()
|
|
|
|
else:
|
|
|
|
volume, chapter, minor, title = 0, 0, "", data["chapter_string"]
|
|
|
|
|
2018-04-20 14:53:21 +02:00
|
|
|
data["volume"] = text.parse_int(volume)
|
|
|
|
data["chapter"] = text.parse_int(chapter)
|
2017-09-19 16:23:13 +02:00
|
|
|
data["chapter_minor"] = "." + minor if minor else ""
|
|
|
|
data["title"] = title if title and title != "Read Online" else ""
|
2017-09-24 15:59:25 +02:00
|
|
|
return data
|
2017-09-19 16:23:13 +02:00
|
|
|
|
2015-11-27 21:31:17 +01:00
|
|
|
|
2018-02-07 11:22:47 +01:00
|
|
|
class KissmangaMangaExtractor(KissmangaBase, MangaExtractor):
|
2017-05-20 11:27:43 +02:00
|
|
|
"""Extractor for manga from kissmanga.com"""
|
2018-12-30 13:19:35 +01:00
|
|
|
pattern = [r"(?i)(?:https?://)?(?:www\.)?kissmanga\.com"
|
|
|
|
r"(/Manga/[^/?&#]+/?)$"]
|
2017-07-26 10:36:59 +02:00
|
|
|
test = [
|
2018-12-30 13:19:35 +01:00
|
|
|
("https://kissmanga.com/Manga/Dropout", {
|
|
|
|
"url": "9e3a6f715b229aa3fafa42a1d5da5d65614cb532",
|
2018-02-12 23:09:34 +01:00
|
|
|
"keyword": "32b09711c28b481845acc32e3bb6054cfc90224d",
|
2017-07-26 10:36:59 +02:00
|
|
|
}),
|
2018-12-30 13:19:35 +01:00
|
|
|
("https://kissmanga.com/manga/feng-shen-ji", None),
|
2017-07-26 10:36:59 +02:00
|
|
|
]
|
2015-11-27 21:31:17 +01:00
|
|
|
|
2018-12-30 13:19:35 +01:00
|
|
|
def __init__(self, match):
|
|
|
|
MangaExtractor.__init__(self, match, self.root + match.group(1))
|
|
|
|
|
2017-09-19 16:23:13 +02:00
|
|
|
def chapters(self, page):
|
|
|
|
results = []
|
2018-12-30 13:19:35 +01:00
|
|
|
manga, pos = text.extract(page, ' class="barTitle">', '\ninformation')
|
|
|
|
page , pos = text.extract(page, ' class="listing">', '</table>', pos)
|
2017-10-12 14:49:27 +02:00
|
|
|
manga = manga.strip()
|
2017-09-19 16:23:13 +02:00
|
|
|
needle = '" title="Read ' + manga + ' '
|
|
|
|
manga = text.unescape(manga)
|
|
|
|
|
|
|
|
for item in text.extract_iter(page, '<a href="', ' online">'):
|
|
|
|
url, _, chapter = item.partition(needle)
|
|
|
|
data = {
|
2018-02-12 23:09:34 +01:00
|
|
|
"manga": manga, "chapter_string": chapter,
|
2018-04-20 14:53:21 +02:00
|
|
|
"chapter_id": text.parse_int(url.rpartition("=")[2]),
|
2018-02-12 23:09:34 +01:00
|
|
|
"lang": "en", "language": "English",
|
2017-09-19 16:23:13 +02:00
|
|
|
}
|
2017-09-24 15:59:25 +02:00
|
|
|
self.parse_chapter_string(data)
|
2017-09-19 16:23:13 +02:00
|
|
|
results.append((self.root + url, data))
|
|
|
|
return results
|
2015-11-27 16:19:21 +01:00
|
|
|
|
|
|
|
|
2018-02-07 11:22:47 +01:00
|
|
|
class KissmangaChapterExtractor(KissmangaBase, ChapterExtractor):
|
2016-09-12 10:20:57 +02:00
|
|
|
"""Extractor for manga-chapters from kissmanga.com"""
|
2018-02-07 11:22:47 +01:00
|
|
|
pattern = [r"(?i)(?:https?://)?(?:www\.)?kissmanga\.com"
|
2018-12-30 13:19:35 +01:00
|
|
|
r"(/Manga/[^/?&#]+/[^/?&#]+\?id=(\d+))"]
|
2015-12-13 04:36:44 +01:00
|
|
|
test = [
|
2018-12-30 13:19:35 +01:00
|
|
|
("https://kissmanga.com/Manga/Dropout/Ch-000---Oneshot-?id=145847", {
|
2018-04-07 15:28:55 +02:00
|
|
|
"url": "46e63fd63e9e16f19bc1e6c7a45dc060815642fd",
|
2018-02-12 23:09:34 +01:00
|
|
|
"keyword": "4a3a9341d453541de0dbfa24cd6b2e3ed39c0182",
|
2015-12-13 04:36:44 +01:00
|
|
|
}),
|
2018-12-30 13:19:35 +01:00
|
|
|
("https://kissmanga.com/Manga/Urban-Tales/a?id=256717", {
|
2018-04-07 15:28:55 +02:00
|
|
|
"url": "c26be8bf9c2abacee2076979d021634092cf38f1",
|
2018-02-12 23:09:34 +01:00
|
|
|
"keyword": "ffc11b630da44fe67709ed0473756cf51b90a05c",
|
2017-06-19 09:55:02 +02:00
|
|
|
}),
|
2018-12-30 13:19:35 +01:00
|
|
|
("https://kissmanga.com/Manga/Monster/Monster-79?id=7608", {
|
2018-01-23 16:54:19 +01:00
|
|
|
"count": 23,
|
2018-04-08 17:50:57 +02:00
|
|
|
"keyword": "d47c94f4c57f4ab690a34b60fefac7b294468856",
|
2017-06-19 09:55:02 +02:00
|
|
|
}),
|
2018-12-14 16:08:36 +01:00
|
|
|
("https://kissmanga.com/Manga/Houseki-no-Kuni/Oneshot?id=404189", {
|
|
|
|
"count": 49,
|
|
|
|
"keyword": "7835a19c9fc54ec4f2b345e8be3e865cfa57da5c",
|
|
|
|
}),
|
2018-12-30 13:19:35 +01:00
|
|
|
("https://kissmanga.com/mAnGa/mOnStEr/Monster-79?id=7608", None),
|
2015-12-13 04:36:44 +01:00
|
|
|
]
|
2015-11-21 04:26:30 +01:00
|
|
|
|
2018-02-07 11:22:47 +01:00
|
|
|
def __init__(self, match):
|
2018-12-30 13:19:35 +01:00
|
|
|
ChapterExtractor.__init__(self, self.root + match.group(1))
|
|
|
|
self.chapter_id = match.group(2)
|
2018-02-07 11:22:47 +01:00
|
|
|
self.session.headers["Referer"] = self.root
|
|
|
|
|
|
|
|
def get_metadata(self, page):
|
2017-10-12 14:49:27 +02:00
|
|
|
title = text.extract(page, "<title>", "</title>")[0].strip()
|
|
|
|
manga, cinfo = title.split("\n")[1:3]
|
2017-09-19 16:23:13 +02:00
|
|
|
data = {
|
2017-10-12 14:49:27 +02:00
|
|
|
"manga": manga.strip(),
|
|
|
|
"chapter_string": cinfo.strip(),
|
2018-04-20 14:53:21 +02:00
|
|
|
"chapter_id": text.parse_int(self.chapter_id),
|
2015-11-07 02:32:59 +01:00
|
|
|
"lang": "en",
|
|
|
|
"language": "English",
|
|
|
|
}
|
2017-09-24 15:59:25 +02:00
|
|
|
return self.parse_chapter_string(data)
|
2015-11-07 02:32:59 +01:00
|
|
|
|
2018-02-07 11:22:47 +01:00
|
|
|
def get_images(self, page):
|
2018-04-06 15:30:09 +02:00
|
|
|
self.session.headers["Referer"] = None
|
2017-04-09 17:15:38 +02:00
|
|
|
try:
|
2017-04-29 15:58:33 +02:00
|
|
|
key = self.build_aes_key(page)
|
2017-04-09 17:15:38 +02:00
|
|
|
return [
|
2018-02-07 11:22:47 +01:00
|
|
|
(aes.aes_cbc_decrypt_text(data, key, IV), None)
|
2017-04-09 17:15:38 +02:00
|
|
|
for data in text.extract_iter(
|
|
|
|
page, 'lstImages.push(wrapKA("', '"'
|
|
|
|
)
|
|
|
|
]
|
|
|
|
except UnicodeDecodeError:
|
2018-12-30 13:19:35 +01:00
|
|
|
self.log.error("Failed to decrypt image URLs")
|
2017-09-24 15:59:25 +02:00
|
|
|
except (ValueError, IndexError):
|
2017-04-29 15:58:33 +02:00
|
|
|
self.log.error("Failed to get AES key")
|
2017-04-21 20:36:47 +02:00
|
|
|
return []
|
2017-04-09 17:15:38 +02:00
|
|
|
|
2017-04-29 15:58:33 +02:00
|
|
|
def build_aes_key(self, page):
|
2017-04-30 11:02:32 +02:00
|
|
|
chko = self._chko_from_external_script()
|
|
|
|
|
2017-04-29 15:58:33 +02:00
|
|
|
for script in self._scripts(page):
|
2017-04-30 11:02:32 +02:00
|
|
|
for stmt in [s.strip() for s in script.split(";")]:
|
2017-04-29 15:58:33 +02:00
|
|
|
|
2017-04-30 11:02:32 +02:00
|
|
|
if stmt.startswith("var _"):
|
|
|
|
name, _, value = stmt[4:].partition(" = ")
|
2017-04-29 15:58:33 +02:00
|
|
|
name += "[0]"
|
|
|
|
value = ast.literal_eval(value)[0]
|
|
|
|
|
2017-04-30 11:02:32 +02:00
|
|
|
elif stmt.startswith("chko = "):
|
|
|
|
stmt = stmt[7:]
|
|
|
|
if stmt == name:
|
2017-04-29 15:58:33 +02:00
|
|
|
chko = value
|
2017-04-30 11:02:32 +02:00
|
|
|
elif stmt == "chko + " + name:
|
2017-04-29 15:58:33 +02:00
|
|
|
chko = chko + value
|
2017-04-30 11:02:32 +02:00
|
|
|
elif stmt == name + " + chko":
|
2017-04-29 15:58:33 +02:00
|
|
|
chko = value + chko
|
|
|
|
else:
|
2017-04-30 11:02:32 +02:00
|
|
|
self.log.warning("unrecognized expression: '%s'", stmt)
|
2017-04-29 15:58:33 +02:00
|
|
|
|
2017-04-30 11:02:32 +02:00
|
|
|
elif stmt.startswith("key = "):
|
2017-04-29 15:58:33 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
else:
|
2017-04-30 11:02:32 +02:00
|
|
|
self.log.warning("unrecognized statement: '%s'", stmt)
|
2017-04-29 15:58:33 +02:00
|
|
|
|
|
|
|
return list(hashlib.sha256(chko.encode("ascii")).digest())
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _scripts(page):
|
|
|
|
end = 0
|
|
|
|
while True:
|
|
|
|
pos = page.find("key = ", end)
|
|
|
|
if pos == -1:
|
|
|
|
return
|
|
|
|
beg = page.rindex('<script type="text/javascript">', 0, pos) + 31
|
|
|
|
end = page.index('</script>', pos)
|
|
|
|
yield page[beg:end]
|
|
|
|
|
2017-04-21 20:36:47 +02:00
|
|
|
@cache(maxage=3600)
|
2017-04-29 15:58:33 +02:00
|
|
|
def _chko_from_external_script(self):
|
2017-04-21 20:36:47 +02:00
|
|
|
script = self.request(self.root + "/Scripts/lo.js").text
|
|
|
|
|
|
|
|
pos = script.index("var chko")
|
|
|
|
var = text.extract(script, "=", "[", pos)[0].lstrip()
|
|
|
|
idx = text.extract(script, "[", "]", pos)[0]
|
|
|
|
|
|
|
|
pos = script.index(var)
|
|
|
|
lst = text.extract(script, "=", ";", pos)[0]
|
2017-04-29 15:58:33 +02:00
|
|
|
return ast.literal_eval(lst.strip())[int(idx)]
|