|
19 | 19 | from . import __version__
|
20 | 20 | from .advisory import TwythonDeprecationWarning
|
21 | 21 | from .compat import json, urlencode, parse_qsl, quote_plus, str, is_py2
|
| 22 | +from .compat import urlsplit |
22 | 23 | from .endpoints import EndpointsMixin
|
23 | 24 | from .exceptions import TwythonError, TwythonAuthError, TwythonRateLimitError
|
24 | 25 | from .helpers import _transparent_params
|
@@ -507,19 +508,27 @@ def cursor(self, function, return_pages=False, **params):
|
507 | 508 |
|
508 | 509 | try:
|
509 | 510 | 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)) |
515 | 518 | 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) |
518 | 524 | elif function.iter_mode == 'cursor':
|
519 | 525 | params['cursor'] = content['next_cursor_str']
|
520 | 526 | except (TypeError, ValueError): # pragma: no cover
|
521 | 527 | raise TwythonError('Unable to generate next page of search \
|
522 | 528 | 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.') |
523 | 532 |
|
524 | 533 | @staticmethod
|
525 | 534 | def unicode2utf8(text):
|
|
0 commit comments