def request_uber_ride(start_lat, start_lng, end_lat, end_lng, uber_auth_flow, code, state): """Send a ride request on behalf of a user.""" uber_auth_url = 'http://0.0.0.0:5000/callback?code=%s&state=%s' # Instantiate new session & client object and retrieve credentials uber_session = uber_auth_flow.get_session(uber_auth_url % (code, state)) uber_client = UberRidesClient(uber_session, sandbox_mode=True) credentials = uber_session.oauth2credential access_token = credentials.access_token response = uber_client.request_ride( start_latitude=start_lat, start_longitude=start_lng, end_latitude=end_lat, end_longitude=end_lng ) ride_details = response.json ride_id = ride_details.get('request_id') # storing ride_id and access_token in visit record to retrieve ride status visit = Visit.query.filter(Visit.user_id==session['user_id']).order_by('visited_at desc').first() visit.ride_id = ride_id visit.uber_access_token = access_token db.session.commit()
def request_uber(): """Make sandbox Uber ride request""" search = Search.query.order_by(Search.date.desc()).first() search.uber_request = True db.session.commit() credentials2 = import_oauth2_credentials() oauth2credential = OAuth2Credential( client_id=credentials2.get('client_id'), access_token=sesh.get('access_token'), expires_in_seconds=credentials2.get('expires_in_seconds'), scopes=credentials2.get('scopes'), grant_type=credentials2.get('grant_type'), redirect_url=credentials2.get('redirect_url'), client_secret=credentials2.get('client_secret'), refresh_token=credentials2.get('refresh_token'), ) uber_session = Session(oauth2credential=oauth2credential) uber_client = UberRidesClient(uber_session, sandbox_mode=True) # receive data from Ajax call start_lat = request.form.get('start_lat') start_lng = request.form.get('start_lng') end_lat = request.form.get('end_lat') end_lng = request.form.get('end_lng') response = uber_client.get_products(37.3688301, -122.0363495) products = response.json.get('products') product_id = products[0].get('product_id') # make sandbox calls ride_request = uber_client.request_ride(product_id=product_id, start_latitude=37.3688301, start_longitude=-122.0363495, end_latitude=37.8003415, end_longitude=-122.4331332) ride_details = ride_request.json ride_id = ride_details.get('request_id') get_ride = uber_client.update_sandbox_ride(ride_id, 'accepted') send_uber_text(); return jsonify(ride_details)
def get(self, company, ride_type): #if company.lower() == 'lyft': if company.lower() == 'uber': session = auth_flow.get_session(redirect_url) auth_uber_client = UberRidesClient(session) credentials = session.oauth2credential product_id = '' if ride_type == 'private': product_id = uber_private_id else: product_id = uber_shared_id estimate = auth_uber_client.estimate_ride( product_id=product_id, start_latitude=last_request_coords[0]['lat'], start_longitude=last_request_coords[0]['lng'], end_latitude=last_request_coords[1]['lat'], end_longitude=-last_request_coords[1]['lng'], seat_count=2 ) fare = estimate.json.get('fare') print(fare['fare_id']) response = auth_uber_client.request_ride( product_id=product_id, start_latitude=last_request_coords[0]['lat'], start_longitude=last_request_coords[0]['lng'], end_latitude=last_request_coords[1]['lat'], end_longitude=-last_request_coords[1]['lng'], seat_count=2, fare_id=fare['fare_id'], state='' ) return response
def call_uber(flight_number, flight_origin_date): coords = get_coords(flight_number, flight_origin_date) server_token = "GSYPRMkSl_a7qQn8FH6d4imBjBnvrTWhh-6OzVPX" session = Session(server_token) client = UberRidesClient(session) response = client.get_products(coords[0], coords[1]) products = response.json.get('products') auth_flow = AuthorizationCodeGrant( "gT2GLeVlXMQkrWWBO872bcjHK168Tr8W", None, "fQMuhWzwuvMiy2yl31qDu4xIRMP0DIVQQUtJy3hj", None, ) auth_url = auth_flow.get_authorization_url() session = auth_flow.get_session() client = UberRidesClient(session, sandbox_mode=True) credentials = session.oauth2credential get_coords() response = client.request_ride( start_latitude=coords[0], start_longitude=coords[1], ) ride_details = response.json ride_id = ride_details.get('request_id')
def requests2(): from uber_rides.client import UberRidesClient from uber_rides.session import Session sessionUberRides = Session(server_token=config.get('server_token')) client = UberRidesClient(sessionUberRides, sandbox_mode=True) print sessionUberRides response = client.get_products(config.get('start_latitude'), config.get('start_longitude')) products = response.json.get('products') product_id = products[0].get('product_id') print product_id print response.status_code client = UberRidesClient(generate_oauth_service(), sandbox_mode=True) responseUber = client.request_ride( product_id=product_id, start_latitude=config.get('start_latitude'), start_longitude=config.get('start_longitude'), end_latitude=config.get('end_latitude'), end_longitude=config.get('end_longitude'), ) print responseUber ride_details = responseUber.json print ride_details ride_id = ride_details.get('request_id') responseUber = client.update_sandbox_ride(ride_id, 'accepted') # url = config.get('base_uber_url_SANDBOX') + 'requests' # params = { # 'start_latitude': config.get('start_latitude'), # 'start_longitude': config.get('start_longitude'), # 'end_latitude': config.get('end_latitude'), # 'end_longitude': config.get('end_longitude'), # } # # # print url, generate_ride_headers(session.get('access_token')) # response = app.requests_session.post( # url, # headers=generate_ride_headers(session.get('access_token')), # data=params # ) # # # print response if response.status_code != 204 : return 'There was an error ' + response return render_template( 'results.html', endpoint='requests', data=responseUber, )
class Uber_api(object): def __init__(self): self.url, self.auth_flow = get_auth_url() def get_AuthUrl(self): return self.url def get_AuthCredentials(self, red_url, cred): if red_url: session = self.auth_flow.get_session(red_url) self.client = UberRidesClient(session, sandbox_mode=False) cred = session.oauth2credential return cred else: session = Session(oauth2credential=cred) self.client = UberRidesClient(session, sandbox_mode=False) def user_profile(self): reponse = self.client.get_user_profile() profile = reponse.json first_name = profile.get('first_name') last_name = profile.get('last_name') email = profile.get('email') return first_name, last_name, email def user_history(self): reponse = self.client.get_user_activity() history = reponse.json return history def get_products(self, lat=37.77, lng=-122.41): response = self.client.get_products(lat, lng).json #self.product_id = pdct[0].get('product_id') return response def get_estimate_pricess(self, start_latitude=37.77, start_longitude=-122.41, end_latitude=37.79, end_longitude=-122.41): res = self.client.get_price_estimates(start_latitude=start_latitude, start_longitude=start_longitude, end_latitude=end_latitude, end_longitude=end_longitude).json return res def get_estimate_time(self, start_latitude=37.77, start_longitude=-122.41, product_id='57c0ff4e-1493-4ef9-a4df-6b961525cf92'): res = self.client.get_pickup_time_estimates( start_latitude=start_latitude, start_longitude=start_longitude, product_id=product_id).json return res def get_estimate_price(self, st_lat=37.77, st_lng=-122.41, dt_lat=37.79, dt_lng=-122.41, seat_count=2, product_id='a1111c8c-c720-46c3-8534-2fcdd730040d'): estimate = self.client.estimate_ride(product_id=product_id, start_latitude=st_lat, start_longitude=st_lng, end_latitude=dt_lat, end_longitude=dt_lng, seat_count=seat_count) est = estimate.json.get('fare') #self.fare_id = est['fare_id'] return est def request_ride(self, st_lat=37.77, st_lng=-122.41, dt_lat=37.79, dt_lng=-122.41, seat_count=2, prod_id='', fare_id=''): response = self.client.request_ride(product_id=prod_id, start_latitude=st_lat, start_longitude=st_lng, end_latitude=dt_lat, end_longitude=dt_lng, seat_count=seat_count, fare_id=fare_id) req = response.json #self.request_id = req.get('request_id') return req def riders_details(self, req_id="221448y7e32ye"): res = self.client.get_ride_details(req_id) ride = res.json return ride def process_request(self, req_id): status = 'accepted' self.client.update_sandbox_ride(req_id, status) #status = ['in_progress','accepted','completed'] # # for i in range(len(status)): # self.client.update_sandbox_ride(req_id,status[i]) # time.sleep(15) def cancel_current_ride(self): res = self.client.cancel_current_ride() return res.json def cancel_ride(self, req_id): res = self.client.cancel_ride(req_id) return res.json
def __uber_action(self, nlu_entities=None): uber = None if nlu_entities is not None: if 'location' in nlu_entities: entities3 = nlu_entities['search_query'][0]["value"] entities1 = nlu_entities['location'][0]["value"] entities2 = nlu_entities['location'][1]["value"] print(entities3) print(entities1) print(entities2) if entities1 and entities2 is not None: response = requests.get( 'https://maps.googleapis.com/maps/api/geocode/json?address=%s&key=AIzaSyAx_nPgGLpiiak3Ey0YgDaJjoqlcjZko1A' % entities1) # add key resp_json_payload = response.json() lat1 = ( resp_json_payload['results'][0]['geometry']['location']['lat']) lng1 = ( resp_json_payload['results'][0]['geometry']['location']['lng']) response = requests.get( 'https://maps.googleapis.com/maps/api/geocode/json?address=%s&key=AIzaSyAx_nPgGLpiiak3Ey0YgDaJjoqlcjZko1A' % entities2) # add key resp_json_payload = response.json() lat2 = ( resp_json_payload['results'][0]['geometry']['location']['lat']) lng2 = ( resp_json_payload['results'][0]['geometry']['location']['lng']) oauth2credential = OAuth2Credential( client_id='FxkO4ImvByXu-7Dm1vYMHcKIt3C-3-LX ', # add client id access_token= 'JA. -HsSUvO2XciOsCC3M25dwxYZTxyr4T9WKRKW8SawMVLucIKNmSZSZtvuE2LUMfb_ykrWxDAAAAIhIo1wfiuri3URzNyQAAABiMGQ4NTgwMy0zOGEwLTQyYjMtODA2ZS03YTRjZjhlMTk2ZWU', # get access token expires_in_seconds='2592000', scopes= 'all_trips delivery history history_lite places profile request request_receipt ride_widgets', grant_type='authorization_code', redirect_url= 'http://localhost:8080/redirect_uri', # add redirect_url client_secret= 'hAjOfJ-OPiQ3dntDT9KtiLxwitcXrxu8-pNPRuzP', # add client secret refresh_token='', # add refresh token ) from uber_rides.auth import AuthorizationCodeGrant redirect_url = 'https://localhost:8080/redirect_uri', # add redirect_url auth_flow = AuthorizationCodeGrant( client_id='FxkO4ImvByXu-7Dm1vYMHcKIt3C-3-LX ', # add client id # access_token='JA.VUNmGAAAAAAAEgASAAAABwAIAAwAAAAAAAAAEgAAAAAAAAG8AAAAFAAAAAAADgAQAAQAAAAIAAwAAAAOAAAAkAAAABwAAAAEAAAAEAAAAKJLlVbnq3rq5cidLOUCeZZsAAAAxfnlfHgkh1D_dT92vApueOOrPLV79RicmAdIVBZXV4jUZfneV1jq3pcF2Xwdd5AIaCpvZxVY32u-HsSUvO2XciOsCC3M25dwxYZTxyr4T9WKRKW8SawMVLucIKNmSZSZtvuE2LUMfb_ykrWxDAAAAIhIo1wfiuri3URzNyQAAABiMGQ4NTgwMy0zOGEwLTQyYjMtODA2ZS03YTRjZjhlMTk2ZWU', # get access token # expires_in_seconds='2592000', scopes= 'all_trips delivery history history_lite places profile request request_receipt ride_widgets', # scopes='ride_widgets', # grant_type='authorization_code', redirect_url= 'https://localhost:8080/redirect_uri', # add redirect_url client_secret= 'hAjOfJ-OPiQ3dntDT9KtiLxwitcXrxu8-pNPRuzP', # add client secret # refresh_token='', # add refresh token ) redirect_url = 'https://localhost:8080/redirect_uri', # add redirect_url auth_url = auth_flow.get_authorization_url() #session = auth_flow.get_session(redirect_url) session = Session(oauth2credential=oauth2credential) print(session) client = UberRidesClient(session, sandbox_mode=True) print(client) response = client.get_products(lat1, lng1) credentials = session.oauth2credential print(response) response = client.get_user_profile() profile = response.json print(profile) # response_uber = client.get_price_estimates( # start_latitude=lat1, # start_longitude=lng1, # end_latitude=lat2, # end_longitude=lng2, # seat_count=1 # ) # estimate = response_uber.json.get('prices') # print estimate[0]['high_estimate'] # print estimate[0]['low_estimate'] # print estimate[0]['localized_display_name'] # print estimate[0]['currency_code'] # currency = estimate[0]['currency_code'] # hi_est = str(estimate[0]['high_estimate']) + str(estimate[0]['currency_code']) # low_est = str(estimate[0]['low_estimate']) + str(estimate[0]['currency_code']) # type_cab = estimate[0]['localized_display_name'] # print estimate[0] response = client.get_products(lat1, lng1) products = response.json.get('products') # print(products) if entities3 == 'Uber go': for i in range(1, 5): if products[i]['display_name'] == 'uberGO': product_id = products[i].get('product_id') type_cab = products[i].get('display_name') elif entities3 == 'Uber pool': for i in range(1, 5): if products[i]['display_name'] == 'POOL': product_id = products[i].get('product_id') type_cab = products[i].get('display_name') else: product_id = products[0].get('product_id') type_cab = products[0].get('display_name') estimate = client.estimate_ride(product_id=product_id, start_latitude=lat1, start_longitude=lng1, end_latitude=lat2, end_longitude=lng2, seat_count=1) fare = estimate.json.get('fare') bas = fare['display'] client.cancel_current_ride() response = client.request_ride(product_id=product_id, start_latitude=lat1, start_longitude=lng1, end_latitude=lat2, end_longitude=lng2, seat_count=1, fare_id=fare['fare_id']) request = response.json print(request) request_id = request.get('request_id') url = 'https://sandbox-api.uber.com/v1.2/sandbox/requests/' + request ur = 'https://sandbox-api.uber.com/v1.2/requests/' + request + '/map' token = "hAjOfJ-OPiQ3dntDT9KtiLxwitcXrxu8-pNPRuzP" # insert token data = {"status": "accepted"} headers = { 'Authorization': 'Bearer ' + token, "Content-Type": "application/json" } # Call REST API response = requests.put(url, data=json.dumps(data), headers=headers) response = requests.get(ur, headers=headers) response = client.get_ride_details(request_id) ride = response.json print(ride) status = ride.get('status') dri_name = ride.get('driver').get('name') dri_pic = ride.get('driver').get('picture_url') eta = ride.get('destination').get('eta') car_pix = ride.get('vehicle').get('picture_url') # product_name1 = products[3]['display_name'] #GO # product_nam2 = products[2]['display_name'] #POOL uber_action = "Sure. Booking your uber from %s to %s. Your cab type is %s and estimated time of arrival is %s and fare will be approx %s" % ( entities1, entities2, type_cab, eta, bas) cab_data = { "cabtype": type_cab, 'maxfare': bas, "minfare": eta, 'to': entities2, 'from': entities1, 'status': status, 'driver': dri_name, 'pic': dri_pic, 'car': car_pix, 'map': ur } # print cab_data requests.post("http://localhost:8080/cab", data=json.dumps(cab_data)) self.speech.synthesize_text(uber_action) else: self.__text_action( "I'm sorry, I don't think that their is any cab available between these two locations." )