-
Notifications
You must be signed in to change notification settings - Fork 8
/
fireeagle_api.py
490 lines (432 loc) · 18 KB
/
fireeagle_api.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
"""
Fire Eagle API Python module v0.6.1
by Steve Marshall <[email protected]>
<http://nascentguruism.com/>
Source repo at <http://github.com/SteveMarshall/fire-eagle-python-binding/>
Example usage:
>>> from fireeagle_api import FireEagle
>>> from pprint import pprint
>>> fe = FireEagle( YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET )
>>> application_token = fe.request_token()
>>> auth_url = fe.authorize( application_token )
>>> print auth_url
>>> pause( 'Please authorize the app at that URL!' )
>>> user_token = fe.access_token( application_token )
>>> pprint( fe.lookup( user_token, q='London, England' ) )
[{'name': 'London, England', 'place_id': '.2P4je.dBZgMyQ'}]
>>> pprint( fe.user( user_token ) )
[ { 'best_guess': True,
'georss:box': [ u'51.2613182068',
u'-0.5090100169',
u'51.6860313416',
u'0.2803600132'],
'level': 3,
'level_name': 'city',
'located_at': datetime.datetime(2008, 2, 29, 13, 22, 2),
'name': 'London, England',
'place_id': '.2P4je.dBZgMyQ'},
{ 'best_guess': True,
'georss:box': [ u'49.8662185669',
u'-6.4506998062',
u'55.8111686707',
u'1.7633299828'],
'level': 5,
'level_name': 'state',
'located_at': datetime.datetime(2008, 2, 29, 13, 22, 2),
'name': 'England, United Kingdom',
'place_id': 'pn4MsiGbBZlXeplyXg'},
{ 'best_guess': True,
'georss:box': [ u'49.1620903015',
u'-8.6495599747',
u'60.8606987',
u'1.7633399963'],
'level': 6,
'level_name': 'country',
'located_at': datetime.datetime(2008, 2, 29, 13, 22, 2),
'name': 'United Kingdom',
'place_id': 'DevLebebApj4RVbtaQ'}]
Copyright (c) 2008, Steve Marshall
All rights reserved.
Unless otherwise specified, redistribution and use of this software in
source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* The name of the author nor the names of any contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import datetime, httplib, os.path, re, string
from xml.dom import minidom
import oauth
# General API setup
API_PROTOCOL = 'https'
API_SERVER = 'fireeagle.yahooapis.com'
API_VERSION = '0.1'
FE_PROTOCOL = 'https'
FE_SERVER = 'fireeagle.yahoo.net'
# Calling templates
API_URL_TEMPLATE = string.Template(
'${server}/api/' + API_VERSION + '/${method}'
)
OAUTH_URL_TEMPLATE = string.Template(
'${server}/oauth/${method}'
)
AUTHORIZE_URL_TEMPLATE = string.Template(
'${server}/oauth/${method}?oauth_token=${token}'
)
POST_HEADERS = {
'Content-type': 'application/x-www-form-urlencoded',
'Accept' : 'text/plain'
}
LOCATION_PARAMETERS = [
'address', 'cid', 'city', 'country', 'geom', 'lac', 'lat',
'lon', 'mcc', 'mnc', 'place_id', 'postal', 'q', 'state', 'woeid'
]
# Error templates
NULL_ARGUMENT_EXCEPTION = string.Template(
'Too few arguments were supplied for the method ${method}; required arguments are: ${args}'
)
# TODO: Allow specification of method name and call-stack?
SPECIFIED_ERROR_EXCEPTION = string.Template(
'${message} (Code ${code})'
)
UNSPECIFIED_ERROR_EXCEPTION = string.Template(
'An error occurred whilst trying to execute the requested method, and the server responded with status ${status}.'
)
# Attribute conversion functions
string = lambda s: s.encode('utf8')
boolean = lambda s: 'true' == s.lower()
def geo_str(s):
if 0 == len(s):
return None
# TODO: Would this be better served returning an array of floats?
return [float(bit) for bit in s.split(' ')]
def date(s):
# 2008-02-08T10:49:03-08:00
bits = re.match(r"""
^(\d{4}) # Year ($1)
-(\d{2}) # Month ($2)
-(\d{2}) # Day ($3)
T(\d{2}) # Hour ($4)
:(\d{2}) # Minute ($5)
:(\d{2}) # Second ($6)
[+-] # TODO: TZ offset dir ($7)
\d{2} # TODO: Offset hour ($8)
:\d{2} # TODO: Offset min ($9)
""", s, re.VERBOSE
).groups()
bits = [bit for bit in bits if bit is not None]
# TODO: Generate fixed-offset tzinfo
return datetime.datetime(*map(int, bits))
# Return types
LOCATION = 'location', {
'name' : string,
'place_id': string,
'woeid' : string,
}
USER_LOCATION = 'location', {
'best_guess' : boolean,
# HACK: I'm not entirely happy using 'georss:box' as the key here
'georss:box' : geo_str,
'georss:point' : geo_str,
'level' : int,
'level_name' : string,
'located_at' : date,
'name' : string,
'place_id' : string,
'woeid' : string,
'query' : string,
}
USER = 'user', {
'token' : string,
'location': USER_LOCATION,
}
FIREEAGLE_METHODS = {
# OAuth methods
'access_token': {
'http_headers': None,
'http_method' : 'GET',
'optional' : [],
'required' : ['oauth_verifier', 'token'],
'returns' : 'oauth_token',
'url_template': OAUTH_URL_TEMPLATE,
},
'authorize': {
'http_headers': None,
'http_method' : 'GET',
'optional' : [],
'required' : ['token'],
'returns' : 'request_url',
'url_template': AUTHORIZE_URL_TEMPLATE,
},
'request_token': {
'http_headers': None,
'http_method' : 'GET',
'optional' : ['oauth_callback'],
'required' : [],
'returns' : 'oauth_token',
'url_template': OAUTH_URL_TEMPLATE,
},
# Fire Eagle methods
'lookup': {
'http_headers': None,
'http_method' : 'GET',
'optional' : LOCATION_PARAMETERS,
'required' : ['token'],
'returns' : LOCATION,
'url_template': API_URL_TEMPLATE,
},
'recent': {
'http_headers': None,
'http_method' : 'GET',
'optional' : ['per_page', 'page', 'time'],
'required' : ['token'],
'returns' : USER,
'url_template': API_URL_TEMPLATE,
},
'update': {
'http_headers': POST_HEADERS,
'http_method' : 'POST',
'optional' : LOCATION_PARAMETERS,
'required' : ['token'],
# We don't care about returns from update: HTTP 200 is success
'returns' : None,
'url_template': API_URL_TEMPLATE,
},
'user': {
'http_headers': None,
'http_method' : 'GET',
'optional' : [],
'required' : ['token'],
'returns' : USER,
'url_template': API_URL_TEMPLATE,
},
'within': {
'http_headers': None,
'http_method' : 'GET',
# HACK: woe_id is ignored if place_id is present, so neither is
# strictly 'required'. Unfortunately, calling with neither
# returns an empty list
'optional' : ['place_id', 'woe_id'],
'required' : ['token'],
'returns' : USER,
'url_template': API_URL_TEMPLATE,
}
}
class FireEagleException( Exception ):
pass
# Used as a proxy for methods of the FireEagle class; when methods are called,
# __call__ in FireEagleAccumulator is called, ultimately calling the
# fireeagle_obj's callMethod()
class FireEagleAccumulator:
def __init__( self, fireeagle_obj, name ):
self.fireeagle_obj = fireeagle_obj
self.name = name
def __repr__( self ):
return self.name
def __call__( self, *args, **kw ):
return self.fireeagle_obj.call_method( self.name, *args, **kw )
class FireEagle:
def __init__( self, rc_or_consumer_key, consumer_secret=None ):
"""
syntax: FireEagle( os.path.expanduser( "~/.fireeaglerc" ) )
or FireEagle( CONSUMER_KEY, CONSUMER_SECRET )
"""
# Prepare object lifetime variables
self.read_config( rc_or_consumer_key, consumer_secret )
self.oauth_consumer = oauth.OAuthConsumer(
self.consumer_key,
self.consumer_secret
)
self.signature_method = oauth.OAuthSignatureMethod_HMAC_SHA1()
proto, host, port = re.search(r"^(https?)://([a-z\.0-9]+)(?:\:(\d+))?$", self.api_server).groups()
self.http_connection = (proto == 'https' and httplib.HTTPSConnection or httplib.HTTPConnection)( host, port )
# Prepare the accumulators for each method
for method, _ in FIREEAGLE_METHODS.items():
if not hasattr( self, method ):
setattr( self, method, FireEagleAccumulator( self, method ))
def read_config( self, rc_or_consumer_key, consumer_secret ):
if consumer_secret is None:
info = {}
for line in open( rc_or_consumer_key ).readlines():
p = line.find( "#" )
if p != -1: line = line[:p]
line = line.strip()
if not line: continue
k, v = line.split("=", 1)
info[ k.strip() ] = v.strip()
else:
info = {
'consumer_key': rc_or_consumer_key,
'consumer_secret': consumer_secret,
}
info.setdefault("api_server", API_SERVER)
info.setdefault("api_protocol", API_PROTOCOL)
self.api_server = self._build_server_url(info, 'api')
info.setdefault("auth_server", FE_SERVER)
info.setdefault("auth_protocol", FE_PROTOCOL)
self.auth_server = self._build_server_url(info, 'auth')
self.consumer_key, self.consumer_secret = info['consumer_key'], info['consumer_secret']
def _build_server_url( self, info, role ):
proto = info['%s_protocol' % role]
default_port = (proto == 'https') and 443 or 80
port = int(info.get('%s_port' % role, default_port))
url = '%s://%s%s' % (
proto,
info['%s_server' % role],
(port != default_port) and (':%d' % port) or '',
)
return url
def fetch_response( self, http_method, url, \
body = None, headers = {}):
"""Pass a request to the server and return the response as a string"""
# Explicity add a host header to prevent Python pre2.6 from adding the port
headers['host'] = API_SERVER
# Prepare the request
if ( body is not None ) or ( headers is not None ):
self.http_connection.request( http_method, url, body, headers )
else:
self.http_connection.request( http_method, url )
# Get the response
response = self.http_connection.getresponse()
response_body = response.read()
# If we've been informed of an error, raise it
if ( 200 != response.status ):
# Try to get the error message
try:
error_dom = minidom.parseString( response_body )
response_errors = error_dom.getElementsByTagName( 'err' )
except: # TODO: Naked except: make this explicit!
response_errors = None
# If we can't get the error message, just raise a generic one
if response_errors:
msg = SPECIFIED_ERROR_EXCEPTION.substitute( \
message = response_errors[0].getAttribute( 'msg' ),
code = response_errors[0].getAttribute( 'code' )
)
else:
msg = UNSPECIFIED_ERROR_EXCEPTION.substitute( \
status = response.status )
raise FireEagleException, msg
# Return the body of the response
return response_body
def build_return( self, dom_element, target_element_name, conversions):
results = []
for node in dom_element.getElementsByTagName( target_element_name ):
data = {}
for key, conversion in conversions.items():
node_key = key.replace( '_', '-' )
key = key.replace( ':', '_' )
data_elements = node.getElementsByTagName( node_key )
# If conversion is a tuple, call build_return again
if isinstance( conversion, tuple ):
child_element, child_conversions = conversion
data[key] = self.build_return( \
node, child_element, child_conversions \
)
else:
# If we've got multiple elements, build a
# list of conversions
if data_elements and ( len( data_elements ) > 1 ):
data_item = []
for data_element in data_elements:
data_item.append( conversion(
data_element.firstChild.data
) )
# If we only have one element, assume text node
elif data_elements:
data_item = conversion( \
data_elements[0].firstChild.data
)
# If no elements are matched, convert the attribute
else:
data_item = conversion( \
node.getAttribute( node_key ) \
)
if data_item is not None:
data[key] = data_item
results.append( data )
return results
def call_method( self, method, *args, **kw ):
# Theoretically, we might want to do 'does this method exits?' checks
# here, but as all the aggregators are being built in __init__(),
# we actually don't need to: Python handles it for us.
meta = FIREEAGLE_METHODS[method]
if args:
# Positional arguments are mapped to meta['required']
# and meta['optional'] in order of specification of those
# (with required first, obviously)
names = meta['required'] + meta['optional']
for i in range( len( args ) ):
kw[names[i]] = args[i]
# Check we have all required arguments
if len( set( meta['required'] ) - set( kw.keys() ) ) > 0:
raise FireEagleException, \
NULL_ARGUMENT_EXCEPTION.substitute( \
method = method, \
args = ', '.join( meta['required'] )
)
# Token shouldn't be handled as a normal arg, so strip it out
# (but make sure we have it, even if it's None)
if 'token' in kw:
token = kw['token']
del kw['token']
else:
token = None
# If the return type is the request_url, simply build the URL
# (without a signature) and return it witout executing
# anything.
if 'request_url' == meta['returns']:
return meta['url_template'].substitute( method=method, server=self.auth_server, token=token.key )
if 'oauth_callback' in meta['optional'] and 'oauth_callback' not in kw:
kw['oauth_callback'] = "oob"
# Build and sign the oauth_request
# NOTE: If ( token == None ), it's handled silently
# when building/signing
oauth_request = oauth.OAuthRequest.from_consumer_and_token(
self.oauth_consumer,
token = token,
http_method = meta['http_method'],
http_url = meta['url_template'].substitute( method=method, server=self.api_server ),
parameters = kw
)
oauth_request.sign_request(
self.signature_method,
self.oauth_consumer,
token
)
if 'POST' == meta['http_method']:
response = self.fetch_response( oauth_request.http_method, \
meta['url_template'].substitute( method=method, server=""), \
oauth_request.to_postdata(), \
meta['http_headers'] )
else:
response = self.fetch_response( oauth_request.http_method, \
meta['url_template'].substitute( method=method, server="") + "?" + oauth_request.to_postdata() )
# Method returns nothing, but finished fine
if not meta['returns']:
return True
# Return the oauth token
elif 'oauth_token' == meta['returns']:
return oauth.OAuthToken.from_string( response )
element, conversions = meta['returns']
response_dom = minidom.parseString( response )
results = self.build_return( \
response_dom, element, conversions )
return results
# TODO: Cached version