Skip to content

Instantly share code, notes, and snippets.

@AngaKoko
Forked from hugobowne/tweet_listener.py
Created February 7, 2019 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AngaKoko/fcf98b5afe6f82adfbc317da99badd96 to your computer and use it in GitHub Desktop.
Save AngaKoko/fcf98b5afe6f82adfbc317da99badd96 to your computer and use it in GitHub Desktop.
Here I define a Tweet listener that creates a file called 'tweets.txt', collects streaming tweets as .jsons and writes them to the file 'tweets.txt'; once 100 tweets have been streamed, the listener closes the file and stops listening.
class MyStreamListener(tweepy.StreamListener):
def __init__(self, api=None):
super(MyStreamListener, self).__init__()
self.num_tweets = 0
self.file = open("tweets.txt", "w")
def on_status(self, status):
tweet = status._json
self.file.write( json.dumps(tweet) + '\n' )
self.num_tweets += 1
if self.num_tweets < 100:
return True
else:
return False
self.file.close()
def on_error(self, status):
print(status)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment