skip to Main Content

I am currently trying to stream live tweets to my console of a specific user using the tweepy library (twitter api v2). I have found documentation for key words or hashtags but not to filter by the user who tweeted. Anyone know how I can add a rule to filter all tweets by say @elonmusk?

client = tweepy.Client(keys.BEARER_TOKEN, keys.API_KEY, keys.API_KEY_SECRET, keys.ACCESS_TOKEN, keys.ACCESS_TOKEN_SECRET)
auth = tweepy.OAuth1UserHandler(keys.API_KEY,keys.API_KEY_SECRET, keys.ACCESS_TOKEN, keys.ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)

class MyStream(tweepy.StreamingClient):

    def on_tweet(self, tweet):
        print(tweet.text)
        time.sleep(0.2)


stream = MyStream(bearer_token=keys.BEARER_TOKEN)

stream.add_rules({'from: elonmusk'})
print(stream.get_rules())

stream.filter()

2

Answers


  1. Chosen as BEST ANSWER

    ANSWER: I used the twarc CLT to solve this issue.

    Steps taken to add a rule to specify tweets by a user:

    1. pip3 install twarc
    2. twarc configure
    3. twarc2 stream-rules add "from:____" <--- put username there
    4. run program with code I have above just delete stream.add_rules({'from: elonmusk'}) print(stream.get_rules())

  2. class TweetListener(tweepy.StreamingClient):
    
        def on_tweet(self, tweet):
            print(tweet.text)
    
    
    stream = TweetListener(bearer_token=tw.bearer_token)
    
    stream.add_rules(tweepy.StreamRule("from:username01 OR from:username02"))
    stream.filter()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search