Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter API: How to get users ID, who favorite specific tweet?

Tags:

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.

like image 834
gaussblurinc Avatar asked Sep 11 '12 11:09

gaussblurinc


People also ask

How can I get Twitter user ID?

Go to www.twitter.com and sign in with your account. Then click on your profile picture at the top bar and select Settings and privacy from the drop-down menu. Open 'Your Twitter data' Tab.

Can people see my favorite tweets?

There's a “Likes” tab right there, which anyone can click in order to see every single tweet you've liked. That's right: anyone who wants to can scroll through every tweet you've ever liked.

What happens when you favorite a tweet?

1. When you favourite a tweet, it is added to your list of favourited tweets, virtually bookmarking or saving it for later. This is great because you can go over your favourites at any time and find the tweets you've found most interesting and funny or save articles or nice thing people have tweeted about you!


2 Answers

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') 
like image 194
Alien Merchant Avatar answered Sep 27 '22 20:09

Alien Merchant


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.

like image 37
Amitash Avatar answered Sep 27 '22 20:09

Amitash