Actual Contact Finder using the Social Graph API

This week I suddenly realized that one month ago, we implemented XFN on iknow.co.jp – the startup I’m working for. I almost completely forgot about this and I needed some 20%-google-time distraction from my day to day tasks. So I thought about doing a quick search and jacking in a simple friend finder that uses the Social Graph API. A friend finder meaning: you input your SNS-X username and you will get all SNS-X usernames that are your friends on SNS-Y, SNS-Z.

To my amazement I couldn’t find any straightforward pieces of code actually doing this. All I could find was the usual pretty printed JSON outputs. So I rolled up my sleeves and did some Javascript hacking myself.

Click here for a Demo

I wrote a Javascript class that you can easily use to enable a friend-finder on your own SNS site. Basically there are two main parameters:

  • A regular expression determining the profile URL structure of an SNS
  • The profile URL of the person to recommend friends to, eg: twitter.com/dominiek

The code should be fully customizable for your own SNS, feel free to use it however you want: (don’t forget to fetch socialgraph.js)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// callback:
function contact_found(username, is_new, on_site_name, on_profile_url) {
  // a user on this site was found and is a friend on_profile_url
  // this is also where the is_friend_already? check goes
}

// callback:
function contact_search_status(percentage) {
  // progress report, might take a while to query everything
}

// setup regexp for this SNS
var site_regexp = new RegExp(/http:\/\/www\.iknow\.co\.jp\/user\/([^\/]+)/);

// setup library and pass in callbacks (contact_search_status is optional)
var social_graph = new SocialGraph(site_regexp, contact_found, contact_search_status); 

// recommend for user dominiek
social_graph.contacts_for('http://www.iknow.co.jp/user/dominiek'); 

When doing a search, about 8 to 40 calls are done to the SocialGraph API to walk through all the data. That’s one of the reasons why I really love JSON – all of this stuff is done on the client side.

Please note that any profile URL used in gathering friends data has to be referred to or must refer some other XFN relationships. The more ‘me’ and ‘contact’ links, the better the results will be. iknow.co.jp is actually a very good example of this.

Note: a fellow developer (Zev Blut) pointed out to me that instead of fooling around with too many regular expressions it’s good to look into Node Canonicalization

This entry was posted in Uncategorized. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Additional comments powered by BackType