-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlacesAPI.py
130 lines (101 loc) · 4.22 KB
/
PlacesAPI.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import requests
import json
import time
import random as random
SEARCH_ENDPOINT = "https://maps.googleapis.com/maps/api/place/textsearch/json"
DETAILS_ENDPOINT = "https://maps.googleapis.com/maps/api/place/details/json"
ROUTE_ENDPOINT = "https://maps.googleapis.com/maps/api/distancematrix/json"
API_KEY = "AIzaSyBYhkmFKfz745tUYWf5CskwslxKan6M_-E" # MUST CHANGE API KEY
RADAR_API_KEY = "prj_test_sk_5e80f86e68d87ab6840de34f8d3ff00f93e476b9"
#This is just some safe default headers to make sure our connection doesnt somehow get dropped! Ignore these for now
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0",
"Accept-Encoding": "*",
"Connection": "keep-alive"
}
#The API to get details about a place given a place id!
def api_details(id):
parameters = {"place_id":id, "key":API_KEY}
response = requests.get(url=DETAILS_ENDPOINT,params=parameters, headers=headers)
return response.json()
#The API to search for a place given some query text
def api_search(text, userLocation):
parameters = {"query": text, "key": API_KEY, "location":"43.662127,-79.387779", "radius":2000}
response = requests.get(url=SEARCH_ENDPOINT,params=parameters, headers=headers)
#print(response.json())
return response.json()
def pull_data(input_text, userLocation):
#print("Called pulldata")
data = {}
for result in api_search(input_text, userLocation)['results']:
#print("Called apisearch")
#print(result)
name = result['name']
data[name] = result
data[name]["descr"] = ""
details = api_details(result['place_id'])
# print("BHours: ")
if 'result' not in details: continue
#These if statements check if the key is actually in the JSON object, AKA they check if the place actually
# has opening hours, or has reviews before trying to access them! This is a safe practice
if 'opening_hours' in details['result'] and 'weekday_text' in details['result']['opening_hours']:
for open_day in details['result']['opening_hours']['weekday_text']:
#print(" "+open_day)
data[name]["descr"] += open_day + "|| "
#print(data)
#print("pull_data done")
if data is None:
return None
else :
print(len(data))
randomNum = random.randint(0,len(data))
x = 0
for k in data.keys():
if x != randomNum:
x = x + 1
else:
output = data.get(k)
return output
# if data.keys is not None:
# output = random.choice(list(data.keys()))
def find_route(place_ids, mode="transit", departure_time=0):
if departure_time == 0:
departure_time = int(time.time())
if len(place_ids) == 0: return place_ids
parameters = {
"key": API_KEY,
"origins": "|".join(["place_id:"+place_id for place_id in place_ids[1:]]), "destinations": "place_id:" + place_ids[0],
"mode": mode,
"departure_time": departure_time
}
response = requests.get(url=ROUTE_ENDPOINT, params=parameters, headers=headers)
return response.json()
def radar_events():
h = headers
h['Authorization']=RADAR_API_KEY
result = requests.get("https://api.radar.io/v1/events", headers = h)
print(result.json())
return result.json()
if __name__ == "__main__":
#Main program loop
while True:
try:
input_text = input("Enter your query text: ")
if (input_text == "quit"): exit(0)
places = pull_data(input_text, {"lat": 123, "lng": 111})
while True:
for i in range(len(places)):
print("{0}: {1}".format(i, places[i]))
input_text = input("Would you like to remove any of these items?")
try:
places.pop(int(input_text))
except:
print("Please input a number")
if input_text == "":
break
print(find_route([place['place_id'] for place in places.values()]))
radar_events()
#Exit on ctrl+c
except KeyboardInterrupt:
print("Exiting program")
exit(0)