forked from RaiVaibhav/python-script
/
Get_travel_time_between_two_location.py
executable file
·92 lines (74 loc) · 2.44 KB
/
Get_travel_time_between_two_location.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
80
81
82
83
84
85
86
87
88
89
90
91
92
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
import datetime
from datetime import datetime
from getpass import getpass
import googlemaps
from googlemaps import Client
# (API key) : Google maps API key
GOOGLE_MAP_API_KEY = ''
def get_direction(
api_key,
from_address,
to_address,
travelling_mode
):
"""Returns all the direction informations between two points
Arguments:
- api_key : (str) googlemaps API key
- from_address : (str) departure location
- to_address : (str) arrival location
- travelling_mode : (str) conveyance
Returns:
- client.directions(...) : (list) information
about the journey
"""
client = Client(api_key)
return client.directions(
from_address,
to_address,
mode=travelling_mode,
departure_time=datetime.now()
)
def get_direction_time(direction, travelling_mode):
"""Get the duration of the journey
Arguments:
- direction : (list) information about the journey
- travelling_mode: (str) conveyance
Returns:
- time : (str) duration of the journey
"""
time = None
if travelling_mode == 'driving':
time = \
direction[0]['legs'][0]['duration_in_traffic']['text']
else:
time = \
direction[0]['legs'][0]['duration']['text']
return time
def main():
api_key = GOOGLE_MAP_API_KEY
from_address = ''
to_address = ''
travelling_mode = ''
message = '\nPlease select your travelling mode: \n'
message += '(driving, walking, bicycling): '
if not api_key:
api_key = getpass('Enter your API Key: ')
while not from_address:
from_address = input('\nBeginning of the journey: ')
while not to_address:
to_address = input('\nArrival of the journey: ')
while travelling_mode not in ['bicycling', 'driving', 'walking']:
travelling_mode = input(message)
direction = get_direction(
api_key,
from_address,
to_address,
travelling_mode
)
time_to_destination = get_direction_time(direction, travelling_mode)
print("\nIt will take %s to travel from %s to %s."
% (time_to_destination, from_address, to_address))
if __name__ == '__main__':
main()