-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindARestaurant.py
More file actions
59 lines (47 loc) · 2.14 KB
/
Copy pathfindARestaurant.py
File metadata and controls
59 lines (47 loc) · 2.14 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
import httplib2
import json
import sys
import enviromental_variables as env
def getGeocodeLocation(nameOfThePlace):
google_api_key=env.getGoogleApiKey()
place_to_search = nameOfThePlace.replace(" ", "+")
url = ('https://maps.googleapis.com/maps/api/geocode/json?address=%s&key=%s'%(place_to_search, google_api_key))
http = httplib2.Http()
response, content = http.request(url, 'GET')
result = json.loads(content)
lat = result['results'][0]['geometry']['location']['lat']
longit = result['results'][0]['geometry']['location']['lng']
return lat, longit
def findARestaurant(mealType, location):
lat, longit = getGeocodeLocation(location)
try:
first_place = getAPlace(lat, longit, mealType)[0]
except:
return "no restaurant"
venue_id = first_place["id"]
response = {}
response["name"] = first_place["name"] if first_place["name"] != None else "no name"
response["address"] = first_place["location"]["formattedAddress"]
response["image"] = getFourSquarePhoto(venue_id)
return response
def getAPlace(lat, longit, mealType):
fourSquare_id, fourSquare_secret=env.getForsquareIdAndSecret()
url = ('https://api.foursquare.com/v2/venues/search?ll=%s,%s&query=%s&v=20180323&client_secret=%s&client_id=%s'%(lat, longit, mealType, fourSquare_secret, fourSquare_id))
http = httplib2.Http()
response, content = http.request(url, 'GET')
result = json.loads(content)
return result["response"]["venues"]
def getFourSquarePhoto(venue_id):
fourSquare_id, fourSquare_secret=env.getForsquareIdAndSecret()
photo_size = '300x300'
url = ('https://api.foursquare.com/v2/venues/%s/photos?client_secret=%s&client_id=%s&v=20180323'%(venue_id, fourSquare_secret, fourSquare_id))
http = httplib2.Http()
response, content = http.request(url, 'GET')
# the content returns a list of pictures, but I just want the first one
content = json.loads(content)
if content["response"]["photos"]["items"]:
first_pic = content["response"]["photos"]["items"][0]
pic_url = first_pic["prefix"]+ photo_size + first_pic["suffix"]
else:
pic_url = 'http://paris.peninsula.com/pt/~/media/Images/Paris/NEW/dining/loiseau-blanc/ppr-oiseau-blanc-interior-evening-1074.ashx?mw=952'
return pic_url