Search code examples
pythontwittertweepy

How to set max number of tweets to fetch


I'm trying to set max number of tweets to fetch. The question was asked before but did not get answered. Any help will be highly appreciated.

from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener


#consumer key, consumer secret, access token, access secret.
ckey=""
csecret=""
atoken=""
asecret=""

class listener(StreamListener):

 def on_data(self, data):
    print(data)
    return(True)

 def on_error(self, status):
    print status

auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)

twitterStream = Stream(auth, listener())
twitterStream.filter(track=["car"])

Solution

  • I've figured out the solution. The problem was solved by returning false on reaching the limit by using if and else. The counter was initialized in the class init method. Hopefully, it will be helpful for others.

    from tweepy import Stream
    from tweepy import OAuthHandler
    from tweepy.streaming import StreamListener
    
    
    #consumer key, consumer secret, access token, access secret.
    ckey=""
    csecret=""
    atoken=""
    asecret=""
    
    class listener(StreamListener)
    
     def __init__(self):
        super().__init__()
        self.max_tweets = 200
        self.tweet_count = 0
    
     def on_data(self, data):
         global dataframe
    
       try:
        data
       except TypeError:
          print(completed)
       else:
        self.tweet_count+=1
        if(self.tweet_count==self.max_tweets):
          print("completed")
          return(False)
        else:
          decoded = json.loads(data)
    
     def on_error(self, status):
        return(False)
    
    auth = OAuthHandler(ckey, csecret)
    auth.set_access_token(atoken, asecret)
    
    twitterStream = Stream(auth, listener())
    twitterStream.filter(track=["car"])