From: Josh Roesslein Date: Tue, 29 Sep 2009 03:25:03 +0000 (-0500) Subject: Update parsers to handle cursor responses. API.friends_ids and API.followers_ids... X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=caec131b8ab611d0c8858631eb80dc75229f9281;p=tweepy.git Update parsers to handle cursor responses. API.friends_ids and API.followers_ids now return a list of integers. --- diff --git a/CHANGES b/CHANGES index c3576cb..83ec47f 100644 --- a/CHANGES +++ b/CHANGES @@ -6,9 +6,16 @@ during upgrade will be listed here. + API + Added cursor parameter to API.friends and API.followers methods. Note: page parameter is being deprecated by twitter on 10/26 + + Update parsing to handle cursor responses. + When using 'cursor' parameter, the API method will return + a tuple with this format: (data, next_cursor, prev_cursor) + Calls not using the 'cursor' parameter are not changed in the way they return. + + API.friends_ids and API.followers_ids now return a list of integers. + Parser updated to handle cursor responses. See above. + Cursor Added the Cursor object to help with pagination within the API. Please see the pagination tutorial for more details. + This is the recommended way for using the 'page' and 'cursor' parameters. 1.0.1 -> 1.1 ======================= diff --git a/tweepy/api.py b/tweepy/api.py index 34466ca..4ebfcd3 100644 --- a/tweepy/api.py +++ b/tweepy/api.py @@ -496,13 +496,13 @@ class API(object): Parameters: id or user_id or screen_name (One of these is required) cursor - Returns: json object + Returns: list http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-friends%C2%A0ids """ friends_ids = bind_api( path = '/friends/ids.json', - parser = parse_json, + parser = parse_ids, allowed_param = ['id', 'user_id', 'screen_name', 'cursor'] ) @@ -514,13 +514,13 @@ class API(object): Parameters: id or user_id or screen_name (One of these is required) cursor - Returns: json object + Returns: list http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-followers%C2%A0ids """ followers_ids = bind_api( path = '/followers/ids.json', - parser = parse_json, + parser = parse_ids, allowed_param = ['id', 'user_id', 'screen_name', 'cursor'] ) diff --git a/tweepy/binder.py b/tweepy/binder.py index 4e8e326..7b6c1cb 100644 --- a/tweepy/binder.py +++ b/tweepy/binder.py @@ -137,9 +137,20 @@ def bind_api(path, parser, allowed_param=None, method='GET', require_auth=False, except Exception: raise TweepError("Failed to parse json response text") + # Parse cursor infomation + if isinstance(jobject, dict): + next_cursor = jobject.get('next_cursor') + prev_cursor = jobject.get('previous_cursor') + else: + next_cursor = None + prev_cursor = None + # Pass json object into parser try: - out = parser(jobject, api) + if next_cursor is not None and prev_cursor is not None: + out = parser(jobject, api), next_cursor, prev_cursor + else: + out = parser(jobject, api) except Exception: raise TweepError("Failed to parse json object") diff --git a/tweepy/parsers.py b/tweepy/parsers.py index c17abce..daae037 100644 --- a/tweepy/parsers.py +++ b/tweepy/parsers.py @@ -6,6 +6,10 @@ from datetime import datetime from . models import models +def _parse_cursor(obj): + + return obj['next_cursor'], obj['prev_cursor'] + def parse_json(obj, api): return obj @@ -73,8 +77,13 @@ def parse_user(obj, api): def parse_users(obj, api): + if isinstance(obj, list) is False: + item_list = obj['users'] + else: + item_list = obj + users = [] - for item in obj: + for item in item_list: users.append(_parse_user(item, api)) return users @@ -157,6 +166,13 @@ def parse_friendship(obj, api): return source, target +def parse_ids(obj, api): + + if isinstance(obj, list) is False: + return obj['ids'] + else: + return obj + def _parse_saved_search(obj, api): ss = models['saved_search']()