Last active
May 8, 2016 23:49
-
-
Save mhawksey/3436048 to your computer and use it in GitHub Desktop.
Google Spreadsheet/Apps Script custom function for getting Klout scores for Twitter screen name(s). You can File > Make a copy of this template https://docs.google.com/spreadsheet/ccc?key=0AqGkLMU9sHmLdDVzZWdLbElhM3NYM2VQZi1lYjZXUUE&usp=drive_web#gid=0 for working version of the code
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
/* Copyright 2012 Martin Hawksey (email : [email protected]) | |
This program is free software; you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation; either version 2 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software | |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
*/ | |
function getKloutScores(screen_names,klout_apikey){ | |
var output = []; | |
for (i in screen_names){ | |
if (screen_names[i][0]!=""){ | |
output.push([getKloutScore(screen_names[i][0],klout_apikey)]); | |
} | |
} | |
return output; | |
} | |
function getKloutScore(screen_name,klout_apikey){ | |
var cache = CacheService.getPrivateCache(); // using Cache service to prevent too many urlfetch | |
var cached = cache.get(screen_name); | |
if (cached != null) { // if value in cache return it | |
return parseFloat(cached); | |
} | |
try { | |
var options = | |
{ | |
"method" : "get", | |
"contentType" : "application/json" | |
}; | |
var response = UrlFetchApp.fetch("http://api.klout.com/v2/identity.json/twitter?screenName="+screen_name+"&key="+klout_apikey, options); | |
var id = Utilities.jsonParse(response.getContentText()).id; | |
if (id){ | |
Utilities.sleep(500); | |
var response = UrlFetchApp.fetch("http://api.klout.com/v2/user.json/"+id+"/score?key="+klout_apikey, options); | |
var data = Utilities.jsonParse(response.getContentText()).score; | |
cache.put(screen_name, data, 604800); // 604800 = cache set for 7 days | |
return data; | |
} | |
} catch(e){ | |
Logger.log(e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment