]> git.scottworley.com Git - paperdoorknob/blame_incremental - paperdoorknob.py
Replies
[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
8from argparse import ArgumentParser
9import os.path
10
11from typing import Iterable
12
13from bs4 import BeautifulSoup
14from bs4.element import Tag
15import requests
16import requests_cache
17from xdg_base_dirs import xdg_cache_home
18
19
20class Post:
21 def __init__(self, html: BeautifulSoup) -> None:
22 self._html = html
23
24 def text(self) -> Tag:
25 body = self._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 replies(self) -> Iterable[Tag]:
32 replies = self._html.find_all("div", class_="post-reply")
33 assert all(isinstance(r, Tag) for r in replies)
34 return replies
35
36
37def command_line_parser() -> ArgumentParser:
38 parser = ArgumentParser(prog='paperdoorknob', description='Print glowfic')
39 parser.add_argument(
40 '--cache_path',
41 metavar='PATH',
42 help='Where to keep the http cache (instead of %(default)s)',
43 default=os.path.join(xdg_cache_home(), "paperdoorknob"))
44 parser.add_argument(
45 '--timeout',
46 help='How long to wait for HTTP requests, in seconds',
47 default=30)
48 parser.add_argument('url', help='URL to retrieve')
49 return parser
50
51
52def fetch(url: str, session: requests.Session, timeout: int) -> BeautifulSoup:
53 with session.get(url, timeout=timeout) as r:
54 r.raise_for_status()
55 return BeautifulSoup(r.text, 'html.parser')
56
57
58def main() -> None:
59 args = command_line_parser().parse_args()
60 with requests_cache.CachedSession(args.cache_path, cache_control=True) as session:
61 html = fetch(args.url, session, args.timeout)
62 Post(html)
63
64
65if __name__ == '__main__':
66 main()