def get_service_level_entries_and_line_ids( feeds_and_service_levels: List[Tuple[GtfsFeed, Dict]]): entries = [] all_line_ids = set() for feed, service_levels in feeds_and_service_levels: for line_id, line_entry in service_levels.items(): all_line_ids.add(line_id) service_level_history = line_entry["history"] line_long_name = line_entry["longName"] line_short_name = line_entry["shortName"] route_ids = line_entry["routeIds"] exception_dates = list( map(date_from_string, line_entry["exceptionDates"])) for service_levels in service_level_history: entry = ServiceLevelsEntry( line_id=line_id, line_short_name=line_short_name, line_long_name=line_long_name, route_ids=route_ids, service_levels=service_levels["serviceLevels"], start_date=date_from_string(service_levels["startDate"]), end_date=date_from_string(service_levels["endDate"]), exception_dates=exception_dates, feed=feed, ) entries.append(entry) return bucket_by(entries, lambda e: e.line_id), all_line_ids
def link_services(calendar_dicts, calendar_attribute_dicts, calendar_date_dicts): services = [] calendar_attributes_by_id = index_by(calendar_attribute_dicts, "service_id") for calendar_dict in calendar_dicts: service_id = calendar_dict["service_id"] attribute_dict = calendar_attributes_by_id[service_id] services.append( Service( id=service_id, days=[ day for day in DAYS_OF_WEEK if calendar_dict[day] == "1" ], description=attribute_dict["service_description"], schedule_name=attribute_dict["service_schedule_name"], schedule_type=attribute_dict["service_schedule_type"], schedule_typicality=int( attribute_dict["service_schedule_typicality"]), start_date=date_from_string(calendar_dict["start_date"]), end_date=date_from_string(calendar_dict["end_date"]), exception_dates=get_service_exception_dates_for_service_id( service_id, calendar_date_dicts, ), )) return index_by(services, lambda s: s.id)
def load_feeds_from_archive(load_start_date: date): feeds = [] req = requests.get(GTFS_ARCHIVE_URL) lines = req.text.splitlines() reader = DictReader(lines, delimiter=",") for entry in reader: start_date = date_from_string(entry["feed_start_date"]) end_date = date_from_string(entry["feed_end_date"]) version = entry["feed_version"] url = entry["archive_url"] if start_date < load_start_date: continue gtfs_feed = GtfsFeed(start_date=start_date, end_date=end_date, version=version, url=url) feeds.append(gtfs_feed) return feeds
def get_service_exception_dates_for_service_id(service_id, calendar_date_dicts): dates = [] for date_dict in calendar_date_dicts: if date_dict["service_id"] == service_id: exception_date = date_from_string(date_dict["date"]) exception_type = date_dict["exception_type"] service_exception_date = ServiceExceptionDate( date=exception_date, service_id=service_id, exception_type=exception_type, ) dates.append(service_exception_date) return dates
def get_weekday_service_levels_history( feeds_and_service_levels: List[Tuple[GtfsFeed, Dict]]): histories = {} for (feed, service_levels) in feeds_and_service_levels: for line_id, service_dates in service_levels.items(): history = histories.setdefault(line_id, []) weekday_service = service_dates["Weekday"] if len(weekday_service) > 0: for service_levels in weekday_service: service_start_date = date_from_string( service_levels["startDate"]) trip_count = sum(service_levels["serviceLevels"]) history.append({ "date": service_start_date.strftime("%Y-%m-%d"), "trips": trip_count, }) else: history.append({ "date": feed.start_date.strftime("%Y-%m-%d"), "trips": 0 }) return histories