]>
git.scottworley.com Git - paperdoorknob/blob - paperdoorknob.py
b88c02d73e64a604763ad5c8ca8785055496d53f
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
10 from bs4
import BeautifulSoup
11 from bs4
.element
import Tag
14 from xdg_base_dirs
import xdg_cache_home
18 def __init__(self
, html
: BeautifulSoup
) -> None:
21 def text(self
) -> Tag
:
22 body
= self
._html
.body
24 text
= body
.find_next("div", class_
="post-post")
25 assert isinstance(text
, Tag
)
29 def command_line_parser() -> ArgumentParser
:
30 parser
= ArgumentParser(prog
='paperdoorknob', description
='Print glowfic')
34 help='Where to keep the http cache (instead of %(default)s)',
35 default
=os
.path
.join(xdg_cache_home(), "paperdoorknob"))
38 help='How long to wait for HTTP requests, in seconds',
40 parser
.add_argument('url', help='URL to retrieve')
44 def fetch(url
: str, session
: requests
.Session
, timeout
: int) -> BeautifulSoup
:
45 with session
.get(url
, timeout
=timeout
) as r
:
47 return BeautifulSoup(r
.text
, 'html.parser')
51 args
= command_line_parser().parse_args()
52 with requests_cache
.CachedSession(args
.cache_path
, cache_control
=True) as session
:
53 html
= fetch(args
.url
, session
, args
.timeout
)
57 if __name__
== '__main__':