Tweepy public streams follow filter


#1

Hello
Is there any way to filter only tweets created by the user in “stream.filter(follow=)” and not to receive other stuff
i just have no idea how to do it

https://dev.twitter.com/docs/streaming-apis/parameters#follow


#2

hi do you find the way to solve the problem?
I am also feel no idea of this question now .


#3

Hello, twt1twt5 and MingyangPan

How about checking status.author in StreamListener class like this:

# users to follow
follow_users_ids = ['271828', '182846']

# override on_status()
class StreamListener(tweepy.StreamListener):
    def on_status(self, status):
        if status.author.id_str in follow_users_ids:
            prosess_tweet(status)
        
# run stream
stream_listener = StreamListener()
stream = tweepy.Stream(auth=api.auth, listener=stream_listener)
stream.filter(follow=follow_users_ids)

#4

hi dear shuuji3,
could you have a look at my code and tell me where it is wrong ?
follow_acc = [‘759251’] #cnn id
track_words = [‘New Zealand flag’]

class listener(StreamListener):



    def on_data(self, data):

        try:

##            print data

            

            

            tweet = data.split(',"text":"')[1].split('","source')[0]

            screen_name = data.split(',"screen_name":"')[1].split('","location')[0]

            followers_count = data.split(',"followers_count":')[1].split(',"friends_count')[0]

            

            saveThis = str(time.time())+'::'+ tweet +'::'+ screen_name +'::'+ followers_count

            print saveThis

            

            saveFile = open('twitDB11.txt','a')

            saveFile.write(saveThis)

            saveFile.write('\n')

            saveFile.close()

            

            return True

        except BaseException, e:

            print 'failed ondata,', str(e)

            time.sleep(5)

            

    def on_error(self, status_code):
        if status_code == 420:
            #returning False in on_data disconnects the stream
            return False

        print status

    def on_status(self, status):

        if status.author.id_str in follow_acc and track_words in status.text.lower() :
            prosess_tweet(status)


if __name__ == '__main__':

    
    twitterStream = Stream(auth, listener())
    twitterStream.filter(follow= follow_acc, track = track_words)

#5

Hi, MingyangPan

Tweepy’s on_data() method gives us a bit raw data. So Tweepy provides useful handlers methods such as on_status(), on_event(), etc. In your case, you don’t have to use on_data(), just use on_status() method.

I’d like to write code to do what your code intends like this code:

follow_acc = ['759251'] #cnn id 
track_words = ['New Zealand flag'] # if remove ReTweets, add '-RT' in the word

class Listener(StreamListener):
    def on_status(self, status):
        with open('twitDB11.txt', 'a') as f:
            # status.created_at is datetime object and status.text is the tweet's text
            text = '::'.join([str(status.created_at), status.text, status.author.screen_name]) + '\n'
            f.write(text)

if __name__ == '__main__':

    twitterStream = Stream(auth, Listener())
    twitterStream.filter(follow= follow_acc, track = track_words)

By running this code, I got twitDB11.txt which contains:

2015-12-18 17:04:05::RT @CNN: Just In: #StarWars: #TheForceAwakens has record $57 million opening night https://t.co/rCl9XK8Nwq https://t.co/RQz0Y38tvR::sddphoto
2015-12-18 17:04:08::@CNN Overhyped crap!!!::garrybluenose
2015-12-18 17:04:09::RT @CNN: Just In: #StarWars: #TheForceAwakens has record $57 million opening night https://t.co/rCl9XK8Nwq https://t.co/RQz0Y38tvR::imbernad88
2015-12-18 17:04:11::RT @CNN: Just In: #StarWars: #TheForceAwakens has record $57 million opening night https://t.co/rCl9XK8Nwq https://t.co/RQz0Y38tvR::SageOmar
2015-12-18 17:04:14::@CNN Just another point of view on stormtroopers...https://t.co/XWnniCiep5::tvn2884
2015-12-18 17:04:16::RT @CNN: Just In: #StarWars: #TheForceAwakens has record $57 million opening night https://t.co/rCl9XK8Nwq https://t.co/RQz0Y38tvR::Josh_B65
2015-12-18 17:04:16::RT @CNN: .@SenAngusKing: "A mistake" for @POTUS to downplay possible attack https://t.co/IPRP4vZ0p2 https://t.co/NFr2wlq1xy::PMorris153

#6

this is the problem i’d like to say that the feedback only give me @cnn but not 'new zealand '. i want to get feedback like @cnn: new zealand…

@shuuji3


#7
def on_status(self, status):
    try:
        if status.author.id_str in follow_acc:

            if track_words in status.text.lower():

                print status.text

            else:

                print("No keywords")

        else:

            print("No coordinates")

        return True

I use this code but keep getting No coordinates. I dont know where i wrong.