-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathget_time_without_apikey.py
79 lines (68 loc) · 2.1 KB
/
get_time_without_apikey.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
74
75
76
77
78
79
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# imports
from urllib import request, parse
import sys
import json
# base url to the api
url = "https://maps.googleapis.com/maps/api/directions/json?"
# block to exit
def exit_point():
e = input("Exit(y/n): ")
if e == 'Y' or e == 'y':
print("\nThanks for using this service. HAPPY TRAVELLING.\n")
sys.exit(1)
elif e == 'n' or e == 'N':
main()
else:
print("Invalid option")
exit_point()
def travel_time(frm, to, mode):
try:
new_url = url + \
parse.urlencode({'origin': frm, 'destination': to, 'mode': mode})
data = request.urlopen(new_url).read().decode()
try:
js = json.loads(data)
except:
js = None
if not js or 'status' not in js or js['status'] != 'OK':
print('==== Failure To Retrieve ====')
exit_point()
return js["routes"][0]["legs"][0]["duration"]["text"]
except SystemExit:
sys.exit(1)
except:
print("Check your network connection")
sys.exit(1)
def main():
try:
frm = input("Origin : ").lower()
if len(frm) < 1:
exit_point()
to = input("Destination : ").lower()
if len(to) < 1:
exit_point()
mode = input("Mode(Driving,Train,Walking) : ").lower()
while mode not in ["driving", "train", "walking"]:
if len(mode) < 1:
exit_point()
print("Choose from given options only")
mode = input("Mode(Driving,Train,Walking) : ").lower()
if mode == 'train':
mode = 'transit'
netTime = travel_time(frm, to, mode)
print("\nIt will take %s to travel from %s to %s." %
(netTime, frm, to))
exit_point()
except KeyboardInterrupt:
print(
"A Keyboard Interrupt was issued by user.\nThank you for using this service.\n")
sys.exit(1)
except SystemExit:
sys.exit(1)
except:
print("Error: Invalid keystroke")
exit_point()
if __name__ == '__main__':
main()