'return value of tweepy listener on status without disconnecting
I try to return the value of the tweepylistener on_status function. It only returns a status when i disconnect the stream. So i thought let's implement a while true loop, so when it disconnects it restarts the stream. This way I have the output which i can use later in the code and the tweepylistener keeps streaming. Now this doesn't work. It does disconnect and reconnect the stream, but I won't get the status returned. So the last for loop doesn't print the tweet.text.
There are no errors.
Is there another way to get the on_status value returned while keeping the stream connected? I use the bot on a remote server, so i want that it keeps listening to a tweet of a specific user.
class TweetListener(tweepy.Stream):
tweets = []
limit = 1
def on_status(self, status):
self.tweets.append(status)
if len(self.tweets) == self.limit:
self.disconnect()
return self.tweets
def on_disconnect(self):
print >> sys.stderr, 'Timeout...'
return True # Don't kill the stream
print("Stream restarted")
def start_stream():
while True:
try:
stream_tweets = TweetListener(consumer_key, consumer_secret, access_token, access_token_secret)
user = 'test'
user_id = [api.get_user(screen_name=user).id]
stream_tweets.filter(follow=user_id)
for tweet in stream_tweets.tweets:
print(tweet.text)
except:
continue
start_stream()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
