Stop streaming after 10 attempts?

filter
streaming

#1

Hi
I am using following piece of code. This code gives me outpur when it found Abhishek in my tweet, but it keeps on searching indefinitely. I want to stop after 5-10 searches and come out of this infinite loop saying. “not found”. But this code goes indefinite…please help

class MyStreamer(TwythonStreamer):
def on_success(self,data):

if ‘text’ in data:
track()
def on_stop(self, status_code, data):
print status_code
self.disconnect
stream = MyStreamer(C_KEY,C_SECRET, A_TOKEN, A_SECRET)
stream.statuses.filter(track= “Abhishek”)

I have even tried this, but no success

    class MyStreamer(TwythonStreamer):
            def on_success(self,data):      
                    if 'text' in data:
                            track()
            def on_stop(self, status_code, data):
                    print status_code
                    self.disconnect
            def on_error(self, status_code, data):
                    print status_code
    i=1     
    while i>0:
            try:

                    stream = MyStreamer(C_KEY,C_SECRET, A_TOKEN, A_SECRET)
                    stream.statuses.filter(track= "Abhishek")
                    i=i-1
                    break
            except KeyboardInterrupt:
                    print "moving out of twitter"
                    break

#2

The stream is designed to be permanently open receiving results as tweets are created, you might want https://dev.twitter.com/rest/reference/get/search/tweets if you want a set number of results.


#3

As @IgorBrigadir says, my immediate reaction to this is that you actually do not want to be using the streaming API here - if you want to make periodic calls to Twitter, the REST API is a better option. Can you explain why you chose the streaming option, in case I misunderstand the situation?


#4

Thank u for replying. I was trying trying to read the status of twitter and if status is same what i expect,i will perform some action else i will call another function.and again repeat the process to search the twitter status for message.


#5

Hi
could any of you provide sample code on how to use REST API in python? I need to acess tweets in real time and REST API could not do so (as I read somewhere) and drops some tweets as well. Is this correct?


#6

This is what the streaming API is for but you also then require that your code drops out and stops when an event occurs, which is odd. Can you describe the use case more clearly?


#7

Hi
Actually I am trying to plan the action of my hardware based upon the posts coming from twitter or facebook. So I need to check the status from both and if relevant text found on any of them, take some action from hardware.So I need to search twitter for specific keywords, if not found search in facebook, if not found anywhere, repeat the process. if found in any of them, perform some predefined action.


#8

Sounds like you have two options:

  • a background thread that listens constantly for events (e.g. on the streaming API), and then dispatches the events to a worker. Downside is that this is not an “if this, then do this, or check there” option.
  • a polling / search based solution that searches on a schedule (covers your if not Twitter then search elsewhere) case. This will hit a rate limit very fast if you keep looping.