I used tweepy for v1.1 Twitter streaming to follow a Twitter account. When someone is tweeting this user for any tweet (let say download this video bot), I get a lot of details about the tweet that the user mentioned and the tweet that has the video. (tweet info, video links etc)
used to be something like this:
class StdOutListener(tweepy.Stream):
def on_data(self, data):
# process stream data here
struct = json.loads(data)
struct
has a lot of data in form of json
In API v2, I have the following:
class StdOutListener(tweepy.StreamingClient):
def on_tweet(self, tweet):
print(tweet)
print(tweet.data)
print(tweet.entities)
print(f"{tweet.id} {tweet.created_at} ({tweet.author_id}): {tweet.text}")
I am getting just few info as follows:
INFO:tweepy.streaming:Stream connected
@2hvQqjddfgfdUY96Ah5yW @7bdsfdsfds3h_bot
{'edit_history_tweet_ids': ['1617291424910688874'], 'id': '1617291424910688874', 'text': '@2hvQqjddfgfdUY96Ah5yW @7bdsfdsfds3h_bot'}
None
1617291424910688874 None (None): @2hvQqjddfgfdUY96Ah5yW @7bdsfdsfds3h_bot
How can I get all the data as it was in v1.1?
The rest of my code is like this:
printer = StdOutListener(bearer_token)
# add new rules
rule = StreamRule(value="@7bdsfdsfds3h_bot")
printer.add_rules(rule)
printer.filter()
2
Answers
I was able to get more details by using
tweet_fields
andexpansions
.Here is an example,
Then from the event
on_data
I was able to get the needed info as follows:You can find all available fields listed in the
postman
example request referenced in the Tweeter’s Filtered Stream documentation:https://www.postman.com/twitter/workspace/twitter-s-public-workspace/request/9956214-977c147d-0462-4553-adfa-d7a1fe59c3ec
So the full blown
tweepy
code will look like this: