Commit 6e78a141 authored by Josh Roesslein's avatar Josh Roesslein
Browse files

Models now configures via tweepy.models dict.

parent 692d5054
......@@ -11,10 +11,7 @@ from error import TweepError
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
......@@ -22,7 +19,6 @@ class API(object):
self.cache = cache
self.secure = secure
self.validate = validate
self.classes = classes
"""Get public timeline"""
public_timeline = bind_api(
......
......@@ -81,3 +81,14 @@ class SavedSearch(Model):
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
}
......@@ -4,6 +4,8 @@
from datetime import datetime
from models import models
try:
import json
except ImportError:
......@@ -43,7 +45,7 @@ def _parse_a_href(atag):
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':
......@@ -67,7 +69,7 @@ def parse_users(data, api):
def _parse_status(obj, api):
status = api.classes['status']()
status = models['status']()
status._api = api
for k,v in obj.items():
if k == 'user':
......@@ -94,7 +96,7 @@ def parse_statuses(data, api):
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':
......@@ -121,12 +123,12 @@ def parse_friendship(data, api):
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)
......@@ -134,7 +136,7 @@ def parse_friendship(data, api):
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':
......@@ -150,14 +152,14 @@ def parse_saved_search(data, api):
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))
......
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