Merge cfb8edb489 into 36ef365d43
This commit is contained in:
commit
b644e7d212
1 changed files with 9 additions and 5 deletions
|
|
@ -9,6 +9,7 @@ Twitter Authentication, and miscellaneous methods that are useful when
|
||||||
dealing with the Twitter API
|
dealing with the Twitter API
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import time
|
||||||
import requests
|
import requests
|
||||||
from requests.auth import HTTPBasicAuth
|
from requests.auth import HTTPBasicAuth
|
||||||
from requests_oauthlib import OAuth1, OAuth2
|
from requests_oauthlib import OAuth1, OAuth2
|
||||||
|
|
@ -374,11 +375,12 @@ class Twython(EndpointsMixin, object):
|
||||||
)
|
)
|
||||||
return self.cursor(self.search, q=search_query, **params)
|
return self.cursor(self.search, q=search_query, **params)
|
||||||
|
|
||||||
def cursor(self, function, **params):
|
def cursor(self, function, sleep_seconds=0, **params):
|
||||||
"""Returns a generator for results that match a specified query.
|
"""Returns a generator for results that match a specified query.
|
||||||
|
|
||||||
:param function: Instance of a Twython function (Twython.get_home_timeline, Twython.search)
|
:param function: Instance of a Twython function (Twython.get_home_timeline, Twython.search)
|
||||||
:param \*\*params: Extra parameters to send with your request (usually parameters excepted by the Twitter API endpoint)
|
:param sleep_seconds: Number of seconds to sleep between each API call (useful for heavily rate-limited endpoints)
|
||||||
|
:param \*\*params: Extra parameters to send with your request (usually parameters accepted by the Twitter API endpoint)
|
||||||
:rtype: generator
|
:rtype: generator
|
||||||
|
|
||||||
Usage::
|
Usage::
|
||||||
|
|
@ -389,8 +391,8 @@ class Twython(EndpointsMixin, object):
|
||||||
>>> results = twitter.cursor(twitter.search, q='python')
|
>>> results = twitter.cursor(twitter.search, q='python')
|
||||||
>>> for result in results:
|
>>> for result in results:
|
||||||
>>> print result
|
>>> print result
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if not hasattr(function, 'iter_mode'):
|
if not hasattr(function, 'iter_mode'):
|
||||||
raise TwythonError('Unable to create generator for Twython method "%s"' % function.__name__)
|
raise TwythonError('Unable to create generator for Twython method "%s"' % function.__name__)
|
||||||
|
|
||||||
|
|
@ -398,7 +400,7 @@ class Twython(EndpointsMixin, object):
|
||||||
|
|
||||||
if not content:
|
if not content:
|
||||||
raise StopIteration
|
raise StopIteration
|
||||||
|
|
||||||
if hasattr(function, 'iter_key'):
|
if hasattr(function, 'iter_key'):
|
||||||
results = content.get(function.iter_key)
|
results = content.get(function.iter_key)
|
||||||
else:
|
else:
|
||||||
|
|
@ -424,7 +426,9 @@ class Twython(EndpointsMixin, object):
|
||||||
except (TypeError, ValueError): # pragma: no cover
|
except (TypeError, ValueError): # pragma: no cover
|
||||||
raise TwythonError('Unable to generate next page of search results, `page` is not a number.')
|
raise TwythonError('Unable to generate next page of search results, `page` is not a number.')
|
||||||
|
|
||||||
for result in self.cursor(function, **params):
|
time.sleep(sleep_seconds)
|
||||||
|
|
||||||
|
for result in self.cursor(function, sleep_seconds, **params):
|
||||||
yield result
|
yield result
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue