Skip to content

Instantly share code, notes, and snippets.

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.
Semi-manual python script to download your ride history including prices.
#!/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
def get_uber_client():
# register app on
auth_flow = AuthorizationCodeGrant(
{'history', 'request_receipt'},
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']
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()
# 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"]'
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment