Twitter error code 429 with Tweepy

DSchana picture DSchana · Jan 22, 2017 · Viewed 21.3k times · Source

I am trying to create a project that accesses a twitter account using the tweepy api but I am faced with status code 429. Now, I've looked around and I see that it means that I have too many requests. However, I am only ever for 10 tweets at a time and within those, only one should exist during my testing.

for tweet in tweepy.Cursor(api.search, q = '@realtwitchess ',lang = ' ').items(10):
                        try:
                                text = str(tweet.text)
                                textparts = str.split(text) #convert tweet into string array to disect
                                print(text)

                                for x, string in enumerate(textparts): 
                                        if (x < len(textparts)-1): #prevents error that arises with an incomplete call of the twitter bot to start a game
                                                if string == "gamestart" and textparts[x+1][:1] == "@": #find games
                                                        otheruser = api.get_user(screen_name = textparts[2][1:]) #drop the @ sign (although it might not matter)
                                                        self.games.append((tweet.user.id,otheruser.id))
                                        elif (len(textparts[x]) == 4): #find moves
                                                newMove = Move(tweet.user.id,string)
                                                print newMove.getMove()
                                                self.moves.append(newMove)
                                if tweet.user.id == thisBot.id: #ignore self tweets
                                        continue

                        except tweepy.TweepError as e:  
                                print(e.reason)
                                sleep(900)
                                continue
                        except StopIteration: #stop iteration when last tweet is reached
                                break

When the error does appear, it is in the first for loop line. The kinda weird part is that it doesn't complain every time, or even in consistent intervals. Sometimes it will work and other times, seemingly randomly, not work.

We have tried adding longer sleep times in the loop and reducing the item count.

Answer

J. Gandra picture J. Gandra · Apr 2, 2018

Add wait_on_rate_limit=True on the API call like this:

api = tweepy.API(auth, wait_on_rate_limit=True)

This will make the rest of the code obey the rate limit