]>
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 |
136277e3 | 10 | from bs4 import BeautifulSoup |
b25a2f90 | 11 | import requests |
b34a368f | 12 | import requests_cache |
ba3b7c52 | 13 | from xdg_base_dirs import xdg_cache_home |
92b11a10 SW |
14 | |
15 | ||
16 | def command_line_parser() -> ArgumentParser: | |
17 | parser = ArgumentParser(prog='paperdoorknob', description='Print glowfic') | |
ba3b7c52 SW |
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")) | |
b25a2f90 SW |
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') | |
92b11a10 SW |
28 | return parser |
29 | ||
30 | ||
136277e3 | 31 | def fetch(url: str, session: requests.Session, timeout: int) -> BeautifulSoup: |
e138a9b4 SW |
32 | with session.get(url, timeout=timeout) as r: |
33 | r.raise_for_status() | |
136277e3 | 34 | return BeautifulSoup(r.text, 'html.parser') |
b25a2f90 SW |
35 | |
36 | ||
92b11a10 | 37 | def main() -> None: |
b25a2f90 | 38 | args = command_line_parser().parse_args() |
4c1cf54e | 39 | with requests_cache.CachedSession(args.cache_path, cache_control=True) as session: |
e138a9b4 | 40 | fetch(args.url, session, args.timeout) |
92b11a10 SW |
41 | |
42 | ||
43 | if __name__ == '__main__': | |
44 | main() |