Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 0fd129e866
Fetching contributors…

Cannot retrieve contributors at this time

98 lines (72 sloc) 2.622 kb
# Tweepy
# Copyright 2009-2010 Joshua Roesslein
# See LICENSE for details.
from tweepy.models import ModelFactory
from tweepy.utils import import_simplejson
from tweepy.error import TweepError
class Parser(object):
def parse(self, method, payload):
Parse the response payload and return the result.
Returns a tuple that contains the result data and the cursors
(or None if not present).
raise NotImplementedError
def parse_error(self, payload):
Parse the error message from payload.
If unable to parse the message, throw an exception
and default error message will be used.
raise NotImplementedError
class RawParser(Parser):
def __init__(self):
def parse(self, method, payload):
return payload
def parse_error(self, payload):
return payload
class JSONParser(Parser):
payload_format = 'json'
def __init__(self):
self.json_lib = import_simplejson()
def parse(self, method, payload):
json = self.json_lib.loads(payload)
except Exception, e:
raise TweepError('Failed to parse JSON payload: %s' % e)
needsCursors = method.parameters.has_key('cursor')
if needsCursors and isinstance(json, dict) and 'previous_cursor' in json and 'next_cursor' in json:
cursors = json['previous_cursor'], json['next_cursor']
return json, cursors
return json
def parse_error(self, payload):
error = self.json_lib.loads(payload)
if error.has_key('error'):
return error['error']
return error['errors']
class ModelParser(JSONParser):
def __init__(self, model_factory=None):
self.model_factory = model_factory or ModelFactory
def parse(self, method, payload):
if method.payload_type is None: return
model = getattr(self.model_factory, method.payload_type)
except AttributeError:
raise TweepError('No model for this payload type: %s' % method.payload_type)
json = JSONParser.parse(self, method, payload)
if isinstance(json, tuple):
json, cursors = json
cursors = None
if method.payload_list:
result = model.parse_list(method.api, json)
result = model.parse(method.api, json)
if cursors:
return result, cursors
return result
Jump to Line
Something went wrong with that request. Please try again.