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);
|
||||||
10
package-lock.json
generated
10
package-lock.json
generated
@@ -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=="
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user