From 33cf3157af561fd99e40fc9be1d7edff59c9ce59 Mon Sep 17 00:00:00 2001 From: Josh Roesslein Date: Sun, 5 Jul 2009 15:12:12 -0500 Subject: [PATCH] Implemented json parser. --- api.py | 8 ++++---- parsers.py | 21 +++++++++++++++++++-- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/api.py b/api.py index 163c740..ad25b61 100644 --- a/api.py +++ b/api.py @@ -1,8 +1,8 @@ import urllib import urllib2 -from misc import * -from models import * +from misc import TweepError, require_auth +from models import Status, User from parsers import * """ @@ -30,7 +30,7 @@ class API(object): self._opener = self._build_opener(username, password) def public_timeline(self): - return self._fetch('statuses/public_timelinee.json') + return parse_list(self._Status, self._fetch('statuses/public_timeline.json')) @require_auth def friends_timeline(self, since_id=None, max_id=None, count=None, page=None): @@ -61,6 +61,6 @@ class API(object): # Send request try: - return self._opener.open(req) + return self._opener.open(req).read() except urllib2.HTTPError, e: raise TweepError(parse_error(e.read())) diff --git a/parsers.py b/parsers.py index 0d5cad4..45e45a0 100644 --- a/parsers.py +++ b/parsers.py @@ -3,6 +3,23 @@ try: except ImportError: import simplejson as json -def parse_error(jsondata): +def parse_error(data): - return json.loads(jsondata)['error'] + return json.loads(data)['error'] + +def parse_item(type, data): + t = type() + for k,v in json.loads(data).items(): + setattr(t, k, v) + return t + +def parse_list(type, data): + types = [] + + for obj in json.loads(data): + t = type() + for k,v in obj.items(): + setattr(t, k, v) + types.append(t) + + return types -- 2.25.1