-
Notifications
You must be signed in to change notification settings - Fork 0
/
harvester.js
42 lines (38 loc) · 1.21 KB
/
harvester.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const fetch = require("node-fetch");
const dotenv = require("dotenv");
const Stats = require("./stats");
const servers = [
{ schema: Stats.EU, name: "eu" },
{ schema: Stats.RU, name: "ru" },
{ schema: Stats.NA, name: "com" },
{ schema: Stats.ASIA, name: "asia" }
];
dotenv.config();
const hourlyHarvest = async () => {
let returns = []
for(const server of servers) {
const apiLink = `https://api.worldoftanks.${server.name}/wgn/servers/info/?application_id=${process.env.APP_ID}`;
try {
const data = await fetch(apiLink)
const load = await data.json()
const stats = load.data.wot
let arr = [];
stats.forEach(values => {
arr.push({ name: values.server, players: values.players_online });
});
const dataToPush = new server.schema({ servers: arr });
const errors = dataToPush.validateSync();
if (errors) console.log(errors);
try {
const back = await dataToPush.save()
returns.push(back)
} catch(e) {
throw e
}
} catch (e) {
throw e;
}
}
return returns
};
module.exports = hourlyHarvest;