简体   繁体   中英

How to get more details on streaming data using Tweepy StreamingClient of Twitter APIv2

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()

I was able to get more details by using tweet_fields and expansions .

Here is an example,

#...
#...
    printer.add_rules(rule)

    tweet_fields=['context_annotations', "referenced_tweets","lang","author_id","created_at","entities"]

    printer.filter(tweet_fields=tweet_fields,expansions=["referenced_tweets.id","in_reply_to_user_id"])

Then from the event on_data I was able to get the needed info as follows:

class StdOutListener(tweepy.StreamingClient):
    def on_data(self, data):
        print(data)

        struct = json.loads(data)
        text = struct['includes']['tweets'][1]['text']
        # get referenced_tweets info (of the second tweet)
        # the tweet that my account was mentioned to as a reply
        ref_user_screen = struct['includes']['users'][1]['name']
        ref_username = struct['includes']['users'][1]['username']
        ref_id = struct['includes']['users'][1]['id']

        # get info about the user who mentioned my account
        caller_username = struct['includes']['users'][0]['username']
        caller_user_screen = struct['includes']['users'][0]['name']

        tweet_id = struct['data']['id']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM