-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfoursquare.py
379 lines (315 loc) · 12.3 KB
/
foursquare.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
""""
Foursquare.py - v0.1.1
=======================
A simple python wrapper for the foursquare API.
http://www.foursquare.com
http://api.foursquare.com
Created by Ismail Dhorat @ http://zyelabs.net
License: see the file 'License'
Usage:
foursquare.method(requiredargs, optionalargs)
help(foursquare) - for help
Notes:
- arguments are required.
- keyword arguments are optional.
- the keyword is mapped to the key required by the foursquare API (except for set_pings).
- if authentication is required, username and password are the first two args.
"""
import urllib2
import urllib
import base64
try:
import simplejson
except:
print 'Please install the required module simplejson'
class Api():
"""
Foursquare API class that implements methods for the API
args: None
"""
def __init__(self):
self.url = 'http://api.foursquare.com/v1/'
self.output = '.json'
def _return_result(self, endpoint, username=None, password=None, params=None, post=None):
"""
Internal method to return the results
Arguments (Required):
``endpoint``
The foursquare API endpoint for this request
Keyword Arguments (Optional):
``username``
Required for authenticated requests
``password``
required for authenticated requests
``params``
Optional GET/POST parameters to send to the foursquare API
``post``
Only required for HTTP POST requests, set to True
"""
query_url = self.url + endpoint + self.output
if not post:
if params:
data = urllib.urlencode(params)
request = urllib2.Request('%s?%s' % (query_url, data) )
else:
request = urllib2.Request(query_url)
else:
if params:
data = urllib.urlencode(params)
request = urllib2.Request(query_url, data)
else:
request = urllib2.Request(query_url)
if username and password:
b64 = base64.encodestring('%s:%s' % (username, password))[:-1]
authheader="Basic %s" % b64
request.add_header('Authorization', authheader)
try:
result = simplejson.load(urllib2.urlopen(request))
except IOError, e:
result = simplejson.load(e)
return result
def test(self):
"""
Test if an API request will succeed
Arguments: None
Returns:
``True``
Test was succesful
``False``
The query resulted in an Error
"""
check = self._return_result('test')
return check['response'] == 'ok'
def get_cities(self):
"""
Returns all cities
Arguments: None
"""
return self._return_result('cities')
def check_city(self, geolat, geolong):
"""
Returns the closest foursquare city for a give lat & lon
Arguments (required):
``geolat``
latitude
``geolong``
longitude
"""
params = {'geolat': geolat, 'geolong': geolong}
return self._return_result('checkcity', params=params)
def switch_city(self, username, password, cityid):
"""
Switch city for authenticated user to a given cityid
Arguments (required):
``username``
username of the user
``password``
password of the user
``cityid``
the foursquare cityid being switched to
"""
return self._return_result('switchcity', username=username, password=password, params={'cityid': cityid}, post=True)
def get_checkins(self, username, password, **kwargs):
"""
Returns checkins for friends of authenticated user
Arguments (required):
``username``
username of the user
``password``
password of the user
Keyword Arguments (optional):
``cityid``
the foursquare cityid
"""
return self._return_result('checkins', username=username, password=password, params=kwargs)
def checkin(self, username, password, **kwargs):
"""
Check in the authenticated user
Arguments (required):
``username``
username of the user
``password``
password of the user
Keyword Arguments (optional):
``vid``
Foursquare venue ID
``venue``
String name of the venue - The API will try and find the closest match
``shout``
Text to send with checkin, max length is 140
``private``
hides location set to 1 or 0
``twitter``
1 or 0, defaults to setting in the users profile
``geolat``
latitude
``geolong``
longitude
"""
if kwargs.has_key('vid') or kwargs.has_key('venue') or kwargs.has_key('shout'):
result = self._return_result('checkin', username=username, password=password, params=kwargs, post=True)
else:
result = 'CheckIn method requires at least one of: vid, venue or shout'
return result
def get_history(self, username, password):
"""
Returns a history for the authenticated user
Arguments (required):
``username``
username of the user
``password``
password of the user
"""
return self._return_result('history', username=username, password=password)
def get_user_detail(self, username, password, **kwargs):
"""
Returns user details for a given uid or authenticated user
Arguments (Required):
``username``
username of the user
``password``
password of the user
Keyword Arguments (optional):
``uid``
userid for the user
``mayor``
default is false, set to 1 to show
``bages``
default is false, set to 1 to show
"""
return self._return_result('user', username=username, password=password, params=kwargs)
def get_friends(self, username, password, **kwargs):
"""
Returns friends for a given uid or authenticated user
Arguments (required):
``username``
username of the user
``password``
password of the user
Keyword Arguments (optional):
``uid``
userid for the user
"""
return self._return_result('friends', username=username, password=password, params=kwargs)
def get_venues(self, geolat, geolong, **kwargs):
"""
Returns venues within range for a given lat & lon
Arguments (required):
``geolat``
latitude
``geolong``
longitude
Keyword Arguments (optional):
``l``
Limit the results returned
``q``
search for a keyword
"""
kwargs['geolat'] = geolat
kwargs['geolong'] = geolong
return self._return_result('venues', params=kwargs)
def get_venue_detail(self, vid , username=None, password=None,):
"""
Returns detailed info for a specific venue
Arguments (required):
``vid``
Foursquare venue ID
Keyword Arguments (optional):
``username``
username of the user
``password``
password of the user
"""
return self._return_result('venue', username=username, password=password, params={'vid': vid })
def add_venue(self, username, password, name, address, crossstreet, city, state, cityid, **kwargs):
"""
Adds a new venue
Arguments (required):
``username``
username of the user
``password``
password of the user
``name``
the venue name
``address``
the address of the venue
``crossstreet``
the cross streets (e.g., "btw Grand & Broome")
``city``
city name where this venue is)
``state``
the state where the city is or for non US insert country for state)
``cityid``
the cityid for the venue
Keyword Arguments (optional)
``zip``
zip or postal code
``phone``
phone number of the venue
"""
params = {'name': name,'address': address,'crossstreet':crossstreet,'city':city,'state':state,'cityid':cityid }
if kwargs.has_key('phone'):
params['phone'] = kwargs['phone']
if kwargs.has_key('zip'):
params['zip'] = kwargs['zip']
return self._return_result('addvenue', username=username, password=password, params=params, post=True)
def get_tips(self, geolat, geolong, **kwargs):
"""
Returns tips within range of a given geplat & geolong
Arguments (required):
``geolat``
latitude
``geolong``
longitude
Keyword Arguments (optional):
``l``
limit results returned
"""
kwargs['geolat'] = geolat
kwargs['geolong'] = geolong
return self._return_result('tips', params=kwargs)
def add_tip(self, username, password, vid, text, **kwargs):
"""
Adds a tip for a specific vid
Arguments (required):
``username``
username of the user
``password``
password of the user
``vid``
Foursquare venue ID
``text``
the tip text
Keyword Arguments (optional):
``type``
Set it as 'todo' or 'tip', defaults to tip
"""
kwargs['vid'] = vid
kwargs['text'] = text
return self._return_result('addtip', username=username, password=password, params=kwargs, post=True)
def set_pings(self, username, password, **kwargs):
"""
Change ping settings for authenticated user or for friends of authenticated user
Arguments (required):
``username``
username of the user
``password``
password of the user
Keyword Arguments (optional):
``me``
Set global ping status for yourself. options are: on, off or goodnight
``friends``
a list of dictionaries
Note:
- friends takes a list of dictionaries in the following format: [{'uid': 123, 'ping': 1},{'uid': 123, 'ping': 0}]
- 1 = On
- 0 = Off
"""
if kwargs.has_key('me'):
kwargs['self'] = kwargs['me']
del kwargs['me']
if kwargs.has_key('friends'):
friendlist = kwargs.pop('friends')
for friend in friendlist:
kwargs[friend['uid']] = friend['ping']
return self._return_result('settings/setpings', username=username, password=password, params=kwargs, post=True)