Stack Exchange
Collect data from twitter in json format and for particular time frame
This code only stream data. I want to pull data for a 3 days time frame.
import tweepy
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy.streaming import StreamListener

# will pass key and token
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)

class TweetsListener( StreamListener ):

    def on_data(self, data):
        try:
            with open('name.json', 'a') as f:
                f.write(data)
                return True
        except BaseException as e:
            print("Error on_data: %s" % str(e))
        return True

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

twitter_stream = Stream( auth, TweetsListener() ) 
twitter_stream.filter( track=['XX'] )


You want to extract data of previous 3 days or the next 3 days, as far as stream is concerned, you can only access future data with the streaming API.

i want to collect previous 3 days data from twitter in json format. Can you help me out !

You are using Twitter Streaming API which is used to capture future tweets with given criteria (on_data is called when a tweet with given search terms is posted). You need to use Twitter REST APIs to search previous data. See tweepy.Search()

Parameters:
q – the search query string
since_id – Returns only
statuses with an ID greater than (that is, more recent than) the
specified ID.


Can you change this code. which can save in json format. Instaed of csv.

auth = tweepy.auth.OAuthHandler('X', 'X')
auth.set_access_token('X', 'X')
api = tweepy.API(auth)
Open/Create a file to append data
csvFile = open('result.csv', 'a')
#Use csv Writer
csvWriter = csv.writer(csvFile)
for tweet in tweepy.Cursor(api.search,
q="google",
since="2014-02-14",
until="2014-02-15",
lang="en").items():
#Write a row to the csv file/ I use encode utf-8
csvWriter.writerow([tweet.created_at, tweet.text.encode('utf-8')])
print tweet.created_at, tweet.text
csvFile.close()

You can use code from your original question for that, from on_data method just change f.write(data) with f.write(json.dumps(data)). You will have to import json though.
