GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: A binding for the Fire Eagle API in Python
Clone URL: git://github.com/SteveMarshall/fire-eagle-python-binding.git
now you can store your key info in ~/.fireeaglerc
myelin (author)
Fri Aug 08 01:41:26 -0700 2008
commit  9f40e6d4d0264eb107ae527a3ade2422be23465c
tree    382bef6f51e285f1fc2b9a18285977bad17de903
parent  09d4d6064af560052e5cf0298c0d9dda0a6fd382
...
91
92
93
94
 
95
96
97
 
98
99
100
 
101
102
103
...
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
...
287
288
289
290
 
 
 
 
 
 
291
292
293
 
294
295
296
297
298
299
 
 
300
301
302
303
304
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
305
306
307
...
419
420
421
422
 
423
424
425
...
428
429
430
431
 
432
433
434
...
91
92
93
 
94
95
96
 
97
98
99
 
100
101
102
103
...
247
248
249
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250
251
252
...
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
...
440
441
442
 
443
444
445
446
...
449
450
451
 
452
453
454
455
0
@@ -91,13 +91,13 @@ FE_SERVER = 'fireeagle.yahoo.net'
0
 
0
 # Calling templates
0
 API_URL_TEMPLATE = string.Template(
0
- '${proto}://${server}/api/' + API_VERSION + '/${method}'
0
+ '${server}/api/' + API_VERSION + '/${method}'
0
 )
0
 OAUTH_URL_TEMPLATE = string.Template(
0
- '${proto}://${server}/oauth/${method}'
0
+ '${server}/oauth/${method}'
0
 )
0
 AUTHORIZE_URL_TEMPLATE = string.Template(
0
- '${proto}://${server}/oauth/${method}?oauth_token=${token}'
0
+ '${server}/oauth/${method}?oauth_token=${token}'
0
 )
0
 POST_HEADERS = {
0
     'Content-type': 'application/x-www-form-urlencoded',
0
@@ -247,27 +247,6 @@ FIREEAGLE_METHODS = {
0
     }
0
 }
0
 
0
-
0
-def read_consumer_tokens():
0
- info = {}
0
-
0
- # try reading key and secret from ~/.fireeaglerc
0
- fn = os.path.expanduser("~/.fireeaglerc")
0
- if os.path.exists(fn):
0
- for line in open(fn).readlines():
0
- p = line.find("#")
0
- if p != -1: line = line[:p]
0
- line = line.strip()
0
- if not line: continue
0
- k, v = line.split("=", 1)
0
- info[k.strip()] = v.strip()
0
-
0
- # if we couldn't find them from .fireeaglerc, ask
0
- if not info.has_key('consumer_key'): info['consumer_key'] = raw_input("Enter consumer key: ")
0
- if not info.has_key('consumer_secret'): info['consumer_secret'] = raw_input("Enter consumer secret: ")
0
-
0
- return (info['consumer_key'], info['consumer_secret'])
0
-
0
 class FireEagleException( Exception ):
0
     pass
0
 
0
@@ -287,21 +266,63 @@ class FireEagleAccumulator:
0
     
0
 
0
 class FireEagle:
0
- def __init__( self, consumer_key, consumer_secret ):
0
+ def __init__( self, rc_or_consumer_key, consumer_secret=None ):
0
+ """
0
+ syntax: FireEagle( os.path.expanduser( "~/.fireeaglerc" ) )
0
+ or FireEagle( CONSUMER_KEY, CONSUMER_SECRET )
0
+ """
0
+
0
         # Prepare object lifetime variables
0
- self.consumer_key = consumer_key
0
- self.consumer_secret = consumer_secret
0
+ self.read_config( rc_or_consumer_key, consumer_secret )
0
         self.oauth_consumer = oauth.OAuthConsumer(
0
             self.consumer_key,
0
             self.consumer_secret
0
         )
0
         self.signature_method = oauth.OAuthSignatureMethod_HMAC_SHA1()
