Common Friends or Followers on Twitter

Yesterday morning, @ambrouk tweeted: “Is there a tool where you can quickly check 2 twitter accs and find out who follows both? I.e who can see the exchanges between A and B?”

(Explanatory note: if you start a tweet with @name, only your twitter followers who also follow @name will see the tweet in their stream.)

There probably is, but I thought it’d be an interesting exercise to see if I could put a script together to do this in a web page without requiring authenticated access to the Twitter API using the Google Social Graph API.

If you go to the Social Graph API Parameter Playground, you can use the tool provided to construct API calling URLs that return the people you follow on Twitter, or who follow you, as well as various other bits of social data…

Google Social API parameter playground

The data is returned as JSON, so it’s easy enough to pull into a web page. So here’s a view of my ‘common twitter friends’ single page web app:

Common twitter friends

And here’s the script that does it, pulling back the common friends or common followers of two folks on Twitter:

<html><head><title></title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script> 
 
<script type="text/javascript">

function compareUsers(typ){
  if (typ=='followers'){
    gtyp='edi'
  } else {
    gtyp='edo'
  }
  url='http://socialgraph.apis.google.com/lookup?q=http://twitter.com/'+$('#user1').val()+',http://twitter.com/'+$('#user2').val()+'&'+gtyp+'=1&callback=?';

  var content = "";
  $.getJSON(url,
   function(json){
     if(json) {
       if (typ=='followers')
         content = commonFollowers(json);
       else 
         content = commonFriends(json);
     } else {
       content = "The request did not return results.";
     }
     var list=''
     for (i in content)
       if (content[i].indexOf('http://twitter.com/account/redirect_by_id')==-1)
     	list+=" "+content[i].replace('http://twitter.com/','')
     $("#output").html(list);
     $("#u1p").html('of which '+content.length+' in common')
     $("#u2p").html('of which '+content.length+' in common')
   }
  );
}

function commonFriends(json){
  return commonXs(json,'friends')
}

function commonFollowers(json){
  return commonXs(json,'followers')
}

function commonXs(json,typ){
  var name=new Array()
  if (typ=='followers')
    nref='nodes_referenced_by'
  else
    nref='nodes_referenced'
  Xers=new Array();
  coXs=new Array();

  for (u in json['nodes']) {
    name[name.length]=u.replace('http://twitter.com/','')
	lXers=new Array()
	for (i in json['nodes'][u][nref])
		lXers[lXers.length]=i	
	Xers[Xers.length]=lXers
  }
  $("#u1n").html(name[0]+': ')
  $("#u1nn").html(' with '+name[0])
  $("#u1c").html(Xers[0].length+' '+typ)
  $("#u2n").html(name[1]+': ')
  $("#u2nn").html(' with '+name[1])
  $("#u2c").html(Xers[1].length+' '+typ)
  for (i in Xers[0]){
    if (Xers[1].indexOf(Xers[0][i])>-1){
	  coXs[coXs.length]=Xers[0][i]
	}
  }
  return coXs
}
</script>

</head>

<body>
<form>
User 1: @<input type='text' id='user1' value="ambrouk" />
User 2: @<input type='text' id='user2' value='psychemedia' />
<input type='button' value='Find Common Followers' onclick='compareUsers("followers")' /> <input type='button' value='Find Common Friends' onclick='compareUsers("friends")' />
</form>
<div><span id='u1n'></span> <span id='u1c'></span> <span id='u1p'></span> <span id='u2nn'></div>
<div><span id='u2n'></span> <span id='u2c'></span> <span id='u2p'></span> <span id='u1nn'></div>
<hr/>
<div id="output"></div>

</body></html>

With a bit of tweaking, it should be possible to generalise the code to find the common followers of N friends (please post a link to the script in a comment if you do this:-)

Rather than me hosting the script, you’ll have to run it yourself. Save the raw code from here as an .html file on your desktop, then drag it and drop it into a browser window (or more laboriously, open the file you saved from the browser file menu).

[UPDATE: try it out here – Common Twitter friends]

PS I’m not sure that the limits are on the Google Social Graph API, whether in terms of the number of accounts that can be included in a single query, the size of the returned payload from a single query, or the number of queries per minute/hour/day, so I’m not sure if this represents a way round the Twitter API limit if all you’re doing is calling on friends and followers lists?

Author: Tony Hirst

I'm a Senior Lecturer at The Open University, with an interest in #opendata policy and practice, as well as general web tinkering...

9 thoughts on “Common Friends or Followers on Twitter”

  1. This is so clever. It’s exactly what I wanted.
    Here’s my use cases:
    – Do I need to point out to C that I’m having this exchange with B?
    – Can A see that B and C are talking about their blog post?
    I wonder if B knows the same people I know and if they saw that thread?
    – I’d never done the drag-into-browser-window thing before, it made me feel quasi technical. The interface is simple, a very easy to use tool that does what I need it to. Yeay to the power of twitter.

  2. I’ve also been doing a bit of tinkering with the google social API as of late. In response to your question about the size of the payload of a single query, I’ve unscientifically determined it to be around 10,000 users.

    I’ve ran my program against a few celebrity twitter accounts and they all seem to top off at the 10,000 mark in terms of number of inbound connections.

    >>> friends = twitter_base.google_social_api_followers(‘neilkod’)
    >>> len(friends)
    1292
    >>> friends = twitter_base.google_social_api_followers(‘aplusk’)
    >>> len(friends)
    10156
    >>> friends = twitter_base.google_social_api_followers(‘therealshaq’)
    >>> len(friends)
    2715
    >>> friends = twitter_base.google_social_api_followers(‘the_real_shaq’)
    >>> len(friends)
    9999
    >>> friends = twitter_base.google_social_api_followers(‘stephenathome’)
    >>> len(friends)
    9911
    >>> friends = twitter_base.google_social_api_followers(‘kanyewest’)
    >>> len(friends)
    9891
    twitter_base.google_social_api_followers(‘kimkardashian’)
    >>> len(friends)
    9996

    I then ran ‘aplusk’ through the google social api parameter playground:
    http://socialgraph.apis.google.com/lookup?q=http://www.twitter.com/aplusk&edi=1&pretty=1&callback=

    I didn’t notice any fields that relate to either cursors or pagination. Then again I didn’t look TOO hard but I did eyeball the data for a bit.

  3. Hi,

    I’m a bit confused by the code. What would be the javascript needed to get a simple list of friends or followers from a single account.
    I know it will be a simpler code, but i’m unable to do it (my javascript skills are very basic).

    Thanks,

Comments are closed.