- User model
- Added lists_memberships, lists_subscriptions, and lists helpers
- Added followers_ids helper
+ - Added ModelFactory to replace 'models' dict in tweepy.models.
+ Extend this factory to plugin customized models then pass into API().
+ API
- lists(), lists_memberships(), and lists_subscriptions() now
take an "user" parameter for specifying which user to query.
__author__ = 'Joshua Roesslein'
__license__ = 'MIT'
-from tweepy.models import Status, User, DirectMessage, Friendship, SavedSearch, SearchResult, models
+from tweepy.models import Status, User, DirectMessage, Friendship, SavedSearch, SearchResult, ModelFactory
from tweepy.error import TweepError
from tweepy.api import API
from tweepy.cache import Cache, MemoryCache, FileCache
from tweepy.binder import bind_api
from tweepy.error import TweepError
from tweepy.parsers import *
+from tweepy.models import ModelFactory
class API(object):
def __init__(self, auth_handler=None,
host='api.twitter.com', search_host='search.twitter.com',
cache=None, secure=False, api_root='/1', search_root='',
- retry_count=0, retry_delay=0, retry_errors=None):
- # you may access these freely
+ retry_count=0, retry_delay=0, retry_errors=None,
+ model_factory=None):
self.auth = auth_handler
self.host = host
self.search_host = search_host
self.retry_count = retry_count
self.retry_delay = retry_delay
self.retry_errors = retry_errors
+ self.model_factory = model_factory or ModelFactory
""" statuses/public_timeline """
public_timeline = bind_api(
def is_subscribed(self, id):
return self._api.is_subscribed_list(self.user.screen_name, self.slug, id)
-# link up default model implementations.
-models = {
- 'status': Status,
- 'user': User,
- 'direct_message': DirectMessage,
- 'friendship': Friendship,
- 'saved_search': SavedSearch,
- 'search_result': SearchResult,
- 'retweet': Retweet,
- 'list': List,
-}
+
+class ModelFactory(object):
+ """
+ Used by parsers for creating instances
+ of models. You may subclass this factory
+ to add your own extended models.
+ """
+
+ status = Status
+ user = User
+ direct_message = DirectMessage
+ friendship = Friendship
+ saved_search = SavedSearch
+ search_result = SearchResult
+ retweet = Retweet
+ list = List
from datetime import datetime
import time
-from tweepy.models import models
def _parse_cursor(obj):
def parse_user(obj, api):
- user = models['user']()
+ user = api.model_factory.user()
user._api = api
for k, v in obj.items():
if k == 'created_at':
def parse_status(obj, api):
- status = models['status']()
+ status = api.model_factory.status()
status._api = api
for k, v in obj.items():
if k == 'user':
def parse_dm(obj, api):
- dm = models['direct_message']()
+ dm = api.model_factory.direct_message()
dm._api = api
for k, v in obj.items():
if k == 'sender' or k == 'recipient':
relationship = obj['relationship']
# parse source
- source = models['friendship']()
+ source = api.model_factory.friendship()
for k, v in relationship['source'].items():
setattr(source, k, v)
# parse target
- target = models['friendship']()
+ target = api.model_factory.friendship()
for k, v in relationship['target'].items():
setattr(target, k, v)
def parse_saved_search(obj, api):
- ss = models['saved_search']()
+ ss = api.model_factory.saved_search()
ss._api = api
for k, v in obj.items():
if k == 'created_at':
def parse_saved_searches(obj, api):
saved_searches = []
- saved_search = models['saved_search']()
+ saved_search = api.model_factory.saved_search()
for item in obj:
saved_searches.append(parse_saved_search(item, api))
return saved_searches
def parse_search_result(obj, api):
- result = models['search_result']()
+ result = api.model_factory.search_result()
for k, v in obj.items():
if k == 'created_at':
setattr(result, k, _parse_search_datetime(v))
def parse_list(obj, api):
- lst = models['list']()
+ lst = api.model_factory.list()
lst._api = api
for k,v in obj.items():
if k == 'user':