Skip to content

Commit

Permalink
Add read-only auth + handle when hitting rate limit #3
Browse files Browse the repository at this point in the history
  • Loading branch information
srbdev committed Sep 12, 2021
1 parent 40cd96b commit d3e41b6
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions collect_tweets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from datetime import datetime
import os

from tweepy.error import RateLimitError

import keys
import config

Expand All @@ -21,9 +23,8 @@ def generate_timestamp():

print("+ Starting the harvest...")

auth = tweepy.OAuthHandler(keys.CONSUMER_KEY, keys.CONSUMER_SECRET)
auth.set_access_token(keys.ACCESS_TOKEN, keys.ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
auth = tweepy.AppAuthHandler(keys.CONSUMER_KEY, keys.CONSUMER_SECRET)
api = tweepy.API(auth, wait_on_rate_limit=True)
print("+ Authenticated with Twitter!")

if not os.path.isdir(config.FARM_PATH):
Expand All @@ -48,9 +49,15 @@ def generate_timestamp():

fetch_intervals = 60 * 1.5
reset_pause = 60 * 60 * 1
rate_limit_pause = 60 * 60 * 1

while True:
tweets = api.user_timeline(args.handle, page=page)
try:
tweets = api.user_timeline(args.handle, page=page)
except RateLimitError as e:
print(f"- Rate limit exceeded, pausing for {rate_limit_pause}s")
time.sleep(rate_limit_pause)
continue

for tweet in tweets:
id = tweet._json["id"]
Expand Down

0 comments on commit d3e41b6

Please sign in to comment.