18

I want to get a list of users who favorited a specific status through the Twitter API. I can see that each statuses have the amount of favorites it got but I need the list of users who made the favorite.

Any ideas how this can be achieved?

Ran
  • 3,455
  • 12
  • 47
  • 60
  • This is not exposed in the API. – Terence Eden Mar 11 '15 at 11:33
  • any workaround to get this data? i've seen people talking about using the stream for that - not exactly the same scenario.... – Ran Mar 11 '15 at 11:36
  • You can use streams to see who favourited you *own* tweets - but not other people's. – Terence Eden Mar 11 '15 at 11:38
  • any other way to get people who favorited my tweets (except stream)? – Ran Mar 11 '15 at 11:42
  • 1
    No. Read the API documentation - it doesn't provide you with that functionality. Sorry. – Terence Eden Mar 11 '15 at 12:20
  • 1
    The official Twitter App for Android can show a list of users who liked a post and retweeted it. So it is exposed somehow through private api calls. – deltaray Feb 09 '18 at 14:51
  • 1
    Exact duplicate of [Twitter API: How to get users ID, who favorite specific tweet?](https://stackoverflow.com/questions/12368684/twitter-api-how-to-get-users-id-who-favorite-specific-tweet) – Dan Dascalescu Sep 03 '19 at 05:53
  • It's now possible with the v2 endpoint. https://github.com/twitterdev/Twitter-API-v2-sample-code/blob/master/Likes-Lookup/liking_users.py https://twittercommunity.com/t/announcing-twitter-api-v2-likes-lookup-and-blocks-lookup/154353 – hsl May 31 '21 at 18:51

1 Answers1

11

Here is a workaround or hack implemented in Python 2.7.x:

import urllib2
import re

def get_user_ids_of_post_likes(post_id):
    try:
        json_data = urllib2.urlopen('https://twitter.com/i/activity/favorited_popup?id=' + str(post_id)).read()
        found_ids = re.findall(r'data-user-id=\\"+\d+', json_data)
        unique_ids = list(set([re.findall(r'\d+', match)[0] for match in found_ids]))
        return unique_ids
    except urllib2.HTTPError:
        return False

# Example: 
# https://twitter.com/golan/status/731770343052972032

print get_user_ids_of_post_likes(731770343052972032)

# ['13520332', '416273351', '284966399']
#
# 13520332 +> @TopLeftBrick
# 416273351 => @Berenger_r
# 284966399 => @FFrink
Darius
  • 10,762
  • 2
  • 29
  • 50
  • It worked! Do you also know what rate limit this is subject to? 15/min or 180/min? – yjc Sep 06 '16 at 18:26
  • 1
    The workaround doesn't use the official Twitter API, so there shouldn't be a rate limit. But there could be an alternative crawler detection by Twitter, so just try it out. – Darius Sep 06 '16 at 19:06
  • 2
    Great, need some code change for the python3 users. 'urllib2' will be 'urllib.request' and need to decode from byte so put a use 'decode("utf8")' before matching – sovon Jun 14 '17 at 14:58
  • One problem I noticed with this, at least with the current HTML layout of twitter.com, is that the posting user is being included in the returned list of users that "liked" the tweet even if the posting user didn't like it. For example, the length of the list returned is 14 elements when, after looking at the tweet online, the tweet has 13 "likes". – Neil Flodin Aug 01 '17 at 19:22
  • 5
    It's worth noting that this method only returns up to 25 results with the current twitter HTML layout. – Tom McGee Aug 07 '17 at 19:01
  • sadly Twitter changed their HTML and frontend architecture recently and these `favorited_popup` URLs now 404. I've been researching alternatives in https://github.com/snarfed/bridgy/issues/823 , but no luck yet. – ryan May 23 '18 at 14:23
  • @ryan no, favorited_popup url still returns json. it is still working – John Aug 26 '18 at 02:21
  • @John thanks for following up! you're right, it was only broken for a couple days, and evidently by whitelisting User-Agent. [details here.](https://github.com/snarfed/bridgy/issues/823#issuecomment-391428316) – ryan Aug 27 '18 at 15:26
  • Nevermind, I had a problem with my Python installation. Now I'm getting this error from `re.findall()` (possibly because I'm using Python 3?): `TypeError: cannot use a string pattern on a bytes-like object` – user124384 Jan 25 '19 at 22:26
  • 2
    It's working but the limit is still up to 25 results. – Miguel Jul 16 '19 at 15:12