3

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).

takendarkk
  • 3,347
  • 8
  • 25
  • 37
tomasyany
  • 1,132
  • 3
  • 15
  • 32
  • What error or output is occurring? – dorvak Apr 09 '14 at 15:59
  • It just stay forever in the stream.next(). No error. I guess I am using it wrong, that is why I wonder if anyone knows how to do it right. I tried then tweepy (another library) which is more easy to use and it worked after some effort, but I would prefer to stay with python-twitter due to its other functions. – tomasyany Apr 09 '14 at 18:30

2 Answers2

2

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)
Diego Cerdan Puyol
  • 1,134
  • 1
  • 12
  • 13
1

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
logc
  • 3,813
  • 1
  • 18
  • 29