skip to Main Content

I am trying to follow the example here but I want more than one listener. How to create multiple listeners without multithreading?

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

#Variables that contains the user credentials to access Twitter API
access_token = "<...>"
access_token_secret = "<...>"
consumer_key = "<...>"
consumer_secret = "<...>"

#This is a basic listener that just prints received tweets to stdout.
class StdOutListener(StreamListener):

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

    def on_error(self, status):
        print status


if __name__ == '__main__':

    #This handles Twitter authetification and the connection to Twitter Streaming API
    l = StdOutListener()
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    stream = Stream(auth, l)

    #This line filter Twitter Streams to capture data by the keywords: 'python', 'javascript', 'ruby'
    stream.filter(track=['python', 'javascript', 'ruby'])

    print "Control never seems to arrive here"

    l = StdOutListener()
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    stream = Stream(auth, l)

    #This line filter Twitter Streams to capture data by the keywords: 'python', 'javascript', 'ruby'
    stream.filter(track=['django', 'angularjs', 'rails'])

Stackoverflow wants more text
Stackoverflow wants more text
Stackoverflow wants more text

2

Answers


  1. I have come across the same question here myself.
    My solution to this has been to run different instances of the script within a new command line process. If you do this, you will need to save each “version” of your script as a separate file.
    There is probably a much better solution for this, but it is the best I have right now.

    Hope this helps at least a little.

    • Az
    Login or Signup to reply.
  2. As noted in the Connections section of the streaming API documentation, Twitter only allows you one connection to the streaming endpoint. Too many connection attempt may lead to them banning your IP.

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