Create allow-list for routes at stops

This commit is contained in:
Nahuel Lofeudo 2023-05-20 17:28:39 +01:00
parent 29cdb35f2e
commit 9b43a973ee
4 changed files with 50 additions and 11 deletions

View File

@ -33,3 +33,13 @@ class Config:
def minutes_to_stop(self, stop_id) -> int: def minutes_to_stop(self, stop_id) -> int:
return self.__walk_time_by_stop.get(stop_id, 0) return self.__walk_time_by_stop.get(stop_id, 0)
def routes_for_stops(self) -> map:
result = {}
for s in self.__config.get("stops"):
for r in s.get("routes", []):
routes = (result.get(s.get("stop_id")) or [])
routes.append(r)
result[s.get("stop_id")] = routes
return result

View File

@ -16,12 +16,20 @@ stops: [
{ {
# Route 15A # Route 15A
stop_id: 1114, stop_id: 1114,
walk_time: 15 walk_time: 15,
routes: ["15A"]
}, },
{ {
# route 54A # route 54A
stop_id: 2410, stop_id: 2410,
walk_time: 9 walk_time: 9,
routes: ["54A"]
},
{
# route 9
stop_id: 2438,
walk_time: 15,
routes: ["9"]
} }
] ]

View File

@ -15,8 +15,12 @@ import zipfile
class GTFSClient(): class GTFSClient():
def __init__(self, feed_url: str, gtfs_r_url: str, gtfs_r_api_key: str, def __init__(self, feed_url: str, gtfs_r_url: str, gtfs_r_api_key: str,
stop_codes: list[str], update_queue: queue.Queue, update_interval_seconds: int = 60): stop_codes: list[str], routes_for_stops: dict[str, str],
update_queue: queue.Queue, update_interval_seconds: int = 60):
self.stop_codes = stop_codes self.stop_codes = stop_codes
self.routes_for_stops = routes_for_stops
feed_name = feed_url.split('/')[-1] feed_name = feed_url.split('/')[-1]
self.gtfs_r_url = gtfs_r_url self.gtfs_r_url = gtfs_r_url
self.gtfs_r_api_key = gtfs_r_api_key self.gtfs_r_api_key = gtfs_r_api_key
@ -28,10 +32,6 @@ class GTFSClient():
last_mtime = 0 last_mtime = 0
refreshed, new_mtime = refresh_feed.update_local_file_from_url_v1(last_mtime, feed_name, feed_url) refreshed, new_mtime = refresh_feed.update_local_file_from_url_v1(last_mtime, feed_name, feed_url)
if refreshed:
print("The feed file was refreshed.")
else:
print("The feed file was up to date")
# Load the feed # Load the feed
self.feed = self._read_feed(feed_name, dist_units='km', stop_codes = stop_codes) self.feed = self._read_feed(feed_name, dist_units='km', stop_codes = stop_codes)
@ -197,6 +197,25 @@ class GTFSClient():
return joined_data return joined_data
def __filter_routes_by_stops(self, next_buses: pd.core.frame.DataFrame) -> pd.core.frame.DataFrame:
"""
Takes a dataframe of a set of bus arrivals and only shows the routes we are interested in
for the given stops (this is to eliminate routes that stop in more than one of our stops)
"""
ids_to_delete = []
for index, next_bus in next_buses.iterrows():
stop_number = next_bus["stop_code"]
route = next_bus["route_short_name"]
routes_for_stop = self.routes_for_stops.get(int(stop_number), [])
if len(routes_for_stop) > 0 and not route in routes_for_stop:
# we should not show this entry. Note the ID
ids_to_delete.append(index)
next_buses.drop(index=ids_to_delete, inplace=True)
return next_buses
def __time_to_seconds(self, s: str) -> int: def __time_to_seconds(self, s: str) -> int:
sx = s.split(":") sx = s.split(":")
if len(sx) != 3: if len(sx) != 3:
@ -315,6 +334,7 @@ class GTFSClient():
trip_ids = self.__trip_ids_for_service_ids(service_ids) trip_ids = self.__trip_ids_for_service_ids(service_ids)
next_buses = self.__next_n_buses(trip_ids, num_entries) next_buses = self.__next_n_buses(trip_ids, num_entries)
joined_data = self.__join_data(next_buses) joined_data = self.__join_data(next_buses)
self.__filter_routes_by_stops(joined_data)
return joined_data return joined_data
@ -338,7 +358,7 @@ class GTFSClient():
arrivals = [] arrivals = []
# take more entries than we need in case there are cancelations # take more entries than we need in case there are cancelations
buses = self.get_next_n_buses(10) buses = self.get_next_n_buses(15)
for index, bus in buses.iterrows(): for index, bus in buses.iterrows():
if not bus["trip_id"] in self.canceled_trips: if not bus["trip_id"] in self.canceled_trips:

View File

@ -93,9 +93,9 @@ def update_screen(config: Config(), updates: list[ArrivalTime]) -> None:
text_color = COLOR_LCD_GREEN if update.is_added else COLOR_LCD_AMBER text_color = COLOR_LCD_GREEN if update.is_added else COLOR_LCD_AMBER
) )
# Add the current time to the bottom line # Add the current time to the bottom line
datetime_text = "Current time: " + datetime.today().strftime("%d/%m/%Y %H:%M") datetime_text = "Current time: " + datetime.today().strftime("%d/%m/%Y %H:%M")
write_line(5, datetime_text) write_line(5, datetime_text)
def clear_screen() -> None: def clear_screen() -> None:
""" Clear screen """ """ Clear screen """
@ -136,6 +136,7 @@ def main():
gtfs_r_url=config.gtfs_api_url, gtfs_r_url=config.gtfs_api_url,
gtfs_r_api_key=config.gtfs_api_key, gtfs_r_api_key=config.gtfs_api_key,
stop_codes=config.stop_codes, stop_codes=config.stop_codes,
routes_for_stops=config.routes_for_stops(),
update_queue=update_queue, update_queue=update_queue,
update_interval_seconds=config.update_interval_seconds) update_interval_seconds=config.update_interval_seconds)