]> git.scottworley.com Git - paperdoorknob/blob - paperdoorknob.py
bb8bdd1ae38a660b3a6f90aa3d5aa317505beee3
[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 import requests
12 import requests_cache
13 from xdg_base_dirs import xdg_cache_home
14
15
16 def command_line_parser() -> ArgumentParser:
17 parser = ArgumentParser(prog='paperdoorknob', description='Print glowfic')
18 parser.add_argument(
19 '--cache_path',
20 metavar='PATH',
21 help='Where to keep the http cache (instead of %(default)s)',
22 default=os.path.join(xdg_cache_home(), "paperdoorknob"))
23 parser.add_argument(
24 '--timeout',
25 help='How long to wait for HTTP requests, in seconds',
26 default=30)
27 parser.add_argument('url', help='URL to retrieve')
28 return parser
29
30
31 def fetch(url: str, session: requests.Session, timeout: int) -> BeautifulSoup:
32 with session.get(url, timeout=timeout) as r:
33 r.raise_for_status()
34 return BeautifulSoup(r.text, 'html.parser')
35
36
37 def main() -> None:
38 args = command_line_parser().parse_args()
39 with requests_cache.CachedSession(args.cache_path, cache_control=True) as session:
40 fetch(args.url, session, args.timeout)
41
42
43 if __name__ == '__main__':
44 main()