Я пытаюсь создать Twitter-бота, который транслирует твиты на основе списка заданных ключевых слов.
При настройке аутентификации с помощью Twitter я указал для wait_on_rate_limit
и wait_on_rate_limit_notify
значение True , как указано в приведенном ниже коде.
tweepy.API(self.auth,wait_on_rate_limit=True,wait_on_rate_limit_notify=True)
Но при запуске потока из Tweepy с этими учетными данными аутентификации он сбрасывает значения как wait_on_rate_limit
, так и wait_on_rate_limit_notify
в Ложь .
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
Как установить эти параметры из Tweepy.stream в True?
Полный код :
#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