-
-
Notifications
You must be signed in to change notification settings - Fork 288
/
scribblehub.py
132 lines (113 loc) · 4.1 KB
/
scribblehub.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# -*- coding: utf-8 -*-
import logging
from math import ceil
from urllib.parse import quote
from bs4 import Tag
from lncrawl.core.crawler import Crawler
logger = logging.getLogger(__name__)
chapter_post_url = "https://www.scribblehub.com/wp-admin/admin-ajax.php"
class ScribbleHubCrawler(Crawler):
base_url = [
"https://www.scribblehub.com/",
"https://scribblehub.com/",
]
def initialize(self) -> None:
self.cleaner.bad_css.update(
[
".p-avatar-wrap",
".sp-head",
".spdiv",
".chp_stats_feature",
".modern-footnotes-footnote",
".modern-footnotes-footnote__note",
".wi_authornotes",
]
)
self.cleaner.whitelist_attributes.update(
[
"border",
"class",
]
)
self.cleaner.whitelist_css_property.update(
[
"text-align",
]
)
def search_novel(self, query):
url = f"{self.home_url}?s={quote(query)}&post_type=fictionposts"
soup = self.get_soup(url)
results = []
for novel in soup.select("div.search_body"):
a = novel.select_one(".search_title a")
info = novel.select_one(".search_stats")
if not isinstance(a, Tag):
continue
results.append(
{
"title": a.text.strip(),
"url": self.absolute_url(a["href"]),
"info": info.text.strip() if isinstance(info, Tag) else "",
}
)
return results
def read_novel_info(self):
soup = self.get_soup(self.novel_url)
possible_title = soup.select_one("div.fic_title")
assert isinstance(possible_title, Tag)
self.novel_title = str(possible_title["title"]).strip()
logger.info("Novel title: %s", self.novel_title)
possible_image = soup.find("div", {"class": "fic_image"})
if isinstance(possible_image, Tag):
possible_image = possible_image.find("img")
if isinstance(possible_image, Tag):
self.novel_cover = self.absolute_url(possible_image["src"])
logger.info("Novel cover: %s", self.novel_cover)
possible_author = soup.find("span", {"class": "auth_name_fic"})
if isinstance(possible_author, Tag):
self.novel_author = possible_author.text.strip()
logger.info("Novel author: %s", self.novel_author)
chapter_count = soup.find("span", {"class": "cnt_toc"})
chapter_count = (
int(chapter_count.text) if isinstance(chapter_count, Tag) else -1
)
page_count = ceil(chapter_count / 15.0)
logger.info("Chapter list pages: %d" % page_count)
possible_mypostid = soup.select_one("input#mypostid")
assert isinstance(possible_mypostid, Tag)
mypostid = int(str(possible_mypostid["value"]))
logger.info("#mypostid = %d", mypostid)
response = self.submit_form(
f"{self.home_url}wp-admin/admin-ajax.php",
{
"action": "wi_getreleases_pagination",
"pagenum": -1,
"mypostid": mypostid,
},
)
soup = self.make_soup(response)
for chapter in reversed(soup.select(".toc_ol a.toc_a")):
self.chapters.append(
{
"id": len(self.chapters) + 1,
"url": self.absolute_url(str(chapter["href"])),
"title": chapter.text.strip(),
}
)
def download_chapter_body(self, chapter):
soup = self.get_soup(chapter["url"])
contents = soup.select_one("div#chp_raw")
self.cleaner.clean_contents(contents)
body = str(contents)
body += """<style type="text/css">
table {
margin: 0 auto;
border: 2px solid;
border-collapse: collapse;
}
table td {
padding: 10px;
}
</style>
"""
return body