Created
June 9, 2011 22:52
-
-
Save mhawksey/1017960 to your computer and use it in GitHub Desktop.
Gets twitter connections for protovis network gadget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getProtovisTwitterConnections(){ | |
// This code is a reworking of code taken from Tony Hirst's (@psychemedia) Friendviz | |
// http://ouseful.open.ac.uk/twitter/friendviz.html | |
// Usage: Select a row or column of twitter usernames from a sheet (without '@') then run this function | |
// This creates a new sheet called pData with data formatted for the Graphing Connections Between | |
// Recent Twitterers (Protovis Demo) gadget. To graph the results from the main spreadsheet select | |
// Insert -> Gadget and enter a Custom url to http://hosting.gmodules.com/ig/gadgets/file/108150762089462716664/friendviz.xml | |
// The data range for the gadget is pData!A1:B2 | |
var doc = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = doc.getActiveSheet(); | |
var users = sheet.getActiveRange().getValues(); | |
var followers={}; | |
//if (users.length>50){ | |
// users=users.slice(0,49); | |
//} | |
var str=""; | |
for (var uic=0; uic<users.length; uic++){ | |
users[uic]=users[uic].toString().toLowerCase(); | |
str += "http://twitter.com/"+users[uic]+","; | |
} | |
var url='http://socialgraph.apis.google.com/lookup?q='+str+'&edo=1'; | |
var response = UrlFetchApp.fetch(url, {method:'get'}); | |
if (response.getResponseCode() == 200) { | |
var json = Utilities.jsonParse(response.getContentText()); | |
graph={}; | |
graph['nodes']=[]; | |
userLoc={}; | |
for (var uic=0; uic<users.length; uic++){ | |
if (users[uic] !='') { | |
graph['nodes'].push({nodeName:users[uic]}); | |
userLoc[users[uic]]=uic; | |
} | |
} | |
graph['links']=[]; | |
for (u in json['nodes']) { | |
name=u.replace('http://twitter.com/',''); | |
for (var i in json['nodes'][u]['nodes_referenced']){ | |
si=i.replace('http://twitter.com/',''); | |
if ( si in userLoc ){ | |
if (json['nodes'][u]['nodes_referenced'][i]['types'][0]=='contact') { | |
graph['links'].push({source:userLoc[name], target:userLoc[si]}); | |
} | |
} | |
} | |
} | |
followers={nodes:graph['nodes'],links:graph['links']}; | |
if (doc.getSheetByName("pData")){ | |
var sheet = doc.getSheetByName("pData"); | |
} else { | |
var sheet = doc.insertSheet("pData"); | |
} | |
//var sheet = ss.getSheetByName("Data"); | |
sheet.getRange("A1").setValue("nodes"); | |
sheet.getRange("B1").setValue("links"); | |
sheet.getRange("A2").setValue(Utilities.jsonStringify(followers['nodes'])); | |
sheet.getRange("B2").setValue(Utilities.jsonStringify(followers['links'])); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment