from binder import bind_api
from parsers import *
+from models import User, Status
"""Twitter API"""
class API(object):
- def __init__(self, username=None, password=None):
+ def __init__(self, username=None, password=None, host='twitter.com', secure=False,
+ classes={'user': User, 'status': Status}):
if username and password:
self._b64up = base64.encode('%s:%s' % (username, password))
+ else:
+ self._b64up = None
+ self.host = host
+ self.secure = secure
+ self.classes = classes
- """Twitter API endpoint bindings"""
-
- """
- Returns the 20 most recent statuses from non-protected users who have
- set a custom icon. The public timeline is cached for 60 seconds
- so requesting it more often than that is a waste of resources.
-
- Requires Authentication: false
- API Rate limited: true
- Response: list of statuses
- """
+ """Get public timeline"""
public_timeline = bind_api(
path = '/statuses/public_timeline.json',
- parser = parse_test,
- allowed_param = [])
+ parser = parse_statuses,
+ allowed_param = []
+ )
+
+ """Get friends timeline"""
+ friends_timeline = bind_api(
+ path = '/statuses/friends_timeline.json',
+ parser = parse_statuses,
+ allowed_param = ['since_id', 'max_id', 'count', 'page'],
+ require_auth = True
+ )
+import httplib
+
from parsers import parse_error
+from error import TweepError
-def bind_api(path, parser, allowed_param=None, method='GET'):
+def bind_api(path, parser, allowed_param=None, method='GET', require_auth=False):
def _call(api, **kargs):
+ # If require auth, throw exception if credentials not provided
+ if require_auth and not api._b64up:
+ raise TweepError('Authentication required!')
+
# Filter out unallowed parameters
if len(kargs) == 0:
parameters = None
headers = {
'User-Agent': 'tweepy'
}
- if api.username and api.b64pass:
- headers['Authorization'] = 'Basic %s' % api.b64pass
+ if api._b64up:
+ headers['Authorization'] = 'Basic %s' % api._b64up
# Build request
conn.request(method, url, headers=headers)
raise TweepError(parse_error(resp.read()))
# Pass returned body into parser and return parser output
- return parser(resp.read())
+ return parser(resp.read(), api.classes)
return _call
--- /dev/null
+"""
+Tweepy exception
+"""
+class TweepError(Exception):
+
+ def __init__(self, reason):
+ self.reason = reason
+
+ def __str__(self):
+ return self.reason
+++ /dev/null
-"""
-Only allow method to be called with authentication.
-"""
-def require_auth(func):
- def wrapper(instance, *args, **kargs):
- if instance._auth:
- return func(instance, **kargs)
- else:
- print 'require auth'
- return wrapper
-
-"""
-Process parameters. Perform validation. Build parameter list.
-"""
-def process_param(allowed_param):
- def decorator(func):
- def wrapper(instance, **kargs):
- if len(kargs):
- instance._parameters = {}
- for k,v in kargs.items():
- if k in allowed_param:
- instance._parameters[k] = v
- return func(instance, **kargs)
- return wrapper
- return decorator
-
-"""
-Tweepy exception
-"""
-class TweepError(Exception):
-
- def __init__(self, reason):
- self.reason = reason
-
- def __str__(self):
- return self.reason
return json.loads(data)['error']
-def parse_test(data):
+def _parse_user(obj, classes):
- return data
+ user = classes['user']()
+ for k,v in obj.items():
+ setattr(user, k, v)
+ return user
-def _parse_item(type, jsondata):
- t = type()
- for k,v in jsondata.items():
+def parse_users(data, classes):
+
+ users = []
+ for obj in json.loads(data):
+ users.append(_parse_user(obj, classes))
+ return users
+
+def _parse_status(obj, classes):
+
+ status = classes['status']()
+ for k,v in obj.items():
if k == 'user':
- setattr(t,k, _parse_item(type._User, v))
+ setattr(status, k, _parse_user(v, classes))
else:
- setattr(t,k,v)
- return t
-
-def parse_item(type, data):
- jsondata = json.loads(data)
- return _parse_item(type, jsondata)
+ setattr(status, k, v)
+ return status
-def parse_list(type, data):
- types = []
+def parse_statuses(data, classes):
+ statuses = []
for obj in json.loads(data):
- types.append(_parse_item(type, obj))
-
- return types
+ statuses.append(_parse_status(obj, classes))
+ return statuses