-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFoursquareCaptor.py
85 lines (73 loc) · 2.59 KB
/
FoursquareCaptor.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
from Captor import Captor
import requests
class FoursquareCaptor(Captor):
url = "https://api.foursquare.com/v2/venues/explore"
client_id = 'YOUR_CLIENT_ID'
client_secret = 'YOUR_CLIENT_SECRET'
limit = 50
sections = ['food', 'drinks', 'coffee', 'shops', 'arts', 'outdoors', 'sights', 'trending', 'specials', 'nextVenues', 'topPicks']
class FoursquareApiError(Exception):
pass
def __init__(self, logger):
self.logger = logger
def __get_only_venues(self, loaded_json):
venues = []
for group in loaded_json['response']['groups']:
for item in group ['items']:
venues.append(item['venue'])
return venues
def __request_venues(self, payload):
r = requests.get(self.url, params=payload, headers=self.headers_anonymous)
try:
loaded_json = r.json()
except:
r.raise_for_status()
if r.status_code <> requests.codes.ok:
error = "[code %d] %s"%(loaded_json['meta']['code'], loaded_json['meta']['errorType'])
if 'errorDetail' in loaded_json['meta']:
error += ": %s"% loaded_json['meta']['errorDetail']
raise self.FoursquareApiError(error)
return loaded_json
def get_explore_data(self, location, version_date, withNear, section=None):
payload = {
'client_id': self.client_id,
'client_secret': self.client_secret,
'v': version_date,
'limit': self.limit,
}
if withNear:
payload['near'] = location
else:
payload['ll'] = location
payload['radius'] = 200
if section <> None:
payload['section'] = section
loaded_json = self.__request_venues(payload)
total_results = loaded_json['response']['totalResults']
self.logger.warning("Total venues: %d"% total_results)
venues = self.__get_only_venues(loaded_json)
nb_venues = len(venues)
self.logger.info("%d venues now"% nb_venues)
could_take_all = False
if total_results <= self.limit:
could_take_all = True
total_results -= nb_venues
while total_results > 0 and could_take_all == False:
payload['offset'] = nb_venues
loaded_json = self.__request_venues(payload)
new_venues = self.__get_only_venues(loaded_json)
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))
if total_results <= self.limit:
could_take_all = True
else:
total_results -= nb_new_venues
return venues
def get_explore_data_with_sections(self, location, version_date, withNear):
venues = []
for section in self.sections:
self.logger.warning("Capting for section %s", section)
venues += self.get_explore_data(location, version_date, withNear, section)
return venues