Unable to stop Streaming in tweepy after one minute

后端 未结 3 536
逝去的感伤
逝去的感伤 2021-02-03 12:26

I am trying to stream twitter data for a period of time of say 5 minutes, using the Stream.filter() method. I am storing the retrieved tweets in a JSON file. The problem is I am

相关标签:
3条回答
  • 2021-02-03 12:31

    Access the variable myListener.running but instead of passing MyListener directly to Stream create a variable as follows:

    myListener = MyListener()
    timeout code here... suchas time.sleep(20)
    myListener.running = False 
    
    0 讨论(0)
  • 2021-02-03 12:35

    So, I was having this issue as well. Fortunately Tweepy is open source so it's easy so dig into the problem.

    Basically the important part is this here:

    def _data(self, data):
        if self.listener.on_data(data) is False:
            self.running = False
    

    On Stream class in streaming.py

    That means, to close the connection you just have to return false on the listener's on_data() method.

    0 讨论(0)
  • 2021-02-03 12:55
    1. In order to close the stream you need to return False from on_data(), or on_status().

    2. Because tweepy.Stream() runs a while loop itself, you don't need the while loop in on_data().

    3. When initializing MyListener, you didn't call the parent's class __init__ method, so it wasn't initialized properly.

    So for what you're trying to do, the code should be something like:

    class MyStreamListener(tweepy.StreamListener):
        def __init__(self, time_limit=60):
            self.start_time = time.time()
            self.limit = time_limit
            self.saveFile = open('abcd.json', 'a')
            super(MyStreamListener, self).__init__()
    
        def on_data(self, data):
            if (time.time() - self.start_time) < self.limit:
                self.saveFile.write(data)
                self.saveFile.write('\n')
                return True
            else:
                self.saveFile.close()
                return False
    
    myStream = tweepy.Stream(auth=api.auth, listener=MyStreamListener(time_limit=20))
    myStream.filter(track=['test'])
    
    0 讨论(0)
提交回复
热议问题