]>
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 |
a0d30541 SW |
10 | |
11 | from typing import Iterable | |
12 | ||
136277e3 | 13 | from bs4 import BeautifulSoup |
6409066b | 14 | from bs4.element import Tag |
b25a2f90 | 15 | import requests |
b34a368f | 16 | import requests_cache |
ba3b7c52 | 17 | from xdg_base_dirs import xdg_cache_home |
92b11a10 SW |
18 | |
19 | ||
6409066b SW |
20 | class Post: |
21 | def __init__(self, html: BeautifulSoup) -> None: | |
22 | self._html = html | |
23 | ||
24 | def text(self) -> Tag: | |
25 | body = self._html.body | |
26 | assert body | |
27 | text = body.find_next("div", class_="post-post") | |
28 | assert isinstance(text, Tag) | |
29 | return text | |
30 | ||
a0d30541 SW |
31 | def replies(self) -> Iterable[Tag]: |
32 | replies = self._html.find_all("div", class_="post-reply") | |
33 | assert all(isinstance(r, Tag) for r in replies) | |
34 | return replies | |
35 | ||
6409066b | 36 | |
92b11a10 SW |
37 | def command_line_parser() -> ArgumentParser: |
38 | parser = ArgumentParser(prog='paperdoorknob', description='Print glowfic') | |
ba3b7c52 SW |
39 | parser.add_argument( |
40 | '--cache_path', | |
41 | metavar='PATH', | |
42 | help='Where to keep the http cache (instead of %(default)s)', | |
43 | default=os.path.join(xdg_cache_home(), "paperdoorknob")) | |
b25a2f90 SW |
44 | parser.add_argument( |
45 | '--timeout', | |
46 | help='How long to wait for HTTP requests, in seconds', | |
47 | default=30) | |
48 | parser.add_argument('url', help='URL to retrieve') | |
92b11a10 SW |
49 | return parser |
50 | ||
51 | ||
136277e3 | 52 | def fetch(url: str, session: requests.Session, timeout: int) -> BeautifulSoup: |
e138a9b4 SW |
53 | with session.get(url, timeout=timeout) as r: |
54 | r.raise_for_status() | |
136277e3 | 55 | return BeautifulSoup(r.text, 'html.parser') |
b25a2f90 SW |
56 | |
57 | ||
92b11a10 | 58 | def main() -> None: |
b25a2f90 | 59 | args = command_line_parser().parse_args() |
4c1cf54e | 60 | with requests_cache.CachedSession(args.cache_path, cache_control=True) as session: |
6409066b SW |
61 | html = fetch(args.url, session, args.timeout) |
62 | Post(html) | |
92b11a10 SW |
63 | |
64 | ||
65 | if __name__ == '__main__': | |
66 | main() |