Commit caec131b authored by Josh Roesslein's avatar Josh Roesslein
Browse files

Update parsers to handle cursor responses. API.friends_ids and...

Update parsers to handle cursor responses. API.friends_ids and API.followers_ids now return a list of integers.
parent d0466327
......@@ -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
=======================
......
......@@ -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<int>
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<int>
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']
)
......
......@@ -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")
......
......@@ -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']()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment