-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi_call.py
More file actions
66 lines (52 loc) · 2.37 KB
/
api_call.py
File metadata and controls
66 lines (52 loc) · 2.37 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import config
import urllib.request
import json
import time
import timeit
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
valid_location_types = {"airport", "hindu_temple", "library", \
"amusement_park", "aquarium",\
"liquor_store", "art_gallery",\
"atm", "bakery", "lodging", "bar", \
"mosque", "shopping_mall",\
"book_store", "movie_theater",\
"museum", "cafe", "shoe_store",\
"campground", "painter", "park", \
"parking", "car_wash", "pharmacy", \
"casino", "church", "night_club",\
"restaurant", "spa", "florist",\
"stadium", "store", "synagogue", "gym", "tourist_attraction", \
"university", "bowling_alley", "zoo" ,"clothing_store"}
def get_places_in_radius(user_info, place):
lat = user_info.lat
lon = user_info.lon
radius = user_info.radius
max_price = user_info.budget
key = config.api_key
endpoint = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?'
location = str(lat) + ',' + str(lon)
token = ''
result = {}
if place in valid_location_types:
place_search_word = 'type' # to be inserted in the API request
else:
place_search_word = 'keyword'
nav_request = 'location={}&opennow&maxprice={}&radius={}&{}={}&rankby=prominence&key={}'\
.format(str(location), str(max_price), str(radius), place_search_word, place, key)
request = endpoint + nav_request
response = json.loads(urllib.request.urlopen(endpoint + nav_request).read())
if response["results"] != []:
if 'next_page_token' in response:
token = response["next_page_token"]
result[place] = response["results"]
while token:
new_request = request + '&pagetoken=' + token
new_response = json.loads(urllib.request.urlopen(new_request).read())
if new_response["status"] == 'OK':
result[place].extend(new_response["results"])
if 'next_page_token' in new_response:
token = new_response["next_page_token"]
else:
token = ''
return result