from tweepy.binder import bind_api
from tweepy.error import TweepError
-from tweepy.parsers import *
-from tweepy.models import ModelFactory
+from tweepy.parsers import ModelParser
class API(object):
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,
- model_factory=None):
+ parser=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
+ self.parser = parser or ModelParser()
""" statuses/public_timeline """
public_timeline = bind_api(
path = '/statuses/public_timeline.json',
- parser = parse_statuses,
+ payload_type = 'status', payload_list = True,
allowed_param = []
)
""" statuses/home_timeline """
home_timeline = bind_api(
path = '/statuses/home_timeline.json',
- parser = parse_statuses,
+ payload_type = 'status', payload_list = True,
allowed_param = ['since_id', 'max_id', 'count', 'page'],
require_auth = True
)
""" statuses/friends_timeline """
friends_timeline = bind_api(
path = '/statuses/friends_timeline.json',
- parser = parse_statuses,
+ payload_type = 'status', payload_list = True,
allowed_param = ['since_id', 'max_id', 'count', 'page'],
require_auth = True
)
""" statuses/user_timeline """
user_timeline = bind_api(
path = '/statuses/user_timeline.json',
- parser = parse_statuses,
+ payload_type = 'status', payload_list = True,
allowed_param = ['id', 'user_id', 'screen_name', 'since_id',
'max_id', 'count', 'page']
)
""" statuses/mentions """
mentions = bind_api(
path = '/statuses/mentions.json',
- parser = parse_statuses,
+ payload_type = 'status', payload_list = True,
allowed_param = ['since_id', 'max_id', 'count', 'page'],
require_auth = True
)
""" statuses/retweeted_by_me """
retweeted_by_me = bind_api(
path = '/statuses/retweeted_by_me.json',
- parser = parse_statuses,
+ payload_type = 'status', payload_list = True,
allowed_param = ['since_id', 'max_id', 'count', 'page'],
require_auth = True
)
""" statuses/retweeted_to_me """
retweeted_to_me = bind_api(
path = '/statuses/retweeted_to_me.json',
- parser = parse_statuses,
+ payload_type = 'status', payload_list = True,
allowed_param = ['since_id', 'max_id', 'count', 'page'],
require_auth = True
)
""" statuses/retweets_of_me """
retweets_of_me = bind_api(
path = '/statuses/retweets_of_me.json',
- parser = parse_statuses,
+ payload_type = 'status', payload_list = True,
allowed_param = ['since_id', 'max_id', 'count', 'page'],
require_auth = True
)
""" statuses/show """
get_status = bind_api(
path = '/statuses/show.json',
- parser = parse_status,
+ payload_type = 'status',
allowed_param = ['id']
)
update_status = bind_api(
path = '/statuses/update.json',
method = 'POST',
- parser = parse_status,
+ payload_type = 'status',
allowed_param = ['status', 'in_reply_to_status_id', 'lat', 'long', 'source'],
require_auth = True
)
destroy_status = bind_api(
path = '/statuses/destroy.json',
method = 'DELETE',
- parser = parse_status,
+ payload_type = 'status',
allowed_param = ['id'],
require_auth = True
)
retweet = bind_api(
path = '/statuses/retweet/{id}.json',
method = 'POST',
- parser = parse_status,
+ payload_type = 'status',
allowed_param = ['id'],
require_auth = True
)
""" statuses/retweets """
retweets = bind_api(
path = '/statuses/retweets/{id}.json',
- parser = parse_statuses,
+ payload_type = 'status', payload_list = True,
allowed_param = ['id', 'count'],
require_auth = True
)
""" users/show """
get_user = bind_api(
path = '/users/show.json',
- parser = parse_user,
+ payload_type = 'user',
allowed_param = ['id', 'user_id', 'screen_name']
)
""" users/search """
search_users = bind_api(
path = '/users/search.json',
- parser = parse_users,
+ payload_type = 'user', payload_list = True,
require_auth = True,
allowed_param = ['q', 'per_page', 'page']
)
""" statuses/friends """
friends = bind_api(
path = '/statuses/friends.json',
- parser = parse_users,
+ payload_type = 'user', payload_list = True,
allowed_param = ['id', 'user_id', 'screen_name', 'page', 'cursor']
)
""" statuses/followers """
followers = bind_api(
path = '/statuses/followers.json',
- parser = parse_users,
+ payload_type = 'user', payload_list = True,
allowed_param = ['id', 'user_id', 'screen_name', 'page', 'cursor']
)
""" direct_messages """
direct_messages = bind_api(
path = '/direct_messages.json',
- parser = parse_directmessages,
+ payload_type = 'direct_message', payload_list = True,
allowed_param = ['since_id', 'max_id', 'count', 'page'],
require_auth = True
)
""" direct_messages/sent """
sent_direct_messages = bind_api(
path = '/direct_messages/sent.json',
- parser = parse_directmessages,
+ payload_type = 'direct_message', payload_list = True,
allowed_param = ['since_id', 'max_id', 'count', 'page'],
require_auth = True
)
send_direct_message = bind_api(
path = '/direct_messages/new.json',
method = 'POST',
- parser = parse_dm,
+ payload_type = 'direct_message',
allowed_param = ['user', 'screen_name', 'user_id', 'text'],
require_auth = True
)
destroy_direct_message = bind_api(
path = '/direct_messages/destroy.json',
method = 'DELETE',
- parser = parse_dm,
+ payload_type = 'direct_message',
allowed_param = ['id'],
require_auth = True
)
create_friendship = bind_api(
path = '/friendships/create.json',
method = 'POST',
- parser = parse_user,
+ payload_type = 'user',
allowed_param = ['id', 'user_id', 'screen_name', 'follow'],
require_auth = True
)
destroy_friendship = bind_api(
path = '/friendships/destroy.json',
method = 'DELETE',
- parser = parse_user,
+ payload_type = 'user',
allowed_param = ['id', 'user_id', 'screen_name'],
require_auth = True
)
""" friendships/exists """
exists_friendship = bind_api(
path = '/friendships/exists.json',
- parser = parse_json,
+ payload_type = 'json',
allowed_param = ['user_a', 'user_b']
)
""" friendships/show """
show_friendship = bind_api(
path = '/friendships/show.json',
- parser = parse_friendship,
+ payload_type = 'friendship',
allowed_param = ['source_id', 'source_screen_name',
'target_id', 'target_screen_name']
)
""" friends/ids """
friends_ids = bind_api(
path = '/friends/ids.json',
- parser = parse_ids,
+ payload_type = 'ids',
allowed_param = ['id', 'user_id', 'screen_name', 'cursor']
)
""" followers/ids """
followers_ids = bind_api(
path = '/followers/ids.json',
- parser = parse_ids,
+ payload_type = 'ids',
allowed_param = ['id', 'user_id', 'screen_name', 'cursor']
)
try:
return bind_api(
path = '/account/verify_credentials.json',
- parser = parse_user,
+ payload_type = 'user',
require_auth = True
)(self)
except TweepError:
""" account/rate_limit_status """
rate_limit_status = bind_api(
path = '/account/rate_limit_status.json',
- parser = parse_json
+ payload_type = 'json'
)
""" account/update_delivery_device """
path = '/account/update_delivery_device.json',
method = 'POST',
allowed_param = ['device'],
- parser = parse_user,
+ payload_type = 'user',
require_auth = True
)
update_profile_colors = bind_api(
path = '/account/update_profile_colors.json',
method = 'POST',
- parser = parse_user,
+ payload_type = 'user',
allowed_param = ['profile_background_color', 'profile_text_color',
'profile_link_color', 'profile_sidebar_fill_color',
'profile_sidebar_border_color'],
return bind_api(
path = '/account/update_profile_image.json',
method = 'POST',
- parser = parse_user,
+ payload_type = 'user',
require_auth = True
)(self, post_data=post_data, headers=headers)
bind_api(
path = '/account/update_profile_background_image.json',
method = 'POST',
- parser = parse_user,
+ payload_type = 'user',
allowed_param = ['tile'],
require_auth = True
)(self, post_data=post_data, headers=headers)
update_profile = bind_api(
path = '/account/update_profile.json',
method = 'POST',
- parser = parse_user,
+ payload_type = 'user',
allowed_param = ['name', 'url', 'location', 'description'],
require_auth = True
)
""" favorites """
favorites = bind_api(
path = '/favorites.json',
- parser = parse_statuses,
+ payload_type = 'status', payload_list = True,
allowed_param = ['id', 'page']
)
create_favorite = bind_api(
path = '/favorites/create/{id}.json',
method = 'POST',
- parser = parse_status,
+ payload_type = 'status',
allowed_param = ['id'],
require_auth = True
)
destroy_favorite = bind_api(
path = '/favorites/destroy/{id}.json',
method = 'DELETE',
- parser = parse_status,
+ payload_type = 'status',
allowed_param = ['id'],
require_auth = True
)
enable_notifications = bind_api(
path = '/notifications/follow.json',
method = 'POST',
- parser = parse_user,
+ payload_type = 'user',
allowed_param = ['id', 'user_id', 'screen_name'],
require_auth = True
)
disable_notifications = bind_api(
path = '/notifications/leave.json',
method = 'POST',
- parser = parse_user,
+ payload_type = 'user',
allowed_param = ['id', 'user_id', 'screen_name'],
require_auth = True
)
create_block = bind_api(
path = '/blocks/create.json',
method = 'POST',
- parser = parse_user,
+ payload_type = 'user',
allowed_param = ['id', 'user_id', 'screen_name'],
require_auth = True
)
destroy_block = bind_api(
path = '/blocks/destroy.json',
method = 'DELETE',
- parser = parse_user,
+ payload_type = 'user',
allowed_param = ['id', 'user_id', 'screen_name'],
require_auth = True
)
try:
bind_api(
path = '/blocks/exists.json',
- parser = parse_none,
allowed_param = ['id', 'user_id', 'screen_name'],
require_auth = True
)(self, *args, **kargs)
""" blocks/blocking """
blocks = bind_api(
path = '/blocks/blocking.json',
- parser = parse_users,
+ payload_type = 'user', payload_list = True,
allowed_param = ['page'],
require_auth = True
)
""" blocks/blocking/ids """
blocks_ids = bind_api(
path = '/blocks/blocking/ids.json',
- parser = parse_json,
+ payload_type = 'json',
require_auth = True
)
report_spam = bind_api(
path = '/report_spam.json',
method = 'POST',
- parser = parse_user,
+ payload_type = 'user',
allowed_param = ['id', 'user_id', 'screen_name'],
require_auth = True
)
""" saved_searches """
saved_searches = bind_api(
path = '/saved_searches.json',
- parser = parse_saved_searches,
+ payload_type = 'saved_search', payload_list = True,
require_auth = True
)
""" saved_searches/show """
get_saved_search = bind_api(
path = '/saved_searches/show/{id}.json',
- parser = parse_saved_search,
+ payload_type = 'saved_search',
allowed_param = ['id'],
require_auth = True
)
create_saved_search = bind_api(
path = '/saved_searches/create.json',
method = 'POST',
- parser = parse_saved_search,
+ payload_type = 'saved_search',
allowed_param = ['query'],
require_auth = True
)
destroy_saved_search = bind_api(
path = '/saved_searches/destroy/{id}.json',
method = 'DELETE',
- parser = parse_saved_search,
+ payload_type = 'saved_search',
allowed_param = ['id'],
require_auth = True
)
""" help/test """
def test(self):
try:
- return bind_api(
+ bind_api(
path = '/help/test.json',
- parser = parse_return_true
)(self)
except TweepError:
return False
+ return True
def create_list(self, *args, **kargs):
return bind_api(
path = '/%s/lists.json' % self.auth.get_username(),
method = 'POST',
- parser = parse_list,
+ payload_type = 'list',
allowed_param = ['name', 'mode', 'description'],
require_auth = True
)(self, *args, **kargs)
return bind_api(
path = '/%s/lists/%s.json' % (self.auth.get_username(), slug),
method = 'DELETE',
- parser = parse_list,
+ payload_type = 'list',
require_auth = True
)(self)
return bind_api(
path = '/%s/lists/%s.json' % (self.auth.get_username(), slug),
method = 'POST',
- parser = parse_list,
+ payload_type = 'list',
allowed_param = ['name', 'mode', 'description'],
require_auth = True
)(self, *args, **kargs)
lists = bind_api(
path = '/{user}/lists.json',
- parser = parse_lists,
+ payload_type = 'list', payload_list = True,
allowed_param = ['user', 'cursor'],
require_auth = True
)
lists_memberships = bind_api(
path = '/{user}/lists/memberships.json',
- parser = parse_lists,
+ payload_type = 'list', payload_list = True,
allowed_param = ['user', 'cursor'],
require_auth = True
)
lists_subscriptions = bind_api(
path = '/{user}/lists/subscriptions.json',
- parser = parse_lists,
+ payload_type = 'list', payload_list = True,
allowed_param = ['user', 'cursor'],
require_auth = True
)
list_timeline = bind_api(
path = '/{owner}/lists/{slug}/statuses.json',
- parser = parse_statuses,
+ payload_type = 'status', payload_list = True,
allowed_param = ['owner', 'slug', 'since_id', 'max_id', 'count', 'page']
)
get_list = bind_api(
path = '/{owner}/lists/{slug}.json',
- parser = parse_list,
+ payload_type = 'list',
allowed_param = ['owner', 'slug']
)
return bind_api(
path = '/%s/%s/members.json' % (self.auth.get_username(), slug),
method = 'POST',
- parser = parse_list,
+ payload_type = 'list',
allowed_param = ['id'],
require_auth = True
)(self, *args, **kargs)
return bind_api(
path = '/%s/%s/members.json' % (self.auth.get_username(), slug),
method = 'DELETE',
- parser = parse_list,
+ payload_type = 'list',
allowed_param = ['id'],
require_auth = True
)(self, *args, **kargs)
list_members = bind_api(
path = '/{owner}/{slug}/members.json',
- parser = parse_users,
+ payload_type = 'user', payload_list = True,
allowed_param = ['owner', 'slug', 'cursor']
)
try:
return bind_api(
path = '/%s/%s/members/%s.json' % (owner, slug, user_id),
- parser = parse_user
+ payload_type = 'user'
)(self)
except TweepError:
return False
subscribe_list = bind_api(
path = '/{owner}/{slug}/subscribers.json',
method = 'POST',
- parser = parse_list,
+ payload_type = 'list',
allowed_param = ['owner', 'slug'],
require_auth = True
)
unsubscribe_list = bind_api(
path = '/{owner}/{slug}/subscribers.json',
method = 'DELETE',
- parser = parse_list,
+ payload_type = 'list',
allowed_param = ['owner', 'slug'],
require_auth = True
)
list_subscribers = bind_api(
path = '/{owner}/{slug}/subscribers.json',
- parser = parse_users,
+ payload_type = 'user', payload_list = True,
allowed_param = ['owner', 'slug', 'cursor']
)
try:
return bind_api(
path = '/%s/%s/subscribers/%s.json' % (owner, slug, user_id),
- parser = parse_user
+ payload_type = 'user'
)(self)
except TweepError:
return False
- """ trends/available [coming soon] """
+ """ trends/available """
trends_available = bind_api(
path = '/trends/available.json',
- parser = parse_json,
+ payload_type = 'json',
allowed_param = ['lat', 'long']
)
- """ trends/location [coming soon] """
+ """ trends/location """
trends_location = bind_api(
path = '/trends/{woeid}.json',
- parser = parse_json,
+ payload_type = 'json',
allowed_param = ['woeid']
)
search = bind_api(
search_api = True,
path = '/search.json',
- parser = parse_search_results,
+ payload_type = 'search_result', payload_list = True,
allowed_param = ['q', 'lang', 'locale', 'rpp', 'page', 'since_id', 'geocode', 'show_user']
)
search.pagination_mode = 'page'
trends = bind_api(
search_api = True,
path = '/trends.json',
- parser = parse_json
+ payload_type = 'json'
)
""" trends/current """
trends_current = bind_api(
search_api = True,
path = '/trends/current.json',
- parser = parse_json,
+ payload_type = 'json',
allowed_param = ['exclude']
)
trends_daily = bind_api(
search_api = True,
path = '/trends/daily.json',
- parser = parse_json,
+ payload_type = 'json',
allowed_param = ['date', 'exclude']
)
trends_weekly = bind_api(
search_api = True,
path = '/trends/weekly.json',
- parser = parse_json,
+ payload_type = 'json',
allowed_param = ['date', 'exclude']
)
# See LICENSE
from tweepy.error import TweepError
+from tweepy.utils import parse_datetime, parse_html_value, parse_a_href, \
+ parse_search_datetime, unescape_html
+
+
+class ResultSet(list):
+ """A list like object that holds results from a Twitter API query."""
class Model(object):
+ def __init__(self, api=None):
+ self._api = api
+
def __getstate__(self):
# pickle
pickle = {}
pickle[k] = v
return pickle
+ @classmethod
+ def parse(cls, api, json):
+ """Parse a JSON object into a model instance."""
+ raise NotImplementedError
+
+ @classmethod
+ def parse_list(cls, api, json_list):
+ """Parse a list of JSON objects into a result set of model instances."""
+ results = ResultSet()
+ for obj in json_list:
+ results.append(cls.parse(api, obj))
+ return results
+
class Status(Model):
+ @classmethod
+ def parse(cls, api, json):
+ status = cls(api)
+ for k, v in json.items():
+ if k == 'user':
+ user = User.parse(api, v)
+ setattr(status, 'author', user)
+ setattr(status, 'user', user) # DEPRECIATED
+ elif k == 'created_at':
+ setattr(status, k, parse_datetime(v))
+ elif k == 'source':
+ if '<' in v:
+ setattr(status, k, parse_html_value(v))
+ setattr(status, 'source_url', parse_a_href(v))
+ else:
+ setattr(status, k, v)
+ elif k == 'retweeted_status':
+ setattr(status, k, User.parse(api, v))
+ else:
+ setattr(status, k, v)
+ return status
+
def destroy(self):
return self._api.destroy_status(self.id)
class User(Model):
+ @classmethod
+ def parse(cls, api, json):
+ user = cls(api)
+ for k, v in json.items():
+ if k == 'created_at':
+ setattr(user, k, parse_datetime(v))
+ elif k == 'status':
+ setattr(user, k, Status.parse(api, v))
+ elif k == 'following':
+ # twitter sets this to null if it is false
+ if v is True:
+ setattr(user, k, True)
+ else:
+ setattr(user, k, False)
+ else:
+ setattr(user, k, v)
+ return user
+
+ @classmethod
+ def parse_list(cls, api, json_list):
+ if isinstance(json_list, list):
+ item_list = json_list
+ else:
+ item_list = json_list['users']
+
+ results = ResultSet()
+ for obj in item_list:
+ results.append(cls.parse(api, obj))
+ return results
+
def timeline(self, **kargs):
return self._api.user_timeline(user_id=self.id, **kargs)
class DirectMessage(Model):
+ @classmethod
+ def parse(cls, api, json):
+ dm = cls(api)
+ for k, v in json.items():
+ if k == 'sender' or k == 'recipient':
+ setattr(dm, k, User.parse(api, v))
+ elif k == 'created_at':
+ setattr(dm, k, parse_datetime(v))
+ else:
+ setattr(dm, k, v)
+ return dm
+
def destroy(self):
return self._api.destroy_direct_message(self.id)
class Friendship(Model):
- pass
+ @classmethod
+ def parse(cls, api, json):
+ relationship = json['relationship']
+
+ # parse source
+ source = cls(api)
+ for k, v in relationship['source'].items():
+ setattr(source, k, v)
+
+ # parse target
+ target = cls(api)
+ for k, v in relationship['target'].items():
+ setattr(target, k, v)
+
+ return source, target
class SavedSearch(Model):
+ @classmethod
+ def parse(cls, api, json):
+ ss = cls(api)
+ for k, v in json.items():
+ if k == 'created_at':
+ setattr(ss, k, parse_datetime(v))
+ else:
+ setattr(ss, k, v)
+ return ss
+
def destroy(self):
return self._api.destroy_saved_search(self.id)
class SearchResult(Model):
- pass
+ @classmethod
+ def parse(cls, api, json):
+ result = cls()
+ for k, v in json.items():
+ if k == 'created_at':
+ setattr(result, k, parse_search_datetime(v))
+ elif k == 'source':
+ setattr(result, k, parse_html_value(unescape_html(v)))
+ else:
+ setattr(result, k, v)
+ return result
+
+ @classmethod
+ def parse_list(cls, api, json_list, result_set=None):
+ results = ResultSet()
+ results.max_id = json_list.get('max_id')
+ results.since_id = json_list.get('since_id')
+ results.refresh_url = json_list.get('refresh_url')
+ results.next_page = json_list.get('next_page')
+ results.results_per_page = json_list.get('results_per_page')
+ results.page = json_list.get('page')
+ results.completed_in = json_list.get('completed_in')
+ results.query = json_list.get('query')
+
+ for obj in json_list['results']:
+ results.append(cls.parse(api, obj))
+ return results
+
class Retweet(Model):
+ #TODO: remove me
def destroy(self):
return self._api.destroy_status(self.id)
class List(Model):
+ @classmethod
+ def parse(cls, api, json):
+ lst = List(api)
+ for k,v in json.items():
+ if k == 'user':
+ setattr(lst, k, User.parse(api, v))
+ else:
+ setattr(lst, k, v)
+ return lst
+
+ @classmethod
+ def parse_list(cls, api, json_list, result_set=None):
+ results = ResultSet()
+ for obj in json_list['lists']:
+ results.append(cls.parse(api, obj))
+ return results
+
def update(self, **kargs):
return self._api.update_list(self.slug, **kargs)
return self._api.is_subscribed_list(self.user.screen_name, self.slug, id)
+class JSONModel(Model):
+
+ @classmethod
+ def parse(cls, api, json):
+ return json
+
+
+class IDModel(Model):
+
+ @classmethod
+ def parse(cls, api, json):
+ if isinstance(json, list):
+ return json
+ else:
+ return json['ids']
+
+
class ModelFactory(object):
"""
Used by parsers for creating instances
retweet = Retweet
list = List
+ json = JSONModel
+ ids = IDModel
+
# Copyright 2009 Joshua Roesslein
# See LICENSE
-import htmlentitydefs
-import re
-from datetime import datetime
-import time
+from tweepy.models import ModelFactory
+from tweepy.utils import import_simplejson
-class ResultSet(list):
- """A list like object that holds results from a Twitter API query."""
+class Parser(object):
+ payload_format = 'json'
-def _parse_cursor(obj):
+ def parse(self, api, payload_type, payload_list, payload):
+ """Parse the response payload and return the result."""
+ raise NotImplementedError
- return obj['next_cursor'], obj['prev_cursor']
-def parse_json(obj, api):
+class ModelParser(Parser):
- return obj
+ def __init__(self, model_factory=None):
+ self.model_factory = model_factory or ModelFactory
+ self.json_lib = import_simplejson()
+ def parse(self, api, payload_type, payload_list, payload):
+ try:
+ if payload_type is None: return
+ model = getattr(self.model_factory, payload_type)
+ except AttributeError:
+ raise TweepError('No model for this payload type: %s' % method.payload_type)
-def parse_return_true(obj, api):
+ try:
+ json = self.json_lib.loads(payload)
+ except Exception, e:
+ raise TweepError('Failed to parse JSON: %s' % e)
- return True
-
-
-def parse_none(obj, api):
-
- return None
-
-
-def parse_error(obj):
-
- return obj['error']
-
-
-def _parse_datetime(str):
-
- # We must parse datetime this way to work in python 2.4
- return datetime(*(time.strptime(str, '%a %b %d %H:%M:%S +0000 %Y')[0:6]))
-
-
-def _parse_search_datetime(str):
-
- # python 2.4
- return datetime(*(time.strptime(str, '%a, %d %b %Y %H:%M:%S +0000')[0:6]))
-
-
-def unescape_html(text):
- """Created by Fredrik Lundh (http://effbot.org/zone/re-sub.htm#unescape-html)"""
- def fixup(m):
- text = m.group(0)
- if text[:2] == "&#":
- # character reference
- try:
- if text[:3] == "&#x":
- return unichr(int(text[3:-1], 16))
- else:
- return unichr(int(text[2:-1]))
- except ValueError:
- pass
- else:
- # named entity
- try:
- text = unichr(htmlentitydefs.name2codepoint[text[1:-1]])
- except KeyError:
- pass
- return text # leave as is
- return re.sub("&#?\w+;", fixup, text)
-
-
-def _parse_html_value(html):
-
- return html[html.find('>')+1:html.rfind('<')]
-
-
-def _parse_a_href(atag):
-
- start = atag.find('"') + 1
- end = atag.find('"', start)
- return atag[start:end]
-
-
-def parse_user(obj, api):
-
- user = api.model_factory.user()
- user._api = api
- for k, v in obj.items():
- if k == 'created_at':
- setattr(user, k, _parse_datetime(v))
- elif k == 'status':
- setattr(user, k, parse_status(v, api))
- elif k == 'following':
- # twitter sets this to null if it is false
- if v is True:
- setattr(user, k, True)
- else:
- setattr(user, k, False)
- else:
- setattr(user, k, v)
- return user
-
-
-def parse_users(obj, api):
-
- if isinstance(obj, list) is False:
- item_list = obj['users']
- else:
- item_list = obj
-
- users = ResultSet()
- for item in item_list:
- if item is None: break # sometimes an empty list with a null in it
- users.append(parse_user(item, api))
- return users
-
-
-def parse_status(obj, api):
-
- status = api.model_factory.status()
- status._api = api
- for k, v in obj.items():
- if k == 'user':
- user = parse_user(v, api)
- setattr(status, 'author', user)
- setattr(status, 'user', user) # DEPRECIATED
- elif k == 'created_at':
- setattr(status, k, _parse_datetime(v))
- elif k == 'source':
- if '<' in v:
- setattr(status, k, _parse_html_value(v))
- setattr(status, 'source_url', _parse_a_href(v))
- else:
- setattr(status, k, v)
- elif k == 'retweeted_status':
- setattr(status, k, parse_status(v, api))
+ if payload_list:
+ return model.parse_list(api, json)
else:
- setattr(status, k, v)
- return status
-
-
-def parse_statuses(obj, api):
-
- statuses = ResultSet()
- for item in obj:
- statuses.append(parse_status(item, api))
- return statuses
-
-
-def parse_dm(obj, api):
-
- dm = api.model_factory.direct_message()
- dm._api = api
- for k, v in obj.items():
- if k == 'sender' or k == 'recipient':
- setattr(dm, k, parse_user(v, api))
- elif k == 'created_at':
- setattr(dm, k, _parse_datetime(v))
- else:
- setattr(dm, k, v)
- return dm
-
-
-def parse_directmessages(obj, api):
-
- directmessages = ResultSet()
- for item in obj:
- directmessages.append(parse_dm(item, api))
- return directmessages
-
-
-def parse_friendship(obj, api):
-
- relationship = obj['relationship']
-
- # parse source
- source = api.model_factory.friendship()
- for k, v in relationship['source'].items():
- setattr(source, k, v)
-
- # parse target
- target = api.model_factory.friendship()
- for k, v in relationship['target'].items():
- setattr(target, k, v)
-
- return source, target
-
-
-def parse_ids(obj, api):
-
- if isinstance(obj, list) is False:
- return obj['ids']
- else:
- return obj
-
-def parse_saved_search(obj, api):
-
- ss = api.model_factory.saved_search()
- ss._api = api
- for k, v in obj.items():
- if k == 'created_at':
- setattr(ss, k, _parse_datetime(v))
- else:
- setattr(ss, k, v)
- return ss
-
-
-def parse_saved_searches(obj, api):
-
- saved_searches = ResultSet()
- 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 = api.model_factory.search_result()
- for k, v in obj.items():
- if k == 'created_at':
- setattr(result, k, _parse_search_datetime(v))
- elif k == 'source':
- setattr(result, k, _parse_html_value(unescape_html(v)))
- else:
- setattr(result, k, v)
- return result
-
-
-def parse_search_results(obj, api):
-
- results = ResultSet()
- results.max_id = obj.get('max_id')
- results.since_id = obj.get('since_id')
- results.refresh_url = obj.get('refresh_url')
- results.next_page = obj.get('next_page')
- results.results_per_page = obj.get('results_per_page')
- results.page = obj.get('page')
- results.completed_in = obj.get('completed_in')
- results.query = obj.get('query')
-
- for item in obj['results']:
- results.append(parse_search_result(item, api))
- return results
-
-
-def parse_list(obj, api):
-
- lst = api.model_factory.list()
- lst._api = api
- for k,v in obj.items():
- if k == 'user':
- setattr(lst, k, parse_user(v, api))
- else:
- setattr(lst, k, v)
- return lst
-
-def parse_lists(obj, api):
-
- lists = ResultSet()
- for item in obj['lists']:
- lists.append(parse_list(item, api))
- return lists
+ return model.parse(api, json)