]> git.scottworley.com Git - paperdoorknob/blame_incremental - paperdoorknob.py
FakeFetcher for faster tests
[paperdoorknob] / paperdoorknob.py
... / ...
CommitLineData
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
8import itertools
9
10from typing import Iterable
11
12from bs4 import BeautifulSoup
13from bs4.element import Tag
14
15from args import spec_from_commandline_args
16from spec import Spec
17
18
19def parse(content: bytes) -> BeautifulSoup:
20 return BeautifulSoup(content, 'html.parser')
21
22
23def 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
31def 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
47def process(spec: Spec) -> None:
48 spec.texout.write(b'\\documentclass{article}\n\\begin{document}\n')
49 html = clean(parse(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
55def main() -> None:
56 with spec_from_commandline_args() as spec:
57 process(spec)
58
59
60if __name__ == '__main__':
61 main()