Skip to content

Instantly share code, notes, and snippets.

@m3nu
Created March 6, 2016 05:31
Show Gist options
  • Save m3nu/092cbf5a0009fb683ad7 to your computer and use it in GitHub Desktop.
Save m3nu/092cbf5a0009fb683ad7 to your computer and use it in GitHub Desktop.

Revisions

  1. Manu created this gist Mar 6, 2016.
    82 changes: 82 additions & 0 deletions uber-ride-history.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    #!/usr/bin/env python2

    """
    Semi-manual script to download trips and cost.
    Currently needs manual steps in 2 places. And chrome-selenium.
    (or any other Selenium browser)
    You also want to adapt the currencies to your needs.
    pip3 install uber_rides selenium pandas
    """

    import re
    import pandas as pd
    from selenium import webdriver
    from uber_rides.client import UberRidesClient
    from uber_rides.auth import AuthorizationCodeGrant

    RIDE_DETAILS_URL = 'https://riders.uber.com/trips/{}'

    def get_uber_client():
    # register app on https://developer.uber.com
    auth_flow = AuthorizationCodeGrant(
    'XXX',
    {'history', 'request_receipt'},
    'XXX',
    'http://localhost'
    )
    auth_url = auth_flow.get_authorization_url()
    # Confirm auth_url and paste below.
    session = auth_flow.get_session('RESPONSE URL')
    client = UberRidesClient(session)
    return client

    def get_rides_df(client):
    rides = []
    i = 0
    while True:
    resp = client.get_user_activity(limit=50, offset=i)
    i += 50
    if len(resp.json['history']) > 0:
    rides += resp.json['history']
    else:
    break

    rides_sel = [ {'request_id': e['request_id'],
    'distance': e['distance'],
    'city': e['start_city']['display_name'],
    'time': e['start_time']} for e in rides]
    df = pd.DataFrame(rides_sel)
    df.time = pd.to_datetime(df.time*1e9)
    return df

    driver = webdriver.Chrome()
    driver.get('https://riders.uber.com/trips/')
    # Login to uber in selenium browser.

    def get_price_km(request_id):
    XP_KM = '//*[@id="slide-menu-content"]/div/div[2]/div/div/div[2]/div[3]/div/div[1]/div[2]/div[2]/div/div[2]/h5'
    XP_PRICE = '//td[@class="text--right alpha weight--semibold"]'

    driver.get(RIDE_DETAILS_URL.format(request_id))
    price = driver.find_element_by_xpath(XP_PRICE).text
    km = driver.find_element_by_xpath(XP_KM).text
    return (float(km), price)

    def get_eur_price(price):
    currencies = {u'RM': 4.5,
    u'₫': 25000,
    u'฿': 40}
    price = re.sub(',', '', price)
    price_float = float(re.findall('\d+\.\d+', price)[0])
    for currency in currencies.keys():
    if currency in price:
    return price_float / currencies[currency]

    df['km'], df['price'] = zip(*km)
    df['eur'] = df.price.apply(get_eur_price)