1

So i this following code is what i did, but it only replies to existing tweets, i tried to run it in a loop, but it hit api limits very very quick, i could delay the time but i want something more efficient and quick, i've hear about working with stream of tweets, i don't understand how that works i am new to coding, anyawy here is the code that hits api limit very quick

import tweepy
from random import randint
from time import sleep

CONSUMER_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'
CONSUMER_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'
ACCESS_TOKEN = 'xxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
ACCESS_TOKEN_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)

num=0
count=0
temp=0

while (count < 100):

    firstTweet = api.search("dogs")[0]
    if firstTweet.id==temp:
        sleep(randint(10,20))
        continue

    rid=firstTweet.id
    rsn=firstTweet.user.screen_name

    m="@%s If you love dogs, follow us" % (rsn)
    api.update_status(status=m, in_reply_to_status_id=rid)
    count=count+1
    print(count)
    temp = firstTweet.id

basically it searches for tweet about dogs, takes the top most tweet, replies to it, and saved the tweet id in temp so the loop keeps running and if the top tweet is still the same it won't reply once it changes (that is there is a new tweet) it will reply, but this code only runs for 5 mins and then hits limit is there any other way to do it?

2 Answers2

1

If you were only making search requests, you would be better off using application authentication (oauth2), which allows you 450 search requests per 15 minutes.

However, you are also making update status requests, which requires that you authenticate using user authentication (oauth1), as you are doing in your example. This lets you make 180 search requests per 15 minutes.

But, the larger problem is the update status request. Twitter does not publish a rate limit for this request because it would make it easier for spammers to do their thing.

Your only recourse is to check if Twitter returns a 403 error, meaning you have reached your limit. Then, wait 15 minutes before trying again. You have to be careful about exceeding this limit too frequently; otherwise, Twitter will think you are spamming.

Jonas
  • 3,969
  • 2
  • 25
  • 30
1

There are a couple of mistakes in your answer. First of all, if you use a statement

firstTweet = api.search("dogs")[0]

you are essentially making an API call but ultimately saving only the first tweet returned by the search API. If you check the Twitter API documentation (https://dev.twitter.com/rest/reference/get/search/tweets), at each call to api.search(), a default count of 15 tweets are returned. This is the reason why you are running out of your limits.

Now the fix would be to use tweepy Cursor. An example of the same can be found here. Also make sure that you use the since_id & max_id parameters in the search API call so that you receive new set of tweets in the next call to search. Else, you will be getting the same set of tweets in every call. This is explained really well in the comment section of the above link.

Community
  • 1
  • 1
Unni
  • 5,348
  • 6
  • 36
  • 55