2016-10-23 17:51:12 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright 2016 Mike Fährmann
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
"""Base classes for extractors for FoolSlide based sites"""
|
|
|
|
|
|
|
|
from .common import Extractor, Message
|
|
|
|
from .. import text, iso639_1
|
|
|
|
import json
|
|
|
|
import re
|
|
|
|
|
|
|
|
class FoolslideChapterExtractor(Extractor):
|
|
|
|
"""Base class for chapter extractors on foolslide based sites"""
|
|
|
|
subcategory = "chapter"
|
2016-10-28 00:35:48 +02:00
|
|
|
directory_fmt = ["{category}", "{manga}", "{chapter:>03} - {title}"]
|
|
|
|
filename_fmt = "{manga}_{chapter:>03}_{page:>03}.{extension}"
|
2017-01-10 00:05:08 +01:00
|
|
|
single = True
|
2016-10-23 17:51:12 +02:00
|
|
|
|
|
|
|
def __init__(self, url, lang):
|
|
|
|
Extractor.__init__(self)
|
|
|
|
self.url = url
|
|
|
|
self.lang = lang
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
page = self.request(self.url, encoding="utf-8",
|
|
|
|
method="post", data={"adult": "true"}).text
|
|
|
|
data = self.get_job_metadata(page)
|
|
|
|
yield Message.Version, 1
|
|
|
|
yield Message.Directory, data
|
|
|
|
for data["page"], image in enumerate(self.get_images(page), 1):
|
|
|
|
try:
|
|
|
|
url = image["url"]
|
|
|
|
del image["url"]
|
|
|
|
del image["thumb_url"]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
data.update(image)
|
|
|
|
text.nameext_from_url(data["filename"], data)
|
|
|
|
yield Message.Url, url, data
|
|
|
|
|
|
|
|
def get_job_metadata(self, page):
|
|
|
|
"""Collect metadata for extractor-job"""
|
|
|
|
_ , pos = text.extract(page, '<h1 class="tbtitle dnone">', '')
|
|
|
|
manga , pos = text.extract(page, 'title="', '"', pos)
|
|
|
|
chapter , pos = text.extract(page, '">', '</a>', pos)
|
2016-10-28 00:35:48 +02:00
|
|
|
|
|
|
|
parts = chapter.split(":", maxsplit=1)
|
|
|
|
match = re.match(r"(?:Vol.(\d+) )?(?:Chapter (\d+)$|(.+))", parts[0])
|
|
|
|
volume = match.group(1) or ""
|
|
|
|
chapter = match.group(2) or match.group(3).strip()
|
|
|
|
|
2016-10-23 17:51:12 +02:00
|
|
|
return {
|
|
|
|
"manga": text.unescape(manga),
|
2016-10-28 00:35:48 +02:00
|
|
|
"chapter": chapter,
|
|
|
|
"volume": volume,
|
2016-10-23 17:51:12 +02:00
|
|
|
"lang": self.lang,
|
|
|
|
"language": iso639_1.code_to_language(self.lang),
|
2016-10-28 00:35:48 +02:00
|
|
|
"title": text.unescape(parts[1].strip() if len(parts) > 1 else ""),
|
2016-10-23 17:51:12 +02:00
|
|
|
}
|
|
|
|
|
2017-01-10 00:05:08 +01:00
|
|
|
def get_images(self, page):
|
2016-10-23 17:51:12 +02:00
|
|
|
"""Return a list of all images in this chapter"""
|
2017-01-10 00:05:08 +01:00
|
|
|
if self.single:
|
|
|
|
pos = 0
|
|
|
|
needle = "var pages = "
|
|
|
|
else:
|
|
|
|
pos = page.find("[{")
|
|
|
|
needle = " = "
|
|
|
|
return json.loads(text.extract(page, needle, ";", pos)[0])
|