Search code examples
pythonapiattributestweepy

Tweepy streaming attribute error


I'm making a text-searching program to get started with tweepy. I ran into an error that I can't seem to fix. The full error message is below.

Traceback (most recent call last):
  File "/Users/nattaylor/Documents/Programming/Python/twitterapi.py", line 53, in <module>
    myStream.filter(track=['Warriors'])
  File "/anaconda3/lib/python3.6/site-packages/tweepy/streaming.py", line 450, in filter
    self._start(async)
  File "/anaconda3/lib/python3.6/site-packages/tweepy/streaming.py", line 364, in _start
    self._run()
  File "/anaconda3/lib/python3.6/site-packages/tweepy/streaming.py", line 297, in _run
    six.reraise(*exc_info)
  File "/anaconda3/lib/python3.6/site-packages/six.py", line 693, in reraise
    raise value
  File "/anaconda3/lib/python3.6/site-packages/tweepy/streaming.py", line 266, in _run
    self._read_loop(resp)
  File "/anaconda3/lib/python3.6/site-packages/tweepy/streaming.py", line 327, in _read_loop
    self._data(next_status_obj)
  File "/anaconda3/lib/python3.6/site-packages/tweepy/streaming.py", line 300, in _data
    if self.listener.on_data(data) is False:
  File "/anaconda3/lib/python3.6/site-packages/tweepy/streaming.py", line 54, in on_data
    status = Status.parse(self.api, data)
AttributeError: 'MyStreamListener' object has no attribute 'api'

My code is here:

from tweepy import OAuthHandler

import tweepy
from tweepy import StreamListener
from tweepy import Stream


import time



consumer_key = 'cant show you these'
consumer_secret = 'Im so desperate ive been working on this for so long'
access_token = 'its not even very hard'
access_secret = 'I just suck'

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'])

I think it must be a problem with my tweepy syntax somewhere. Any input is greatly appreciated. (I'm so lost and confused and scared please you all know so much more than me I need you)


Solution

  • As you are importing as follows

    import tweepy
    from tweepy import StreamListener
    from tweepy import Stream
    

    Try inheriting from StreamListener instead of tweepy.StreamListener

    Replace

    class MyStreamListener(tweepy.StreamListener):
    

    with

    class MyStreamListener(StreamListener):
    

    For good measure change your init method from

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

    to

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

    From tweepy's sources

    https://github.com/tweepy/tweepy/blob/master/tweepy/streaming.py

    ...
    from tweepy.api import API
    ...
    class StreamListener(object):
    
        def __init__(self, api=None):
            self.api = api or API()
    ...
    

    You probably "get away" with only updating the init method to initialize the parent object, but it would be better practice to choose one import, if you are explicitly importing StreamListener you may as well use this instead of the full tweepy import.