Home:ALL Converter>Twitter error code 429 with Tweepy

Twitter error code 429 with Tweepy

Ask Time:2017-01-22T09:59:02         Author:DSchana

Json Formatter

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.

Author:DSchana,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/41786569/twitter-error-code-429-with-tweepy
J. Gandra :

Add wait_on_rate_limit=True on the API call like this:\n\napi = tweepy.API(auth, wait_on_rate_limit=True)\n\n\nThis will make the rest of the code obey the rate limit",
2018-04-02T01:22:32
Giordano :

You found the correct information about error code. In fact, the 429 code is returned when a request cannot be served due to the application’s rate limit having been exhausted for the resource.(from documentation) \nI suppose that your problem regards not the quantity of data but the frequency.\nCheck the Twitter API rate limits (that are the same for tweepy).\n\nRate limits are divided into 15 minute intervals. All endpoints require authentication, so there is no concept of unauthenticated calls and rate limits.\nThere are two initial buckets available for GET requests: 15 calls every 15 minutes, and 180 calls every 15 minutes.\n\nI think that you can try to use API in this range to avoid the problem\nUpdate\nFor the latest versions of Tweepy (from 3.2.0), the wait_on_rate_limit has been introduced. \nIf set to True, it allows to automatically avoid this problem.\nFrom documentation:\n\nwait_on_rate_limit – Whether or not to automatically wait for rate limits to replenish\n",
2017-01-23T21:26:25
yy