0

As a new user to Python I have hit an issue with the following code. Instead of only printing the results of Twitter search on the screen I need to save the file (ideally pipe-delimited which I don't yet know how to produce...). However the following code runs ok but doesn't create the Output.txt file. It did once and then never again. I am running it on Mac OS and ending the code with Ctrl+C (as I still don't know how to modify it only to return specific number of tweets). I thought that the issue might be related to Flush'ing but after trying to include the options from this post:Flushing issues none of them seemed to work (unless I did something wrong which is more than probable...)

import tweepy
import json
import sys

# Authentication details. To  obtain these visit dev.twitter.com
consumer_key = 'xxxxxx'
consumer_secret = 'xxxxx'
access_token = 'xxxxx-xxxx'
access_token_secret = 'xxxxxxxx'

# This is the listener, resposible for receiving data
class StdOutListener(tweepy.StreamListener):
    def on_data(self, data):
        # Twitter returns data in JSON format - we need to decode it first
        decoded = json.loads(data)

        # Also, we convert UTF-8 to ASCII ignoring all bad characters sent by users
        print '@%s: %s' % (decoded['user']['screen_name'], decoded['text'].encode('ascii', 'ignore'))
        print ''
        return True

    def on_error(self, status):
        print status

if __name__ == '__main__':
    l = StdOutListener()
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)

    print "Showing all new tweets for #Microsoft"

    stream = tweepy.Stream(auth, l)
    stream.filter(track=['Microsoft'])

    sys.stdout = open('Output.txt', 'w')
Community
  • 1
  • 1
Zegro
  • 3
  • 1

1 Answers1

2

I think you would be much better off chaning StdOutListener and having it write to the file directly. Assigning sys.stdout to a file is... weird. This way, you can print things for debug output. Also note that file mode "w" will truncate the file when it's opened.

class TweepyFileListener(tweepy.StreamListener):
    def on_data(self, data):
        print "on_data called"
        # Twitter returns data in JSON format - we need to decode it first
        decoded = json.loads(data)
        msg = '@%s: %s\n' % (
            decoded['user']['screen_name'], 
            decoded['text'].encode('ascii', 'ignore'))

        #you should really open the file in __init__
        #You should also use a RotatingFileHandler or this guy will get massive
        with open("Output.txt", "a") as tweet_log:
            print "Received: %s\n" % msg
            tweet_log.write(msg)
ErlVolton
  • 6,714
  • 2
  • 15
  • 26
  • Thanks, that makes sense. I'm afraid that my level of coding is so limited that more sophisticated suggestions might just take me too long:/ But anyway, this brings back my original question - this works once, and then it seems that Output.txt is only getting amended; even if I change the name and re-run the script (i close it with Ctrl+C, which i am not sure is correct). so in case I want to run this script for different keyword and write to a different file it doesn't work... – Zegro Oct 31 '14 at 14:37