Frage

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?

War es hilfreich?

Lösung

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

Andere Tipps

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top