-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
58 lines (51 loc) · 1.83 KB
/
main.py
File metadata and controls
58 lines (51 loc) · 1.83 KB
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
import ssl
import certifi
from mvg import MvgApi
import aiohttp
# SSL Patch
ssl_context = ssl.create_default_context(cafile=certifi.where())
import mvg.mvgapi as mvgapi
original_get = aiohttp.ClientSession._request
def patched_request(self, method, url, **kwargs):
kwargs["ssl"] = ssl_context
return original_get(self, method, url, **kwargs)
aiohttp.ClientSession._request = patched_request
# ------------------------------
# Universelle MVG-API-Funktion
# ------------------------------
def mvg_api(stations, api_type="departures", combine_departures=False):
if isinstance(stations, str):
stations = [stations]
results = {}
combined = []
for name in stations:
# Suche Station und benutze globalId
station_data = MvgApi.station(name)
if not station_data:
if combine_departures:
continue
else:
results[name] = None
continue
# Manche Suchanfragen liefern mehrere Stationen, wir nehmen die erste
station_id = station_data.get("id") or station_data.get("globalId")
if not station_id:
continue
if api_type == "station_search":
results[name] = station_data
elif api_type == "departures":
mvg_instance = MvgApi(station_id)
departures = mvg_instance.departures()
if combine_departures:
for dep in departures:
dep["_station_name"] = station_data.get("name", name)
combined.append(dep)
else:
results[station_data.get("name", name)] = departures
else:
raise ValueError(f"Unbekannter api_type: {api_type}")
if combine_departures:
combined.sort(key=lambda x: x.get("planned", 0))
return combined
else:
return results