]>
git.scottworley.com Git - paperdoorknob/blob - paperdoorknob.py
1 # paperdoorknob: Print glowfic
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.
8 from argparse
import ArgumentParser
11 from typing
import Iterable
13 from bs4
import BeautifulSoup
14 from bs4
.element
import Tag
17 from xdg_base_dirs
import xdg_cache_home
21 def __init__(self
, html
: BeautifulSoup
) -> None:
24 def text(self
) -> Tag
:
25 body
= self
._html
.body
27 text
= body
.find_next("div", class_
="post-post")
28 assert isinstance(text
, Tag
)
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
)
37 def command_line_parser() -> ArgumentParser
:
38 parser
= ArgumentParser(prog
='paperdoorknob', description
='Print glowfic')
42 help='Where to keep the http cache (instead of %(default)s)',
43 default
=os
.path
.join(xdg_cache_home(), "paperdoorknob"))
46 help='How long to wait for HTTP requests, in seconds',
48 parser
.add_argument('url', help='URL to retrieve')
52 def fetch(url
: str, session
: requests
.Session
, timeout
: int) -> BeautifulSoup
:
53 with session
.get(url
, timeout
=timeout
) as r
:
55 return BeautifulSoup(r
.text
, 'html.parser')
59 args
= command_line_parser().parse_args()
60 with requests_cache
.CachedSession(args
.cache_path
, cache_control
=True) as session
:
61 html
= fetch(args
.url
, session
, args
.timeout
)
65 if __name__
== '__main__':