Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter error code 429 with Tweepy

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.

like image 307
DSchana Avatar asked Jan 22 '17 01:01

DSchana


2 Answers

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

like image 75
J. Gandra Avatar answered Sep 24 '22 22:09

J. Gandra


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)
I suppose that your problem regards not the quantity of data but the frequency.

Check the Twitter API rate limits (that are the same for tweepy).

Rate limits are divided into 15 minute intervals. All endpoints require authentication, so there is no concept of unauthenticated calls and rate limits. There are two initial buckets available for GET requests: 15 calls every 15 minutes, and 180 calls every 15 minutes.

I think that you can try to use API in this range to avoid the problem

Update
For the latest versions of Tweepy (from 3.2.0), the *wait_on_rate_limit * has been introduced.
If set to True, it allows to automatically avoid this problem.

From documentation:

wait_on_rate_limit – Whether or not to automatically wait for rate limits to replenish

like image 20
Giordano Avatar answered Sep 23 '22 22:09

Giordano