extracted atb api query to function

This commit is contained in:
Bjornar Orjansen Kaarevik 2024-12-12 23:07:32 +01:00
parent a352264efe
commit 034e923bb7

View File

@ -13,6 +13,9 @@ STOP_PLACES = {
"hesthagen": "41620" "hesthagen": "41620"
} }
def get_departures(stop_place: str, atb_api_url: str = ATB_API_URL):
return requests.get(atb_api_url + stop_place).json()["departures"]
def format_departure(departure_dict: dict) -> str: def format_departure(departure_dict: dict) -> str:
"""Formats the json supplied by mpolden's atb api to a single line of text """Formats the json supplied by mpolden's atb api to a single line of text
quickly summarizing the scheduled bus. quickly summarizing the scheduled bus.
@ -39,14 +42,17 @@ def write_to_socket(s: str, mpv_socket) -> None:
if __name__ == "__main__": if __name__ == "__main__":
try:
input_stop_place = sys.argv[1] input_stop_place = sys.argv[1]
except:
input_stop_place = "no input was given"
if input_stop_place in STOP_PLACES.keys(): if input_stop_place in STOP_PLACES.keys():
active_stop_place = STOP_PLACES[input_stop_place] active_stop_place = STOP_PLACES[input_stop_place]
else: else:
active_stop_place = STOP_PLACES["gløshaugen"] active_stop_place = STOP_PLACES["gløshaugen"]
r = requests.get(ATB_API_URL + active_stop_place) departures = get_departures(active_stop_place)
departures = r.json()["departures"]
sorted_departures = sorted(departures, key = lambda d: d["scheduledDepartureTime"]) sorted_departures = sorted(departures, key = lambda d: d["scheduledDepartureTime"])
# ensure mpv is running with the ipc-server socket set in MPV_SOCKET # ensure mpv is running with the ipc-server socket set in MPV_SOCKET