skip to Main Content

I have a pretty basic application which uses Tweepy’s StreamingClient to stream tweets from a defined list of users. When one of them tweets, I have a couple conditions based on keywords to determine if I should alert myself. I want it to send me both the text of the tweet and the username of who sent it. I can do the former, but can figure out how to get the username.

From what I have seen I need to use expansions, but I’m a complete novice and don’t know how I would integrate that into my code because the majority of the documentation for expansions is on the twitter API and I’m not sure how to apply that to my python code which uses tweepy.

Stripped down version of my code, currently just handling the tweet text is as follows ( I want to print user name in addition to tweet.text):

import tweepy
import json
import re
import logging


class MyListener(tweepy.StreamingClient):
    def on_tweet(self, tweet):
        keyword = ["xxxx", "yyyy","zzzz"]
        key_patterns = [r'b%sb' % re.escape(s.strip()) for s in keyword]
        key_there = re.compile('|'.join(key_patterns))
        if key_there.search(tweet.text):
            print(tweet.text)
       
        else:
            print("No Match")
        
        
    def on_error(self, status):
        print(status)
        return True
 
twitter_stream = MyListener("token")

twitter_stream.get_rules()
twitter_stream.filter()

2

Answers


  1. tweet objects has a user atribute, and the user attribute has a screen_name attribute, this is the username.
    You can get it this way:

    class MyListener(tweepy.StreamingClient):
        def on_tweet(self, tweet):
            keyword = ["xxxx", "yyyy","zzzz"]
            key_patterns = [r'b%sb' % re.escape(s.strip()) for s in keyword]
            key_there = re.compile('|'.join(key_patterns))
            if key_there.search(tweet.text):
                print(tweet.text)
                print(tweet.user.screen_name)
           
            else:
                print("No Match")
    
    Login or Signup to reply.
  2. I’m a little late here, nevertheless, I shall be dropping an answer in case anybody finds it helpful in the future. To access User data in Twitter API V2, you need to pass expansions = author_id to the filter method. Now, you can use the user_fields parameter to pass your desired user fields. However, by default, name, username and id will be returned. Specify any other desired fields in a comma-separated list without spaces between commas and fields. (Refer https://docs.tweepy.org/en/stable/expansions_and_fields.html)

    For the different user fields, refer https://developer.twitter.com/en/docs/twitter-api/data-dictionary/object-model/user

    You also need to change the on_tweet method to on_data. The data will be received as a json string, consisting of data, includes, and matching_rules fields
    (Refer https://docs.tweepy.org/en/stable/streamingclient.html).

    Your code will look like this:

    import tweepy
    import json
    import re
    import logging
    
    
    class MyListener(tweepy.StreamingClient):
         def on_data(self, data):
            jsonData = json.loads(data)
            print(jsonData) #notice the different fields. 'username' comes with the key 'users' which itself comes in the 'includes' field.
            tweetText = jsonData['data']['text']
            username = jsonData['includes']['users']['username'] 
            print(username)
            keyword = ["xxxx", "yyyy","zzzz"]
            key_patterns = [r'b%sb' % re.escape(s.strip()) for s in keyword]
            key_there = re.compile('|'.join(key_patterns))
            if key_there.search(tweetText):
                print(tweetText)
           
            else:
                print("No Match")
            
            
        def on_error(self, status):
            print(status)
            return True
     
    twitter_stream = MyListener("token")
    
    twitter_stream.get_rules()
    twitter_stream.filter(expansions = ['author_id'], user_fields = ["<required fields>"])
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search