Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I proper use GetStreamFilter from Python-Twitter library?

I've been having some troubles using the GetStreamFilter function from the Python-Twitter library.

I have used this code:

import twitter
import time

consumer_key = 'myConsumerKey'
consumer_secret = 'myConsumerSecret'
access_token = 'myAccessToken'
access_token_secret = 'myAccessTokenSecret'

apiTest = twitter.Api(consumer_key,
                      consumer_secret,
                      access_token,
                      access_token_secret)

#print(apiTest.VerifyCredentials())

while (True):
    stream = apiTest.GetStreamFilter(None, ['someWord'])
    try:
        print(stream.next())
    except:
        print ("No posts")

    time.sleep(3)

What I want to do is to fetch new tweets that include the word "someWord", and to do these every three seconds (or every time there's a new one that is published).

like image 398
tomasyany Avatar asked Sep 09 '26 20:09

tomasyany


2 Answers

You need to create the stream only once and outside the loop.

stream = apiTest.GetStreamFilter(None, ['someWord'])

while True:
    for tweet in stream:
        print(tweet)
like image 84
Diego Cerdan Puyol Avatar answered Sep 11 '26 08:09

Diego Cerdan Puyol


How about replacing your while True with a loop that extracts things out of the stream?

for tweet in apiTest.GetStreamFilter(track=['someWord']):
    print tweet
like image 39
logc Avatar answered Sep 11 '26 10:09

logc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!