gael1130
gael1130

Reputation: 51

How to get the total engagement on twitter

I would like to get the average engagement on twitter, which is: likes + retweets + comments over the last 30 tweets. I saw that you can get likes and retweets with tweepy:

tweepy_api = tweepy.API(auth)
user = tweepy_api.get_user('test_name')
# fetching the last 30 tweets
my_tweets = tweepy_api.user_timeline(screen_name=('test_name',
                                     count=30,
                                     include_rts=True,
                                     tweet_mode='extended'

)
reactions = []
for el in my_tweets:
    reactions.append(el.retweet_count)
    reactions.append(el.favorite_count)

average = sum(reactions) / len(reactions)

However it doesn't get the retweets when I double checked and I do not know why.

I also found this to extract a tweet's replies:

name = 'LunarCRUSH'
tweet_id = '1270923526690664448'
replies=[]
for tweet in tweepy.Cursor(api.search,q='to:'+name, result_type='recent', timeout=999999).items(1000):
    if hasattr(tweet, 'in_reply_to_status_id_str'):
        if (tweet.in_reply_to_status_id_str==tweet_id):
            replies.append(tweet)

And I guess I would just have to add the length of the replies to my favorites and retweets to get engagement but once again, it doesn't work and I do not know what to do. Any help would be really appreciated. Thanks,

Upvotes: 0

Views: 1171

Answers (1)

christheliz
christheliz

Reputation: 306

import tweepy
from tweepy import OAuthHandler
import json
from timeit import default_timer as timer

consumer_key = 'YOUR-CONSUMER-KEY'  # Use your own key. To get a key https://apps.twitter.com/
consumer_secret = 'YOUR-CONSUMER-SECRET'
access_token = 'YOUR-ACCESS-TOKEN'
access_secret = 'YOUR-ACCESS-SECRET'


auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)

Set up the API with the authentication handler

api = tweepy.API(auth)

tweets = api.user_timeline(screen_name='someAccount', count=30)

likes = 0 
retweets = 0  
comments = 0 

Iterating through all the tweets one by one from the beginning (the most recent one). We are using tweet mode to get extended text instead of truncated text which is default behaviour for python-twitter library. This will help us to get more information about each tweet like comments, retweets and likes etc.. . More info at https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/tweet-object and https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/intro-to-tweet-json#extendedtweet.

Also read this link : Tweepy - Exclude Retweets . It has some other alternatives too

The below code will give us total number of likes, retweets and comments for each tweet in a single line separated by commas as shown below : Likes, Retweets, Comments for each tweet

So we are appending them to their respective variables likes, retweets and comments.

for tweet in tweets:   


    likes += tweet.favorite_count  #Likes on the last 30 tweets of some twitter account.
    retweets += tweet.retweet_count  #Retweets on the last 30 tweets of some twitter account.
    comments += len(tweet._json['entities']['hashtags'])  #Comments on the last 30 tweets of some twitter account.
total = likes + retweets + comments  #Total engagement on twitter for some twitter account is : Likes + Retweets + Comments over the last 30 tweets

print("Likes =",likes)   #Printing total number of likes

             

We can print other two variables retweets and comments similarly:

print("Retweets =",retweets)
print("Comments =",comments)

Total engagement over the last 30 tweets will be printed as shown below:

print("Total engagement =",total)

Upvotes: 2

Related Questions