def compare_routes(): threshold = 1320 # 0.25 mile in feet cta_client = CTABustracker() predictions = cta_client.get_predictions_for_stops(route_id=request.args.get('route'), stp_id=request.args.get('current_stop_id')) closest = min(predictions, key=lambda x: abs(x.distance_to_stop - threshold)) return json.dumps(closest, cls=MyEncoder)
#!/usr/bin/env python import csv import sys import logging logger = logging.getLogger() handler = logging.StreamHandler() formatter = logging.Formatter( '%(asctime)s %(name)-12s %(levelname)-8s %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) logger.setLevel(logging.DEBUG) sys.path.append('/Users/lorenamesa/Desktop/pyten/lib/python2.7/site-packages/') from api.cta_api import CTABustracker bus_tracker = CTABustracker("T6ketWk5cWetPgYVqkvEVJVng") bulletins = bus_tracker.get_bulletins(stp_id="890,944,3183,3006") with open("/Users/lorenamesa/Desktop/pytennessee/bus_service_bulletins.csv", "ab") as csvdata: if bulletins: logging.info("Writing service bulletins for bus data...") wr = csv.writer(csvdata, dialect='excel') for bulletin in bulletins: wr.writerow(bulletin.__dict__.values())
import sys import logging logger = logging.getLogger() handler = logging.StreamHandler() formatter = logging.Formatter( '%(asctime)s %(name)-12s %(levelname)-8s %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) logger.setLevel(logging.DEBUG) sys.path.append('/Users/lorenamesa/Desktop/pyten/lib/python2.7/site-packages/') from api.cta_api import CTABustracker, CTATraintracker bus_tracker = CTABustracker() predictions = bus_tracker.get_predictions_for_stops(route_id=72, stp_id="890,944") cali_predictions = bus_tracker.get_predictions_for_stops(route_id=52, stp_id="3183,3006") with open("/Users/lorenamesa/Desktop/pytennessee/bus_data.csv", "ab") as csvdata: headers = ['distance_to_stop', 'route', 'vehicle_id', 'arrival_time', 'requested_time', 'route_direction', 'stop_name'] if predictions: logging.info("Writing data for bus data...") wr = csv.writer(csvdata, dialect='excel') for prediction in predictions: wr.writerow(prediction.__dict__.values()) for cali_p in cali_predictions: wr.writerow(cali_p.__dict__.values()) # ['distance_to_stop', 'route', 'vehicle_id', 'arrival_time', 'requested_time', 'route_direction', 'stop_name'] train_tracker = CTATraintracker()