This is what I've been using, hopefully it helps some
//Shrink an array down to at most 3 elements
//Helper function
function get3(info) {
//If you want more or less payouts, change this 3
while(info.length > 3)
info.shift();
return info;
}
//Fetches asset info based on ticker
function getInfo(ticker) {
return glbseCachedFetch("/api/asset/" + ticker);
}
//Fetches dividend info based on ticker
function getDiv(ticker) {
return glbseCachedFetch("/api/dividends/asset/" + ticker);
}
//Fetches the price of an asset
//Returns first non-zero price from the list of 24h average, 5 day average, 7 day average, or last trade
function getPrice(ticker) {
var info = getInfo(ticker);
return (info.t24havg/100000000) || (info.t5davg/100000000) || (info.t7davg/100000000) || (info.latest_trade/100000000);
}
//Gets the average dividend from passed in array
function getDivArray(info) {
Logger.log("Into getDivArray");
if(info === null) return null;
if(info.length == 0) return 0;
Logger.log("Not null or zero length");
var divTotal = 0;
for(var i = 0; i < info.length; i++) {
divTotal += info[i].pps;
Logger.log("Step " + i + "/" + info.length + " " + String(divTotal));
}
Logger.log(String(divTotal));
return divTotal / info.length / 100000000;
}
//Gets average dividend of all payouts
function getAvgDividend(ticker) {
Logger.log("Into getAvgDividend " + ticker);
var info = getDiv(ticker);
Logger.log(info);
Logger.log("Got Info");
return getDivArray(info);
}
//Gets Average dividend of last 3 payouts
function get3AvgDividend(ticker) {
var info = get3(getDiv(ticker));
return getDivArray(info);
}
//Fetches info from GLBSE, caches when fetched and attempts to use the cache
//Caches info for 30 minutes
function glbseCachedFetch(apiUrl) {
var publicCache = CacheService.getPublicCache();
var cached = publicCache.get("http://glbse.com" + apiUrl);
if(cached !== null && JSON.parse(cached).length > 1 ){
return JSON.parse(cached);
} else {
getSleep();
//Browser.msgBox("Fetching");
Logger.log("Fetching");
var response = UrlFetchApp.fetch("http://glbse.com"+apiUrl);
//check for status code
var result = response.getContentText();
if(response.getResponseCode() < 300)//Only save if good fetch
publicCache.put("http://glbse.com"+apiUrl, result, 60 * 30);//30 * 60 seconds - 30 minutes
return JSON.parse(result);
}
}
//Allows small delay between multiple fetches to lighten the load on GLBSE site
function getSleep() {
var delay = 50;
var date = new Date();
var now = date.getTime();
var cache = CacheService.getPublicCache();
var cacheKey = "sleepUntil";
var timeString = cache.get(cacheKey);
var time = parseInt(timeString);
var sleep = time - now;
Logger.log(cacheKey + " " + timeString);
Logger.log("parseInt " + time);
Logger.log("sleeping " + sleep);
if((timeString === null) || (timeString == "NaN") || (time === null) || (time == NaN) || (sleep < 0)) {
cache.put(cacheKey, String(now + delay));
} else {
cache.put(cacheKey, String(time + delay));
Utilities.sleep(sleep);
}
}
//Days between payouts, based on all payouts
function daysBetween(ticker) {
var info = getDiv(ticker);
if(info === null) return null;
if(info.length < 2) return Infinity;
var first = info[0];
var last = info.pop();
return (last.timestamp - first.timestamp) / info.length / 3600 / 24;
}
//Days between payouts, based on the last 3 payouts
function daysBetween3(ticker) {
var info = get3(getDiv(ticker));
if(info === null) return null;
if(info.length < 2) return Infinity;
var first = info[0];
var last = info.pop();
return (last.timestamp - first.timestamp) / info.length / 3600 / 24;
}
These are the functions I'm using in my sheet, and while I'm not quite ready to show off the whole thing I hope this might be helpful to someone. I'm looking to get these published to the gallergy, but approval takes a while. These can be used regularly in a sheet as a docs formula or as a function on other scripts. These may be used as if under any license or as public domain where available.
Tips to 18ef54UQ3t9ieqU3MebyqMHjCpzKrZzS5N are greatly appreciated if you've got the spare coin and find these useful.