开发者

tweepy stream.filter() method doesn't work properly

开发者 https://www.devze.com 2022-12-27 13:41 出处:网络
i\'ve got some problems with the tweepy api. I\'m just tryin to write a little app that gets me a stream of statuses of one user (ore more), but one would be fine to start with ;-)

i've got some problems with the tweepy api.

I'm just tryin to write a little app that gets me a stream of statuses of one user (ore more), but one would be fine to start with ;-)

now: my code is like that:

    def main():
       config = ConfigParser.ConfigParser()
       config.read('twitter.cfg')

       username = config.get('Twitter', 'username')
       password = config.get('Twitter', 'password') 
           listener = StreamWatcherListener()

       stream = tweepy.开发者_StackOverflow中文版Stream(username, password, listener, timeout=None)
       stream.filter('132897940')

in StreamWatcherListener I have a method "on_status" that prints the text of a status, whenever a new one arrives (everything seems to work, when I try stream.sample() instead of stream.filter())

the given ID is my testaccount, so whenever I tweet I should get some response in the console....but nothing happens.

when I try

curl -d @following http://stream.twitter.com/1/statuses/filter.json -uAnyTwitterUser:Password

in the terminal as I could find in the twitter api, everything runs fine.

So maybe I make wrong use of the filter()-method?

any suggestions?

-andy


I found it out myself

the stream.filter() method needs an array

so i had to code

stream.filter(['1234567'])

et voilà


class TweetListener(StreamListener):
    def on_status(self,status):           
        print "TWEET ARRIVED!!!"
        print "Tweet Text : %s" % status.text
        print "Author's name : %s" % status.author.screen_name
        print "Time of creation : %s" % status.created_at
        print "Source of Tweet : %s" % status.source    
        time.sleep(10)       
        return True

    def on_error(self, status):        
       print status
       if status == 420:
            print "Too soon reconnected, Exiting!!"
            return False
        sys.exit()

def search_tweets():
    twitterStream = Stream(connect().auth, TweetListener())        
    twitterStream.filter(track=['Cricket','Maths','Army','Sports'],languages = ["en"],async=True)

Here I used the async parameter, it runs each stream on a different thread. Refer this link for documentation or more details.

0

精彩评论

暂无评论...
验证码 换一张
取 消