From 470897f889d307c2a16df3124e57b5bb840090a7 Mon Sep 17 00:00:00 2001 From: Joshua Date: Sat, 30 Jan 2010 11:08:34 -0600 Subject: [PATCH] Added JSONParser which ModelParser now extends. This allows developers to access the raw JSON by using the JSONParser. Example: api = API(parser=JSONParser()) json = api.user_timeline('twitter') Also fixed the Cursor objects so they should be working again. --- tweepy/cursor.py | 3 ++- tweepy/parsers.py | 48 +++++++++++++++++++++++++++++++++++++---------- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/tweepy/cursor.py b/tweepy/cursor.py index 0f762f4..66637c4 100644 --- a/tweepy/cursor.py +++ b/tweepy/cursor.py @@ -56,9 +56,10 @@ class CursorIterator(BaseIterator): def next(self): if self.next_cursor == 0 or (self.limit and self.count == self.limit): raise StopIteration - data, self.next_cursor, self.prev_cursor = self.method( + data, cursors = self.method( cursor=self.next_cursor, *self.args, **self.kargs ) + self.prev_cursor, self.next_cursor = cursors if len(data) == 0: raise StopIteration self.count += 1 diff --git a/tweepy/parsers.py b/tweepy/parsers.py index 2f7177e..e56cbf1 100644 --- a/tweepy/parsers.py +++ b/tweepy/parsers.py @@ -8,18 +8,40 @@ from tweepy.utils import import_simplejson class Parser(object): + def parse(self, api, payload_type, payload_list, 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 + + +class JSONParser(Parser): + payload_format = 'json' + def __init__(self): + self.json_lib = import_simplejson() + def parse(self, api, payload_type, payload_list, payload): - """Parse the response payload and return the result.""" - raise NotImplementedError + try: + json = self.json_lib.loads(payload) + except Exception, e: + raise TweepError('Failed to parse JSON payload: %s' % e) + + if payload_list and isinstance(json, dict): + cursors = json['previous_cursor'], json['next_cursor'] + return json, cursors + else: + return json -class ModelParser(Parser): +class ModelParser(JSONParser): def __init__(self, model_factory=None): + JSONParser.__init__(self) self.model_factory = model_factory or ModelFactory - self.json_lib = import_simplejson() def parse(self, api, payload_type, payload_list, payload): try: @@ -28,13 +50,19 @@ class ModelParser(Parser): except AttributeError: raise TweepError('No model for this payload type: %s' % method.payload_type) - try: - json = self.json_lib.loads(payload) - except Exception, e: - raise TweepError('Failed to parse JSON: %s' % e) + json = JSONParser.parse(self, api, payload_type, payload_list, payload) + if isinstance(json, tuple): + json, cursors = json + else: + cursors = None if payload_list: - return model.parse_list(api, json) + result = model.parse_list(api, json) + else: + result = model.parse(api, json) + + if cursors: + return result, cursors else: - return model.parse(api, json) + return result -- 2.25.1