-
Notifications
You must be signed in to change notification settings - Fork 0
/
yelp.py
160 lines (132 loc) · 5.41 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""Command line interface to the Yelp Search API."""
import json
import oauth2
import optparse
import urllib
import urllib2
parser = optparse.OptionParser()
parser.add_option('-c', '--consumer_key', dest='consumer_key', help='OAuth consumer key (REQUIRED)')
parser.add_option('-s', '--consumer_secret', dest='consumer_secret', help='OAuth consumer secret (REQUIRED)')
parser.add_option('-t', '--token', dest='token', help='OAuth token (REQUIRED)')
parser.add_option('-e', '--token_secret', dest='token_secret', help='OAuth token secret (REQUIRED)')
parser.add_option('-a', '--host', dest='host', help='Host', default='api.yelp.com')
parser.add_option('-q', '--term', dest='term', help='Search term')
parser.add_option('-l', '--location', dest='location', help='Location (address)')
parser.add_option('-b', '--bounds', dest='bounds', help='Bounds (sw_latitude,sw_longitude|ne_latitude,ne_longitude)')
parser.add_option('-p', '--point', dest='point', help='Latitude,longitude')
# Not sure if current location hints are currently working
parser.add_option('-i', '--current_location', dest='current_location', help='Current location latitude,longitude for location disambiguation')
parser.add_option('-o', '--offset', dest='offset', help='Offset (starting position)')
parser.add_option('-r', '--limit', dest='limit', help='Limit (number of results to return)')
parser.add_option('-u', '--cc', dest='cc', help='Country code')
parser.add_option('-n', '--lang', dest='lang', help='Language code')
parser.add_option('-d', '--radius', dest='radius', help='Radius filter (in meters)')
parser.add_option('-g', '--category', dest='category', help='Category filter')
parser.add_option('-z', '--deals', dest='deals', help='Deals filter')
parser.add_option('-m', '--sort', dest='sort', help='Sort')
options, args = parser.parse_args()
"""
# Required options
if not options.consumer_key:
parser.error('--consumer_key required')
if not options.consumer_secret:
parser.error('--consumer_secret required')
if not options.token:
parser.error('--token required')
if not options.token_secret:
parser.error('--token_secret required')
"""
if not options.location and not options.bounds and not options.point:
parser.error('--location, --bounds, or --point required')
# Setup URL params from options
url_params = {}
if options.term:
url_params['term'] = options.term
if options.location:
url_params['location'] = options.location
if options.bounds:
url_params['bounds'] = options.bounds
if options.point:
url_params['ll'] = options.point
if options.offset:
url_params['offset'] = options.offset
if options.limit:
url_params['limit'] = options.limit
if options.cc:
url_params['cc'] = options.cc
if options.lang:
url_params['lang'] = options.lang
if options.current_location:
url_params['cll'] = options.current_location
if options.radius:
url_params['radius_filter'] = options.radius
if options.category:
url_params['category_filter'] = options.category
if options.deals:
url_params['deals_filter'] = options.deals
if options.sort:
url_params['sort'] = options.sort
def request(host, path, url_params, consumer_key="FH-RvZ_ScMpTCdMb1esSoQ", consumer_secret="QvfkebxG2ol0aZqem4Bka4LZZ6Y", token="-4NwiPsWveiVcTkVBC-5vXXgnnVk6r_l", token_secret="F2Q00ewraK8qfnm6yW2h3h-HG2g"):
"""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)
#print 'URL: %s' % (url,)
# 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()
#print 'Signed URL: %s\n' % (signed_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
#response = request(options.host, '/v2/search', url_params, options.consumer_key, options.consumer_secret, options.token, options.token_secret)
response = request(options.host, '/v2/search', url_params)
data = json.dumps(response, sort_keys=True, indent=2)
#print data
data = json.loads(data)
print json.dumps(data, sort_keys=True, indent=2)
#if (options.offset == "0"):
total = 0
error = ""
for k, v in data.iteritems():
if 'total' in k:
if (k == "total"):
total = v
elif 'error' in k:
error = v
fname = ""
fcont = ""
if total > 0:
#fname = "/data/yelp/json/"+options.term[1:]+".total"
fname = "/data/yelp/json/"+options.term[1:]+"_over1000.total"
fcont = options.bounds+"\t"+str(total)+"\n"
elif error != '':
#fname = "/data/yelp/json/"+options.term[1:]+".error"
fname = "/data/yelp/json/"+options.term[1:]+"_over1000.error"
fcont = options.bounds+"\t"+str(error)+"\n"
if fname != "" and str != "":
f = open(fname, "a")
f.write(fcont)
f.close()
"""
datas = []
print json.dumps(datas, sort_keys=True, indent=2)
#f = open('/data/yelp/store/'+url_params['location']+'.json', 'w')
#f.write(j)
"""