Carbon
Carbon

Reputation: 143

How to json dump tweepy stream into text file?

Hi I have looked at many guides and tutorials on how to do this, but I am having trouble with being able to use tweepy to store the JSON data in a text file.

class StreamListener(tweepy.StreamListener): 

def on_status(self, status):

    print(status)

def on_error(self, status):

    print status
    if status == 420:

        return False



if __name__ == '__main__':

stream_listener = StreamListener()
auth = tweepy.OAuthHandler(consumer_token, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = tweepy.Stream(auth, stream_listener)

I have another python file which is supposed to read data into a list:

import pandas
import json

json_data = 'twitter_data.txt'

data_list = []
#load file 
tweets_file = open(json_data, "r")
for line in tweets_file:
try:
    tweet = json.loads(line) #this line causes problems
    data_list.append(tweet)
except:
    continue


print len(data_list)

I thought the data received from twitter comes in JSON format, and the guides I'm following all say it does, but it's actually in another object.

Should I just store everything in a list then json dump that list into the new text file?

Upvotes: 2

Views: 1659

Answers (2)

Christopher Brindle
Christopher Brindle

Reputation: 11

I think something like this may be what your looking for.

def on_status(self, tweet):
    json_dumps = json.dumps(tweet._json)
    tweet_json = json.loads(json_dumps)
    print(tweet_json['created_at'])

These are all the keys you can use in the tweet_json[ ]

dict_keys(['created_at', 'id', 'id_str', 'text', 'source', 'truncated', 'in_reply_to_status_id', 'in_reply_to_status_id_str', 'in_reply_to_user_id', 'in_reply_to_user_id_str', 'in_reply_to_screen_name', 'user', 'geo', 'coordinates', 'place', 'contributors', 'retweeted_status', 'is_quote_status', 'quote_count', 'reply_count', 'retweet_count', 'favorite_count', 'entities', 'favorited', 'retweeted', 'filter_level', 'lang', 'timestamp_ms'])

Upvotes: 1

Robbie
Robbie

Reputation: 4882

It seems like you're on the right track. You can modify the stream listener to write tweets to a file directly.

Edit: this now writes out in JSON format.

#Import the necessary methods from tweepy library
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy import API

#Variables that contains the user credentials to access Twitter API
CONSUMER_KEY = #YOUR CONSUMER KEY
CONSUMER_SECRET = #YOUR CONSUMER SECRET
ACCESS_TOKEN = #YOUR ACCESS TOKEN
ACCESS_TOKEN_SECRET = #YOUR ACCESS TOKEN SECRET

class FileWriteListener(StreamListener):

    def __init__(self):
        super(StreamListener, self).__init__()
        self.save_file = open('tweets.json','w')
        self.tweets = []

    def on_data(self, tweet):
        self.tweets.append(json.loads(tweet))
        self.save_file.write(str(tweet))

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


auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

api = API(auth)

twitter_stream = Stream(auth, MyListener())
# Here you can filter the stream by:
#    - keywords (as shown)
#    - users
twitter_stream.filter(track=['hello'])

This code will run indefinitely, so you either need to exit the process after some time (Ctrl-C) or modify the code.

Then you can load the data:

import json

json_data = []
with open('tweets.json','r') as f:
    json_data.append( json.loads(f.readline()) )

Hope this helps!

Upvotes: 3

Related Questions