-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYelpCaptor.py
66 lines (52 loc) · 1.78 KB
/
YelpCaptor.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
from Captor import Captor
import requests
from requests_oauthlib import OAuth1
class YelpCaptor(Captor):
url = "http://api.yelp.com/v2/search"
oauth_consumer_key = "YOUR_CONSUMER_KEY"
oauth_consumer_secret = "YOUR_CONSUMER_SECRET"
oauth_token = "YOUR_TOKEN"
oauth_token_secret = "YOUR_TOKEN_SECRET"
class YelpApiError(Exception):
pass
def __init__(self, logger):
self.logger = logger
def __request_api(self, payload):
auth = OAuth1(self.oauth_consumer_key,
client_secret=self.oauth_consumer_secret,
resource_owner_key=self.oauth_token,
resource_owner_secret=self.oauth_token_secret,
signature_type='query')
r = requests.get(self.url, params=payload, auth=auth, headers=self.headers)
try:
loaded_json = r.json()
except:
r.raise_for_status()
if 'error' in loaded_json:
error = "[%s] %s"% (loaded_json['error']['id'], loaded_json['error']['text'])
if 'field' in loaded_json['error']:
error += " (field: %s)"% loaded_json['error']['field']
raise self.YelpApiError(error)
return loaded_json
def get_data(self, lat_long):
payload = {
'll':lat_long,
'radius_filter':200
}
loaded_json = self.__request_api(payload)
total_results = loaded_json['total']
self.logger.warning("Total venues: %d"% total_results)
venues = loaded_json['businesses']
nb_venues = len(venues)
self.logger.info("%d venues now"% nb_venues)
total_results -= nb_venues
while total_results > 0:
payload['offset'] = nb_venues
loaded_json = self.__request_api(payload)
new_venues = loaded_json['businesses']
venues += new_venues
nb_venues = len(venues)
nb_new_venues = len(new_venues)
self.logger.info("%d more venues => %d venues now"% (nb_new_venues, nb_venues))
total_results -= nb_new_venues
return venues