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

我怕爱的太早我们不能终老 提交于 2020-01-05 11:34:13

问题


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


回答1:


I found it out myself

the stream.filter() method needs an array

so i had to code

stream.filter(['1234567'])

et voilà




回答2:


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.



来源:https://stackoverflow.com/questions/2646295/tweepy-stream-filter-method-doesnt-work-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!