Python gets Facebook users with the friends they have a common interest in

Source: Internet
Author: User

CODE:

#!/usr/bin/python #-*-Coding:utf-8-*-' Created on 2014-8-13@author:guaguastd@name:common_friends_finding.py ' ' # Impot loginfrom Login Import facebook_login# Import helper#from Helper import pp# import Prettytablefrom prettytable impor T prettytablefrom collections import counterfrom operator import itemgetter# access to Facebookfacebook_api = Facebook_log In () # Get friends like through single request#friends_like = Facebook_api.get_object (' Me ', fields= ' Id,name, Friends.fields (id,name,likes) ') #pp (friends_like) # Get friends like through multi requestfriends = facebook_api.get_ Connections ("Me", "Friends") [' data ']likes = {friend[' name ']: facebook_api.get_connections (friend[' id '], "likes") ['                           Data '] for friend in friends[:10]}friends_likes = Counter ([like[' name '] for friend in likes For as Likes[friend] if Like.get (' name ')] # which of your like S is in common with which friendsmy_likes = [like[' NAMe '] for like in Facebook_api.get_connections ("Me", "likes") [' data ']]# use the set intersection as Represente D by the ampersand# operator to find common likescommon_likes = list (set (My_likes) & Set (Friends_likes)) # Which of you R friends like things so like?similar_friends = [(Friend, friend_like[' name ']) for friend, Frien D_likes in Likes.items () for friend_like in Friend_likes if Friend_like.get (' N  Ame ') in Common_likes]# Filter out any possible duplicates that could occurranked_friends = Counter ([Friend, Like) in List (set (Similar_friends))]) pt = prettytable (field_names=["friend", "Common Likes"]) pt.align["Friend"], pt.align["Common likes"] = ' l ', ' R ' [Pt.add_row (RF) for RF in sorted (Ranked_friends.items (), Key=item Getter (1), reverse=true)]print "My similar Friends (ranked)" Print PT

RESULT:

My similar Friends (ranked) +--------------------------+--------------+| Friend                   | Common likes |+--------------------------+--------------+| Los Mas Lindos del Mundo |            1 | | Soofi Cat                |            1 | | Jorge Rodriguez          |            1 | | Kevin Swaggy             |            1 |+--------------------------+--------------+


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.