Stream tweets in tweepy only by user id

Viewed 1106

While this code does display tweets by the user, But even if the user is mentioned by someone, then the tweets display. In short, how to just display tweets by user and not Mentions and not retweets.

class listener(StreamListener):

    def on_status(self, data):
        try:
            if (not data.retweeted) and ('RT @' not in data.text):

                print(data.text)
                print(data.user.name)
                    #print(data.user.location)
                    #print tweetf
                saveThis=str(time.ctime())+'::'+data.text+'/n'
                saveFile = open('twitDB3.txt','a')
                saveFile.write(saveThis)
                saveFile.write('\n')
                saveFile.close()
                return(True)
        except BaseException, e:
            print 'failed on data,',str(e)
            time.sleep(5)

    def on_error(self, status):
        print status

auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)

twitterStream = Stream(auth, listener())
twitterStream.filter(follow=["87725838498"]);
1 Answers

Below code will give you user tweets. Update kwargs data according to you

auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
api = tweepy.API(auth)
kwargs = {'screen_name': <Username of user>,'include_rts': 0, 'count': <Number of tweets, max:200>}
tweets = api.user_timeline(**kwargs) #tweet object has tweets of user
Related