Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"AttributeError: 'MyStreamListener' object has no attribute 'api'" tweepy error

Tags:

python

tweepy

I have a python program that searches twitter for a word and counts all mentions of that word. However, I've run into an odd problem and I can't find an answer elsewhere. I'm getting a "AttributeError: 'MyStreamListener' object has no attribute 'api'" error. This is the first time I've seen this error. Any suggestions on how to fix?

Code:

from tweepy import OAuthHandler

import tweepy
from tweepy import StreamListener
from tweepy import Stream


import time



consumer_key = 'super secret consumer key'
consumer_secret = 'Please help Ive been stuck with this error for days'
access_token = 'Im so desperate'
access_secret = 'I suck at coding please help'

auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)

api = tweepy.API(auth)
print('')


class MyStreamListener(tweepy.StreamListener):



    def __init__(self):
        #initializes the counter
        self.counter = 0    



    def on_status(self, status):
        #prints status text. Also counts the mentions. 
        self.counter = self.counter + 1
        print(status.text)


    def on_error(self, status_code):
        if status_code == 420:
            print('420 error')
            #Ends stream in case of rate limiting
            return False


myStreamListener = MyStreamListener()

myStream = tweepy.Stream(auth = api.auth, listener = myStreamListener)

#Word
myStream.filter(track=['Warriors'])
like image 908
Nat Avatar asked Jan 28 '23 00:01

Nat


1 Answers

Adding super(MyStreamListener, self).__init__() at the beginning of __init__ fixes it for me.

def __init__(self):
    super(MyStreamListener, self).__init__()
    #initializes the counter
    self.counter = 0 
like image 151
domi Avatar answered Jan 30 '23 13:01

domi