I've started a small git repo with useful Google Docs JavaScript macros to automatically interact
Thought those that want to import a wallet balance may find this helpful:
It was the Bitcointalk forum that inspired us to create Bitcointalksearch.org - Bitcointalk is an excellent site that should be the default page for anybody dealing in cryptocurrency, since it is a virtual gold-mine of data. However, our experience and user feedback led us create our site; Bitcointalk's search is slow, and difficult to get the results you need, because you need to log in first to find anything useful - furthermore, there are rate limiters for their search functionality.
The aim of our project is to create a faster website that yields more results and faster without having to create an account and eliminate the need to log in - your personal data, therefore, will never be in jeopardy since we are not asking for any of your data and you don't need to provide them to use our site with all of its capabilities.
We created this website with the sole purpose of users being able to search quickly and efficiently in the field of cryptocurrency so they will have access to the latest and most accurate information and thereby assisting the crypto-community at large.
function fetchPrice(
currency, // currency to quote
period // optional report period of (30d, 7d, 24h) defaults to 24h
)
{
if (arguments.length == 1)
period='24h'
var json = UrlFetchApp.fetch('http://bitcoincharts.com/t/weighted_prices.json')
if ('undefined' == typeof(json))
return 'Error retrieving quote data'
json = json.getContentText()
if (json.length<=0)
return 'Quote data was invalid'
json = Utilities.jsonParse(json)
if ('undefined' == typeof(json))
return 'Quote data was malformed JSON data'
json = json[currency]
if ('undefined' == typeof(json))
return 'Quote of that currency not found'
json = json[period]
if ('undefined' == typeof(json))
return 'Invalid quote period'
return parseFloat(json)
}
function fetchTicker(
tickerName, // e.g. : 'EUR'
fieldName // e.g. : 'buy' (see code for full list of args)
)
{
// Fetch live data from MtGox JSON feed
var json = UrlFetchApp.fetch("https://mtgox.com/api/0/data/ticker.php?Currency=" + tickerName)
if ('undefined' == typeof(json))
return 'No data for ticker ' + tickerName + ' from MtGox'
json = json.getContentText()
if (json.length<=0)
return 'MtGox Returned nothing ' + tickerName
json = Utilities.jsonParse(json)
if ('undefined' == typeof(json))
return 'Malformed JSON data returned by MtGox for ticker ' + tickerName
json = json[fieldName]
if ('undefined' == typeof(json))
return 'Unknown field ' + fieldName + ' for ticker ' + tickerName
/*
Fields are:
{
"high":0,
"low":0,
"avg":0,
"vwap":0,
"vol":0,
"last_all":0,
"last_local":0,
"buy":0,
"sell":0,
*/
return json
}
//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;
}
=fetchTickerRefresh(ImportData("https://glbse.com/api/asset/ASSETNAME"&"?workaround="&INT(NOW()*1E3)&REPT(GoogleFinance("AAPL");0)),"fieldName")/100000000
function fetchTickerRefresh(input,fieldName
)
{
var json = input
json = Utilities.jsonParse(json)
if ('undefined' == typeof(json))
return 'Malformed JSON data returned by GLBSE for ticker ' + tickerName
json = json[fieldName]
if ('undefined' == typeof(json))
return 'Unknown field ' + fieldName + ' for ticker ' + tickerName
/*
Fields are:
{
"ask":0
"bid":0,
"btc_vol_total":0,
"latest_trade":0,
"max":0,
"min":0,
"t24havg":0,
"t24hvol":0,
"t5davg":0,
"t5dvol":0,
"t7davg":0,
}
*/
return json
}
=fetchTickerRefresh(ImportData("https://glbse.com/api/asset/TYGRR.BOND-P"&"?workaround="&INT(NOW()*1E3)&REPT(GoogleFinance("AAPL");0)),"latest_trade")/100000000