r"""Returns a generator for results that match a specified query. :param function: Instance of a Twython function (Twython.get_home_timeline, Twython.search) :param \*\*params: Extra parameters to send with your request (usually parameters accepted by the Twitter API
(self, function, return_pages=False, **params)
| 470 | return self.cursor(self.search, q=search_query, **params) |
| 471 | |
| 472 | def cursor(self, function, return_pages=False, **params): |
| 473 | r"""Returns a generator for results that match a specified query. |
| 474 | |
| 475 | :param function: Instance of a Twython function |
| 476 | (Twython.get_home_timeline, Twython.search) |
| 477 | :param \*\*params: Extra parameters to send with your request |
| 478 | (usually parameters accepted by the Twitter API endpoint) |
| 479 | :rtype: generator |
| 480 | |
| 481 | Usage:: |
| 482 | |
| 483 | >>> from twython import Twython |
| 484 | >>> twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, |
| 485 | OAUTH_TOKEN_SECRET) |
| 486 | |
| 487 | >>> results = twitter.cursor(twitter.search, q='python') |
| 488 | >>> for result in results: |
| 489 | >>> print result |
| 490 | |
| 491 | """ |
| 492 | if not callable(function): |
| 493 | raise TypeError('.cursor() takes a Twython function as its first \ |
| 494 | argument. Did you provide the result of a \ |
| 495 | function call?') |
| 496 | |
| 497 | if not hasattr(function, 'iter_mode'): |
| 498 | raise TwythonError('Unable to create generator for Twython \ |
| 499 | method "%s"' % function.__name__) |
| 500 | |
| 501 | while True: |
| 502 | content = function(**params) |
| 503 | |
| 504 | if not content: |
| 505 | return |
| 506 | |
| 507 | if hasattr(function, 'iter_key'): |
| 508 | results = content.get(function.iter_key) |
| 509 | else: |
| 510 | results = content |
| 511 | |
| 512 | if return_pages: |
| 513 | yield results |
| 514 | else: |
| 515 | for result in results: |
| 516 | yield result |
| 517 | |
| 518 | if function.iter_mode == 'cursor' and \ |
| 519 | content['next_cursor_str'] == '0': |
| 520 | return |
| 521 | |
| 522 | try: |
| 523 | if function.iter_mode == 'id': |
| 524 | # Set max_id in params to one less than lowest tweet id |
| 525 | if hasattr(function, 'iter_metadata'): |
| 526 | # Get supplied next max_id |
| 527 | metadata = content.get(function.iter_metadata) |
| 528 | if 'next_results' in metadata: |
| 529 | next_results = urlsplit(metadata['next_results']) |