]> git.scottworley.com Git - paperdoorknob/blob - paperdoorknob.py
2bcf31afe3c1b38407eec51f79babed956847fc9
[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 replies(html: BeautifulSoup) -> Iterable[Tag]:
24 def text() -> Tag:
25 body = html.body
26 assert body
27 text = body.find_next("div", class_="post-post")
28 assert isinstance(text, Tag)
29 return text
30
31 def the_replies() -> Iterable[Tag]:
32 rs = html.find_all("div", class_="post-reply")
33 assert all(isinstance(r, Tag) for r in rs)
34 return rs
35
36 return itertools.chain([text()], the_replies())
37
38
39 def process(spec: Spec) -> None:
40 spec.texout.write(b'\\documentclass{article}\n\\begin{document}\n')
41 html = parse(spec.htmlfilter(spec.fetcher.fetch(spec.url)))
42 for r in replies(html):
43 spec.domfilter(r)
44 spec.texout.write(spec.texifier.texify(r))
45 spec.texout.write(b'\\end{document}\n')
46
47
48 def main() -> None:
49 with spec_from_commandline_args() as spec:
50 process(spec)
51
52
53 if __name__ == '__main__':
54 main()