class API(object):
def __init__(self, auth_handler=None, username=None, host='twitter.com', cache=None,
- secure=False, api_root='', validate=True,
- classes={'user': User, 'status': Status,
- 'direct_message': DirectMessage, 'friendship': Friendship,
- 'saved_search': SavedSearch, 'search_result': SearchResult}):
+ secure=False, api_root='', validate=True):
self.auth_handler = auth_handler
self.username = username
self.host = host
self.cache = cache
self.secure = secure
self.validate = validate
- self.classes = classes
"""Get public timeline"""
public_timeline = bind_api(
class SearchResult(Model):
pass
+
+# link up default model implementations.
+models = {
+ 'status': Status,
+ 'user': User,
+ 'direct_message': DirectMessage,
+ 'friendship': Friendship,
+ 'saved_search': SavedSearch,
+ 'search_result': SearchResult
+}
+
from datetime import datetime
+from models import models
+
try:
import json
except ImportError:
def _parse_user(obj, api):
- user = api.classes['user']()
+ user = models['user']()
user._api = api
for k,v in obj.items():
if k == 'created_at':
def _parse_status(obj, api):
- status = api.classes['status']()
+ status = models['status']()
status._api = api
for k,v in obj.items():
if k == 'user':
def _parse_dm(obj, api):
- dm = api.classes['direct_message']()
+ dm = models['direct_message']()
dm._api = api
for k,v in obj.items():
if k == 'sender' or k == 'recipient':
relationship = json.loads(data)['relationship']
# parse source
- source = api.classes['friendship']()
+ source = models['friendship']()
for k,v in relationship['source'].items():
setattr(source, k, v)
# parse target
- target = api.classes['friendship']()
+ target = models['friendship']()
for k,v in relationship['target'].items():
setattr(target, k, v)
def _parse_saved_search(obj, api):
- ss = api.classes['saved_search']()
+ ss = models['saved_search']()
ss._api = api
for k,v in obj.items():
if k == 'created_at':
def parse_saved_searches(data, api):
saved_searches = []
- saved_search = api.classes['saved_search']()
+ saved_search = models['saved_search']()
for obj in json.loads(data):
saved_searches.append(_parse_saved_search(obj, api))
return saved_searches
def _parse_search_result(obj, api):
- result = api.classes['search_result']()
+ result = models['search_result']()
for k,v in obj.items():
if k == 'created_at':
setattr(result, k, _parse_search_datetime(v))