Created
December 2, 2020 18:05
-
-
Save anteriovieira/fa371c20dcff1bbcbf64a773f2e05f50 to your computer and use it in GitHub Desktop.
Aegon Script
This file contains hidden or 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
function AEGON(market, attribute, option, refresh_cell) { | |
// Sanitize input | |
var market = (market+"") || ""; | |
var attribute = (attribute+"") || ""; | |
var option = (option+"") || ""; | |
// Get user anonymous token (https://developers.google.com/apps-script/reference/base/session#getTemporaryActiveUserKey()) | |
// Mandatory to authenticate request origin | |
var GSUUID = encodeURIComponent(Session.getTemporaryActiveUserKey()); | |
// Get Data Availability Service and Historical Plan API Keys, if any | |
var userProperties = PropertiesService.getUserProperties(); | |
var APIKEYDATAAVAIBILITYSERVICE = userProperties.getProperty("APIKEYDATAAVAIBILITYSERVICE") || ""; | |
var APIKEY_HISTPLAN = userProperties.getProperty("APIKEY_HISTPLAN") || ""; | |
// Fetch data | |
try { | |
var data = {}; | |
var CACHE_KEY = "CF__"+ market.toLowerCase() + "_" + attribute.toLowerCase() + "_" + option.toLowerCase(); | |
// First check if we have a cached version | |
var cache = CacheService.getUserCache(); | |
var cached = false; | |
var url = "https://api.cryptofinance.ai/v1/data?histplanapikey=" + APIKEY_HISTPLAN + "&gsuuid=" + GSUUID + "&dataproxyapikey=" + APIKEYDATAAVAIBILITYSERVICE; | |
url += "&m=" + market; | |
url += "&a=" + attribute; | |
url += "&o=" + option; | |
url += "&s=os"; | |
// Send request | |
var response = UrlFetchApp.fetch(url, {muteHttpExceptions: true, validateHttpsCertificates: true}) | |
data = JSON.parse(response.getContentText()); | |
// Stop here if there is an error | |
if (data["error"] != "") { | |
throw new Error(data["error"]) | |
} | |
// If everything went fine, cache the raw data returned | |
else if (response && response.getResponseCode() == 200 && data.length > 1 && data.length < 99900) { | |
cache.put(CACHE_KEY, response.getContentText(), 0) | |
} | |
// Return with the proper type | |
var out = "-"; | |
if (data["type"] == "float") { | |
out = parseFloat(data["value"]); | |
} | |
else if (data["type"] == "int") { | |
out = parseInt(data["value"]); | |
} | |
else if (data["type"] == "csv") { | |
out = Utilities.parseCsv(data["value"]); | |
out = cast_matrix__(out); | |
} | |
else { | |
out = data["value"] | |
} | |
return out; | |
} | |
catch (e) { | |
var msg = e.message.replace(/https:\/\/api.*$/gi,'') | |
throw new Error(msg) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment