2

Basically when I loop up tweets that I know have been "liked" by seeing them on Twitter, and print their favorite count attribute, the count is always 0. Is favorite count not the same as the number of likes/why is favorite count always 0/how do I get the number of likes of a tweet?

Right now I am doing the following:

print(the_tweet.favorite_count)

When I print:

print(dir(the_tweet)) 

I see a lot of stuff to do with a tweet, including retweet_count and favorite_count but nothing that looks like a "like_count".

Mark Keane
  • 984
  • 2
  • 11
  • 26
  • `favorite_count`: `Integer`, Nullable. "Indicates approximately how many times this Tweet has been liked by Twitter users. Example: "favorite_count":1138" https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/tweet-object – chickity china chinese chicken Oct 03 '17 at 02:58
  • I thought it meant "like count" but i just dont get why it is 0 for literally every tweet I look up- so weird. – Mark Keane Oct 03 '17 at 03:01
  • maybe some helpful / related information in this thread: [Retweet_count and favorite count always zero](https://twittercommunity.com/t/retweet-count-and-favorite-count-always-zero/11092/30). "Newly created retweets have non-zero retweet_count and favorite_count values as children of the "retweeted_status" node, as is appropriate/typical depending on the states of the original tweet...", etc – chickity china chinese chicken Oct 03 '17 at 03:01

4 Answers4

0

I just used this and worked for me. I was having the same problem, it happened because I was using 'tweepy' and it returns two values for favorite_count in the json api, one with the right value and other with '0'. When you use the 'Twython' it returns just one value, the first one.

pip install twython
from twython import Twython

id_of_tweet = <tweet_number_id>

CONSUMER_KEY = "<consumer key>"
CONSUMER_SECRET = "<consumer secret>"
OAUTH_TOKEN = "<application key>"
OAUTH_TOKEN_SECRET = "<application secret>"

twitter = Twython(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, 
OAUTH_TOKEN_SECRET)
tweet = twitter.show_status(id=id_of_tweet)
print(tweet)
print(tweet['retweet_count'])
print(tweet['favorite_count'])

Part of this solution was in this slack: Twitter API - get tweets with specific id

0

I had the same issue, favorite_count shows the likes of your own tweet, while the_tweet object is your user_timeline object which is a mixture of tweets and retweets. Therefore you need to check to see if retweeted_status exist and if so favorite_count for the retweeted tweets are under this list. Please check my example below:

def user_tweets(count):
    '''
    This function uses tweepy to read the user's twitter feed. 
    '''
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth)
    # user = api.get_user(twitter_user_name)

    user_tweets = api.user_timeline(twitter_user_name, count=count)
    return user_tweets

user_tweets(4)# returns your latest 4 tweets and retweets

#to list the number of likes for each tweet and retweet:
for tweet in user_tweets: 
    try: 
        print(tweet.retweeted_status.favorite_count) 
    except: 
        print(tweet.favorite_count)

Also if you are using Django template tags you can use the following:

            {% if tweet.retweeted_status %}
            <small>{{ tweet.retweeted_status.favorite_count }}</small>
            {% else %}
            <small>{{ tweet.favorite_count }}</small>
            {% endif %}
0

I had a similar issue. For me the problem was that the tweet was a retweet, which is handled as follows. Include a new tweet.fields value referenced_tweets along with public_metrics, which will return any referenced tweets as IDs. Then use this ID(s) in a new call (same route) along with public_metrics to get the true metrics for a retweet tweet.

chrisheseltine
  • 193
  • 2
  • 13
0

It works for me


try:
    likecount = tweet._json['retweeted_status']['favorite_count']
except:
    likecount = 0