Last active
September 5, 2022 11:12
-
-
Save squigglezworth/da37cc506f9ddc5a3370d4274b033dbb to your computer and use it in GitHub Desktop.
Apps Script for fetching price information for EVE Online items
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
const METRICS = {'AVGBUY': 'buy.weightedAverage', 'MAXBUY': 'buy.max', 'MINBUY': 'buy.min', 'BUYDEV': 'buy.stddev', 'MEDBUY': 'buy.median', 'BUYVOL': 'buy.volume', 'BUYNUM': 'buy.orderCount', 'PERCBUY': 'buy.percentile', 'AVGSELL': 'sell.weightedAverage', 'MAXSELL': 'sell.max', 'MINSELL': 'sell.min', 'SELLDEV': 'sell.stddev', 'MEDSELL': 'sell.median', 'SELLVOL': 'sell.volume', 'SELLNUM': 'sell.orderCount', 'PERCSELL': 'sell.percentile' }; | |
/** | |
* Retrieve market information for EVE items | |
* | |
* @param {number|string} type Name or ID of an item | |
* @param {string} metric One of AVGBUY, MAXBUY, MINBUY, BUYDEV, MEDBUY, BUYVOL, BUYNUM, PERCBUY, AVGSELL, MAXSELL, MINSELL, SELLDEV, MEDSELL, SELLVOL, SELLNUM, PERCSELL | |
* @param {number} region Optional - Region ID. Uses The Forge (Jita) by default | |
* @return The price for the item based on the metric specified | |
* @customfunction | |
*/ | |
function getPrice(type="Tritanium", metric="AVGBUY", region=null) { | |
var region = (region) ? region : 30000142; | |
if (!METRICS.hasOwnProperty(metric)) { | |
throw('Invalid metric'); | |
} | |
metric = METRICS[metric] | |
var [m1, m2] = metric.split('.') | |
var id = (typeof type === 'number') ? type : 0; | |
if(typeof type === 'string') { | |
var cache = CacheService.getScriptCache(); | |
id = cache.get(`${type}.id`); | |
if (id) { | |
console.log(`Got cache hit for ${type}.id: ${id}`); | |
} else { | |
var response = UrlFetchApp.fetch(`http://www.fuzzwork.co.uk/api/typeid.php?typename=${type}`); | |
json = JSON.parse(response.getContentText()); | |
if (json.typeName == "bad item") { | |
throw("Invalid item!"); | |
} | |
id = json.typeID; | |
cache.put(`${type}.id`, id, 21600); | |
console.log(`Cached ${type}.id: ${id}`); | |
} | |
} | |
var result = cache.get(`${type}.${region}.${m1}.${m2}`) | |
if (result) { | |
console.log(`Got cache hit for ${type}.${m1}.${m2}: ${result}`); | |
return parseFloat(result); | |
} else { | |
console.log(`Fetching ${m1} ${m2} for ${id} in ${region}...`); | |
var url = `https://market.fuzzwork.co.uk/aggregates/?region=${region}&types=${id}`; | |
var response = UrlFetchApp.fetch(url); | |
var json = JSON.parse(response.getContentText()); | |
var ret = json[id][m1][m2] | |
console.log(`Got ${ret}, caching...`) | |
cache.put(`${type}.${region}.${m1}.${m2}`, ret, 1800) | |
return parseFloat(ret); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simply add this to your sheets' Apps Script, then call
getPrice()
in a formula