From fe5a0508f97326e0be4fddca5db65a655607c2f9 Mon Sep 17 00:00:00 2001 From: Harmon Date: Sat, 30 Oct 2021 12:20:38 -0500 Subject: [PATCH] Add rate limit handling example --- examples/rate_limit_handling.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 examples/rate_limit_handling.py diff --git a/examples/rate_limit_handling.py b/examples/rate_limit_handling.py new file mode 100644 index 0000000..3845a58 --- /dev/null +++ b/examples/rate_limit_handling.py @@ -0,0 +1,24 @@ +import tweepy + + +consumer_key = "" +consumer_secret = "" +access_token = "" +access_token_secret = "" + +auth = tweepy.OAuthHandler(consumer_key, consumer_secret) +auth.set_access_token(access_token, access_token_secret) + +# Setting wait_on_rate_limit to True when initializing API will initialize an +# instance, called api here, that will automatically wait, using time.sleep, +# for the appropriate amount of time when a rate limit is encountered +api = tweepy.API(auth, wait_on_rate_limit=True) + +# This will search for Tweets with the query "Twitter", returning up to the +# maximum of 100 Tweets per request to the Twitter API + +# Once the rate limit is reached, it will automatically wait / sleep before +# continuing + +for tweet in tweepy.Cursor(api.search_tweets, "Twitter", count=100).items(): + print(tweet.id) -- 2.25.1