Instantly share code, notes, and snippets.

@AparaV AparaV/streamer.py
Last active Mar 6, 2018

Embed
What would you like to do?
Using the tweepy library to stream tweets feels like a breeze. But there is a catch. There is not built-in feature that allows you to stop streaming after a period of time passes. This means you have to manually terminate the program to stop streaming. This code proposes a simple solution to that problem without any (complex) multi-threading inv…
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.filer(track=['twitter'], async=True)
time.sleep(runtime)
twitterstream.disconnect()
@pilou-K75VJ

This comment has been minimized.

Copy link

pilou-K75VJ commented Mar 6, 2018

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

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