我正在制作一个简单的应用程序来流式传输 twitter 的公共时间线,我希望流式传输在一小时后自动停止,但我不知道该怎么做。我阅读了 datetime 和 timeit 文档,但无法理解它们。这是我的代码,它完美地但无限期地流式传输我想要的时间线。
from twython import TwythonStreamer
import json
import os
import datetime
from datetime import *
APP_KEY = 'XX'
APP_SECRET = 'XX'
OAUTH_TOKEN = 'XX'
OAUTH_TOKEN_SECRET = 'XX'
class MyStreamer(TwythonStreamer):
def on_success(self, data):
print data['text']
with open('scratch1.json', 'ab') as outfile:
json.dump(data, outfile, indent = 4)
with open('scratch2.json', 'ab') as xoutfile:
json.dump(data, xoutfile, indent = 4)
return
def on_error(self, status_code, data):
print status_code
return True # Don't kill the stream
def on_timeout(self):
print >> sys.stderr, 'Timeout...'
return True # Don't kill the stream
stream = MyStreamer(APP_KEY, APP_SECRET,
OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
stream.statuses.filter(follow = [95995660, 8820362])
谁能帮我?