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