]>
Commit | Line | Data |
---|---|---|
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 | ||
8 | from argparse import ArgumentParser | |
ba3b7c52 | 9 | import os.path |
b25a2f90 | 10 | import requests |
b34a368f | 11 | import requests_cache |
ba3b7c52 | 12 | from xdg_base_dirs import xdg_cache_home |
92b11a10 SW |
13 | |
14 | ||
15 | def 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 |
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() | |
b25a2f90 SW |
33 | |
34 | ||
92b11a10 | 35 | def main() -> None: |
b25a2f90 | 36 | args = command_line_parser().parse_args() |
4c1cf54e | 37 | with requests_cache.CachedSession(args.cache_path, cache_control=True) as session: |
e138a9b4 | 38 | fetch(args.url, session, args.timeout) |
92b11a10 SW |
39 | |
40 | ||
41 | if __name__ == '__main__': | |
42 | main() |