This repository has been archived by the owner on Sep 28, 2021. It is now read-only.
/
api_wrapper.py
52 lines (43 loc) · 1.55 KB
/
api_wrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""api wrapper for the lower saxony vaccination portal"""
import logging
from requests.sessions import Session
from requests.exceptions import ConnectionError as RequestConnectionError
from common import sleep
log = logging.getLogger(__name__)
class ShadowBanException(Exception):
"""Exception for ip ban detection"""
def fetch_api(
zip_code: int,
birthdate_timestamp: int = None,
group_size: int = None,
with_vector: bool = True,
max_retries: int = 10,
sleep_after_error: int = 30,
jitter: int = 5,
user_agent: str = 'python'
) -> any:
"""fetches the api with ip ban avoidance"""
url = f"https://www.impfportal-niedersachsen.de/portal/rest/appointments/findVaccinationCenterListFree/{zip_code}?stiko="
if birthdate_timestamp:
url += f"&count=1&birthdate={int(birthdate_timestamp)*1000}"
elif group_size:
url += f"&count={group_size}"
if with_vector:
url += f"&showWithVectorVaccine=true"
fail_counter = 0
headers = {
'Accept': 'application/json',
'User-Agent': user_agent
}
for fail_counter in range(0, max_retries+1):
try:
session = Session()
with session.get(url=url, headers=headers, timeout=10) as data:
return data.json()["resultList"]
except RequestConnectionError as ex:
raise ex
except Exception as ex:
log.debug(f"Exeption during request {ex}")
sleep_time = sleep_after_error*fail_counter
sleep(sleep_time, jitter)
raise ShadowBanException