Created
August 15, 2011 10:06
-
-
Save mhawksey/1145992 to your computer and use it in GitHub Desktop.
Google Apps Script to get social bookmark/share counts for a list of urls (in this case on a sheet called Main with urls in column 5). Used in http://mashe.hawksey.info/2011/08/and-the-most-engaging-jisc-project-is/
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
var doc = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = doc.getSheetByName("Main"); | |
var TITLE_ROW = 1; | |
var URL_COL = 5; | |
var FBL_COL = 6; | |
var FBS_COL = 7; | |
var FBC_COL = 8; | |
var TW_COL = 9; | |
var TOP_COL = 10; | |
var BZ_COL = 11; | |
var GP_COL = 12; | |
var LI_COL = 13; | |
var DI_COL = 14; | |
var DE_COL = 15; | |
var SU_COL = 16; | |
function getStats() { | |
for (var rowIdx=0; rowIdx < sheet.getLastRow(); rowIdx++){ | |
var rowNum = rowIdx+TITLE_ROW+1; | |
var itemurl = sheet.getRange(rowNum,URL_COL).getValues(); | |
Utilities.sleep(500); | |
var counts = getSharedCount(itemurl); | |
sheet.getRange(rowNum, FBL_COL).setValue(c_val(counts["Facebook"]["like_count"])); | |
sheet.getRange(rowNum, FBS_COL).setValue(c_val(counts["Facebook"]["share_count"])); | |
sheet.getRange(rowNum, FBC_COL).setValue(c_val(counts["Facebook"]["comment_count"])); | |
sheet.getRange(rowNum, TW_COL).setValue(c_val(counts["Twitter"])); | |
sheet.getRange(rowNum, BZ_COL).setValue(c_val(counts["Buzz"])); | |
sheet.getRange(rowNum, GP_COL).setValue(c_val(counts["GooglePlusOne"])); | |
sheet.getRange(rowNum, LI_COL).setValue(c_val(counts["LinkedIn"])); | |
sheet.getRange(rowNum, DI_COL).setValue(c_val(counts["Diggs"])); | |
sheet.getRange(rowNum, DE_COL).setValue(c_val(counts["Delicious"])); | |
sheet.getRange(rowNum, SU_COL).setValue(c_val(counts["StumbleUpon"])); | |
sheet.getRange(rowNum, TOP_COL).setValue(c_val(getTopsyCount(itemurl))); | |
/* | |
sheet.getRange(rowNum, FBL_COL).setValue(c_val(getFacebookLike(itemurl))); | |
sheet.getRange(rowNum, FBS_COL).setValue(c_val(getFacebookShare(itemurl))); | |
sheet.getRange(rowNum, FBC_COL).setValue(c_val(getFacebookComment(itemurl))); | |
sheet.getRange(rowNum, TW_COL).setValue(c_val(getTweetCount(itemurl))); | |
sheet.getRange(rowNum, BZ_COL).setValue(c_val(getBuzzCount(itemurl))); | |
sheet.getRange(rowNum, GP_COL).setValue(c_val(getPlusones(itemurl))); | |
sheet.getRange(rowNum, LI_COL).setValue(c_val(getLinkedInCount(itemurl))); | |
sheet.getRange(rowNum, DI_COL).setValue(c_val(getDiggCount(itemurl))); | |
sheet.getRange(rowNum, DE_COL).setValue(c_val(getDeliciousCount(itemurl))); | |
sheet.getRange(rowNum, SU_COL).setValue(c_val(getStumbleCount(itemurl))); | |
sheet.getRange(rowNum, TOP_COL).setValue(c_val(getTopsyCount(itemurl))); | |
*/ | |
} | |
} | |
function getSharedCount(url){ | |
try { | |
var options = | |
{ | |
"method" : "get", | |
"contentType" : "application/json" | |
}; | |
var response = UrlFetchApp.fetch("http://api.sharedcount.com/?url="+encodeURI(url), options); | |
var results = Utilities.jsonParse(response.getContentText()); | |
return results; | |
} catch(e){ | |
Logger.log(e); | |
} | |
} | |
function c_val(aValue){ | |
if (aValue == undefined){ | |
return "-"; | |
} else { | |
return aValue; | |
} | |
} | |
function getFacebookLike(url){ | |
try { | |
var options = | |
{ | |
"method" : "get", | |
"contentType" : "application/json" | |
}; | |
var response = UrlFetchApp.fetch("https://api.facebook.com/method/fql.query?query=select%20like_count%20%20from%20link_stat%20where%20url=%22"+encodeURIComponent(url)+"%22&format=json", options); | |
var results = Utilities.jsonParse(response.getContentText()); | |
var result = results[0]; | |
return result.like_count; | |
} catch(e){ | |
Logger.log(e); | |
} | |
var err = "-"; | |
return err; | |
} | |
function getFacebookShare(url){ | |
try { | |
var options = | |
{ | |
"method" : "get", | |
"contentType" : "application/json" | |
}; | |
var response = UrlFetchApp.fetch("https://api.facebook.com/method/fql.query?query=select%20share_count%20%20from%20link_stat%20where%20url=%22"+encodeURIComponent(url)+"%22&format=json", options); | |
var results = Utilities.jsonParse(response.getContentText()); | |
var result = results[0]; | |
return result.share_count; | |
} catch(e){ | |
} | |
var err = "-"; | |
return err; | |
} | |
function getFacebookComment(url){ | |
try { | |
var options = | |
{ | |
"method" : "get", | |
"contentType" : "application/json" | |
}; | |
var response = UrlFetchApp.fetch("https://api.facebook.com/method/fql.query?query=select%20comment_count%20%20from%20link_stat%20where%20url=%22"+encodeURIComponent(url)+"%22&format=json", options); | |
var results = Utilities.jsonParse(response.getContentText()); | |
var result = results[0]; | |
return result.comment_count; | |
} catch(e){ | |
} | |
var err = "-"; | |
return err; | |
} | |
function getTweetCount(url){ | |
try { | |
var options = | |
{ | |
"method" : "get", | |
"contentType" : "application/json" | |
}; | |
var response = UrlFetchApp.fetch("http://urls.api.twitter.com/1/urls/count.json?url="+encodeURI(url), options); | |
var results = Utilities.jsonParse(response.getContentText()); | |
return results.count; | |
} catch(e){ | |
} | |
var err = "-"; | |
return err; | |
} | |
function getTopsyCount(url){ | |
try { | |
var options = | |
{ | |
"method" : "get", | |
"contentType" : "application/json" | |
}; | |
var response = UrlFetchApp.fetch("http://otter.topsy.com/stats.json?url="+encodeURI(url), options); | |
var results = Utilities.jsonParse(response.getContentText()); | |
return results.response.all; | |
} catch(e){ | |
Logger.log(e); | |
} | |
var err = "-"; | |
return err; | |
} | |
function getBuzzCount(url){ | |
try { | |
var options = | |
{ | |
"method" : "get", | |
"contentType" : "application/json" | |
}; | |
var response = UrlFetchApp.fetch("http://www.google.com/buzz/api/buzzThis/buzzCounter?url="+encodeURI(url)+"&callback=buzzData", options); | |
var result = eval(response.getContentText()); | |
return result; | |
} catch(e){ | |
} | |
var err = "-"; | |
return err; | |
} | |
function buzzData(arr){ | |
for (i in arr){ | |
var result = arr[i]; | |
} | |
return(result); | |
} | |
function getLinkedInCount(url){ | |
// ref http://justthatidontforget.blogspot.com/2011/04/script-to-get-linkedin-share-count-for.html | |
try{ | |
var options = | |
{ | |
"method" : "get", | |
"contentType" : "application/json" | |
}; | |
var response = UrlFetchApp.fetch("http://www.linkedin.com/cws/share-count?url="+encodeURI(url)+"&callback=?", options); | |
var IN={}; | |
IN.Tags={}; | |
IN.Tags.Share={}; | |
IN.Tags.Share.handleCount=function(e){result = e.count}; | |
eval(response.getContentText()); | |
return result; | |
} catch(e){ | |
} | |
var err = "-"; | |
return err; | |
} | |
function getDiggCount(url){ | |
try { | |
var options = | |
{ | |
"method" : "get", | |
"contentType" : "application/json" | |
}; | |
var response = UrlFetchApp.fetch("http://widgets.digg.com/buttons/count?url="+encodeURI(url)+"&callback=?", options); | |
var __DBW={}; | |
__DBW.collectDiggs=function(e){result = e.diggs}; | |
eval(response.getContentText()); | |
return result; | |
} catch(e){ | |
} | |
var err = "-"; | |
return err; | |
} | |
function getDeliciousCount(url){ | |
// md5 hash calculated using techworker1 code http://www.google.com/support/forum/p/apps-script/thread?tid=0a18d7ca49d1cdf4&hl=en | |
try { | |
var theHash = Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, url); | |
var txt_hash = ''; | |
for (j = 0; j < theHash.length; j++) { | |
var hashVal = theHash[j]; | |
if (hashVal < 0) | |
hashVal += 256; | |
if (hashVal.toString(16).length == 1) | |
txt_hash += "0"; | |
txt_hash += hashVal.toString(16); | |
} | |
var options = | |
{ | |
"method" : "get", | |
"contentType" : "application/json" | |
}; | |
var response = UrlFetchApp.fetch("http://feeds.delicious.com/v2/json/urlinfo/blogbadge?hash="+txt_hash, options); | |
var result = Utilities.jsonParse(response.getContentText()); | |
return result[0].total_posts; | |
} catch(e){ | |
} | |
var err = "-"; | |
return err; | |
} | |
function getStumbleCount(url){ | |
try { | |
var options = | |
{ | |
"method" : "get", | |
"contentType" : "application/json" | |
}; | |
var response = UrlFetchApp.fetch("http://www.stumbleupon.com/services/1.01/badge.getinfo?url="+encodeURI(url), options); | |
var results = Utilities.jsonParse(response.getContentText()); | |
return results.result.views; | |
} catch(e){ | |
} | |
var err = "-"; | |
return err; | |
} | |
function getPlusones(url){ | |
// from http://www.tomanthony.co.uk/blog/google_plus_one_button_seo_count_api/ | |
try{ | |
var options = | |
{ | |
"method" : "post", | |
"contentType" : "application/json", | |
"payload" : '{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"'+url+'","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}' | |
}; | |
var response = UrlFetchApp.fetch("https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ", options); | |
var results = JSON.parse(response.getContentText()); | |
return results.result.metadata.globalCounts.count; | |
} catch(e){ | |
} | |
var err = "-"; | |
return err; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment