Skip to content

Instantly share code, notes, and snippets.

@SahilFruitwala
Last active December 15, 2020 15:51
Show Gist options
  • Save SahilFruitwala/f69b1a965686f075f7b1a724546db972 to your computer and use it in GitHub Desktop.
Save SahilFruitwala/f69b1a965686f075f7b1a724546db972 to your computer and use it in GitHub Desktop.
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
class TwitterStreamer():
"""
Class for streaming and processing live tweets.
"""
def __init__(self):
pass
def stream_tweets(self, fetched_tweets_filename, hash_tag_list):
# This handles Twitter authetification and the connection to Twitter Streaming API
listener = StdOutListener(fetched_tweets_filename)
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token,access_token_secret)
stream = Stream(auth, listener)
# This line filter Twitter Streams to capture data by the keywords:
stream.filter(track=hash_tag_list, is_async=True)
# # # # TWITTER STREAM LISTENER # # # #
class StdOutListener(StreamListener):
"""
This is a basic listener that just prints received tweets to stdout.
"""
def __init__(self, fetched_tweets_filename):
self.fetched_tweets_filename = fetched_tweets_filename
def on_data(self, data):
try:
print(data)
with open(self.fetched_tweets_filename, 'a') as tf:
tf.write(data)
return True
except BaseException as e:
print("Error on_data %s" % str(e))
return True
def on_error(self, status):
print(status)
if __name__ == '__main__':
# Authenticate using config.py and connect to Twitter Streaming API.
hash_tag_list = ["canada revenue agency", "cra"]
fetched_tweets_filename = "tweets.txt"
twitter_streamer = TwitterStreamer()
twitter_streamer.stream_tweets(fetched_tweets_filename, hash_tag_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment