Beispiel #1
0
    parser.add_argument('--timetables', dest='timetables', action='store_true', help='also save timetables')
    parser.add_argument('--scheduled-stats', dest='scheduled_stats', action='store_true', help='also compute scheduled stats if the timetable has new dates (requires --timetables)')
    parser.set_defaults(s3=False)
    parser.set_defaults(timetables=False)
    parser.set_defaults(scheduled_stats=False)

    args = parser.parse_args()

    agencies = [config.get_agency(args.agency)] if args.agency is not None else config.agencies

    save_to_s3 = args.s3
    d = date.today()

    errors = []

    for agency in agencies:
        scraper = gtfs.GtfsScraper(agency)
        scraper.save_routes(save_to_s3, d)

        if args.timetables:
            timetables_updated = scraper.save_timetables(save_to_s3=save_to_s3, skip_existing=True)

            if timetables_updated and args.scheduled_stats:
                dates = sorted(scraper.get_services_by_date().keys())
                compute_stats_for_dates(dates, agency, scheduled=True, save_to_s3=save_to_s3)

        errors += scraper.errors

    if errors:
        raise Exception("\n".join(errors))
Beispiel #2
0
    parser.add_argument("--inpath",
                        required=True,
                        help="Path to directory containing GTFS data")
    parser.add_argument("--s3",
                        dest="s3",
                        action="store_true",
                        help="Option to upload files to the s3 bucket")
    parser.add_argument("--agency", help="Agency name - default is 'sf-muni'")
    parser.add_argument("--version", help="Version number for timetable")
    parser.set_defaults(s3=False)
    parser.set_defaults(agency="sf-muni")
    parser.set_defaults(version="v1")

    args = parser.parse_args()
    inpath = args.inpath
    outpath = util.get_data_dir()
    s3 = args.s3
    agency = args.agency
    version = args.version

    gtfs_scraper = gtfs.GtfsScraper(inpath, agency, version)

    start_time = datetime.now()
    print(f"Begin scraping GTFS data: {start_time}")

    gtfs_scraper.save_all_stops(s3)
    gtfs_scraper.save_date_ranges(s3, version)

    end_time = datetime.now()
    print(f"Finished scraping GTFS data: {end_time}")
    print(f"Elapsed time: {end_time - start_time}")
        with zipfile.ZipFile(zip_path, 'r') as zip_ref:
            zip_ref.extractall(gtfs_cache_dir)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Merge route information from Nextbus API and GTFS')
    parser.add_argument('--s3', dest='s3', action='store_true', help='store in s3')
    parser.set_defaults(s3=False)

    args = parser.parse_args()

    gtfs_cache_dir = f'{util.get_data_dir()}/gtfs-{agency_id}'

    download_gtfs_data(agency_id, gtfs_url, gtfs_cache_dir)

    gtfs_scraper = gtfs.GtfsScraper(gtfs_cache_dir, agency_id, 'v1')

    routes = nextbus.get_route_list(agency_id)

    routes_data = []

    for route in routes:

        route_id = route.id

        try:
            gtfs_route_id = gtfs_scraper.get_gtfs_route_id(route_id)
        except gtfs.NoRouteError as ex:
            print(ex)
            gtfs_route_id = None
    parser.add_argument('--scheduled',
                        dest='scheduled',
                        action='store_true',
                        help='compute scheduled stats from timetable')
    parser.set_defaults(s3=False)
    parser.set_defaults(scheduled=False)

    args = parser.parse_args()

    agencies = [config.get_agency(args.agency)
                ] if args.agency is not None else config.agencies

    if args.date:
        dates = util.get_dates_in_range(args.date, args.date)
    elif args.start_date is not None and args.end_date is not None:
        dates = util.get_dates_in_range(args.start_date, args.end_date)
    elif args.scheduled and len(agencies) == 1:
        # save all dates in current GTFS feed if no date range is provided
        scraper = gtfs.GtfsScraper(agencies[0])
        dates = sorted(scraper.get_services_by_date().keys())
    else:
        raise Exception('missing date, start-date, or end-date')

    scheduled = args.scheduled

    for agency in agencies:
        compute_stats_for_dates(dates,
                                agency,
                                scheduled=scheduled,
                                save_to_s3=args.s3)