试图让我的tweepy侦听器定期工作

时间:2018-12-24 05:37:40

标签: python time tweepy

这是我必须发送推文的代码的修改版本。现在,我尝试添加一些行以使其符合Twitter的速率限制,但是我无法使其正常工作。我的目标是让它运行一段时间,以避免所有错误。

start_time = time.time() #grabs the system time

class CustomStreamListener(tweepy.StreamListener):
    def __init__(self, start_time, time_limit):
        self.time = start_time
        self.limit = time_limit
        self.tweet_data = []

    def on_data(self, data):
        while (time.time() - self.time) < self.limit:
            try:
                with open('unlimited.json', 'a') as f:
                    f.write(data)
                    return True
            except BaseException as e:
                print("Error on_data: %s" % str(e))
            return True

    def on_error(self, status_code):
        print >> sys.stderr, 'Encountered error with status code:', status_code
        time.sleep(5)
        return True # Don't kill the stream

    def on_timeout(self):
        print >> sys.stderr, 'Timeout...'
        return True # Don't kill the stream

sapi = tweepy.streaming.Stream(auth, CustomStreamListener(start_time, time_limit=20))

0 个答案:

没有答案