Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Zahra Rajabi
tweepy
Commits
94a0e11a
Commit
94a0e11a
authored
Oct 15, 2015
by
obskyr
Browse files
Added snippet for handling rate limit using cursors.
parent
a439c3d9
Changes
1
Hide whitespace changes
Inline
Side-by-side
docs/code_snippet.rst
View file @
94a0e11a
...
...
@@ -51,3 +51,29 @@ This snippet will follow every follower of the authenticated user.
for follower in tweepy.Cursor(api.followers).items():
follower.follow()
Handling the rate limit using cursors
=====================================
Since cursors raise ``RateLimitError``\ s in their ``next()`` method,
handling them can be done by wrapping the cursor in an iterator.
Running this snippet will print all users you follow that themselves follow
less than 300 people total - to exclude obvious spambots, for example - and
will wait for 15 minutes each time it hits the rate limit.
.. code-block :: python
# In this example, the handler is time.sleep(15 * 60),
# but you can of course handle it in any way you want.
def limit_handled(cursor):
while True:
try:
yield cursor.next()
except tweepy.RateLimitError:
time.sleep(15 * 60)
for follower in limit_handled(tweepy.Cursor(api.followers).items()):
if follower.friends_count < 300:
print follower.screen_name
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment