Good morning everyone,
I'm having a rave with my twitter bot - I need to dump the streamed tweets (which arrive in json) to a file.
I previously have done this by writing it as utf8 formatted strings, however it now turns out that I still need to filter some data, so storing it away as json in the file seemed like the easiest way to go.
I edited the code accordingly:
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import datetime
import json
access_token = #####
access_token_secret = #####
consumer_key = #####
consumer_secret = #####
class StdOutListener(StreamListener):
def on_status(self, status):
print(status)
today = datetime.datetime.now()
with open('/git/twttrbots/data/Twitter_Raw %s' %
today.strftime("%a-%Y-%m-%d"), 'a') as f:
json.dump(status, f) # <- doesn't work
#f.write(json.dumps(status)) # <- doesn't work
#f.write("Blah") # <- works perfectly fine
if __name__ == '__main__':
while True:
try:
#login using auth
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
#filter by hashtag
stream.filter(track=['bitcoin', 'cryptocurrency', 'wonderlandcoin',
'btc', 'fintech', 'satoshi', 'blockchain',
'litecoin', 'btce'])
except:
print("Whoops, dicsonnected at %s. Retrying"
% datetime.datetime.now())
continue
The file is created, the status definitely is read (there's print output in my terminal) but somewhere along the way my data is blasted out into nirvana, instead of my file - as that remains empty at 0
bytes.
I found similar cases here and on other platforms, however, they used json.dumps()
instead of json.dump()
- albeit I have tried both functions as well (using f.write(dumps(status))
), but none of them seem to work.
Now, I'm not a complete fool; I am well aware that it's probably on my end - not a JSON
error - but I can't figure out what it is i am doing wrong.
The only thing I was able to do, is boil it down to an error that occurs in my with open()
statement, leading me to believe it's something about either the open()
mode, or the way I write my data to the file. I know this, since the above linked question's answer works fine on my machine.
I could, of course, use the subprocess module and call a pipe that dumps the print(status) to a file, but that can't be the solution to this?
Addendum
As requested, here's my console output.
Here's what the logger caught when I called logger.debug('status dump: %s', json.dumps(status))
.