23

I'm trying to get info about users, who added specific tweet to favorites, but I can't find it in documentation.

It is unfair that twitter can do that, but doesn't give this method as API.

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
gaussblurinc
  • 3,642
  • 9
  • 35
  • 64
  • 2
    see https://stackoverflow.com/questions/28982850/twitter-api-getting-list-of-users-who-favorited-a-status – sovon Jun 14 '17 at 14:59
  • 1
    Twitter has officially stated that [they have no plans of providing an API endpoint for this functionality](https://stackoverflow.com/questions/12368684/twitter-api-how-to-get-users-id-who-favorite-specific-tweet). – Dan Dascalescu Sep 03 '19 at 05:58

4 Answers4

12

Apparently, the only way to do this is to scrape Twitter's website:

import urllib2
from lxml.html import parse

#returns list(retweet users),list(favorite users) for a given screen_name and status_id
def get_twitter_user_rts_and_favs(screen_name, status_id):
    url = urllib2.urlopen('https://twitter.com/' + screen_name + '/status/' + status_id)
    root = parse(url).getroot()

    num_rts = 0
    num_favs = 0
    rt_users = []
    fav_users = []

    for ul in root.find_class('stats'):
        for li in ul.cssselect('li'):

            cls_name = li.attrib['class']

            if cls_name.find('retweet') >= 0:
                num_rts = int(li.cssselect('a')[0].attrib['data-tweet-stat-count'])

            elif cls_name.find('favorit') >= 0:
                num_favs = int(li.cssselect('a')[0].attrib['data-tweet-stat-count'])

            elif cls_name.find('avatar') >= 0 or cls_name.find('face-pile') >= 0:#else face-plant

                for users in li.cssselect('a'):
                    #apparently, favs are listed before retweets, but the retweet summary's listed before the fav summary
                    #if in doubt you can take the difference of returned uids here with retweet uids from the official api
                    if num_favs > 0:#num_rt > 0:
                        #num_rts -= 1
                        num_favs -= 1
                        #rt_users.append(users.attrib['data-user-id'])
                        fav_users.append(users.attrib['data-user-id'])
                    else:                        
                        #fav_users.append(users.attrib['data-user-id'])
                        rt_users.append(users.attrib['data-user-id'])

        return rt_users, fav_users


#example
if __name__ == '__main__':
    print get_twitter_user_rts_and_favs('alien_merchant', '674104400013578240')
Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
Alien Merchant
  • 149
  • 1
  • 3
11

Short answer: You can't do this perfectly.

Long answer: You can do this with some effort but it isn't going to be even close to perfect. You can use the twitter api to monitor the activity of up to 4000 user id's. If a tweet is created by one of the 4k people you monitor, then you can get all the information including the people who have favourited the tweet. This also requires that you push all the information about the people you monitor onto a database (I use mongodb). You can then query the database for information about your tweet.

Amitash
  • 1,029
  • 6
  • 16
  • 26
  • 1
    i like short answers. if twitter doesn't want to show me favorites, it is not my trouble, but trouble of api, uh :\ Thanks! – gaussblurinc Sep 11 '12 at 11:48
  • You can use https://dev.twitter.com/docs/api/1.1/get/favorites/list to get the 20 latest favourites of a user and search for your tweet in it. But this is very limited as it returns only 20. – Amitash Sep 11 '12 at 11:51
  • 6
    yeah, and i can't get all users id, yes? i need method like: `statuses/favorites/users` – gaussblurinc Sep 11 '12 at 11:54
2

Twitter API v2 has new likes functionality:

https://twittercommunity.com/t/announcing-twitter-api-v2-likes-lookup-and-blocks-lookup/154353

To get users who have liked a Tweet, use the GET /2/tweets/:id/liking_users endpoint.

They've also provided example code on their github repo.

hsl
  • 670
  • 2
  • 10
  • 22
-9

Use the endpoint favorites/list with max_id set to the tweet you're looking for.

https://dev.twitter.com/rest/reference/get/favorites/list

Karthik Murugan
  • 1,429
  • 3
  • 17
  • 28
  • 3
    For those wondering about the downvotes, this API gets the favourites/likes of a *user*. Yes, you could use some of the parameters to find whether a specific user has liked the tweet, but you can't use it to say "out of EVERY user on Twitter, who are the 42 users who liked tweet 123456?" – IBBoard May 03 '20 at 09:30