Skip to content

Instantly share code, notes, and snippets.

@Meigs2
Created December 28, 2020 23:11
Show Gist options
  • Save Meigs2/d981d5c713972d25808799bc86aee691 to your computer and use it in GitHub Desktop.
Save Meigs2/d981d5c713972d25808799bc86aee691 to your computer and use it in GitHub Desktop.
Calls fuzzworks market API from within Excel.
name: Fuzzworks
description: Calls fuzzworks market API from within Excel.
host: EXCEL
api_set: {}
script:
content: >
// Written by Meigs2 (Meigs Abre-Kai in-game)
// Credit to Steve for providing the market API:
https://market.fuzzwork.co.uk/api/
// Credit to whoever wrote the OG google scripts fucntion, this is adapted
to work with Script Lab in Excel.
// This code is rather sloppy, if there are improvements to be made, hit me
up.
async function safeParseJSON(response: Response) {
console.log(response);
const body = await response.text();
try {
return JSON.parse(body);
} catch (err) {
console.error("Error:", err);
console.error("Response body:", body);
}
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
/**
* Fetches the Fuzzworks market API for current market data.
* @customFunction
* @param {number[][]} typeIds Range of typeIds to query for.
* @param {number} [locationId] Location to query orders for. Takes in a system or station id. Default = The Forge
* @returns {string[][]} Dynamic array
*/
async function loadRegionAggregates(typeIds, locationId) {
// Squash 2d array to 1d array
let priceIDs = [].concat(...typeIds);
if (locationId === null) {
locationId = 10000002;
}
if (typeof priceIDs == "undefined") {
console.log("Need a list of typeids");
}
var prices = new Array();
var url = "https://market.fuzzwork.co.uk/aggregates/?region=" + locationId + "&types=";
prices.push([
"typeId",
"buyWeightedAverage",
"buyMax",
"buyMin",
"buyStddev",
"buyMedian",
"buyVolume",
"buyOrderCount",
"buyPercentile",
"sellWeightedAverage",
"sellMax",
"sellMin",
"sellStddev",
"sellMedian",
"sellVolume",
"sellOrderCount",
"sellPercentile"
]);
var parameters = { method: "get", payload: "" };
var o,
j,
temparray,
chunk = 100;
for (o = 0, j = priceIDs.length; o < j; o += chunk) {
await sleep(200);
temparray = priceIDs.slice(o, o + chunk);
let types = temparray.join(",").replace(/,$/, "");
let json = await fetch(url + types, parameters)
.then(safeParseJSON)
.catch((reason) => console.log(reason));
if (json) {
for (var i in json) {
var price = [
parseInt(i),
parseInt(json[i].buy.weightedAverage),
parseFloat(json[i].buy.max),
parseFloat(json[i].buy.min),
parseFloat(json[i].buy.stddev),
parseFloat(json[i].buy.median),
parseInt(json[i].buy.volume),
parseInt(json[i].buy.orderCount),
parseFloat(json[i].buy.percentile),
parseFloat(json[i].sell.weightedAverage),
parseFloat(json[i].sell.max),
parseFloat(json[i].sell.min),
parseFloat(json[i].sell.stddev),
parseFloat(json[i].sell.median),
parseInt(json[i].sell.volume),
parseInt(json[i].sell.orderCount),
parseFloat(json[i].sell.percentile)
];
prices.push(price);
}
}
}
return prices;
}
language: typescript
libraries: |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js
@types/office-js
office-ui-fabric-js@1.4.0/dist/css/fabric.min.css
office-ui-fabric-js@1.4.0/dist/css/fabric.components.min.css
core-js@2.4.1/client/core.min.js
@types/core-js
jquery@3.1.1
@types/jquery@3.3.1
@alexandruconea
Copy link

Is this addon still working?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment