]> git.scottworley.com Git - paperdoorknob/blame - paperdoorknob.py
Strip all  
[paperdoorknob] / paperdoorknob.py
CommitLineData
92b11a10
SW
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
55958ec0 8import itertools
a0d30541 9
23f31879 10from typing import Iterable
a0d30541 11
136277e3 12from bs4 import BeautifulSoup
6409066b 13from bs4.element import Tag
23f31879
SW
14
15from args import spec_from_commandline_args
16from spec import Spec
92b11a10
SW
17
18
bf06f467
SW
19def parse(content: bytes) -> BeautifulSoup:
20 return BeautifulSoup(content, 'html.parser')
b25a2f90
SW
21
22
47cfa3cd
SW
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
23f31879
SW
47def process(spec: Spec) -> None:
48 spec.texout.write(b'\\documentclass{article}\n\\begin{document}\n')
929db576 49 html = clean(parse(spec.htmlfilter(spec.fetcher.fetch(spec.url))))
36ae1d5f 50 for r in replies(html):
23f31879
SW
51 spec.texout.write(spec.texifier.texify(r))
52 spec.texout.write(b'\\end{document}\n')
47cfa3cd
SW
53
54
92b11a10 55def main() -> None:
23f31879
SW
56 with spec_from_commandline_args() as spec:
57 process(spec)
92b11a10
SW
58
59
60if __name__ == '__main__':
61 main()