]>
git.scottworley.com Git - paperdoorknob/blob - fetch.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 abc
import ABC
, abstractmethod
9 from contextlib
import contextmanager
10 from typing
import Iterator
18 def fetch(self
, url
: str) -> bytes:
19 raise NotImplementedError()
22 class _SessionFetcher(Fetcher
):
24 def __init__(self
, session
: requests
.Session
, timeout
: int) -> None:
25 self
._session
= session
26 self
._timeout
= timeout
28 def fetch(self
, url
: str) -> bytes:
29 with self
._session
.get(url
, timeout
=self
._timeout
) as r
:
35 def DirectFetcher(timeout
: int) -> Iterator
[_SessionFetcher
]:
36 with requests
.session() as session
:
37 yield _SessionFetcher(session
, timeout
)
41 def CachingFetcher(cache_path
: str, timeout
: int) -> Iterator
[_SessionFetcher
]:
42 with requests_cache
.CachedSession(cache_path
, cache_control
=True) as session
:
43 yield _SessionFetcher(session
, timeout
)