-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUbervLyft.py
More file actions
97 lines (83 loc) · 3.6 KB
/
UbervLyft.py
File metadata and controls
97 lines (83 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import requests
from geopy.geocoders import Nominatim
import pdb
from pprint import pprint
locator = Nominatim(user_agent='myGeocoder')
#turns address into a tuple, of form,: latitude, longitude
def get_coordinates(address):
location = locator.geocode(address)
return location.latitude,location.longitude
#Enter addresses in the following format: '123 Some Street, City, Country'
def get_lyft_fare(source_address,destination_address):
source_latitude, source_longitude = get_coordinates(source_address)
destination_latitude, destination_longitude = get_coordinates(destination_address)
parameters = {
'start_lat': source_latitude,
'start_lng': source_longitude,
'end_lat': destination_latitude,
'end_lng': destination_longitude
}
r = requests.get(url = 'https://www.lyft.com/api/costs', params = parameters)
pprint (r.json())
def get_uber_address_ids(session, address):
headers = {
'content-type':'application/json',
'sec-fetch-mode':'cors',
'sec-fetch-site':'same-origin',
'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36',
'x-csrf-token':'x'
}
#params
body = {
"type":"pickup",
"q":address,
"locale":"en"
}
response= session.post(url = 'https://www.uber.com/api/loadFESuggestions', headers = headers , json = body)
r_dict = response.json()
id_list = [suggestion['id'] for suggestion in r_dict['data']['candidates']]
return id_list
def get_uber(source_address,destination_address):
source_latitude, source_longitude = get_coordinates(source_address)
destination_latitude, destination_longitude = get_coordinates(destination_address)
headers={"Accept":"text/html"}
session = requests.Session()
session.get('https://www.uber.com/ca/en/price-estimate/', headers= headers)
#for the cookies
#session has the cookie for that site
origin_id= get_uber_address_ids(session, source_address)[0]
destination_id= get_uber_address_ids(session, destination_address)[0]
headers = {
'x-csrf-token':'x',
'origin':'https://www.uber.com',
'referer':'https://www.uber.com/ca/en/price-estimate/',
'sec-fetch-mode':'cors', 'sec-fetch-site':'same-origin',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36',
'content-type': 'application/json'
}
#params
body ={
'destination': {
'id': destination_id,
'latitude': destination_latitude,
'locale': 'en',
'longitude':destination_longitude,
'provider': 'google_places'
},
'locale': 'en',
'origin': {
'id': origin_id,
'latitude': source_latitude,
'locale': 'en',
'longitude': source_longitude,
'provider': 'google_places'
}
}
r = session.post(url = 'https://www.uber.com/api/loadFEEstimates', headers = headers , json=body)
pprint(r.json())
#Combine Lyft and Uber fare functions
def combined(pickup, destination):
print("Lyft Fare Information")
get_lyft_fare(pickup,destination)
print("Uber Fare Inforamtion")
get_uber(pickup,destination)