add function to get marketprices using fetch

This commit is contained in:
2020-02-22 11:29:58 +01:00
parent 2bdf452bb3
commit f6defba262
3 changed files with 38 additions and 2 deletions

24
index.js Normal file
View File

@@ -0,0 +1,24 @@
const fetch = require("node-fetch");
const marketAPI = "https://www.simcompanies.com/api/v1/market-ticker/";
var date = new Date();
//don't ask me why, but simcompanies uses yesterday to get the values for today...
date.setDate(date.getDate() - 1);
const url = marketAPI + date.toISOString() + "/";
//function to get marketprices from API:
const getData = async url => {
var marketData = await fetch(url);
if (marketData.status === 200) {
marketData = await marketData.text();
marketData = await JSON.parse(marketData);
return marketData;
} else {
console.error("fetch returned status code " + marketData.status);
return false;
}
}
getData(url);