为什么直播推文时会有延迟?
Why is there a delay when streaming tweets live?
我正在尝试通过来自特定用户的 tweepy 流获取实时推文数据,但是我发现从发布推文的确切时间戳到我的打印文本的时间戳恰好有 4 秒的延迟tweepy 程序。这是 normal/expected 还是有什么方法可以让我的代码更有效率?谢谢!
# # # # TWITTER STREAMER # # # #
class TwitterStreamer():
"""
Class for streaming and processing live tweets.
"""
def __init__(self):
pass
def stream_tweets(self):
# This handles Twitter authetification and the connection to Twitter Streaming API
listener = TweetListener()
auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
stream = Stream(auth, listener)
# This line filter Twitter Streams to capture data by the keywords:
stream.filter(follow=['user_id'])
# # # # TWITTER STREAM LISTENER # # # #
class TweetListener(StreamListener):
#This is a basic listener that just prints received tweets
#Only returns the tweets of given user
def on_status(self, status):
if status.user.id_str != 'user_id':
return
print(status.text)
def on_data(self, data):
try:
json_load = json.loads(data)
text = json_load['text']
if 'RT @' not in text:
print(text)
print(datetime.now())
return True
except BaseException as e:
print("Error on_data %s" % str(e))
return True
def on_error(self, status):
print(status)
if __name__ == '__main__':
streamer=TwitterStreamer()
streamer.stream_tweets()
那 more-or-less 会是真的,是的。延迟取决于许多因素,例如网络连接和位置,但通常我希望 small 延迟几秒钟。
我正在尝试通过来自特定用户的 tweepy 流获取实时推文数据,但是我发现从发布推文的确切时间戳到我的打印文本的时间戳恰好有 4 秒的延迟tweepy 程序。这是 normal/expected 还是有什么方法可以让我的代码更有效率?谢谢!
# # # # TWITTER STREAMER # # # #
class TwitterStreamer():
"""
Class for streaming and processing live tweets.
"""
def __init__(self):
pass
def stream_tweets(self):
# This handles Twitter authetification and the connection to Twitter Streaming API
listener = TweetListener()
auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
stream = Stream(auth, listener)
# This line filter Twitter Streams to capture data by the keywords:
stream.filter(follow=['user_id'])
# # # # TWITTER STREAM LISTENER # # # #
class TweetListener(StreamListener):
#This is a basic listener that just prints received tweets
#Only returns the tweets of given user
def on_status(self, status):
if status.user.id_str != 'user_id':
return
print(status.text)
def on_data(self, data):
try:
json_load = json.loads(data)
text = json_load['text']
if 'RT @' not in text:
print(text)
print(datetime.now())
return True
except BaseException as e:
print("Error on_data %s" % str(e))
return True
def on_error(self, status):
print(status)
if __name__ == '__main__':
streamer=TwitterStreamer()
streamer.stream_tweets()
那 more-or-less 会是真的,是的。延迟取决于许多因素,例如网络连接和位置,但通常我希望 small 延迟几秒钟。