Skip to content

Instantly share code, notes, and snippets.

@AparaV
Last active Jul 22, 2021
Embed
What would you like to do?
Using the tweepy library to stream tweets has a catch. There is no built-in feature that allows you to stop streaming after a fixed time. To avoid manually terminating the stream, this code proposes a simple solution without any (complex) multi-threading.
import os
import time
import tweepy
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy import StreamListener
def authenticate():
'''Use credentials to authenticate user'''
consumerkey = os.environ.get('CONSUMER_KEY')
consumersecret = os.environ.get('CONSUMER_SECRET')
accesstoken = os.environ.get('ACCESS_TOKEN')
accesssecret = os.environ.get('ACCESS_SECRET')
auth = OAuthHandler(consumerkey, consumersecret)
auth.set_access_token(accesstoken, accesssecret)
api = tweepy.API(auth)
return auth, api
if __name__ == "__main__":
auth, api = authenticate()
'''Live streaming tweets for a specific interval of time'''
runtime = 60 #Tracking for 60 seconds
tweetstream = Stream(auth, StreamListener())
tweetstream.filter(track=['twitter'], is_async=True)
time.sleep(runtime)
tweetstream.disconnect()
@pilou-K75VJ

This comment has been minimized.

Copy link

@pilou-K75VJ pilou-K75VJ commented Mar 6, 2018

line 28, small typo : "filer" to "filter"

@stevenmcsorley

This comment has been minimized.

Copy link

@stevenmcsorley stevenmcsorley commented Apr 5, 2021

v3 line 28; should be is_async=True and should line 30 be tweetstream.disconnect() ?

@AparaV

This comment has been minimized.

Copy link
Owner Author

@AparaV AparaV commented Apr 5, 2021

Thank you for catching the typos and API updates @pilou-K75VJ and @stevenmcsorley!

@Kwieeciol

This comment has been minimized.

Copy link

@Kwieeciol Kwieeciol commented Jul 19, 2021

Doesn't seem to work, I put some print statements after stream.filter, time.sleep and stream.disconnect, everything prints but the console hangs. How can I terminate the whole process?

@AparaV

This comment has been minimized.

Copy link
Owner Author

@AparaV AparaV commented Jul 21, 2021

@Kwieeciol Not sure why the process will not terminate if you are able to successfully disconnect (as evidenced by your print statement after .disconnect()). Maybe you have some other thread that is running? Or maybe there was a breaking change in recent API updates?

I am not actively maintaining this anymore. Perhaps, ask on stack overflow or open an issue on tweepy. I am happy to update the gist if you figure out the issue!

@Kwieeciol

This comment has been minimized.

Copy link

@Kwieeciol Kwieeciol commented Jul 22, 2021

@AparaV, it's the only thread. I think the issue might be that the thread where stream.filter is being ran doesn't terminate. Thanks anyways! I'll investigate the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment