Commit 75fc30f7 authored by Josh Roesslein's avatar Josh Roesslein
Browse files

finished binder. implemneted public timeline and friends timeline endpoints.

parent 7a22e2de
......@@ -2,26 +2,32 @@ import base64
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='', secure=False,
classes={'user': User, 'status': Status}):
if username and password:
self._b64up = base64.encode('%s:%s' % (username, password))
self._b64up = None = host = 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
......@@ -27,8 +34,8 @@ def bind_api(path, parser, allowed_param=None, method='GET'):
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)
......@@ -41,6 +48,6 @@ def bind_api(path, parser, allowed_param=None, method='GET'):
raise TweepError(parse_error(
# Pass returned body into parser and return parser output
return parser(
return parser(, api.classes)
return _call
Tweepy exception
class TweepError(Exception):
def __init__(self, reason):
self.reason = reason
def __str__(self):
return self.reason
Only allow method to be called with authentication.
def require_auth(func):
def wrapper(instance, *args, **kargs):
if instance._auth:
return func(instance, **kargs)
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
......@@ -7,27 +7,33 @@ def parse_error(data):
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))
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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment