Reputation: 251
I created a simple python script that listens to a filtered twitter stream and that writes the data into a simple text file.
# -*- coding: utf-8 -*-
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
# security credentials ommited
searchstr = 'SEARCHSTRING'
class listener(StreamListener):
def on_data(self, data):
with open('data.txt', 'a') as fp:
fp.write(data)
return True
def on_error(self, status):
print(status)
return True
auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track=[searchstr])
How can I change the output file every day without stopping the stream of data? I would like to save the file in the format YYYY-MM-DD-data.txt.
My intuition tells me that I need another while loop that continuously checks the time and opens a new file for the output as soon as the date changes. Could you please point me in the right direction? Thank you!
Upvotes: 1
Views: 165
Reputation: 39834
Alternatively, if you want the file timestamp in UTC instead of localtime:
>>> import time
>>> time.strftime("%Y-%m-%d-data.txt", time.gmtime(time.time()))
'2016-06-15-data.txt'
Upvotes: 1
Reputation: 6357
Just replace
'data.txt'
with
time.strftime('%Y-%m-%d',time.localtime())+'-data.txt'
Do not forget to import time
before using the above code.
time.localtime()
will get the current local time which will be formatted to YYYY-MM-DD using time.strftime()
.
Upvotes: 1