3

working on a project to get tweets for the celebrities who celebrate birthday today but problem is some of them are not active in twitter or tweets come very late. Is there any way to timeout from the tweepy streaming API after x seconds or minutes?

I tried searching on this but got below link which talks about stop collecting after x amount but my query is on time Tweepy Streaming - Stop collecting tweets at x amount

In my streamlistner class I used below function which never gets executed

class MyStreamListener(StreamListener):

    def __init__(self, api=None):
        super(MyStreamListener, self).__init__()
        self.num_tweets = 0
        self.start_time = time.time()
        self.time_limit = 120

    def on_status(self, status):
            global twee_file,twee_outfile
        #Don't wait forever for tweets instead wait for 2 minutes
        #while (time.time() - self.start_time) < self.time_limit :
            self.num_tweets += 1
            if self.num_tweets < 2:
                twee_outfile.write(status.text)
                twee_outfile.write(str("\n"))
                print(status.text)
                print "Author's Screen name : %s" % status.author.screen_name
                print "Time of creation : %s" % status.created_at
                print "Source of Tweet : %s" % status.source
                return True
            else:
                twee_outfile.close()
                return False
        #else:
           # print "Time Exhausted"
           # return False

    def on_error(self, status_code):
        print "Too soon reconnected . Will terminate the program"
        print status_code
        sys.exit()
    def on_timeout(self):
        print  'Timeout...'
twitterStream = Stream(auth,listener= MyStreamListener(),timeout=60)

I set timeout as 60 but even if there is no tweet for 5 or more minutes the above function never called. I also tried checking time in on_status but problem is if we don't have any tweet at all then this function wont get executed.

Is there any way to solve this?

Vinod Srivastav
  • 3,644
  • 1
  • 27
  • 40
sireesh
  • 31
  • 2

0 Answers0