无论何时创建状态流对象,我都会从Twitter API开始流式传输信息。如果我想做一种停止流的方法,那将是什么呢?我需要帮助的方法是stopStream(),该方法将在调用该方法时停止Twitter流。
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import time
import json
import csv
import sys
import Tweet
import DatabaseInteractor
import twitter_credentials
class StatusStream(StreamListener):
def __init__(self):
self.continueStream = True
super(StatusStream, self).__init__()
def on_status(self, status):
print(status.text)
if(self.continueStream ==False):
return False
def on_error(self, status):
print(status)
return False
@staticmethod
def stopStream():
self.continueStream = False
auth = OAuthHandler(twitter_credentials.API_KEY,twitter_credentials.API_SECRET_KEY)
auth.set_access_token(twitter_credentials.ACCESS_TOKEN,twitter_credentials.ACCESS_TOKEN_SECRET)
twitterStream = Stream(auth=auth, listener=StatusStream(),tweet_mode='extended')
twitterStream.filter(track=["plumber","visa","bathtub"])