예제 #1
0
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)
예제 #2
0
def search():
    """Show search form and create user object if not in database."""

    if 'access_token' not in sesh:
        return redirect(url_for('index'))
    else:
        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)

        user_profile = uber_client.get_user_profile().json

        sesh['user'] = {
            'first_name': user_profile.get('first_name'),
            'last_name': user_profile.get('last_name'),
            'email': user_profile.get('email'),
            'phone': user_profile.get('phone'),
            'img_url': user_profile.get('picture')
        }

        if db.session.query(User).filter(
                User.email == user_profile['email']).count() == 0:
            user = User(first_name=user_profile.get('first_name'),
                        last_name=user_profile.get('last_name'),
                        img_url=user_profile.get('picture'),
                        email=user_profile.get('email'))

            db.session.add(user)
            db.session.commit()

        return render_template(
            'search.html',
            first_name=sesh['user'].get('first_name'),
            img_url=sesh['user'].get('img_url'),
        )
예제 #3
0
def search():
    """Show search form and create user object if not in database."""
    
    if 'access_token' not in sesh:
        return redirect(url_for('index'))
    else:
        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)

        user_profile = uber_client.get_user_profile().json

        sesh['user'] = {
                        'first_name': user_profile.get('first_name'),
                        'last_name': user_profile.get('last_name'),
                        'email': user_profile.get('email'),
                        'phone': user_profile.get('phone'),
                        'img_url': user_profile.get('picture')
        }

        if db.session.query(User).filter(User.email == user_profile['email']).count() == 0:
            user = User(first_name=user_profile.get('first_name'),
                        last_name= user_profile.get('last_name'),
                        img_url=user_profile.get('picture'),
                        email=user_profile.get('email'))

            db.session.add(user)
            db.session.commit()

        return render_template('search.html',
                                first_name=sesh['user'].get('first_name'),
                                img_url=sesh['user'].get('img_url'),
                                )
