2
# Copyright 2009-2010 Joshua Roesslein
3
# See LICENSE for details.
5
from tweepy.models import ModelFactory
6
from tweepy.utils import import_simplejson
7
from tweepy.error import TweepError
12
def parse(self, method, payload):
14
Parse the response payload and return the result.
15
Returns a tuple that contains the result data and the cursors
16
(or None if not present).
18
raise NotImplementedError
20
def parse_error(self, payload):
22
Parse the error message from payload.
23
If unable to parse the message, throw an exception
24
and default error message will be used.
26
raise NotImplementedError
29
class JSONParser(Parser):
31
payload_format = 'json'
34
self.json_lib = import_simplejson()
36
def parse(self, method, payload):
38
json = self.json_lib.loads(payload)
40
raise TweepError('Failed to parse JSON payload: %s' % e)
42
if isinstance(json, dict) and 'previous_cursor' in json and 'next_cursor' in json:
43
cursors = json['previous_cursor'], json['next_cursor']
48
def parse_error(self, payload):
49
error = self.json_lib.loads(payload)
50
if error.has_key('error'):
53
return error['errors']
56
class ModelParser(JSONParser):
58
def __init__(self, model_factory=None):
59
JSONParser.__init__(self)
60
self.model_factory = model_factory or ModelFactory
62
def parse(self, method, payload):
64
if method.payload_type is None: return
65
model = getattr(self.model_factory, method.payload_type)
66
except AttributeError:
67
raise TweepError('No model for this payload type: %s' % method.payload_type)
69
json = JSONParser.parse(self, method, payload)
70
if isinstance(json, tuple):
75
if method.payload_list:
76
result = model.parse_list(method.api, json)
78
result = model.parse(method.api, json)
81
return result, cursors