如何在tweepy中刷新twitter过滤器流线程?

2024-05-19 10:54:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我用tweepy来跟踪带有不断变化的标签列表的tweets

twitterStream=Stream(OAuthObject,listenerFunction())
twitterStream.filter(track=[u'hashtags separated by comma'])

现在每隔3个小时,我应该从数据库中获取最新的标签,并刷新流线程,我该怎么做?


Tags: 列表streambytrack标签filtertweets小时
2条回答

您可以从on_status或其他回调方法返回False。这将取消流,该流将控制权返回给应用程序。在

on_status方法中,可以检查当前时间,如果已经过了三个小时,则返回False。然后可以再次调用filter,从数据库查询中传递不同的哈希标记。在

当我查看流类构造函数并找到(async)参数并将其设置为true时,我解决了这个问题,下面是我的代码:

twitterStream=Stream(OAuthObject,listenerFunction())
while True:
    if twitterStream.running is True:
        twitterStream.disconnect()
    keywords=getKeywordsFromDb() # return string of keywords seaprated by comma
    if keywords=='':
        print 'no keywords to listen to'
    else:
        twitterStream.filter(track=[keywords],async=True) # Open the stream to work on asynchronously on a different thread
    time.sleep(3600) # sleep for one hour

相关问题 更多 >

    热门问题