-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathext_requests.py
43 lines (37 loc) · 1.74 KB
/
ext_requests.py
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
import geopandas as gpd
import requests
from geopy import exc
from geopy.geocoders import Nominatim
from sys import exit
geolocator = Nominatim(user_agent="metrobike_application")
geocode = lambda query: geolocator.geocode("%s, Los Angeles County CA" % query)
def do_geocode(address, attempt = 1, max_attempts = 5):
try:
return geocode(address)
except exc.GeocoderTimedOut:
if attempt <= max_attempts:
return do_geocode(address, attempt=attempt+1)
exit("Geocoding service requests timed out. Please try again.\n")
except TimeoutError:
if attempt <= max_attempts:
return do_geocode(address, attempt=attempt+1)
exit("Geocoding service requests timed out. Please try again.\n")
def getStationData():
url = "https://bikeshare.metro.net/stations/json/"
r = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
#GeoJSON import using requests because the geopandas.read_file(url) doesn't allow for
##specifying a request header, which results in a 403 denied response
data = r.json()
fulldf = gpd.GeoDataFrame.from_features(data)
smalldf = fulldf[["addressStreet", "addressZipCode", "bikesAvailable", "docksAvailable", "latitude", "longitude"]]
smalldf = smalldf.assign(userDist='NV')
return smalldf
def directions(startLat, startLon, destLat, destLon, mode="bicycle"):
url = "https://route-and-directions.p.rapidapi.com/v1/routing"
querystring = {"waypoints":f"{str(startLat)},{str(startLon)}|{str(destLat)},{str(destLon)}", "mode":mode}
headers = {
"X-RapidAPI-Key": "INSERT YOUR KEY HERE",
"X-RapidAPI-Host": "route-and-directions.p.rapidapi.com"
}
response = requests.get(url, headers=headers, params=querystring)
return response