-
Notifications
You must be signed in to change notification settings - Fork 0
/
travel.py
73 lines (48 loc) · 1.64 KB
/
travel.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import sys
import requests
import re
search = sys.argv[1:]
if(len(search)!=3):
exit()
origin=search[0]
destination=search[1]
mode=search[2]
origin=origin.replace(" ", "+")
destination=destination.replace(" ", "+")
mode=mode.replace(" ", "+")
# print(origin)
# print(destination)
# print(mode)
# Format: py travel.py "553 powell road whitby on" "26 barnes drive ajax on" walking
#py travel.py <"origin"> <"destination"> <"ModeOfTravel">
regex= re.compile(r'<[^>]+>')
def removeHTML(html):
return regex.sub('', html)
link = "https://maps.googleapis.com/maps/api/directions/json?origin={}&destination={}&mode={}&key=AIzaSyAcQJy-FIV-bKCYsm4Q9TRP8BKfKizIbEM".format(origin, destination, mode)
response = requests.get(link)
response=response.json()
# print(response['status'])
if response['status']== "ZERO_RESULTS":
print("No results, try specifying addresses")
exit()
if (response['status'] == "NOT_FOUND") or (response['status'] == "INVALID_REQUEST"):
print("Invalid Request")
exit()
else:
# print(len(response))
routes=response['routes'][0]
# print(response['status'])
legs=routes['legs'][0]
distance=legs['distance']['text']
print("Distance: " + distance)
time = legs['duration']['text']
print("Time: " + time )
steps=legs['steps']
# print(len(steps))
i=0
while i < len(steps):
print(removeHTML(steps[i]['html_instructions']))
print('Proceed ' + steps[i]['distance']['text'] + ". Approximately " + steps[i]['duration']['text'])
i+=1
# # Front End: Add + between every character before sending to backend python
# # weather = response['main']['temp']-273.15