import os import requests from pprint import pprint from flask import Flask, render_template import datetime API_KEY = os.environ['API_KEY'] API_URL = "https://api.golemio.cz/v2/pid/departureboards" STOPS = ["U632Z3P", "U436Z1P", "U127Z2P"] #STOPS = ["U632Z3P"] CAR_AMOUNT = 10 app = Flask(__name__) def get_departures(api, key, stop): headers = {"accept": "application/json", "X-Access-Token": key} params = {"ids": stop, "limit": CAR_AMOUNT} r = requests.get(api, params=params, headers=headers) return r.json() def get_cars(stops): cars = [] for stop in stops: deps = get_departures(API_URL, API_KEY, stop) stop_name = deps["stops"][0]["stop_name"] stop_platform = deps["stops"][0]["platform_code"] stop_display = stop_name #+ " (" + stop_platform + ")" stop_cars = [] for car in deps["departures"]: # print(car) car_id = car["route"]["short_name"] car_dir = car["trip"]["headsign"] car_ac = car["trip"]["is_air_conditioned"] car_departure = car["departure_timestamp"]["minutes"] if car["delay"]["is_available"]: # car_ahead = "+ " car_delay_sec = (car["delay"]["minutes"] * 60) + car["delay"]["seconds"] # if car_delay_sec < 0: # car_ahead = "- " # elif car_delay_sec == 0: # car_ahead = "" # m, s = divmod(car_delay_sec, 60) # car_delay = car_ahead + str(m) + "min, " + str(s) + "sec" car_delay = car_delay_sec else: car_delay = "?" car_data = { "id": car_id, "dir": car_dir, "ac": car_ac, "departure": car_departure, "delay": car_delay, } stop_cars.append(car_data) cars.append({"id": stop, "name": stop_display, "cars": stop_cars}) return cars @app.route("/") def hello_world(): return render_template('index.html', data=get_cars(STOPS)) @app.route('/flask-health-check') def flask_health_check(): return "success"