forked from maratonusp/contestwatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.js
38 lines (31 loc) · 1.08 KB
/
fetch.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
const logger = require('./logger');
const alerts = require('./alerts');
const fs = require('fs');
const schedule = require('node-schedule');
// Getting every available fetcher
const fetchers = fs.readdirSync('./fetchers')
.filter((file) => { return file.endsWith('.js'); })
.map((file) => { return require('./fetchers/' + file); });
const fetch = module.exports = {};
fetch.upcoming = [];
fetch.updateUpcoming = function() {
const upcoming = fetch.upcoming;
upcoming.length = 0;
alerts.reset_alerts();
fetchers.forEach((fetcher) => {
let contests = [];
fetcher.updateUpcoming(contests).on('end', () => {
logger.info('merging ' + fetcher.name + ' (found: ' + contests.length + ')');
if(contests.length > 0) {
alerts.add_alerts(contests, fetcher);
upcoming.push.apply(upcoming, contests);
upcoming.sort((a, b) => { return a.time - b.time; });
}
});
});
}
fetch.init = function () {
// Makes initial fetch and schedules one for 3am everyday
fetch.updateUpcoming();
schedule.scheduleJob({ hour: 3, minute: 0, second: 0}, () => { fetch.updateUpcoming(); });
}