How to save a tweepy Twitter stream to a file?

Anton picture Anton · Jan 24, 2015 · Viewed 11.3k times · Source

I have a working script that successfully gathers tweets that mention "stackoverflow". However, I want to run the script in iPython (rather than executive a separate .py file). Ideally, I just want to open it ipyb file, select run all, and let it run for a week or so (not closing my laptop of course) and in result I have a .json file with a week's worth of tweets.

Here is what I have so far:

from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener

access_token = "x"
access_token_secret = "x"
consumer_key = "x"
consumer_secret = "x"

# file name that you want to open is the second argument
save_file = open('data.json', 'a')

class listener(StreamListener):

    def on_data(self, data):
        print(data)
        return True

    def on_error(self, status):
        print(status)

auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track=["stackoverflow"])

Answer

Ajeet Khan picture Ajeet Khan · Feb 24, 2015

add the following code to your existing code. 'fetched_tweets.txt' is the name of file in which you want to save the tweets which is opened in 'a'(append mode).

class StdOutListener(StreamListener):

    def on_data(self, data):
        #print data
        with open('fetched_tweets.txt','a') as tf:
            tf.write(data)
        return True

    def on_error(self, status):
        print status