From: Josh Roesslein Date: Sun, 5 Jul 2009 20:12:12 +0000 (-0500) Subject: Implemented json parser. X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=33cf3157af561fd99e40fc9be1d7edff59c9ce59;p=tweepy.git Implemented json parser. --- 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