【问题标题】:how can i refresh twitter filter stream thread in tweepy?如何在 tweepy 中刷新 twitter 过滤器流线程?
【发布时间】:2014-03-12 00:56:32
【问题描述】:

我使用 tweepy 来跟踪带有不断变化的主题标签列表的推文

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

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

【问题讨论】:

    标签: python multithreading twitter tweepy


    【解决方案1】:

    当我查看 Stream 类构造函数并找到(异步)参数并将其设置为 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
    

    【讨论】:

    • 值得指出的是,从 v3.8.0 开始,async 参数已重命名为 is_async
    • 现在是threaded
    【解决方案2】:

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

    on_status 方法中,您可以检查当前时间,如果三个小时过去,则返回False。然后,您可以再次调用 filter,从您的数据库查询中传递不同的主题标签。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-11
      • 1970-01-01
      • 2017-01-07
      • 2017-07-31
      • 2021-12-16
      • 2018-10-15
      • 2017-07-03
      • 2015-05-17
      相关资源
      最近更新 更多