Merge pull request #386 from eoso/cursor-fix

Cursor fix
This commit is contained in:
Mike Helmick 2018-05-07 11:35:25 -04:00 committed by GitHub
commit 62e45e9637
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 9 deletions

View file

@ -19,6 +19,7 @@ from requests_oauthlib import OAuth1, OAuth2
from . import __version__ from . import __version__
from .advisory import TwythonDeprecationWarning from .advisory import TwythonDeprecationWarning
from .compat import json, urlencode, parse_qsl, quote_plus, str, is_py2 from .compat import json, urlencode, parse_qsl, quote_plus, str, is_py2
from .compat import urlsplit
from .endpoints import EndpointsMixin from .endpoints import EndpointsMixin
from .exceptions import TwythonError, TwythonAuthError, TwythonRateLimitError from .exceptions import TwythonError, TwythonAuthError, TwythonRateLimitError
from .helpers import _transparent_params from .helpers import _transparent_params
@ -507,19 +508,27 @@ class Twython(EndpointsMixin, object):
try: try:
if function.iter_mode == 'id': if function.iter_mode == 'id':
if 'max_id' not in params: # Set max_id in params to one less than lowest tweet id
# Add 1 to the id because since_id and if hasattr(function, 'iter_metadata'):
# max_id are inclusive # Get supplied next max_id
if hasattr(function, 'iter_metadata'): metadata = content.get(function.iter_metadata)
since_id = content[function.iter_metadata].get('since_id_str') if 'next_results' in metadata:
next_results = urlsplit(metadata['next_results'])
params = dict(parse_qsl(next_results.query))
else: else:
since_id = content[0]['id_str'] # No more results
params['since_id'] = (int(since_id) - 1) raise StopIteration
else:
# Twitter gives tweets in reverse chronological order:
params['max_id'] = str(int(content[-1]['id_str']) - 1)
elif function.iter_mode == 'cursor': elif function.iter_mode == 'cursor':
params['cursor'] = content['next_cursor_str'] params['cursor'] = content['next_cursor_str']
except (TypeError, ValueError): # pragma: no cover except (TypeError, ValueError): # pragma: no cover
raise TwythonError('Unable to generate next page of search \ raise TwythonError('Unable to generate next page of search \
results, `page` is not a number.') results, `page` is not a number.')
except (KeyError, AttributeError): #pragma no cover
raise TwythonError('Unable to generate next page of search \
results, content has unexpected structure.')
@staticmethod @staticmethod
def unicode2utf8(text): def unicode2utf8(text):

View file

@ -25,7 +25,7 @@ except ImportError:
if is_py2: if is_py2:
from urllib import urlencode, quote_plus from urllib import urlencode, quote_plus
from urlparse import parse_qsl from urlparse import parse_qsl, urlsplit
str = unicode str = unicode
basestring = basestring basestring = basestring
@ -33,7 +33,7 @@ if is_py2:
elif is_py3: elif is_py3:
from urllib.parse import urlencode, quote_plus, parse_qsl from urllib.parse import urlencode, quote_plus, parse_qsl, urlsplit
str = str str = str
basestring = (str, bytes) basestring = (str, bytes)