]> git.scottworley.com Git - paperdoorknob/blame - paperdoorknob.py
fetch: cache: Keep cache in XDG_CACHE_HOME (eg: ~/.cache)
[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
8from argparse import ArgumentParser
ba3b7c52 9import os.path
b25a2f90 10import requests
b34a368f 11import requests_cache
ba3b7c52 12from xdg_base_dirs import xdg_cache_home
92b11a10
SW
13
14
15def command_line_parser() -> ArgumentParser:
16 parser = ArgumentParser(prog='paperdoorknob', description='Print glowfic')
ba3b7c52
SW
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"))
b25a2f90
SW
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')
92b11a10
SW
27 return parser
28
29
e138a9b4
SW
30def fetch(url: str, session: requests.Session, timeout: int) -> None:
31 with session.get(url, timeout=timeout) as r:
32 r.raise_for_status()
b25a2f90
SW
33
34
92b11a10 35def main() -> None:
b25a2f90 36 args = command_line_parser().parse_args()
ba3b7c52 37 with requests_cache.CachedSession(args.cache_path) as session:
e138a9b4 38 fetch(args.url, session, args.timeout)
92b11a10
SW
39
40
41if __name__ == '__main__':
42 main()