Skip to content

Bug fix: German Transit #3023

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions apps/germantransit/german_transit.star
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ BERLIN_TIMEZONE = "Europe/Berlin"
#Departure Board API Tuning Parameters
MAX_DEPARTURES = 8 #maximum number of departures to fetch
MAX_MINUTES_IN_FUTURE = "59" #limit to departures in the next hour
DEPARTURES_TTL_CACHE_LENGTH_SECONDS = 60 #cache the departure board for one minute
ICON_TTL_CACHE_LENGTH_SECONDS = 14400 #cache the modality icon for 4 hours
DEPARTURES_TTL_CACHE_LENGTH_SECONDS = 300 #cache the departure board for 5 minutes
DEPARTURES_DEMO_CACHE_LENGTH_SECONDS = 86400 #cache the departure board for 1 day
ICON_TTL_CACHE_LENGTH_SECONDS = 604800 #cache the modality icon for one week
JSON_FORMAT = "json"

#Station Lookup API Tuning Parameters
Expand All @@ -97,11 +98,15 @@ def main(config):
#Parse the station data. The "value" field is a stringified JSON object holding the station-id and station-name
station = config.get(CONFIG_STATION)
if not station:
return get_error_message("No station selected")
#Print results for default station (Heidelberg Hbf) until user selects a station
station_id = "6001160"
product_list = parse_class_configs(True, True, True, True, True, True)
offset_minutes = int(0)
departures = get_station_departures(station_id, product_list, offset_minutes, demo_cache=True)
return get_root_element(departures)

data = json.decode(json.decode(station)[CONFIG_STATION_VALUE])
station_id = data[CONFIG_STATION_ID]
if not station_id:
return get_error_message("No station selected")

#Pull product class configurations from the schema
show_u_bahn = config.bool(CONFIG_SHOW_U_BAHN, True)
Expand All @@ -128,7 +133,7 @@ def main(config):
#departures: the list of departures to render
def get_root_element(departures):
return render.Root(
max_age = 120,
max_age = 30,
child = render.Column(
children = [
render_departures(departures),
Expand Down Expand Up @@ -290,7 +295,7 @@ def get_error_message(errorMessage):
#included_mots: the modes of transportation to be included in the request
#departure_offset_minutes: exclude departures leaving within the offset minutes parameter
#Returns a list of dictionaries, each representing a departure
def get_station_departures(station_id, included_mots, departure_offset_minutes):
def get_station_departures(station_id, included_mots, departure_offset_minutes, demo_cache=False):
params = {
"name_dm": station_id,
"limit": str(MAX_DEPARTURES),
Expand All @@ -314,6 +319,10 @@ def get_station_departures(station_id, included_mots, departure_offset_minutes):
if departure_offset_minutes > 0: #we only need to add the time parameter if we're not looking for immediate departures - it defautls to now
params["timeOffset"] = str(departure_offset_minutes)

if demo_cache == True:
resp = execute_http_get(departure_req, params, DEPARTURES_DEMO_CACHE_LENGTH_SECONDS)
return parse_departures_json(resp)

resp = execute_http_get(departure_req, params, DEPARTURES_TTL_CACHE_LENGTH_SECONDS)
return parse_departures_json(resp)

Expand Down Expand Up @@ -632,7 +641,7 @@ def fetch_stations(location):
truncated_lng = math.round(1000.0 * float(location["lng"])) / 1000.0 # Means to the nearest ~110 metres.
params = {
"type_sf": "coord",
"name_sf": "coord:" + str(truncated_lat) + ":" + str(truncated_lng) + ":WGS84[dd.ddddd]",
"name_sf": "coord:" + str(truncated_lng) + ":" + str(truncated_lat) + ":WGS84[dd.ddddd]",
"anyObjFilter_sf": "2", #limits this to just stations
"outputFormat": JSON_FORMAT,
"coordOutputFormat": "EPSG:4326",
Expand Down
Loading