Quantcast
Viewing all articles
Browse latest Browse all 4

Answer by Mahesh Jamdade for Using tweepy to stream users' timeline and filtered tweets

You can fetch a particular users tweets in real time as per the below code sample.

import tweepyfrom tweepy import OAuthHandler, Stream, StreamListenerusername="maheshmnj" # Add your target users username hereuserId=12345678 # Add target users userId class TweetListener(StreamListener):""" A listener handles tweets that are received in real time.  """    def on_data(self, status):        try:            data = json.loads(status)            user = data['user']['screen_name']            tweet_id = data['id']            if(user == f'{username}'):                print(f'user tweeted, his tweetId=@{tweet_id} do something with the Tweet');             else:                print('found user',user)        except Exception as e:            print(e)            print('failed to retweet')        return True    def on_error(self, status_code):        if status_code == 420:            # returning False in on_data disconnects the stream            return False        print(status_code)if __name__ == '__main__':    print(f'bot started listening to {username}')    listen = TweetListener()    auth = tweepy.OAuthHandler(APP_KEY, APP_SECRET)    auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)    api = tweepy.API(auth)    stream = Stream(auth, listen)    stream.filter(follow=[userId],)

Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>