Mohan
Mohan

Reputation: 1

Tweepy Wait on Rate Limit Not Working in Tweepy Streams

I am Trying to Build a Twitter Bot which Streams Tweets Based on List of Given KeyWords.

While Setting Up Auth with Twitter I have specified both wait_on_rate_limit and wait_on_rate_limit_notify to be True as mentioned in Below Code.

tweepy.API(self.auth,wait_on_rate_limit=True,wait_on_rate_limit_notify=True)

But When Starting a Stream from Tweepy with this Auth Credentials it is resetting the values of both wait_on_rate_limit and wait_on_rate_limit_notify to False.

print(f'Twitter API Client Retry and Wait :',api.api.wait_on_rate_limit)
print(f'Streaming Class Retry and Wait :',myStreamListener.api.wait_on_rate_limit)
print(f'Stream Object Retry and Wait :',myStream.api.wait_on_rate_limit)

Twitter API Client Retry and Wait : True Streaming Class Retry and Wait : False Stream Object Retry and Wait : False

How can I Set These Options from Tweepy.stream to True?

Full Code:

#import re 
import tweepy 
from tweepy import OAuthHandler 
#from textblob import TextBlob 

# creating object of TwitterClient Class 
class TwitterClient(object): 
    ''' 
    Generic Twitter Class 
    '''
    def __init__(self): 
        ''' 
        Class constructor or initialization method. 
        '''
        # keys and tokens from the Twitter Dev Console 
        consumer_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
        consumer_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
        access_token = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
        access_token_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

        # attempt authentication 
        try: 
            # create OAuthHandler object 
            self.auth = OAuthHandler(consumer_key, consumer_secret) 
            # set access token and secret 
            self.auth.set_access_token(access_token, access_token_secret) 
            # create tweepy API object to fetch tweets 
            self.api = tweepy.API(self.auth,wait_on_rate_limit=True,
                         wait_on_rate_limit_notify=True) 
        except: 
            print("Error: Authentication Failed") 

# Streaming Class
class MyStreamListener(tweepy.StreamListener):

    def on_status(self, status):
        print(status.user.screen_name)
        print('--------------------------------------------------------------')
        print(status.text)
        print('--------------------------------------------------------------')

    def on_error(self, status):
        print (status)



# Twitter Client API Auth
api = TwitterClient() 

# Initialize Streaming Object
myStreamListener = MyStreamListener()

# Establish a Stream With API and Streaming Object
myStream = tweepy.Stream(auth = api.auth,tweet_mode="extended",
                         include_rts=False ,listener=myStreamListener)     

# Stream Tweets Based on Key Words
myStream.filter(track=['CAA NRC India'], is_async=True)

print(f'Twitter API Client Retry and Wait :',api.api.wait_on_rate_limit)
print(f'Streaming Class Retry and Wait :',myStreamListener.api.wait_on_rate_limit)
print(f'Stream Object Retry and Wait :',myStream.api.wait_on_rate_limit)

myStream.running = False 

Twitter API Client Retry and Wait : True Streaming Class Retry and Wait : False Stream Object Retry and Wait : False

Upvotes: 0

Views: 1636

Answers (1)

Tin Nguyen
Tin Nguyen

Reputation: 5330

https://github.com/tweepy/tweepy/blob/master/tweepy/streaming.py#L33

In your case: myStreamListener = MyStreamListener(api=api.api) (It is really confusing how you are calling your TwitterClient instance api.)

For the Stream I don't have something since it seems to always create a new API() instance: https://github.com/tweepy/tweepy/blob/master/tweepy/streaming.py#L235
You can overwrite it with myStream.api = api.api

I'm curious however how you are running into rate limiting problems when streaming. This shouldn't happen. It's not an endpoint you manually call repeatedly.

Upvotes: 1

Related Questions