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