-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnovelfull_dl.py
226 lines (170 loc) · 6.63 KB
/
novelfull_dl.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env python
import requests
import bs4
import argparse
import sys
from dataclasses import dataclass
#import cloudscraper
import pdfkit
import os
import time
import subprocess
import datetime
#-------- constants --------
URL_MAIN = "https://novelfull.com/{novelName}"
URL_CHAPTER = "https://novelfull.com{chapterLink}"
URL = "https://novelfull.com/{novelName}.html?page={pageNum}"
POLITENESS_FACTOR = 1 # delay between downloads, in seconds
# html5lib is needed because the HTML of novelfull is often broken
HTML_PARSER = "html5lib" # alt: html.parser
HEADERS_TO_REPLACE = ["h3", "h4"]
#-------- /constants --------
@dataclass
class Chapter:
"""Class for storing chapters and links"""
index: int
name: str
url: str # useless for this case (or change format of URL_CHAPTER)
def clean_chapter_name(chapName):
"""
Replace colons and other troublesome characters
"""
return chapName.replace(':', '_')
def get_chapter_content(chapter_html):
soup = bs4.BeautifulSoup(chapter_html, HTML_PARSER)
div = soup.find("div", {"id": "chapter-content"})
node = div.find("script")
content = []
while True:
node = node.nextSibling
if node.name == 'a':
break
content.append(node)
return content
def download_from_url(url):
resp = requests.get(url)
if not resp.ok:
return None
return resp.text
def get_chapter_processed(chapter_list, chapter_idx):
ch1 = chapter_list[chapter_idx]
contents = download_from_url(URL_CHAPTER.format(chapterLink=ch1.url))
soup = bs4.BeautifulSoup(contents, HTML_PARSER)
content = soup.find("div", {"id": "chapter-content"})
# replace h3 headings with h2 headers
# for proper chapter titles
for header in HEADERS_TO_REPLACE:
hx = content.find(header)
if hx:
h2 = soup.new_tag("h2")
h2.string = hx.string
hx.replace_with(h2)
'''
adjacentTags = soup.findAll("ins")
for tag in adjacentTags:
if 'data-ad-slot' in tag.attrs:
div = tag.findNext('div')
p = tag.findNext('p')
if p:
contentString += str(p)
if div:
contentString += str(p)
'''
for div in content.findAll("div", {"class": "ads"}):
div.decompose()
for script in content.findAll("script"):
script.decompose()
return str(content) #+ contentString
def make_pdf(chapter_list, novelName):
#generate default toc
if not os.path.exists("default_toc.xsl"):
with open('default_toc.xsl', 'w') as outfile:
subprocess.call(['wkhtmltopdf', '--dump-default-toc-xsl'], stdout=outfile)
toc = {
'xsl-style-sheet': 'default_toc.xsl'
}
body = ''
print("Downloading chapters and generating the PDF file...")
#for index in range(len(chapter_list)):
num_processed = 0
print(f"Downloading {len(chapter_list)} chapters")
for index in range(len(chapter_list)):
num_processed += 1
if num_processed % 50 == 0:
print(f"Processed {num_processed} chapters")
chapter = chapter_list[index]
content = get_chapter_processed(chapter_list, index)
# add this chapter to the body
if index == 0:
#body += f'<h2>{chapter.name}</h2>'+ content
body += content
else:
#body += f'<h2 style="page-break-before: always;">{chapter.name}</h2>'+ "".join(content)
body += '<hr style="page-break-before: always;"/>' + content
time.sleep(POLITENESS_FACTOR)
options = {
'margin-bottom': '20mm',
'footer-center': '[page]'
}
output = """
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
{body}
<p style="page-break-before: always;">
<b>Extra metadata</b>
<p>Downloaded from: <a href="{url}">{name}</a></p>
<p >Created on: {date} </p>
<p>Created using: novelfull_dl.py /p>
</p>
</body>
</html>""".format(body=body,
date=str(datetime.datetime.now()),
name=novelName,
url=URL_MAIN.format(novelName=novelName)
) # handle unicode
#pdfkit.from_string(output, "{name}.pdf", options=options)
pdfkit.from_string(output, f"{novelName}.pdf", toc=toc, options=options)
def extract_chapters(novelName, html_page):
print("Obtaining initial data...")
def extract_links_and_titles(pageSoup):
title_list_cont = pageSoup.find("div", {"id": "list-chapter"})
# should be two containers
chap_containers = title_list_cont.findAll("ul", {"class": "list-chapter"})
li = []
for cont in chap_containers:
li.extend([(anchor["title"], anchor["href"]) for anchor in cont.select("li > a")])
return li
chapter_list = []
soup = bs4.BeautifulSoup(html_page, HTML_PARSER)
title_list_cont = soup.find("div", {"id": "list-chapter"})
pagination = title_list_cont.find("ul", {"class": "pagination"})
last = pagination.find("li", {"class": "last"})
last_page = int( (last.select("li > a")[0]["data-page"]).strip() ) + 1
#title_list = soup.find("ul", {"class": "list-chapter"})
title_link_list = []
title_link_list.extend(extract_links_and_titles(soup)) # for page 1
print("Gathering links...")
for pageNum in range(2, last_page+1):
time.sleep(POLITENESS_FACTOR)
page_content = download_from_url(URL.format(novelName=novelName, pageNum=pageNum))
page_soup = bs4.BeautifulSoup(page_content, HTML_PARSER)
title_link_list.extend(extract_links_and_titles(page_soup))
for idx, (title, link) in enumerate(title_link_list):
chapter_list.append( Chapter(idx, title, link) )
#chapter_list.append( Chapter(idx, chapterName, chapterURL) )
return chapter_list
def main():
if len(sys.argv) == 1:
novelName = 'my-senior-brother-is-too-steady'
else:
novelName = sys.argv[1]
page_content = download_from_url(URL.format(novelName=novelName, pageNum=1))
chapter_list = extract_chapters(novelName, page_content)
#print(chapter_list)
make_pdf(chapter_list, novelName)
if __name__ == '__main__':
main()