문제

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