]>
git.scottworley.com Git - paperdoorknob/blob - fetch.py
d5267534a100e1d0e527ef347547cb57b2ae6f31
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
)
46 class FakeFetcher(Fetcher
):
48 def __init__(self
, resources
: dict[str, bytes]) -> None:
49 self
._resources
= resources
52 def fetch(self
, url
: str) -> bytes:
53 self
._fetch
_count
+= 1
54 if url
not in self
._resources
:
55 raise requests
.HTTPError("URL not found")
56 return self
._resources
[url
]
58 def request_count(self
) -> int:
59 return self
._fetch
_count