I have a python script that hooks into the Twitter Streaming API using basic authentication and making use of the tweetstream module.
Im gathering around 10 tweets a minute.
I was getting intermittent disconnections, so currently logging how often they are occurring.
I have been hitting my rate limit and getting 420 HTTP errors.
I know that for the search API, you get a higher quota with using OAuth authentication. For streaming, I could not find any reference to differences in rate limiting between basic and OAuth. Anyhow, it would appear that the python Tweetstream I am using, does not support this with the streaming API.
I noticed the Ruby version of Tweetstream supports OAuth, but I am doing this project as a learning experience for python.
From reading the Twitter help, it talks of 'backoff strategies' and mentions:
it is essential to stop further connection attempts for a few minutes if a HTTP 420 response is received.
I am no longer getting the errors, but been trying to formulate better logic in my code to avoid getting these errors permanently.
My current proposal is below, which now waits for 200s before attempting to reconnect.
while True:
try:
with tweetstream.FilterStream(uname, passwd, locations=extent) as stream:
# do stuff
except tweetstream.ConnectionError as e:
print e.message + " time: " + datetime.now
time.sleep(200)
pass
except tweetstream.AuthenticationError as e:
now = datetime.datetime.now()
print e.message + " time: " + str(now)
pass
My question is - Is this a good way to get around receiving the 420 errors from Twitter? Those that are more familiar with the Twitter API, can you recommend an approach?