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