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
tweet
objects has auser
atribute, and theuser
attribute has ascreen_name
attribute, this is the username.You can get it this way:
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 theuser_fields
parameter to pass your desired user fields. However, by default,name
,username
andid
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 toon_data
. The data will be received as a json string, consisting ofdata
,includes
, andmatching_rules
fields(Refer https://docs.tweepy.org/en/stable/streamingclient.html).
Your code will look like this: