-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_data.py
65 lines (45 loc) · 2.06 KB
/
server_data.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
53
54
55
56
57
58
59
60
61
62
63
64
65
from datetime import datetime
from typing import Dict
import pytz
import requests
import canteen_properties as props
from canteenday import CanteenDay
class ServerData:
def __init__(self, logger, user_name, api_password) -> None:
super().__init__()
# dict of canteen_key : timestamp : canteen day data
self._data = {}
self.logger = logger
self._api_username = user_name
self._api_password = api_password
def fetch_mensa_menu(self):
self.logger.info('refreshing data...')
supported_canteens = props.QUEUE_NAMES.keys()
tmp_canteen_data = {}
# reformat data to Dict[timestamp][canteen_key] containing canteen data json
for canteen_key in supported_canteens:
r = requests.get(f'https://www.sw-ka.de/json_interface/canteen/?mensa[]={canteen_key}',
auth=(self._api_username, self._api_password))
if r.status_code == 200:
result: dict = r.json()[canteen_key]
days_dict: Dict[str, CanteenDay] = {}
# iterate over the days
for date_unix, day_json in result.items():
# mensa uses (weird) local timestamp
timestamp = datetime.fromtimestamp(int(date_unix), pytz.timezone('Europe/Berlin'))
day = CanteenDay(canteen_key, day_json, timestamp)
days_dict[timestamp.strftime('%d.%m.%Y')] = day
tmp_canteen_data[canteen_key] = days_dict
else:
# no valid data
tmp_canteen_data[canteen_key] = None
self._data = tmp_canteen_data
def get_canteen(self, canteen_key) -> Dict:
# returns dict containing day objects of the specified canteen
return self._data[canteen_key]
def contains_timestamp(self, timestamp: datetime):
timestamp_str = timestamp.strftime('%d.%m.%Y')
for canteen_data in self._data.values():
if timestamp_str in canteen_data.keys():
return True
return False