forked from rocurley/glowfic-dl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepub.py
342 lines (286 loc) · 10.7 KB
/
epub.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
from bs4 import BeautifulSoup
from ebooklib import epub
from tqdm.asyncio import tqdm
import sys
from urllib.parse import urljoin, urlparse
from collections import OrderedDict
import asyncio
import aiohttp
import aiolimiter
import os
import re
import streamlit as st
from stqdm import stqdm
# TODO:
# * Better kobo handling
# * Rewrite internal links
# Include linkbacks at the end of the thing that was linked to, eg: "This
# post was linked to from reply #114 of Mad investor chaos". <a>Return
# there</a>.
# * Less bad covers
class ImageMap:
def __init__(self):
self.map = {}
self.next = 0
def insert(self, url):
if url not in self.map:
path = urlparse(url).path
ext = path.split(".")[-1]
self.map[url] = "img%i.%s" % (self.next, ext)
self.next += 1
return self.map[url]
class RenderedPost:
def __init__(self, html, author, permalink, permalink_fragment):
self.html = html
self.author = author
self.permalink = permalink
self.permalink_fragment = permalink_fragment
def render_post(post, image_map):
try:
character = post.find("div", "post-character").text.strip()
except AttributeError:
character = None
try:
screen_name = post.find("div", "post-screenname").text.strip()
except AttributeError:
screen_name = None
try:
author = post.find("div", "post-author").text.strip()
except AttributeError:
author = None
content = post.find("div", "post-content")
header = BeautifulSoup("<p><strong></strong></p>", "html.parser")
header.find("strong").string = " / ".join(
[x for x in [character, screen_name, author] if x is not None]
)
post_html = BeautifulSoup('<div class="post"></div>', "html.parser")
post_div = post_html.find("div")
permalink = post.find("img", title="Permalink", alt="Permalink").parent["href"]
permalink_fragment = urlparse(permalink).fragment
reply_anchor = post_html.new_tag("a", id=permalink_fragment)
post_div.extend([reply_anchor]) # for linking to this reply
image = post.find("img", "icon")
if image:
local_image = BeautifulSoup('<img class="icon"></img>', "html.parser")
local_image.find("img")["src"] = image_map.insert(image["src"])
post_div.extend([header, local_image] + content.contents)
else:
post_div.extend([header] + content.contents)
return RenderedPost(
html=post_html,
author=author,
permalink=permalink,
permalink_fragment=permalink_fragment,
)
stylesheet = """
img.icon {
width:100px;
float:left;
margin-right: 1em;
margin-bottom: 1em;
}
div.post {
overflow: hidden;
margin: 0.5em;
background: white;
page-break-inside:avoid;
}
div.posts {
background: grey;
}
"""
output_template = """
<html>
<head>
</head>
<body>
<div class="posts">
</div>
</body>
</html>
"""
SECTION_SIZE_LIMIT = 200000
class Section:
def __init__(self):
self.html = BeautifulSoup(output_template, "html.parser")
self.body = self.html.find("div")
self.size = 0
self.link_targets = []
def append(self, post):
post_size = len(post.html.encode())
self.size += post_size
self.body.append(post.html)
self.link_targets.append(post.permalink)
def render_posts(posts, image_map, authors):
out = Section()
for post in posts:
rendered = render_post(post, image_map)
post_size = len(rendered.html.encode())
if out.size + post_size > SECTION_SIZE_LIMIT and out.size > 0:
yield out
out = Section()
out.append(rendered)
authors[rendered.author] = True
yield out
async def download_chapter(session, limiter, i, url, image_map, authors):
await limiter.acquire()
resp = await session.get(url, params={"view": "flat"})
soup = BeautifulSoup(await resp.text(), "html.parser")
resp.close()
posts = soup.find_all("div", "post-container")
title = validate_tag(soup.find("span", id="post-title"), soup).text.strip()
return (title, list(render_posts(posts, image_map, authors)))
REPLY_RE = re.compile(r"/(replies|posts)/\d*")
def compile_chapters(chapters):
anchor_sections = {}
for (i, (title, sections)) in enumerate(chapters):
for (j, section) in enumerate(sections):
file_name = "chapter%i_%i.html" % (i, j)
for permalink in section.link_targets:
anchor_sections[permalink] = file_name
for (i, (title, sections)) in enumerate(chapters):
for (j, section) in enumerate(sections):
for a in section.html.find_all("a"):
if "href" not in a.attrs:
continue
raw_url = a["href"]
url = urlparse(raw_url)
if REPLY_RE.match(raw_url) and raw_url in anchor_sections:
a["href"] = url._replace(path=anchor_sections[raw_url]).geturl()
elif url.netloc == "": # Glowfic link to something not included here
a["href"] = url._replace(
scheme="https", netloc="glowfic.com"
).geturl()
for (i, (title, sections)) in enumerate(chapters):
compiled_sections = []
for (j, section) in enumerate(sections):
file_name = "chapter%i_%i.html" % (i, j)
compiled_section = epub.EpubHtml(
title=title, file_name=file_name, media_type="application/xhtml+xml"
)
compiled_section.content = str(section.html)
compiled_section.add_link(
href="style.css", rel="stylesheet", type="text/css"
)
compiled_sections.append(compiled_section)
yield compiled_sections
def validate_tag(tag, soup):
if tag is not None:
return tag
err = soup.find("div", "flash error")
if err is not None:
raise RuntimeError(err.text.strip())
else:
raise RuntimeError("Unknown error: tag missing")
GLOWFIC_ROOT = "https://glowfic.com"
async def get_post_urls_and_title(session, limiter, url):
if "posts" in url:
return (None, [url])
if "board_sections" in url or "boards" in url:
await limiter.acquire()
resp = await session.get(url)
soup = BeautifulSoup(await resp.text(), "html.parser")
rows = validate_tag(soup.find("div", id="content"), soup).find_all(
"td", "post-subject"
)
posts = [urljoin(GLOWFIC_ROOT, row.find("a")["href"]) for row in rows]
title = soup.find("th", "table-title").contents[0].strip()
return (title, posts)
async def download_image(session, url, id):
try:
async with session.get(url, timeout=15) as resp:
item = epub.EpubItem(
uid=id,
file_name=id,
media_type=resp.headers["Content-Type"],
content=await resp.read(),
)
return item
except (aiohttp.ClientError, asyncio.TimeoutError):
st.write("Failed to download %s" % url)
return None
async def download_images(session, image_map):
in_flight = []
for (k, v) in image_map.map.items():
in_flight.append(download_image(session, k, v))
return [image for image in await stqdm.gather(*in_flight) if image is not None]
COOKIE_NAME = "_glowfic_constellation_production"
st.set_page_config(page_title="Glowfic to Epub", page_icon="⬇️")
params = st.experimental_get_query_params()
with st.sidebar:
st.write("## Glowfic to Epub")
st.write("""
Turn any glowfic into an .epub:
- Post url: https://glowfic.com/posts/5111
***
*Made by [Austin](https://manifold.markets/Austin),
based on [rocurley's code](https://github.com/rocurley/glowfic-dl)*
""")
async def main():
cookies = {}
if os.path.exists("cookie"):
with open("cookie") as fin:
raw = fin.read()
(name, cookie) = raw.split("=")
if name != COOKIE_NAME:
raise ValueError(
'cookie file must start with "%s=" (no quotes)' % COOKIE_NAME
)
cookies[COOKIE_NAME] = cookie.strip()
slow_conn = aiohttp.TCPConnector(limit_per_host=1)
async with aiohttp.ClientSession(
connector=slow_conn, cookies=cookies
) as slow_session:
async with aiohttp.ClientSession() as fast_session:
limiter = aiolimiter.AsyncLimiter(1, 1)
post = params["post"][0] if "post" in params else 5111
default_url = f"https://glowfic.com/posts/{post}"
url = st.text_input(label="Glowfic URL", value=default_url)
(book_title, urls) = await get_post_urls_and_title(
slow_session, limiter, url
)
st.info("Found %i chapters..." % len(urls))
book = epub.EpubBook()
image_map = ImageMap()
authors = OrderedDict()
st.info("Downloading text...")
chapters = await stqdm.gather(
*[
download_chapter(slow_session, limiter, i, url, image_map, authors)
for (i, url) in enumerate(urls)
]
)
chapters = list(compile_chapters(chapters))
for chapter in chapters:
for section in chapter:
book.add_item(section)
if book_title is None:
book_title = chapters[0][0].title
book.set_title(book_title)
style = epub.EpubItem(
uid="style",
file_name="style.css",
media_type="text/css",
content=stylesheet,
)
book.add_item(style)
st.info("Downloading images...")
images = await download_images(fast_session, image_map)
for image in images:
book.add_item(image)
for author in authors.keys():
book.add_author(author)
book.toc = [chapter[0] for chapter in chapters]
book.add_item(epub.EpubNcx())
book.add_item(epub.EpubNav())
book.spine = ["nav"] + [
section for chapter in chapters for section in chapter
]
out_path = "%s.epub" % book_title
st.info(f"Saving book as {out_path}...")
epub.write_epub(out_path, book, {})
# Create a button to download the file
with open(out_path, "rb") as f:
st.download_button(f"Download {out_path}", f, file_name=out_path)
st.write("*Pro tip: [Calibre](https://calibre-ebook.com/) can convert .epub into a .azw3 for your Kindle*")
asyncio.run(main())