我有一个Tweepy侦听器,以流式传输包含特定用户的推文(例如,我想检测提及)。我的问题是在update_status
类内部时如何在api中调用TwitterListener
方法?如何将它(api实例)作为变量传递?
class TwitterAuthenticator():
def authenticate_twitter_app(self):
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
return auth
class TwitterListener(StreamListener):
"""
This a basic listener class that just prints received tweets
"""
def __init__(self,hash_list):
def on_data(self,data):
try:
#parse data
if condition:
#this is where the PROBLEM is located
api.update_status(status='some text')
except BaseException as e:
print("Error on_data: %s" % str(e))
return True
def on_error(self,status):
if status == 420:
# Returning False on_data method in case rate limit
return False
class TwitterStreamer():
"""
Class for streamming and processing line tweets.
"""
def __init__(self):
self.twitter_authenticator = TwitterAuthenticator()
pass
def stream_tweets(self,hash_list):
# This handels Twitter authentication and the connection to the Twitter Streaming API
listener = TwitterListener(hash_list)
auth = self.twitter_authenticator.authenticate_twitter_app()
api = tweepy.API(auth,wait_on_rate_limit=True, wait_on_rate_limit_notify=True, compression=True)
stream = Stream(auth,listener)
stream.filter(track=hash_list)
hash_list = ['user_screen_name']
twiiter_streamer = TwitterStreamer()
twiiter_streamer.stream_tweets(hash_list)