]> git.scottworley.com Git - paperdoorknob/blob - paperdoorknob.py
34223e4c35eb7636370c1032764de6455c075933
[paperdoorknob] / paperdoorknob.py
1 # paperdoorknob: Print glowfic
2 #
3 # This program is free software: you can redistribute it and/or modify it
4 # under the terms of the GNU General Public License as published by the
5 # Free Software Foundation, version 3.
6
7
8 import itertools
9
10 from typing import Iterable
11
12 from bs4 import BeautifulSoup
13 from bs4.element import Tag
14
15 from args import spec_from_commandline_args
16 from spec import Spec
17
18
19 def parse(content: bytes) -> BeautifulSoup:
20 return BeautifulSoup(content, 'html.parser')
21
22
23 def clean(html: BeautifulSoup) -> BeautifulSoup:
24 for eb in html.find_all("div", class_="post-edit-box"):
25 eb.decompose()
26 for footer in html.find_all("div", class_="post-footer"):
27 footer.decompose()
28 return html
29
30
31 def replies(html: BeautifulSoup) -> Iterable[Tag]:
32 def text() -> Tag:
33 body = html.body
34 assert body
35 text = body.find_next("div", class_="post-post")
36 assert isinstance(text, Tag)
37 return text
38
39 def the_replies() -> Iterable[Tag]:
40 rs = html.find_all("div", class_="post-reply")
41 assert all(isinstance(r, Tag) for r in rs)
42 return rs
43
44 return itertools.chain([text()], the_replies())
45
46
47 def process(spec: Spec) -> None:
48 spec.texout.write(b'\\documentclass{article}\n\\begin{document}\n')
49 html = clean(parse(spec.htmlfilter(spec.fetcher.fetch(spec.url))))
50 for r in replies(html):
51 spec.texout.write(spec.texifier.texify(r))
52 spec.texout.write(b'\\end{document}\n')
53
54
55 def main() -> None:
56 with spec_from_commandline_args() as spec:
57 process(spec)
58
59
60 if __name__ == '__main__':
61 main()