Python Tweepy – Checking whether a tweet has been liked or not
In this article we will see how we can check whether a tweet/status has been liked/favourited by the authenticated user or not. The favorited attribute of the Status object indicates whether the status has been favourited by the authenticated user or not.
Identifying whether the status has been favourited by the authenticated user or not in the GUI :
In the above mentioned status, the heart icon is in red color, therefore the authenticated user has liked the tweet.
In order to check whether the status has been favourited by the authenticated user or not, we have to do the following :
- Identify the status ID of the status from the GUI.
- Get the Status object of the status using the
get_status()
method with the status ID.
- From this object, fetch the favorited attribute present in it.
Example 1 : Consider the following status :
We will use the status ID to fetch the status. The status ID of the above mentioned status is 1272771459249844224.
import tweepy
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
id = 1272771459249844224
status = api.get_status( id )
favorited = status.favorited
if favorited = = True :
print ( "The authenticated user has liked the tweet." )
else :
print ( "The authenticated user has not liked the tweet." )
|
Output :
The authenticated user has not liked the tweet.
Example 2 : Consider the following status :
We will use the status ID to fetch the status. The status ID of the above mentioned status is 1272479136133627905.
id = 1272479136133627905
status = api.get_status( id )
favorited = status.favorited
if favorited = = True :
print ( "The authenticated user has liked the tweet." )
else :
print ( "The authenticated user has not liked the tweet." )
|
Output :
The authenticated user has liked the tweet.
Last Updated :
18 Jun, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...