I use tweepy to track tweets with changing list of hashtags

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

Now every 3 hours ,i should get the latest hashtags from database , and refresh the stream thread , how can i do it?

有帮助吗?

解决方案

I solve it when i take a look to a Stream class constructor and found (async) parameter,and set it to true ,and here is my code :

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

其他提示

You can return False from on_status, or some other callback method. This cancels the stream, which returns control back to your application.

In the on_status method, you could check the current time, and return False if three hours have passed. You could then call filter again, passing different hashtags from your database query.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top