Add rate limit handling example
authorHarmon <Harmon758@gmail.com>
Sat, 30 Oct 2021 17:20:38 +0000 (12:20 -0500)
committerHarmon <Harmon758@gmail.com>
Sat, 30 Oct 2021 17:20:38 +0000 (12:20 -0500)
examples/rate_limit_handling.py [new file with mode: 0644]

diff --git a/examples/rate_limit_handling.py b/examples/rate_limit_handling.py
new file mode 100644 (file)
index 0000000..3845a58
--- /dev/null
@@ -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)