42 lines
1.3 KiB
Python
42 lines
1.3 KiB
Python
import json
|
|
import requests
|
|
|
|
|
|
class VagApi:
|
|
def __init__(self):
|
|
self.station_data = []
|
|
|
|
def load_station_data(self, filename):
|
|
with open(filename, 'r', encoding='utf-8') as f:
|
|
self.station_data = json.load(f)['Haltestellen']
|
|
|
|
def get_station_info(self, query):
|
|
matches = []
|
|
if type(query) is str:
|
|
query = query.lower()
|
|
str_query = True
|
|
else:
|
|
str_query = False
|
|
|
|
for station in self.station_data:
|
|
if str_query:
|
|
if query in station['Haltestellenname'].lower() or query in station['VAGKennung'].lower():
|
|
matches.append(station)
|
|
else:
|
|
if station['VGNKennung'] == query:
|
|
matches.append(station)
|
|
|
|
return matches
|
|
|
|
def get_stations(self, query = ""):
|
|
resp = requests.get("https://start.vag.de/dm/api/haltestellen.json/vgn", params={'name': query})
|
|
resp.raise_for_status()
|
|
data = resp.json()
|
|
return data
|
|
|
|
def get_departures(self, station, delay_min=0, products=['Ubahn', 'Tram', 'Bus']):
|
|
resp = requests.get(f"https://start.vag.de/dm/api/abfahrten.json/vgn/{station}", params={'timedelay': delay_min, 'product': ",".join(products)})
|
|
resp.raise_for_status()
|
|
data = resp.json()
|
|
return data['Abfahrten']
|