forked from stuartlangridge/unsong-book-fetcher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_unsong.py
278 lines (255 loc) · 9.45 KB
/
get_unsong.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import sys
from bs4 import BeautifulSoup
import re
import os
import json
import io
from PIL import Image, ImageDraw, ImageFont
import requests
try:
from base64 import encodebytes
except ImportError:
from base64 import encodestring as encodebytes
CHAPTERS = []
AUTHOR_NOTES = []
TOSEFTA = []
header = """<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Unsong</title>
</head>
<body>
"""
footer = """</body></html>"""
INCLUDE_AUTHOR_NOTES = True #True to leave them in, False to exclude them, or "appendix" to put them all at the end.
INCLUDE_AUTOGEN_COVER = True
INCLUDE_TOSEFTA = True
def make_cover():
title_img = Image.open("assets/unsong_header.png") #this is the header image hotlinked on unsongbook.com as of 2024-11-16
tw, th = title_img.size
cw = int(tw * 1.5)
ch = int(cw * 1.6)
cover_img = Image.new("RGBA", (cw, ch))
draw = ImageDraw.Draw(cover_img)
gradient = ((180,119,14), (210,181,100))
height = cover_img.size[1]
rs, gs, bs = gradient[0]
re, ge, be = gradient[1]
rr = re - rs
gr = ge - gs
br = be - bs
for i in range(height):
r = rs + int(rr*i/height)
g = gs + int(gr*i/height)
b = bs + int(br*i/height)
draw.line([(0,i), (cw,i)], fill=(r,g,b))
tlx = int((cw - tw) / 2)
tly = int((ch - th) / 2)
cover_img.paste(title_img, (tlx, tly), title_img)
font = None
try:
font = ImageFont.truetype("/usr/share/texlive/texmf-dist/fonts/truetype/public/opensans/OpenSans-Light.ttf", size=24)
except OSError: # This handles the font being unfindable, as it is on Windows, and also if it couldn't load for some other reason I guess.
font = None
txt = "Scott Alexander"
txtw = draw.textlength(txt, font=font)
draw.text((int((cw - txtw) / 2), ch - 100), txt, fill=(0,0,0), font=font)
bio = io.BytesIO()
cover_img.save(bio, "PNG")
return "data:image/png;base64,%s" % (encodebytes(bio.getvalue()).decode("utf-8"))
def create_book():
nchapters = []
c18 = None
for c in CHAPTERS:
# Special handling for text replacement:
# Replace an ambiguous use of brackets not for telepathy or normal bracket stuff.
c = c.replace("[EVEN THOUGH THIS IS NOT FUNNY AT ALL]", "(EVEN THOUGH THIS IS NOT FUNNY AT ALL).")
# Special handling for chapter 18, which should be in book II but Alexander has done the navigation links wrong, so we manually insert it before c19.
if "Chapter 18:" in c:
c18 = c
continue
elif "Chapter 19" in c:
nchapters.append(c18)
nchapters.append(c)
os.makedirs("ebooks/", exist_ok=True)
fp = open("ebooks/Unsong.html", encoding="utf-8", mode="w")
fp.write(header)
fp.write("<header>")
if INCLUDE_AUTOGEN_COVER:
fp.write("<h1>Cover</h1>")
fp.write("<img src='%s' alt='Unsong by Scott Alexander'>" % make_cover())
fp.write("</header>")
fp.write("<main>")
fp.write("\n\n\n".join(nchapters))
fp.write("</main>")
if INCLUDE_AUTHOR_NOTES == "appendix":
fp.write("<section>")
fp.write("<h1>Appendix: Author Notes</h1>")
fp.write("\n\n\n".join(AUTHOR_NOTES))
fp.write("</section>")
if INCLUDE_TOSEFTA:
fp.write("<section>")
fp.write("\n\n\n".join(TOSEFTA))
fp.write("</section>")
fp.write(footer)
fp.close()
def slugify(url):
return re.sub(r"[^A-Za-z0-9]", "_", url)
def fetch_or_get(url, binary=False):
slug = slugify(url)
slug = "cache/%s" % slug
if os.path.exists(slug):
if binary:
fp = open(slug, mode="rb")
else:
fp = open(slug, encoding="utf-8")
data = fp.read()
fp.close()
#print("Got", url, "from cache")
else:
print("Fetching", url, "from web")
if url[:2] == "//":
url = "https:"+url
req = requests.get(
url,
headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
}
)
data = req.content
if binary:
fp = open(slug, mode="wb")
fp.write(data)
fp.close()
else:
fp = open(slug, encoding="utf-8", mode="w")
fp.write(data.decode("utf-8"))
fp.close()
return data
def get_cached_parsed(url):
slug = "CACHED_PARSED_%s" % (slugify(url),)
slug = "cache/%s" % slug
if not os.path.exists(slug):
return
fp = open(slug, encoding="utf-8")
data = json.load(fp)
fp.close()
return data
def put_cached_parsed(url, data):
slug = "CACHED_PARSED_%s" % (slugify(url),)
slug = "cache/%s" % slug
fp = open(slug, encoding="utf-8", mode="w")
json.dump(data, fp)
fp.close()
def remove_cache(url):
# first remove the HTML cache
slug = slugify(url)
slug = "cache/%s" % slug
if os.path.exists(slug):
os.unlink(slug)
# next, remove the cached parsed
slug = "CACHED_PARSED_%s" % (slugify(url),)
slug = "cache/%s" % slug
if os.path.exists(slug):
os.unlink(slug)
def get_url(url):
global ALL_CACHED
data = fetch_or_get(url, binary=False)
cached_parsed = get_cached_parsed(url)
if cached_parsed:
return cached_parsed
details = {}
soup = BeautifulSoup(data, "html.parser")
post = soup.find("div", ["post", "page"], recursive=True)
nav = soup.find_all("div", "pjgm-navigation")
try:
heading = post.find("h1", "pjgm-posttitle")
content = post.find("div", "pjgm-postcontent")
except AttributeError:
#we want to handle the nemorathwald appendix here without complicating the regular code
post = soup.find("div", "entry", recursive=True)
heading = post.find("h3", "entry-title", recursive=True)
heading = BeautifulSoup("<h1>Appendices</h1>", "html.parser") # Special handling: this is hard-coded here because, have to do it somehow.
content = post
if heading.text.lower().startswith("book"):
details["type"] = "book"
elif heading.text.lower().startswith(("author","postscript")):
details["type"] = "author note"
elif heading.text.lower().startswith(("prologue","epilogue")):
details["type"] = "logue"
elif heading.text.lower().startswith("tosefta"):
details["type"] = "tosefta"
elif heading.text.lower().startswith("appendices"):
details["type"] = "appendices"
else:
details["type"] = "chapter"
if details["type"] in ("book", "logue", "tosefta", "appendices"):
heading.name = "h1"
else:
heading.name = "h2"
prev = None
next = None
if nav:
prevs = nav[0].find_all("a", {"rel": "prev"})
if prevs:
prev = prevs[0].attrs["href"]
nexts = nav[0].find_all("a", {"rel": "next"})
if nexts:
next = nexts[0].attrs["href"]
share = soup.find_all("div", "sharedaddy")
[s.extract() for s in share]
# cache images
for img in content.find_all("img"):
img_url = img["src"]
if "5qMRb0F" in img_url:
#Special handling: I did not like the old Book I image. It was too tall. So here I replace it with an edited version Wyatt S Carpenter made.
img_data = open("assets/unsong book i fixed.png", "rb").read()
else:
# Not-so-special handling: here we throw out everything after a ?w= so that we can get bigger sizes of most images.
img_url = img_url.split("?w=")[0]
img_data = fetch_or_get(img_url, binary=True)
img_type = "image/" #vague to avoid having to detect specific image type.
img["src"] = "data:%s;base64,%s" % (img_type, encodebytes(img_data).decode("utf-8"))
html = '<article class="%s">\n%s\n%s\n</article>\n' % (details["type"], heading, content)
output = (prev, html, details, next)
put_cached_parsed(url, output)
ALL_CACHED = False
return output
def get_next(next):
global AUTHOR_NOTES, CHAPTERS, FORCE
last_fetched = next
previous, html, details, next = get_url(next)
if details["type"] == "author note":
if INCLUDE_AUTHOR_NOTES == "appendix":
AUTHOR_NOTES.append(html)
elif INCLUDE_AUTHOR_NOTES:
CHAPTERS.append(html)
elif details["type"] == "tosefta":
TOSEFTA.append(html)
else:
CHAPTERS.append(html)
if next:
get_next(next)
else:
if ALL_CACHED:
if FORCE:
print("Forcing the program to go check on the last chapter to see if there are new updates...")
if details["type"] == "author note":
AUTHOR_NOTES= AUTHOR_NOTES[:-1]
else:
CHAPTERS = CHAPTERS[:-1]
remove_cache(last_fetched)
get_next(last_fetched)
else:
print("Whole book retrieved from cache.")
print("To force an update (to pull in more recent chapters), pass --force parameter. To completely clear the cache and force a total refetch, delete all of the files in the cache/ folder (except the .gitignore file). You may also just force reparsing by deleting the appropriate CACHED_PARSED files.")
FORCE = False
ALL_CACHED = True
if __name__ == "__main__":
if "--force" in sys.argv:
FORCE = True
get_next("http://unsongbook.com/prologue-2/")
get_next("http://unsongbook.com/tosefta/")
get_next("https://nemorathwald.dreamwidth.org/404330.html")
create_book()