在尝试学习 Python-tweepy api 时,我整理了一些代码,告诉我在最近的约 10 秒间隔内是否有任何新推文(当然,实际上它超过 10 秒,因为代码除了sleep(10)
还需要一些时间来运行):
from getApi import getApi
from time import sleep
from datetime import datetime
api = getApi()
top = api.home_timeline()[0].id
while True:
l = api.home_timeline()
if top != l[0].id:
top = l[0].id
print 'New tweet recognized by Python at: %s' % str(datetime.now())
sleep(10)
getApi 只是我编写的几行 Python 代码,用于使用 tweepy 管理 OAuth。getApi 方法返回 tweepy api,我的帐户已通过身份验证。
我不得不一次又一次地询问推特是否有任何新推文,这似乎非常低效。这是通常的做法吗?如果不是,那么“规范”的做法是什么?
我本来以为会有一些代码,例如:
api.set_home_timeline_handler(tweetHandler)
只要有新推文,就会调用 tweetHandler。