-
-
Notifications
You must be signed in to change notification settings - Fork 312
/
Copy pathmangatoon.py
73 lines (56 loc) · 2.48 KB
/
mangatoon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# -*- coding: utf-8 -*-
import logging
import re
import ast
from lncrawl.core.crawler import Crawler
logger = logging.getLogger(__name__)
book_url = "https://mangatoon.mobi/%s/detail/%s/episodes"
search_url = "https://mangatoon.mobi/%s/search?word=%s"
class MangatoonMobiCrawler(Crawler):
base_url = [
"https://mangatoon.mobi/",
]
def initialize(self):
self.home_url = "https://mangatoon.mobi"
def read_novel_info(self):
novel_id = self.novel_url.split("/")[5]
logger.info("Novel Id: %s", novel_id)
novel_region = self.novel_url.split("/")[3]
logger.info("Novel Region: %s", novel_region)
self.novel_url = book_url % (novel_region, novel_id)
logger.debug("Visiting %s", self.novel_url)
soup = self.get_soup(self.novel_url)
possible_title = soup.select_one("h1.comics-title, .detail-title")
assert possible_title, "No novel title"
self.novel_title = possible_title.text
logger.info("Novel title: %s", self.novel_title)
possible_image = soup.select_one(".detail-top-right img, .detail-img .big-img")
if possible_image:
self.novel_cover = self.absolute_url(possible_image["src"])
logger.info("Novel cover: %s", self.novel_cover)
volumes = set([])
for a in soup.select("a.episode-item, a.episode-item-new"):
chap_id = len(self.chapters) + 1
vol_id = len(self.chapters) // 100 + 1
volumes.add(vol_id)
self.chapters.append(
{
"id": chap_id,
"volume": vol_id,
"url": self.absolute_url(a["href"]),
"title": a.select_one(".episode-title, .episode-title-new").text,
}
)
self.volumes = [{"id": x} for x in volumes]
def download_chapter_body(self, chapter):
soup = self.get_soup(chapter["url"])
pictures = soup.select_one(".pictures")
if pictures:
return str(pictures)
script = soup.find("script", text=re.compile(r"initialValue\s+="))
initialValue = re.search("var initialValue = (?P<value>.*);", script.string)
content = initialValue.group("value")
chapter_content = ast.literal_eval(content)
chapter_content = [p.replace(r"\-", "-") for p in chapter_content]
chapter_content = [p.replace("\\", "") for p in chapter_content]
return "<p>" + "</p><p>".join(chapter_content) + "</p>"