2015-11-07 02:32:59 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2017-04-05 12:16:23 +02:00
|
|
|
# Copyright 2015-2017 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
|
|
|
|
2017-05-20 11:27:43 +02:00
|
|
|
from .common import Extractor, MangaExtractor, Message
|
2017-12-01 15:22:50 +01:00
|
|
|
from .. import text, util, 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
|
|
|
|
2015-11-27 21:31:17 +01:00
|
|
|
class KissmangaExtractor(Extractor):
|
2015-11-28 22:21:35 +01:00
|
|
|
"""Base class for kissmanga extractors"""
|
2015-11-27 16:19:21 +01:00
|
|
|
category = "kissmanga"
|
2017-10-02 19:06:24 +02:00
|
|
|
directory_fmt = [
|
|
|
|
"{category}", "{manga}",
|
|
|
|
"{volume:?v/ />02}c{chapter:>03}{chapter_minor}{title:?: //}"]
|
|
|
|
filename_fmt = (
|
|
|
|
"{manga}_c{chapter:>03}{chapter_minor}_{page:>03}.{extension}")
|
2016-12-16 13:28:36 +01:00
|
|
|
root = "http://kissmanga.com"
|
2015-11-27 16:19:21 +01:00
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
Extractor.__init__(self)
|
|
|
|
self.url = match.group(0)
|
2016-12-16 13:28:36 +01:00
|
|
|
self.session.headers["Referer"] = self.root
|
2016-11-20 18:05:49 +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-09-24 15:59:25 +02:00
|
|
|
match = re.match(
|
|
|
|
r"[\w ]+?(?: -)? 0*()(\d+)()(?: *[:-]? *(.+))?",
|
|
|
|
data["chapter_string"])
|
2017-09-19 16:23:13 +02:00
|
|
|
|
|
|
|
volume, chapter, minor, title = match.groups()
|
2017-09-24 15:59:25 +02:00
|
|
|
data["volume"] = util.safe_int(volume)
|
|
|
|
data["chapter"] = util.safe_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
|
|
|
|
2017-05-20 11:27:43 +02:00
|
|
|
class KissmangaMangaExtractor(KissmangaExtractor, MangaExtractor):
|
|
|
|
"""Extractor for manga from kissmanga.com"""
|
2017-10-09 22:37:50 +02:00
|
|
|
pattern = [r"(?i)(?:https?://)?(?:www\.)?kissmanga\.com/"
|
|
|
|
r"Manga/[^/?&#]+/?$"]
|
2017-07-26 10:36:59 +02:00
|
|
|
test = [
|
|
|
|
("http://kissmanga.com/Manga/Dropout", {
|
|
|
|
"url": "992befdd64e178fe5af67de53f8b510860d968ca",
|
2017-09-19 16:23:13 +02:00
|
|
|
"keyword": "1d23ea07296e004b33bee17fe2f5cd5177c58680",
|
2017-07-26 10:36:59 +02:00
|
|
|
}),
|
|
|
|
("http://kissmanga.com/manga/feng-shen-ji", None),
|
|
|
|
]
|
2015-11-27 21:31:17 +01:00
|
|
|
|
2017-09-19 16:23:13 +02:00
|
|
|
def chapters(self, page):
|
|
|
|
results = []
|
|
|
|
manga, pos = text.extract(
|
2017-10-12 14:49:27 +02:00
|
|
|
page, '<div class="barTitle">', '\ninformation')
|
2017-09-19 16:23:13 +02:00
|
|
|
page, pos = text.extract(
|
|
|
|
page, '<table 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 = {
|
|
|
|
"manga": manga, "id": url.rpartition("=")[2],
|
|
|
|
"chapter_string": chapter, "lang": "en", "language": "English",
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
2015-11-27 21:31:17 +01:00
|
|
|
class KissmangaChapterExtractor(KissmangaExtractor):
|
2016-09-12 10:20:57 +02:00
|
|
|
"""Extractor for manga-chapters from kissmanga.com"""
|
2015-11-30 01:11:13 +01:00
|
|
|
subcategory = "chapter"
|
2017-07-26 10:36:59 +02:00
|
|
|
pattern = [r"(?i)(?:https?://)?(?:www\.)?kissmanga\.com/"
|
2017-10-09 22:37:50 +02:00
|
|
|
r"Manga/[^/?&#]+/[^/?&#]+\?id=\d+"]
|
2015-12-13 04:36:44 +01:00
|
|
|
test = [
|
|
|
|
("http://kissmanga.com/Manga/Dropout/Ch-000---Oneshot-?id=145847", {
|
2016-07-12 12:08:36 +02:00
|
|
|
"url": "4136bcd1c6cecbca8cc2bc965d54f33ef0a97cc0",
|
2017-09-19 16:23:13 +02:00
|
|
|
"keyword": "68384c1167858fb4aa475c4596f0a685c45fff36",
|
2015-12-13 04:36:44 +01:00
|
|
|
}),
|
|
|
|
("http://kissmanga.com/Manga/Urban-Tales/a?id=256717", {
|
2016-07-12 12:08:36 +02:00
|
|
|
"url": "de074848f6c1245204bb9214c12bcc3ecfd65019",
|
2017-09-19 16:23:13 +02:00
|
|
|
"keyword": "089158338b4cde43b2ff244814effeb13297de33",
|
2017-06-19 09:55:02 +02:00
|
|
|
}),
|
|
|
|
("http://kissmanga.com/Manga/Monster/Monster-79?id=7608", {
|
|
|
|
"url": "6abec8178f35fe7846586280ca9e38eacc32452c",
|
2017-09-19 16:23:13 +02:00
|
|
|
"keyword": "558da596e86ca544eb72cf303f3694bbf0b1f2f5",
|
2017-06-19 09:55:02 +02:00
|
|
|
}),
|
2017-07-26 10:36:59 +02:00
|
|
|
("http://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
|
|
|
|
2015-11-07 02:32:59 +01:00
|
|
|
def items(self):
|
|
|
|
page = self.request(self.url).text
|
|
|
|
data = self.get_job_metadata(page)
|
|
|
|
imgs = self.get_image_urls(page)
|
|
|
|
data["count"] = len(imgs)
|
|
|
|
yield Message.Version, 1
|
|
|
|
yield Message.Directory, data
|
2016-11-08 00:15:27 +01:00
|
|
|
for data["page"], url in enumerate(imgs, 1):
|
2015-11-16 17:32:26 +01:00
|
|
|
yield Message.Url, url, text.nameext_from_url(url, data)
|
2015-11-07 02:32:59 +01:00
|
|
|
|
2015-11-21 04:26:30 +01:00
|
|
|
def get_job_metadata(self, page):
|
2015-11-07 02:32:59 +01:00
|
|
|
"""Collect metadata for extractor-job"""
|
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(),
|
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
|
|
|
|
2017-04-09 17:15:38 +02:00
|
|
|
def get_image_urls(self, page):
|
2015-11-07 02:32:59 +01:00
|
|
|
"""Extract list of all image-urls for a manga chapter"""
|
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 [
|
|
|
|
aes.aes_cbc_decrypt_text(data, key, IV)
|
|
|
|
for data in text.extract_iter(
|
|
|
|
page, 'lstImages.push(wrapKA("', '"'
|
|
|
|
)
|
|
|
|
]
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
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)]
|