skip to Main Content

I have the following code to make a Stream Listener and get tweets from the Twitter API.

class MyStreamListener(tweepy.StreamListener):

    #class constructor
    def __init__(self,api=None):

        super(MyStreamListener,self).__init__()

        #creates class variables and instantiates to file and number
        self.num_tweets = 0
        self.file = open("tweets.txt",'w') 

    #function to collect tweets and add to file    
    def on_status (self,status):
        tweet = status._json
        print("Tweet: " + tweet)
        print("STATUS: " + status.text)
        self.file.write(json.dumps(tweet)+'n')

        print(status)
        self.num_tweets+=1

        if self.num_tweets < 100:
            return True
        else:
            return False
        self.file.close()

l = MyStreamListener()
stream = tweepy.Stream(auth,l)

stream.filter(track=[])  

Although there are no errors with this code, the program does not print anything as is the projected output in the lines

print("Tweet: " + tweet)
print("STATUS: " + status.text)  

I have tried to adjust the line

stream.filter(track=[]) 

to include

stream.filter(track=['trump','clinton'])

but I still did not receive any output.

Help on why this problem is occurring or a possible solution would be appreciated.

EDIT: Future debugging shows that the on_status function is not even running at any time, despite the fact that this is shown in the tweepy docs.

3

Answers


  1. Chosen as BEST ANSWER

    It appears the problem was in an incorrect access key, the error code was not printing for some reason. I solved the problem by rewriting the class and creating a new streaming application with a new key.


  2. If you want to use Tweepy’s StreamListener to filter tweets by just a keyword like trump or clinton, I think you’re better off overriding the on_data method instead. You should receive the data like you’re expecting then.

    I’d double check this but it seems like their docs are down?

    Login or Signup to reply.
  3. Why don’t you use the json.loads() function to obtain all the data? That gives a nicer format to work from:

    def on_status (self, status):
        all_data = json.loads(status)
        tweet = all_data['text']
        print("Tweet: " + tweet)
        print("STATUS: " + all_data + 'n')
        self.file.write(tweet) + 'n')
    

    Now you have your status as a dictionary format in all_data that you can access to find the Tweet and all other variables.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search