2
votes

I am new to python, and I am using tweepy library for twitter streaming and I am trying to create a class that handles that OAth authentication for me.

from config.config import consumer_secret, consumer_key, secret, token
import tweepy
from tweepy.auth import OAuthHandler
from tweepy import API
from tweepy import OAuthHandler


class Authentication:
    """
    Class that handles the authentication for streaming
    http://docs.tweepy.org/en/v3.5.0/auth_tutorial.html
    """

    def __init__(self):
        self.consumer_key = consumer_key
        self.consumer_secret = consumer_secret
        self.secret = secret
        self.token = token

    def getAuthorization(self):
        self.consumer_key = consumer_key
        self.consumer_secret = consumer_secret
        self.secret = secret
        self.token = token
        auth = OAuthHandler(self.consumer_key, self.consumer_secret)
        auth.set_access_token(self.token, self.secret)
        return auth

I have another class that implements the Twitter Streaming

import tweepy
from tweepy.streaming import StreamListener
from tweepy import Stream
from tweepy.auth import OAuthHandler


class Listener(tweepy.StreamListener):
    """
    Class that inherits from tweepy StreamListener
    http://tweepy.readthedocs.io/en/v3.5.0
    """

    def on_status(self, status):
        print status
        print(status.text)

    def on_data(self, data):
        print data
        return True

    def on_error(self, status):
        if status == 420:
            # returning False in on_data disconnects the stream
            print status
            return False

The problem is when I try to access the method of myStream on the main class, like My Stream Filter for example, I get the error below:

# main  class for the bigDataTweetListener project
import tweepy
from tweepy import Stream
from lib.authentication import Authentication
from lib.tweetListener import Listener
from tweepy.auth import OAuthHandler

# API Authentication
auth = Authentication()
auth.getAuthorization()
api = tweepy.API(auth)
print("authorization  successful")

#Listen a Stream by topic
myStreamListener = Listener()
myStream = tweepy.Stream(auth=api.auth, listener=myStreamListener)
myStream.filter(track="world  cup")

Traceback (most recent call last): File "C:/Users/Sosa9/PycharmProjects/bigDataTweetListener/main.py", line 17, in myStream.filter(track="world cup") File "C:\Users\Sosa9\PycharmProjects\bigDataTweetListener\venv\lib\site-packages\tweepy\streaming.py", line 228, in filter self._start(async) File "C:\Users\Sosa9\PycharmProjects\bigDataTweetListener\venv\lib\site-packages\tweepy\streaming.py", line 172, in _start self._run() File "C:\Users\Sosa9\PycharmProjects\bigDataTweetListener\venv\lib\site-packages\tweepy\streaming.py", line 105, in _run self.auth.apply_auth(url, 'POST', self.headers, self.parameters) AttributeError: Authentication instance has no attribute 'apply_auth'

I don't know why the methods.attributes from the streaming class are not accesible if I have granted the access creating an instance of the authentication class. Any Ideas?

1

1 Answers

0
votes

First of all, you are mixin python2 and 3. Your should : "print(status)", not "print status" to use Python3. (print(data) too).

Secondly, the four first lines in getAuthorization() are useless : You already have declared those variables in __init__. So you can delete or comment them :

def getAuthorization(self):
    # self.consumer_key = consumer_key
    # self.consumer_secret = consumer_secret
    # self.secret = secret
    # self.token = token
    auth = OAuthHandler(self.consumer_key, self.consumer_secret)
    auth.set_access_token(self.token, self.secret)
    return auth

Finally, getAuthorization() returns auth, so replace this :

auth.getAuthorization()

by :

auth = auth.getAuthorization()