def order(): auth_flow = AuthorizationCodeGrant( creds.lyft_client_id, creds.lyft_client_secret, "public", ) auth_url = auth_flow.get_authorization_url() my_redirect_url = input(print("Redirected URL: ")).strip() session = auth_flow.get_session(my_redirect_url) client = LyftRidesClient(session) x = len(client.get_pickup_time_estimates(lyft_start_latitude, lyft_start_longitude).json['eta_estimates']) y = 0 lyft_eta_seconds = [] while y < x: lyft_eta_seconds.append(client.get_pickup_time_estimates(lyft_start_latitude, lyft_start_longitude).json['eta_estimates'][y]['eta_seconds']) y += 1 eta_seconds = int(min(lyft_eta_seconds)) response = client.request_ride( ride_type='lyft', start_latitude=lyft_start_latitude, start_longitude=lyft_start_longitude, end_latitude=lyft_end_latitude, end_longitude=-lyft_end_longitude, )
def order(): if request.method == "POST": mymap = Map( identifier="mymap", lat=37.4419, lng=-122.1419, markers=[{ 'icon': 'http://maps.google.com/mapfiles/ms/icons/green-dot.png', 'lat': 37.4419, 'lng': -122.1419, 'infobox': "Your Location" }, { 'icon': 'http://maps.google.com/mapfiles/ms/icons/blue-dot.png', 'lat': nearest_hospital()[0], 'lng': nearest_hospital()[1], 'infobox': "Nearest Hospital" }]) lyft_start_latitude = locator.locate_me()[0] lyft_start_longitude = locator.locate_me()[1] lyft_end_latitude = nearest_hospital()[0] lyft_end_longitude = nearest_hospital()[1] auth_flow = ClientCredentialGrant(creds.lyft_client_id, creds.lyft_client_secret, "public") session = auth_flow.get_session() client = LyftRidesClient(session) x = len( client.get_pickup_time_estimates( lyft_start_latitude, lyft_start_longitude).json['eta_estimates']) lyft_eta_seconds = [] myauth_flow = AuthorizationCodeGrant( creds.lyft_client_id, creds.lyft_client_secret, "public", ) auth_url = myauth_flow.get_authorization_url() y = 0 while y < x: lyft_eta_seconds.append( client.get_pickup_time_estimates( lyft_start_latitude, lyft_start_longitude).json['eta_estimates'][y] ['eta_seconds']) y += 1 eta_seconds = int(min(lyft_eta_seconds)) return render_template('order.html', eta_seconds=eta_seconds, auth_url=auth_url, yourmap=yourmap) return render_template('order.html', eta_seconds=eta_seconds, auth_url=auth_url, yourmap=yourmap)
def __real_update(self): from lyft_rides.client import LyftRidesClient client = LyftRidesClient(self._session) self.products = {} products_response = client.get_ride_types(self.start_latitude, self.start_longitude) products = products_response.json.get('ride_types') for product in products: self.products[product['ride_type']] = product if self.end_latitude is not None and self.end_longitude is not None: price_response = client.get_cost_estimates(self.start_latitude, self.start_longitude, self.end_latitude, self.end_longitude) prices = price_response.json.get('cost_estimates', []) for price in prices: product = self.products[price['ride_type']] if price.get("is_valid_estimate"): product['estimate'] = price eta_response = client.get_pickup_time_estimates( self.start_latitude, self.start_longitude) etas = eta_response.json.get('eta_estimates') for eta in etas: if eta.get("is_valid_estimate"): self.products[eta['ride_type']]['eta'] = eta
def get_lyft_pickup_time(start_latitude, start_longitude, ride_type=None): client = LyftRidesClient(session) response = client.get_pickup_time_estimates( start_latitude, start_longitude, ride_type ) if response.status_code == 200: eta_estimates = response.json.get('eta_estimates') etas = {} for eta in eta_estimates: ride_type = eta['display_name'] eta_seconds = eta['eta_seconds'] valid_estimate = eta['is_valid_estimate'] if valid_estimate and eta_seconds is not None: etas[ride_type] = eta_seconds if 'Lyft' in etas.keys(): return ['Lyft', etas['Lyft']] elif len(etas.keys()) > 0: fastest_ride_option = min(etas, key=etas.get) return [fastest_ride_option, etas[fastest_ride_option]] else: return ['No rides available', float('inf')] else: print(response.status_code) print(response.json.get('error')) print(response.json.get('error_description'))
def fetch_data(self): """Get the latest product info and estimates from the Lyft API.""" from lyft_rides.client import LyftRidesClient client = LyftRidesClient(self._session) self.products = {} products_response = client.get_ride_types( self.start_latitude, self.start_longitude) products = products_response.json.get('ride_types') for product in products: self.products[product['ride_type']] = product if self.end_latitude is not None and self.end_longitude is not None: price_response = client.get_cost_estimates( self.start_latitude, self.start_longitude, self.end_latitude, self.end_longitude) prices = price_response.json.get('cost_estimates', []) for price in prices: product = self.products[price['ride_type']] if price.get("is_valid_estimate"): product['estimate'] = price eta_response = client.get_pickup_time_estimates( self.start_latitude, self.start_longitude) etas = eta_response.json.get('eta_estimates') for eta in etas: if eta.get("is_valid_estimate"): self.products[eta['ride_type']]['eta'] = eta
def getData(nhoods, client_id, client_secret, username, pswd): auth_flow = ClientCredentialGrant(client_id=client_id, client_secret=client_secret, scopes='public') session = auth_flow.get_session() client = LyftRidesClient(session) con = psycopg2.connect( "dbname='Pulse' user='******' host='localhost' password='******'" % (username, pswd)) cur = con.cursor() for row in range(len(nhoods.index)): lat = nhoods.iloc[row]['lat'] lon = nhoods.iloc[row]['lon'] response = client.get_cost_estimates(lat, lon, 37.468051, -122.447088, ride_type='lyft') costs = response.json.get('cost_estimates') geoID = nhoods.iloc[row]['geoid'] low = costs[0]['estimated_cost_cents_min'] high = costs[0]['estimated_cost_cents_max'] percent = costs[0]['primetime_percentage'] perc = percent.replace('%', '') outperc = int(perc) / 100 + 1 response = client.get_pickup_time_estimates(lat, lon, ride_type='lyft') timeEstimate = response.json.get('eta_estimates') etaSeconds = timeEstimate[0]['eta_seconds'] ts = time.time() timeStamp = datetime.datetime.fromtimestamp(ts).strftime( '%Y-%m-%d %H:%M:%S') query = "INSERT INTO lyft_sf (geoid, time, outperc, low, high, etaseconds) VALUES (%s, %s, %s, %s,%s,%s);" data = (geoID, timeStamp, outperc, low, high, etaSeconds) cur.execute(query, data) con.commit() print('Wrote data') con.close()
def fetch_data(self): """Get the latest product info and estimates from the Lyft API.""" from lyft_rides.client import LyftRidesClient client = LyftRidesClient(self._session) self.products = {} products_response = client.get_ride_types( self.start_latitude, self.start_longitude ) products = products_response.json.get("ride_types") for product in products: self.products[product["ride_type"]] = product if self.end_latitude is not None and self.end_longitude is not None: price_response = client.get_cost_estimates( self.start_latitude, self.start_longitude, self.end_latitude, self.end_longitude, ) prices = price_response.json.get("cost_estimates", []) for price in prices: product = self.products[price["ride_type"]] if price.get("is_valid_estimate"): product["estimate"] = price eta_response = client.get_pickup_time_estimates( self.start_latitude, self.start_longitude ) etas = eta_response.json.get("eta_estimates") for eta in etas: if eta.get("is_valid_estimate"): self.products[eta["ride_type"]]["eta"] = eta