Best Practices to Prevent 420 Error when Learning


#1

Hello,

I’ve recently started teaching myself how to program and connect to Twitter’s API using Python. I got my first Error: 420 message and I believe it was because I was connecting/disconnecting multiple times while testing and trying to make my code work.

Since I’m still a newbie, does anyone have any suggestions on how to prevent this? I don’t want to get blacklisted. The documentation that I found in dev.twitter.com gives best practices for code in a production environment, but nothing on what to do when testing code changes.

Is it possible to create the connection, then modify my code and re-run it, all while leaving the connection open? This is my code

import tweepy import time import json from HTMLParser import HTMLParser

Consumer keys and access tokens, used for OAuth

consumer_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’
consumer_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX’
access_token = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’
access_token_secret = ‘XXXXXXXXXXXXXXXXXXXXXXXXXX’

OAuth process, using the keys and tokens

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

Creation of the actual interface, using authentication

api = tweepy.API(auth)

class StreamListener(tweepy.StreamListener):
def on_status(self, status):
print 'Ran on_status’
print status.text
if status.coordinates:
print ‘Coords:’, status.coordinates
if status.place:
print ‘Place:’, status.place.full_name
if status.name:
print ‘Name:’, status.user.name
return
def on_error(self, status_code):
print 'Error: ’ + repr(status_code)
return False

l = StreamListener()
streamer = tweepy.Stream(auth=auth, listener=l)
#setTerms = [‘hello’, ‘goodbye’, ‘goodnight’, ‘good morning’]
setTerms = [‘twitter’]
streamer.filter(track = setTerms)

Any feedback would be appreciated.


#2

Hi John, if you are being rate limited, unfortunately you’ll have to wait for around 15 minutes until the rate limit window expires. Take a look at https://dev.twitter.com/docs/rate-limiting/1.1 for more detail on how this works.