-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyelp.py
67 lines (53 loc) · 2.11 KB
/
yelp.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
import json
import oauth2
import optparse
import urllib
import urllib2
def locate_food(location="san francisco westfield mall", search_term="vegetarian"):
consumer_key = "1Q5TbWVfwe5U3krF3eb9yw"
consumer_secret = "CWCxOOjtX1zKStlu6p_BcY_7abo"
token = "4gL5o2gxNnEFt5DgKkETDGEVPmocfKOJ"
token_secret = "mDXZKcKOSNW7toDrsTyUyGUkGO4"
url_params = {}
url_params['location'] = location
url_params['term'] = search_term
response = request('api.yelp.com', '/v2/search', url_params, consumer_key, consumer_secret, token, token_secret)
j = json.dumps(response, sort_keys=True, indent=2)
a = json.loads(j)
restaurants_list =[]
restaurant = {}
for i in range(1,5):
restaurant["type"] = "restaurant_info"
restaurant["name"] = a["businesses"][i][u'name']
restaurant["address"] = a["businesses"][i][u'location'][u'address']
restaurant["image"] = a["businesses"][i][u'image_url']
restaurant["rating"] = a["businesses"][i][u'rating']
restaurants_list.append(restaurant)
return restaurants_list
def request(host, path, url_params, consumer_key, consumer_secret, token, token_secret):
"""Returns response for API request."""
# Unsigned URL
encoded_params = ''
if url_params:
encoded_params = urllib.urlencode(url_params)
url = 'http://%s%s?%s' % (host, path, encoded_params)
# Sign the URL
consumer = oauth2.Consumer(consumer_key, consumer_secret)
oauth_request = oauth2.Request('GET', url, {})
oauth_request.update({'oauth_nonce': oauth2.generate_nonce(),
'oauth_timestamp': oauth2.generate_timestamp(),
'oauth_token': token,
'oauth_consumer_key': consumer_key})
token = oauth2.Token(token, token_secret)
oauth_request.sign_request(oauth2.SignatureMethod_HMAC_SHA1(), consumer, token)
signed_url = oauth_request.to_url()
# Connect
try:
conn = urllib2.urlopen(signed_url, None)
try:
response = json.loads(conn.read())
finally:
conn.close()
except urllib2.HTTPError, error:
response = json.loads(error.read())
return response