add function to get marketprices using fetch
This commit is contained in:
24
index.js
Normal file
24
index.js
Normal 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);
|
||||
Reference in New Issue
Block a user