From faeebad921d712b01d58d44c17bf7e46ff604bd2 Mon Sep 17 00:00:00 2001 From: s0ckz Date: Mon, 2 Jun 2014 13:02:40 -0300 Subject: [PATCH] Some corrections for twython cursor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It’s now working properly for search and get_user_timeline. I didn’t test other functions though. --- twython/api.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/twython/api.py b/twython/api.py index 1d23a35..91682e8 100644 --- a/twython/api.py +++ b/twython/api.py @@ -430,19 +430,15 @@ class Twython(EndpointsMixin, object): if function.iter_mode == 'cursor' and content['next_cursor_str'] == '0': raise StopIteration - try: - if function.iter_mode == 'id': - if not 'max_id' in params: - # Add 1 to the id because since_id and max_id are inclusive - if hasattr(function, 'iter_metadata'): - since_id = content[function.iter_metadata].get('since_id_str') - else: - since_id = content[0]['id_str'] - params['since_id'] = (int(since_id) - 1) - elif function.iter_mode == 'cursor': - params['cursor'] = content['next_cursor_str'] - except (TypeError, ValueError): # pragma: no cover - raise TwythonError('Unable to generate next page of search results, `page` is not a number.') + if function.iter_mode == 'id': + if hasattr(function, 'iter_key') and len(content[function.iter_key]) > 0: + params['max_id'] = min(content[function.iter_key], key=lambda i: i['id'])['id'] - 1 + elif not hasattr(function, 'iter_key') and len(content) > 0: + params['max_id'] = min(content, key=lambda i: i['id'])['id'] - 1 + else: + raise StopIteration + elif function.iter_mode == 'cursor': + params['cursor'] = content['next_cursor_str'] @staticmethod def unicode2utf8(text):