Skip to content

Commit 62e45e9

Browse files
Merge pull request #386 from eoso/cursor-fix
Cursor fix
2 parents 677849a + 0b3df41 commit 62e45e9

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

twython/api.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from . import __version__
2020
from .advisory import TwythonDeprecationWarning
2121
from .compat import json, urlencode, parse_qsl, quote_plus, str, is_py2
22+
from .compat import urlsplit
2223
from .endpoints import EndpointsMixin
2324
from .exceptions import TwythonError, TwythonAuthError, TwythonRateLimitError
2425
from .helpers import _transparent_params
@@ -507,19 +508,27 @@ def cursor(self, function, return_pages=False, **params):
507508

508509
try:
509510
if function.iter_mode == 'id':
510-
if 'max_id' not in params:
511-
# Add 1 to the id because since_id and
512-
# max_id are inclusive
513-
if hasattr(function, 'iter_metadata'):
514-
since_id = content[function.iter_metadata].get('since_id_str')
511+
# Set max_id in params to one less than lowest tweet id
512+
if hasattr(function, 'iter_metadata'):
513+
# Get supplied next max_id
514+
metadata = content.get(function.iter_metadata)
515+
if 'next_results' in metadata:
516+
next_results = urlsplit(metadata['next_results'])
517+
params = dict(parse_qsl(next_results.query))
515518
else:
516-
since_id = content[0]['id_str']
517-
params['since_id'] = (int(since_id) - 1)
519+
# No more results
520+
raise StopIteration
521+
else:
522+
# Twitter gives tweets in reverse chronological order:
523+
params['max_id'] = str(int(content[-1]['id_str']) - 1)
518524
elif function.iter_mode == 'cursor':
519525
params['cursor'] = content['next_cursor_str']
520526
except (TypeError, ValueError): # pragma: no cover
521527
raise TwythonError('Unable to generate next page of search \
522528
results, `page` is not a number.')
529+
except (KeyError, AttributeError): #pragma no cover
530+
raise TwythonError('Unable to generate next page of search \
531+
results, content has unexpected structure.')
523532

524533
@staticmethod
525534
def unicode2utf8(text):

twython/compat.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@
2525

2626
if is_py2:
2727
from urllib import urlencode, quote_plus
28-
from urlparse import parse_qsl
28+
from urlparse import parse_qsl, urlsplit
2929

3030
str = unicode
3131
basestring = basestring
3232
numeric_types = (int, long, float)
3333

3434

3535
elif is_py3:
36-
from urllib.parse import urlencode, quote_plus, parse_qsl
36+
from urllib.parse import urlencode, quote_plus, parse_qsl, urlsplit
3737

3838
str = str
3939
basestring = (str, bytes)

0 commit comments

Comments
 (0)