]> git.scottworley.com Git - paperdoorknob/blame - paperdoorknob.py
Extensible, flag-controlled DOM filters
[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 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
23f31879
SW
39def process(spec: Spec) -> None:
40 spec.texout.write(b'\\documentclass{article}\n\\begin{document}\n')
8be20b9d 41 html = parse(spec.htmlfilter(spec.fetcher.fetch(spec.url)))
36ae1d5f 42 for r in replies(html):
8be20b9d 43 spec.domfilter(r)
23f31879
SW
44 spec.texout.write(spec.texifier.texify(r))
45 spec.texout.write(b'\\end{document}\n')
47cfa3cd
SW
46
47
92b11a10 48def main() -> None:
23f31879
SW
49 with spec_from_commandline_args() as spec:
50 process(spec)
92b11a10
SW
51
52
53if __name__ == '__main__':
54 main()