-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
58 lines (42 loc) · 1.71 KB
/
helpers.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import requests
import json
import os
API_KEY = os.getenv('API_KEY')
def get_distance(origin: tuple, dest: tuple) -> dict:
"""Method to determine the distance by Public Transport between an origin and destination.
Args:
origin - tuple of latitude and longitude
dest - tuple of latitude and longitude
Returns:
json-object converted to dict of distance data
"""
BASE_URL = 'https://maps.googleapis.com/maps/api/distancematrix/json?'
origin = str(origin[0]) + ',' + str(origin[1])
origin_qs = 'origins=' + origin
dest = str(dest[0]) + ',' + str(dest[1])
dest_qs = '&destinations=' + dest
mode = '&mode=transit&departure_time=1587223538' # time is 16:25
url = BASE_URL + origin_qs + dest_qs + mode + '&key=' + API_KEY
return requests.get(url).json()
def parse_request_data(response: dict) -> int:
"""Parse API response from Google Distance Matrix API.
Args:
response - response from Google Distance Matrix API
Returns:
time - time taken to travel between origin and destination in seconds.
"""
return response['rows'][0]['elements'][0]['duration']['value']
def geocode_location(location: str) -> tuple:
"""Geocode location name into coordinates.
Args:
location - name of location
Returns:
location coordiantes
"""
BASE_URL = 'https://maps.googleapis.com/maps/api/geocode/json?'
location = 'address=' + location.replace(' ', '+')
components = '&components=locality:paris|country:FR'
url = BASE_URL + location + components + '&key=' + API_KEY
response = requests.get(url).json()
geo = response['results'][0]['geometry']['location']
return (geo['lat'], geo['lng'])