-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapsapi.py
More file actions
49 lines (33 loc) · 1.16 KB
/
mapsapi.py
File metadata and controls
49 lines (33 loc) · 1.16 KB
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
import json
import pprint
from datetime import datetime
import requests
import os
def get_key(filename="mapsapikey.txt"):
# first line should be the api key
return open(filename, "r").read()
def get_data(destination):
key = get_key()
# Polyline encoding for BCA
origin = "enc:iqsxFfyzbM:"
r = requests.get("https://maps.googleapis.com/maps/api/distancematrix/json?origins=%s&destinations=%s&key=%s" % (origin, destination, key))
r_obj = json.loads(r.text)
return r_obj
def dealWithTheData(response):
output = 0
output = response["rows"][0]["elements"][0]["distance"]["value"]
return output
def addToFile(data, busid):
f = open("maps.json", "a")
f.seek(f.tell() - 1, os.SEEK_SET)
f.truncate()
# this assumes that maps.json already contains something
# if it is empty, add "{}" to it, run the script and then remove the leading comma
toWrite = ", \"" + busid + "\": " + str(data) + "}"
f.write(toWrite.replace("'", "\""))
f.close()
def main(location):
response = get_data(location)
return dealWithTheData(response)
if __name__ == "__main__":
main("200+Hackensack+Ave+Hackensack+NJ")