예제 #4
0
def uber():
    try:
        UFP_PRODUCT_ID = '26546650-e557-4a7b-86e7-6a3942445247'

        SURGE_PRODUCT_ID = 'd4abaae7-f4d6-4152-91cc-77523e8165a4'

        save_speech('whereDoYouWantToGo')
        speech = speech2text()
        START_LAT, START_LNG = currentad()
        try:
            loc = location(speech)
        except IndexError:
            loc = speech

        END_LAT, END_LNG = find_loc_address(loc)

        def estimate_ride(api_client):

            try:
                estimate = api_client.estimate_ride(
                    product_id=SURGE_PRODUCT_ID,
                    start_latitude=START_LAT,
                    start_longitude=START_LNG,
                    end_latitude=END_LAT,
                    end_longitude=END_LNG,
                    seat_count=2)

            except (ClientError, ServerError) as error:
                fail_print(error)

            else:
                success_print(estimate.json)

        def update_surge(api_client, surge_multiplier):

            try:
                update_surge = api_client.update_sandbox_product(
                    SURGE_PRODUCT_ID,
                    surge_multiplier=surge_multiplier,
                )

            except (ClientError, ServerError) as error:
                fail_print(error)

            else:
                success_print(update_surge.status_code)

        def update_ride(api_client, ride_status, ride_id):

            try:
                update_product = api_client.update_sandbox_ride(
                    ride_id, ride_status)

            except (ClientError, ServerError) as error:
                fail_print(error)

            else:
                message = '{} New status: {}'
                message = message.format(update_product.status_code,
                                         ride_status)
                success_print(message)

        def request_ufp_ride(api_client):

            try:

                estimate = api_client.estimate_ride(product_id=UFP_PRODUCT_ID,
                                                    start_latitude=START_LAT,
                                                    start_longitude=START_LNG,
                                                    end_latitude=END_LAT,
                                                    end_longitude=END_LNG,
                                                    seat_count=2)
                fare = estimate.json.get('fare')

                request = api_client.request_ride(product_id=UFP_PRODUCT_ID,
                                                  start_latitude=START_LAT,
                                                  start_longitude=START_LNG,
                                                  end_latitude=END_LAT,
                                                  end_longitude=END_LNG,
                                                  seat_count=2,
                                                  fare_id=fare['fare_id'])

            except (ClientError, ServerError) as error:
                fail_print(error)
                return

            else:
                success_print(estimate.json)
                success_print(request.json)
                return request.json.get('request_id')

        def request_surge_ride(api_client, surge_confirmation_id=None):

            try:
                request = api_client.request_ride(
                    product_id=SURGE_PRODUCT_ID,
                    start_latitude=START_LAT,
                    start_longitude=START_LNG,
                    end_latitude=END_LAT,
                    end_longitude=END_LNG,
                    surge_confirmation_id=surge_confirmation_id,
                    seat_count=2)

            except SurgeError as e:
                surge_message = 'Confirm surge by visiting: \n{}\n'
                surge_message = surge_message.format(e.surge_confirmation_href)
                response_print(surge_message)

                confirm_url = 'Copy the URL you are redirected to and paste here: \n'
                result = input(confirm_url).strip()

                querystring = urlparse(result).query
                query_params = parse_qs(querystring)
                surge_id = query_params.get('surge_confirmation_id')[0]

                # automatically try request again
                return request_surge_ride(api_client, surge_id)

            except (ClientError, ServerError) as error:
                fail_print(error)
                return

            else:
                success_print(request.json)
                return request.json.get('request_id')

        def get_ride_details(api_client, ride_id):

            try:
                ride_details = api_client.get_ride_details(ride_id)

            except (ClientError, ServerError) as error:
                fail_print(error)

            else:
                success_print(ride_details.json)

        if __name__ == '__main__':
            credentials = import_oauth2_credentials()
            api_client = create_uber_client(credentials)

            # ride request with upfront pricing flow

            modular_speech("Request a ride with upfront pricing product.")
            ride_id = request_ufp_ride(api_client)

            modular_speech("Update ride status to accepted.")
            update_ride(api_client, 'accepted', ride_id)

            modular_speech("Updated ride details.")
            get_ride_details(api_client, ride_id)
            update_ride(api_client, 'in_progress', ride_id)

            modular_speech("Updated ride details.")
            get_ride_details(api_client, ride_id)

            modular_speech("Update ride status to completed.")
            update_ride(api_client, 'completed', ride_id)

            modular_speech("Updated ride details.")
            get_ride_details(api_client, ride_id)

            # ride request with surge flow

            modular_speech("Ride estimates before surge.")
            estimate_ride(api_client)

            modular_speech("Activate surge.")
            update_surge(api_client, 2.0)

            modular_speech("Ride estimates after surge.")
            estimate_ride(api_client)

            modular_speech("Request a ride with surging product.")
            ride_id = request_surge_ride(api_client)

            modular_speech("Update ride status to accepted.")
            update_ride(api_client, 'accepted', ride_id)

            modular_speech("Updated ride details.")
            get_ride_details(api_client, ride_id)
            update_ride(api_client, 'in_progress', ride_id)

            modular_speech("Updated ride details.")
            get_ride_details(api_client, ride_id)

            modular_speech("Update ride status to completed.")
            update_ride(api_client, 'completed', ride_id)

            modular_speech("Updated ride details.")
            get_ride_details(api_client, ride_id)

            modular_speech("Deactivate surge.")
            update_surge(api_client, 1.0)

    except Exception:
        pass
예제 #5
0

def write_to_file():
    global master_data
    global display_fare_start_to_end
    global display_fare_end_to_start
    time_stamp = str(datetime.datetime.now())
    master_data = [[
        time_stamp, display_fare_start_to_end, display_fare_end_to_start
    ]]
    print(master_data
          )  # See the data in list form """""""""""""""""""""""""""""""""
    with open("attempt_4.csv", 'ab') as uber_data:
        writing_appending = csv.writer(uber_data)
        writing_appending.writerows(master_data)


'Script'
"""""" """""" """"""
if __name__ == '__main__':
    credentials = import_oauth2_credentials()
    api_client = create_uber_client(credentials)

#    Requesting a ride estimate.
# request_available_products(api_client)

while True:
    request_ufp_ride_start_to_end(api_client)
    request_ufp_ride_end_to_start(api_client)
    write_to_file()
    time.sleep(120.0 - ((time.time() - starttime) % 120.0))