def __init__(self, db, key, svc=VEHICLES_URL, loc='Portland', format='json'): ''' call the car2go service, retrieve new positions, and update car position database ''' # step 0: setup self.house_num_and_zip_re = re.compile('[0-9]*, 9[0-9]+') # step 1: new car2go data self.pos = [] self.url = "{0}?oauth_consumer_key={1}&format={2}&loc={3}".format(svc, key, format, loc) print self.url raw = urllib.urlopen(self.url) car2go_data = json.load(raw) # step 2: if we have valid data, update the database if car2go_data and len(car2go_data['placemarks']) > 0: session = db.get_session() # step 2a: clear out the 'latest' flag from the position table (so we know the new service data is the 'latest' positions) Position.clear_latest_column(session, Car2GoVehicle.identity) # step 2b: update the positions for v in car2go_data['placemarks']: self.append_pos(session, v) continue # step 2c: commit the new positions session.flush() session.commit()
def __init__(self, db, key, svc=VEHICLES_URL, loc='Portland', format='json'): ''' call the car2go service, retrieve new positions, and update car position database ''' # step 0: setup self.house_num_and_zip_re = re.compile('[0-9]*, 9[0-9]+') # step 1: new car2go data self.pos = [] self.url = "{0}?oauth_consumer_key={1}&format={2}&loc={3}".format( svc, key, format, loc) print self.url raw = urllib.urlopen(self.url) car2go_data = json.load(raw) # step 2: if we have valid data, update the database if car2go_data and len(car2go_data['placemarks']) > 0: session = db.get_session() # step 2a: clear out the 'latest' flag from the position table (so we know the new service data is the 'latest' positions) Position.clear_latest_column(session, Car2GoVehicle.identity) # step 2b: update the positions for v in car2go_data['placemarks']: self.append_pos(session, v) continue # step 2c: commit the new positions session.flush() session.commit()
def update_zipcar_db(cls, db, pods, vehicles): """ NOTE: key parameter is being passed around, since that will eventually be needed for Zipcar """ session = None try: session = db.get_session() # step 1: remove old stuff.... zlist = session.query(ZipcarVehicle).all() # note: looping through and calling session.delete(z) is the only way I could get SQLAlchemy to delete the # FK relational entry to position table. for z in zlist: session.delete(z) session.query(ZipcarPod).delete() session.commit() session.flush() # step 2: add pods for p in pods: session.add(p) session.commit() session.flush() # step 3: add vehicles for v in vehicles: session.add(v) session.commit() session.flush() # step 4: add position data to each vehicle Position.clear_latest_column(session, ZipcarVehicle.identity) for v in vehicles: v.update_position(session, v.lat, v.lon, v.street, v.city, v.state, v.zip, 1) except Exception as err: log.exception('Exception: {0}'.format(err)) pass finally: if session: # step 3: commit stuff... session.commit() session.flush()
def update_zipcar_db(cls, db, pods, vehicles): ''' NOTE: key parameter is being passed around, since that will eventually be needed for Zipcar ''' session = None try: session = db.get_session() # step 1: remove old stuff.... zlist = session.query(ZipcarVehicle).all() # note: looping through and calling session.delete(z) is the only way I could get SQLAlchemy to delete the # FK relational entry to position table. for z in zlist: session.delete(z) session.query(ZipcarPod).delete() session.commit() session.flush() # step 2: add pods for p in pods: session.add(p) session.commit() session.flush() # step 3: add vehicles for v in vehicles: session.add(v) session.commit() session.flush() # step 4: add position data to each vehicle Position.clear_latest_column(session, ZipcarVehicle.identity) for v in vehicles: v.update_position(session, v.lat, v.lon, v.street, v.city, v.state, v.zip, 1) except Exception, err: log.exception('Exception: {0}'.format(err)) pass