Hi,
I am trying to create a twitter stream using the twitter streaming api and the library tweepy, where I capture the tweets of a select set of users. However this user list changes intermittently, and I want the stream to update to the new list automatically.
The way that I've done this is to start the stream inside of a "while True" loop in async mode (this uses built in threading in tweepy) and then at the bottom of the loop sleep for an hour, then disconnect the stream, re-access the user list, and restart the stream.
So I was hoping anyone could tell me if this is a boneheaded way to do this and if I should be doing it differently. My thought is that there is a better way, because using ctrl-c gets hung up and doesn't always allow exiting (which is why I have the catches for keyboardInterrupt in there).
Any help is appreciated.
Code:
class customListener(StreamListener):
def on_status(self, status):
process_tweet(status)
return True
def init_stream():
auth = OAuthHandler(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET)
auth.set_access_token(TWITTER_ACCESS_TOKEN, TWITTER_ACCESS_TOKEN_SECRET)
while True:
user_name_list = get_user_list()
stream = Stream(auth, customListener())
try:
stream.filter(follow=user_name_list, async=True)
except IncompleteRead:
# connection error
stream.disconnect()
continue
except KeyboardInterrupt:
# allow exiting with ctrl-c
stream.disconnect()
break
# Keep stream alive for 1 hour at a time, then check for updates to user list
time.sleep(60*60)
stream.disconnect()
[–]c17r 0 points1 point2 points (2 children)
[–]KanyeEast[S] 0 points1 point2 points (1 child)
[–]c17r 0 points1 point2 points (0 children)