Stephen Wong
Stephen Wong

Reputation: 187

Obtaining Friends Count And Favorites Count From User Object

I am having trouble obtaining friends_count and favorites_count using the search_all_tweets Tweepy V2 API call.

GeeksForGeeks lists friends_count and favorites_count as attributes ( https://www.geeksforgeeks.org/python-user-object-in-tweepy/). Unfortunately, I get an Attribute Error raise AttributeError from None with the last 2 lines of code.

user.public_metrics only consists of followers_count,following_count,tweet_count, and listed_count.

user.entities consist of extraneous url data.

Code is shown below:

client = tweepy.Client(bearer_token=config.BEARER_TOKEN, consumer_key=
config.CONSUMER_KEY,consumer_secret= config.CONSUMER_SECRET,access_token=
config.ACCESS_TOKEN,access_token_secret= config.ACCESS_TOKEN_SECRET)

for response in tweepy.Paginator(client.search_all_tweets, query=s,
                                    tweet_fields=['context_annotations','created_at', 'public_metrics','author_id', 'lang', 'geo', 'entities'], 
                                    user_fields=['username','entities','public_metrics','location','verified','description'],
                                    max_results=100, expansions='author_id'):

    for user in response.includes["users"]:
        print(user.public_metrics)
        print(user.entities)
        print(user.friends_count)
        print(user.favorites_count)

Upvotes: 1

Views: 268

Answers (1)

Mickaël Martinez
Mickaël Martinez

Reputation: 1843

The fields listed by GeeksForGeeks are the User's fields in the Twitter V1 API.

There is unfortunately no way to get the number of likes of an User with the Twitter V2 API. You can try to get all his likes and count the total number of returned tweets, but that will work only if the User has only a few likes (and that will consume your monthly Tweet cap).

And friends was the previous name of followings, so the equivalent of friends_count in the Twitter V2 API is following_count. If you were looking for the mutuals, you have to get the full list of followers and the full list of followings of the user and count the number of common elements.

Finally, I would advise you to use the Twitter API documentation (here for User objects).

Upvotes: 2

Related Questions