简体   繁体   中英

429 Too many requests error while running a bot that likes tweets using tweepy

I made a twitter bot recently which gives me this error. Sometimes it runs correctly and it shows tweets with specified keywords in the console but other times it also shows the tweet itself but doesnt likes it and shows this error between one tweet and another. This is what I see (the language is portuguese, not relevant):

pois eu reclamo mesmo vcs q se fodam
429 Too Many Requests
Too Many Requests
so fumando 70 mesmo
429 Too Many Requests
Too Many Requests

The code is the following:

TWITTER_API_KEY = "XXXX"
TWITTER_API_KEY_SECRET = "XXXX"
BEARER_TOKEN = "XXXX"
TWITTER_ACCESS_TOKEN = "XXXX"
TWITTER_ACCESS_TOKEN_S = "XXXX"

client = tweepy.Client(BEARER_TOKEN, TWITTER_API_KEY, TWITTER_API_KEY_SECRET, TWITTER_ACCESS_TOKEN, TWITTER_ACCESS_TOKEN_S)

auth = tweepy.OAuth1UserHandler(TWITTER_API_KEY, TWITTER_API_KEY_SECRET, TWITTER_ACCESS_TOKEN, TWITTER_ACCESS_TOKEN_S)
api = tweepy.API(auth, wait_on_rate_limit=True)

alive()

class MyStream(tweepy.StreamingClient):
    def on_tweet(self, tweet):
        try:
            print(tweet.text)
            client.like(tweet.id)
        except Exception as error:
            print(error)
        time.sleep(10)


stream = MyStream(bearer_token=BEARER_TOKEN)

rule = tweepy.StreamRule("(fumar OR fumando OR "
                         " OR fumeque OR fumo) (-is:retweet)")
stream.add_rules(rule)

stream.filter()

I tried to increase the value of the sleep function but it doesnt solve the problem. Does any one of you know what may be happening and how can I fix this?

Every request you send to a server requires effort to process, and if too many people are sending requests at the same time, the server processing the API requests basically gets DDoS'd. To prevent this, API implementors will commonly communicate that you need to slow down the rate at which you are making requests by returning a 429 error rather than the thing that was requested.

The problem you're having is that you're trying to comply with the request, but you're doing it without actually knowing how much to take your foot off the gas with your API requests, which is just leading you to hit the rate-limiter over and over again.

I want to address right off the bat that you need to be careful with this. System administrators and site reliability engineers send those 429 error codes for a reason, and when you don't comply, they have no way of knowing if you're DDoS'ing them on purpose or by accident.

That 429 error code you are getting comes with a x-rate-limit-reset header that tells you exactly how long you need to wait before sending another request. You just need to make sure you're checking the status code and the headers of each request you're making.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM