-
-
Notifications
You must be signed in to change notification settings - Fork 317
/
Copy pathroyalroad.py
92 lines (71 loc) · 3.05 KB
/
royalroad.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -*- coding: utf-8 -*-
import logging
from lncrawl.core.crawler import Crawler
logger = logging.getLogger(__name__)
search_url = "https://www.royalroad.com/fictions/search?keyword=%s"
class RoyalRoadCrawler(Crawler):
base_url = "https://www.royalroad.com/"
def initialize(self):
self.init_executor(1)
def search_novel(self, query):
query = query.lower().replace(" ", "+")
soup = self.get_soup(search_url % query)
results = []
for a in soup.select("h2.fiction-title a")[:5]:
url = self.absolute_url(a["href"])
results.append(
{
"url": url,
"title": a.text.strip(),
}
)
return results
def read_novel_info(self):
logger.debug("Visiting %s", self.novel_url)
soup = self.get_soup(self.novel_url)
self.novel_title = soup.find("h1", {"class": "font-white"}).text.strip()
logger.info("Novel title: %s", self.novel_title)
self.novel_cover = self.absolute_url(
soup.find("img", {"class": "thumbnail inline-block"})["src"]
)
logger.info("Novel cover: %s", self.novel_cover)
self.novel_author = soup.find("a", {"class": "font-white"}).text.strip()
logger.info("Novel author: %s", self.novel_author)
self.novel_synopsis = self.cleaner.extract_contents(
soup.find("div", {"class": "hidden-content"})
)
logger.info("Novel synopsis: %s", self.novel_synopsis)
for tag in soup.find_all("a", {"class": "fiction-tag"}):
self.novel_tags.append(tag.text)
logger.info("Novel tags: %s", self.novel_tags)
chapter_rows = soup.find("tbody").findAll("tr")
chapters = [row.find("a", href=True) for row in chapter_rows]
for x in chapters:
chap_id = len(self.chapters) + 1
vol_id = 1 + len(self.chapters) // 100
if len(self.volumes) < vol_id:
self.volumes.append({"id": vol_id})
self.chapters.append(
{
"id": chap_id,
"volume": vol_id,
"url": self.absolute_url(x["href"]),
"title": x.text.strip() or ("Chapter %d" % chap_id),
}
)
def download_chapter_body(self, chapter):
soup = self.get_soup(chapter["url"])
possible_title = soup.select_one("h2")
if possible_title and "Chapter" in possible_title.text:
chapter["title"] = possible_title.text.strip()
classnames = []
for style in soup.select("style"):
style = style.text.replace(" ", "").replace("\n", "")
if style.endswith("{display:none;speak:never;}"):
classnames.append(style[1:-27])
for classname in classnames:
for div in soup.find_all("p", {"class": classname}):
div.decompose()
contents = soup.select_one(".chapter-content")
self.cleaner.clean_contents(contents)
return str(contents)