diff --git a/index.js b/index.js new file mode 100644 index 0000000..ee6d78d --- /dev/null +++ b/index.js @@ -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); diff --git a/package-lock.json b/package-lock.json index 2d38186..0d42a53 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,13 @@ { "name": "simcompanies-dataminer", "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==" + } + } } diff --git a/package.json b/package.json index f350ba1..212b971 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,12 @@ "description": "A small JS-Script to mine some data from the game Sim Companies", "main": "index.js", "scripts": { + "start": "node index.js", "test": "echo \"Error: no test specified\" && exit 1" }, "author": "Oliver Boehlk", - "license": "GPL-3.0" + "license": "GPL-3.0", + "dependencies": { + "node-fetch": "^2.6.0" + } }