Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Friendviz add-on for Twitteralytics
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.getSheetByName("Summary")
var users = sheet.getRange(2,1,sheet.getLastRow(),1).getValues();
var followers={};
//if (users.length>60){
// users=users.slice(0,60);
// }
var str='';
for (var uic=0; uic<users.length; uic++){
users[uic]=users[uic].toString().toLowerCase();
if (users[uic]!=""){
str+='http://twitter.com/'+users[uic]+',';
}
}
var url='http://socialgraph.apis.google.com/lookup?q='+str+'&edo=1';
//Logger.log(url);
var response = UrlFetchApp.fetch(url, {method:'get'});
if (response.getResponseCode() == 200) {
var json = Utilities.jsonParse(response.getContentText());
// get names from ids
var temp = [];
var id = [];
for (u in json['nodes']) {
//name=u.replace('http://twitter.com/','');
for (var i in json['nodes'][u]['nodes_referenced']){
if (i.indexOf('http://twitter.com/account/redirect_by_id?')==0){
si = i.replace("http://twitter.com/account/redirect_by_id?id=", "");
// temp.push(si);
id["id="+si] = 1;
}
}
}
Logger.log(id.length);
var oauthConfig = UrlFetchApp.addOAuthService("twitter");
oauthConfig.setAccessTokenUrl(
"https://api.twitter.com/oauth/access_token");
oauthConfig.setRequestTokenUrl(
"https://api.twitter.com/oauth/request_token");
oauthConfig.setAuthorizationUrl(
"https://api.twitter.com/oauth/authorize");
oauthConfig.setConsumerKey(getConsumerKey());
oauthConfig.setConsumerSecret(getConsumerSecret());
var requestData = {
"method": "GET",
"oAuthServiceName": "twitter",
"oAuthUseToken": "always"
};
var c = 0;
var idurl ="http://api.twitter.com/1/users/lookup.json?user_id=";
var size=0;
for (i in id){
size++;
}
var run = 0;
for (i in id){
run++;
c++;
if (c <100){
idurl += i.replace("id=","")+",";
}
if (c == 99 || size == run){
var response = UrlFetchApp.fetch(idurl, requestData);
if (response.getResponseCode() == 200) {
var objects = Utilities.jsonParse(response.getContentText());
for (j in objects){
id["id="+objects[j]["id_str"]]=objects[j]["screen_name"];
}
}
idurl ="http://api.twitter.com/1/users/lookup.json?user_id=";
c = 0;
}
}
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']){
if (i.indexOf('http://twitter.com/account/redirect_by_id?')==0){
ti = i.replace("http://twitter.com/account/redirect_by_id?", "");
si = id[ti];
} else {
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