0
- self.http_connection = (API_PROTOCOL == 'https' and httplib.HTTPSConnection or httplib.HTTPConnection)( API_SERVER )
0
+ proto, host, port = re.search(r"^(https?)://([a-z\.0-9]+)(?:\:(\d+))?$", self.api_server).groups()
0
+ self.http_connection = (proto == 'https' and httplib.HTTPSConnection or httplib.HTTPConnection)( host, port )
0
         
0
         # Prepare the accumulators for each method
0
         for method, _ in FIREEAGLE_METHODS.items():
0
             if not hasattr( self, method ):
0
                 setattr( self, method, FireEagleAccumulator( self, method ))
0
+
0
+ def read_config( self, rc_or_consumer_key, consumer_secret ):
0
+ if consumer_secret is None:
0
+ info = {}
0
+ for line in open( rc_or_consumer_key ).readlines():
0
+ p = line.find( "#" )
0
+ if p != -1: line = line[:p]
0
+ line = line.strip()
0
+ if not line: continue
0
+ k, v = line.split("=", 1)
0
+ info[ k.strip() ] = v.strip()
0
+ else:
0
+ info = {
0
+ 'consumer_key': rc_or_consumer_key,
0
+ 'consumer_secret': consumer_secret,
0
+ }
0
+
0
+ info.setdefault("api_server", API_SERVER)
0
+ info.setdefault("api_protocol", API_PROTOCOL)
0
+ self.api_server = self._build_server_url(info, 'api')
0
+
0
+ info.setdefault("auth_server", FE_SERVER)
0
+ info.setdefault("auth_protocol", FE_PROTOCOL)
0
+ self.auth_server = self._build_server_url(info, 'auth')
0
+
0
+ self.consumer_key, self.consumer_secret = info['consumer_key'], info['consumer_secret']
0
+
0
+ def _build_server_url( self, info, role ):
0
+ proto = info['%s_protocol' % role]
0
+ default_port = (proto == 'https') and 443 or 80
0
+ port = int(info.get('%s_port' % role, default_port))
0
+ url = '%s://%s%s' % (
0
+ proto,
0
+ info['%s_server' % role],
0
+ (port != default_port) and (':%d' % port) or '',
0
+ )
0
+ return url
0
     
0
     def fetch_response( self, http_method, url, \
0
             body = None, headers = None ):
0
@@ -419,7 +440,7 @@ class FireEagle:
0
         # (without a signature) and return it witout executing
0
         # anything.
0
         if 'request_url' == meta['returns']:
0
- return meta['url_template'].substitute( method=method, proto=FE_PROTOCOL, server=FE_SERVER, token=token.key )
0
+ return meta['url_template'].substitute( method=method, server=self.auth_server, token=token.key )
0
         
0
         # Build and sign the oauth_request
0
         # NOTE: If ( token == None ), it's handled silently
0
@@ -428,7 +449,7 @@ class FireEagle:
0
             self.oauth_consumer,
0
             token = token,
0
             http_method = meta['http_method'],
0
- http_url = meta['url_template'].substitute( method=method, proto=API_PROTOCOL, server=API_SERVER ),
0
+ http_url = meta['url_template'].substitute( method=method, server=self.api_server ),
0
             parameters = kw
0
         )
0
         oauth_request.sign_request(
...
1
 
2
3
4
5
 
 
 
 
 
 
 
 
 
 
6
7
8
...
1
2
3
4
 
 
5
6
7
8
9
10
11
12
13
14
15
16
17
0
@@ -1,8 +1,17 @@
0
 import fireeagle_api
0
+import os.path
0
 from pprint import pprint
0
 
0
-consumer_key, consumer_secret = fireeagle_api.read_consumer_tokens()
0
-fe = fireeagle_api.FireEagle( consumer_key, consumer_secret )
0
+"""
0
+
0
+First, paste your consumer key and secret into ~/.fireeaglerc, in the following format:
0
+
0
+consumer_key = YNqmPYEMEOzA
0
+consumer_secret = 7W6t7UwHXhe7UtAVo2KO5VGbK6I1UjOS
0
+
0
+"""
0
+
0
+fe = fireeagle_api.FireEagle( os.path.expanduser( '~/.fireeaglerc' ) )
0
 
0
 print "Getting a request token"
0
 request_token = fe.request_token()

Comments

    No one has commented yet.