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);

10
package-lock.json generated
View File

@@ -1,5 +1,13 @@
{ {
"name": "simcompanies-dataminer", "name": "simcompanies-dataminer",
"version": "0.1.0", "version": "0.1.0",
"lockfileVersion": 1 "lockfileVersion": 1,
"requires": true,
"dependencies": {
"node-fetch": {
"version": "2.6.0",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz",
"integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA=="
}
}
} }

View File

@@ -4,8 +4,12 @@
"description": "A small JS-Script to mine some data from the game Sim Companies", "description": "A small JS-Script to mine some data from the game Sim Companies",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"author": "Oliver Boehlk", "author": "Oliver Boehlk",
"license": "GPL-3.0" "license": "GPL-3.0",
"dependencies": {
"node-fetch": "^2.6.0"
}
} }