Hello guys, before going into the topic, I wanna say you one thing. This method may not fetch all of your facebook friends’ profile pictures. But, this is the efficient way*(considering the number of pictures you get) you can find on whole internet(exaggerating 😛 ).
*depends on your popularity on FB.
Let me make sure you have:(Prerequisites, and don’t proceed without understanding these topics well.)
- Considerable knowledge of facebook GraphAPI. Nice video explaining GraphAPI.
- Installed third-party facebook-sdk for python. As I am going to use python.
So, with a simple google search for getting friends’ pics, you might land on a hell lot of pages with most of them using a call to friends (/friends) with a user node(me/friends).
Okay. So, why isn’t it efficient?
Here’s the catch. The permissions required to make this call from this page. The ‘second point’ states it will only return those who have use the app making the request. I don’t know what’s your case, but, for me it returned only 14 friends of 970 in my list. Very disappointing. Try it for yourself and let me know how many nodes( 😛 ) it returned in comments.
import facebook graph= facebook.GraphAPI() friends= graph.get_connections("me", "friends") print(len(friends["data"]))
I think our aim is to fetch profile pics, not getting friends list!
If you are thinking like that, just take a minute to re-think. To get pics through GraphAPI we can call me/picture which returns present profile picture of user. We can give a username or user id in the place of ‘me‘ while we are calling for pictures. That’s the reason we are obsessed to know our friends’ list to get user id or username. But, here’s another catch. The ids you got are not the real ids. 😦 See this…
So, now, some how, find a way to get user-id of our friends.
Likes! Yes the answer is likes.
You can get your friends’ user-ids (depends on their settings. But, you can access 90% of them.) by passing any of our pics or statuses with most number of likes. Find its id in search bar by clicking on your pic. Now, you can pass <pic-id>/likes which returns a list of friends who liked your pic. Now, here’s the brilliant part. FB provides a link to their profile who all liked your nodes(pics or statuses).
likes= graph.get_connections("<node-id>", "likes?fields=link,name&limit=500") #returns only 1000 friends maximum. #returns your friends profile as "https://www.facebook.com/<username>"
Now, we got our friends’ usernames. Lets start downloading pics. 🙂
for i in range (0,len(likes["data"])): myid= likes["data"][i]["id"] myname= likes["data"][i]["name"] mylink= likes["data"][i]["link"] username= mylink[25:] print(myname) pic= graph.get_connections(username, "picture?height=9000&redirect=false") urllib.request.urlretrieve(pic["data"]["url"], "fb/likers/"+myname+".jpg") print("downloaded the pic")
In this process you may get some errors fetching friends’ username, because of their settings. In that case we check for it and try to ignore those links.
#'https://www.facebook.com/profile.php?id=100005090622885&#8242' It looks similar to this. From which we can’t extract username. check= (username[:7]) if(check == "profile"): print("Can’t fetch username due to their settings.") continue
If you are planning to download pictures of friends who liked your nodes from two or more nodes, you may get repeated users i.e. one of your friends may liked both of your nodes. In that case we try not to re-download their pictures.
if (os.path.exists("fb/likers/"+nodename+".jpg")): print("Already downloaded "+nodename+"’s picture!") continue
OMG! omg! I got a lot of pics. So, now what to do?
You can do a lot of things. Really a lot of things. You can build a crazy face-recogniser like this one here.
And you can put it in your room, running, recognises the person entered your room. 😛
For entire code you can visit my GitHub page.
Any questions, feedback is always welcome at email@example.com