def process_prediction(prediction_data): # logging.warning("===========PROCESS PRED METHOD CALLED=============") cur = get_cur() predictions = [] if prediction_data == None: # logging.warning("===========PREDICTION DATA IS NONE=============") pass else: if type(prediction_data == list): try: for prediction in prediction_data: # print("type of prediction list...", type(prediction)) # logging.warning("===========ADDING TO PREDICTION LIST THE FOLLOWING=============") logging.warning(prediction) predictions.append(Prediction(prediction)) except: pass # logging.warning("===========TIME TO MAKE SOME SQLLLLS=============") for prediction in predictions: predicted_departure_dt = datetime.strptime( prediction.predicted_departure, "%Y-%m-%dT%H:%M:%S") prediction_datetime_dt = datetime.strptime( prediction.prediction_datetime, "%Y-%m-%dT%H:%M:%S") delta = predicted_departure_dt - prediction_datetime_dt print("here is the time delta.... ", delta) if (delta.seconds > 600): print("Delta too much..... ", delta.seconds > 600) continue sql_string = """ INSERT INTO predictions VALUES ('{stop_id}', '{trip_id}', '{vehicle_id}', '{route_name}', '{predicted_delay}', '{predicted_departure}', '{prediction_datetime}'); """.format(stop_id=prediction.stop_id, trip_id=prediction.trip_id, vehicle_id=prediction.vehicle_id, route_name=prediction.route_name, predicted_delay=prediction.predicted_delay, predicted_departure=prediction.predicted_departure, prediction_datetime=prediction.prediction_datetime) logging.warning("=========SEEE SQL STRING===========") logging.warning(sql_string) cur.execute(sql_string)
def process_prediction(prediction_data): cur = get_cur() predictions = [] if prediction_data == None: pass else: if type(prediction_data == list): try: for prediction in prediction_data: # print("type of prediction list...", type(prediction)) predictions.append(Prediction(prediction)) except: pass bulk_template = "INSERT INTO predictions (stop_id, trip_id, vehicle_id, route_name, predicted_delay, predicted_departure, prediction_datetime ) VALUES" print("OG bs len", len(bulk_template)) print("HOW MANYT PREDS???", len(predictions)) for prediction in predictions: predicted_departure_dt = datetime.strptime( prediction.predicted_departure, "%Y-%m-%dT%H:%M:%S") prediction_datetime_dt = datetime.strptime( prediction.prediction_datetime, "%Y-%m-%dT%H:%M:%S") delta = predicted_departure_dt - prediction_datetime_dt print("here is the time delta.... ", delta) if (delta.seconds > 600): print("Delta too much..... ", delta.seconds > 600) continue sql_string = """ INSERT INTO predictions VALUES ('{stop_id}', '{trip_id}', '{vehicle_id}', '{route_name}', '{predicted_delay}', '{predicted_departure}', '{prediction_datetime}'); """.format(stop_id=prediction.stop_id, trip_id=prediction.trip_id, vehicle_id=prediction.vehicle_id, route_name=prediction.route_name, predicted_delay=prediction.predicted_delay, predicted_departure=prediction.predicted_departure, prediction_datetime=prediction.prediction_datetime) print(sql_string) cur.execute(sql_string) print("prediction inserted??")
def process_prediction(prediction_data): logging.warning("=======ARE YOU EVEN WORKING??=====") cur = get_cur() predictions = [] if prediction_data == None: pass else: if type(prediction_data == list): try: for prediction in prediction_data: # # print("type of prediction list...", type(prediction)) predictions.append(Prediction(prediction)) except: pass for prediction in predictions: predicted_departure_dt = datetime.strptime( prediction.predicted_departure, "%Y-%m-%dT%H:%M:%S") prediction_datetime_dt = datetime.strptime( prediction.prediction_datetime, "%Y-%m-%dT%H:%M:%S") delta = predicted_departure_dt - prediction_datetime_dt if (delta.seconds > 600): continue sql_string = """ INSERT INTO predictions VALUES ('{stop_id}', '{trip_id}', '{vehicle_id}', '{route_name}', '{predicted_delay}', '{predicted_departure}', '{prediction_datetime}'); """.format(stop_id=prediction.stop_id, trip_id=prediction.trip_id, vehicle_id=prediction.vehicle_id, route_name=prediction.route_name, predicted_delay=prediction.predicted_delay, predicted_departure=prediction.predicted_departure, prediction_datetime=prediction.prediction_datetime) cur.execute(sql_string)
import requests import psycopg2 from google.transit import gtfs_realtime_pb2 from database import get_cur import csv cur = get_cur() cur.execute("SELECT * FROM stops") stops = cur.fetchall() # with open('stops.csv', "w") as file: # writer = csv.writer(file, delimiter=',') # for stop in stops: # print(stop) # writer.writerow(stop) with open('stops.csv', "r") as file: reader = csv.reader(file, delimiter=',') for row in reader: print(row) break print("donzo!")
def loop(): try: truth = True while (truth): cur = get_cur() base_url = "https://api.actransit.org/transit/gtfsrt/vehicles/?token=369BB8F6542E51FF57BC06577AFE829C" # d = datetime.date.today() # print("today",d) feed = gtfs_realtime_pb2.FeedMessage() response = requests.get(base_url) feed.ParseFromString(response.content) print("HOW LONG IS THIS FEED... ", len(feed.entity)) for entity in feed.entity: try: # print("I AM ENTITyYYY.... ",entity) id=entity.vehicle.id print("ID... ", id) lon=entity.vehicle.position.longitude, print("LON.... ", lon) lat=entity.vehicle.position.latitude, print("LAT.... ", lat) try: speed=entity.vehicle.position.speed, except: speed=None timestamp=entity.vehicle.timestamp print("TIMESTAMP... ", timestamp) vehicle_id=entity.vehicle.vehicle.id print("VEHICLE ID... ", vehicle_id) except: print("PROBLEM WIth ACCEESSING JSON OBJECT") ts = time() # string_ts = datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') # string_feed_ts = datetime.fromtimestamp(timestamp[0]).strftime('%Y-%m-%d %H:%M:%S') string_ts = datetime.fromtimestamp(ts) string_feed_ts = datetime.fromtimestamp(timestamp[0]) delta = string_ts - string_feed_ts if delta.days > 0: print("bad data", delta) continue print("MADE IT TO THE STRING") sql_string = """ INSERT INTO vehicles VALUES('{id}', '{trip_id}', '{route_id}', ST_GeomFromText('POINT({lon} {lat})', 4326), '{speed}', '{timestamp}', {vehicle_id}); """.format( id=id, trip_id=trip_id[0], route_id=route_id[0], lon=lon[0], lat=lat[0], speed=speed[0], timestamp=datetime.fromtimestamp(timestamp[0]), vehicle_id=vehicle_id ) print("HAIII") print(sql_string) PRINT("TRIP ID... ", trip_id[0]) try: cur.execute(sql_string) except: print("CANNOT INSERT INTO DATABASE") sleep(30) except: print("CALLED GENERAL EXCEPTION") sleep(10) loop()