This repository has been archived by the owner on Jul 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregate.js
62 lines (55 loc) · 1.59 KB
/
aggregate.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const sauertracker = require('./sauertracker');
const database = require('./database');
const util = require('util');
const debuglog = util.debuglog('aggregate');
const game_debuglog = util.debuglog('games'); // Meh.
const timers = require('timers');
const process = require('process');
/**
* Returns a generator starting at index
* @param {number} index [index=1]
* @return {Generator}
*/
function* games(index = 1) {
var ok = true;
var p;
while(ok) {
p = sauertracker.game(index);
p.catch(() => ok = false);
yield p;
debuglog('Fetching game with index %d', index)
index += 1;
}
}
// Currently the value is around 333400 - 334000, atm 333426
/**
* Aggregates over the API and inserts games
* @param {number} index [1]
* @param {Object} collection - a MongoDB collection object
*/
function aggregate(index = 1, collection) {
debuglog('Starting aggregation at index %d', index)
let g = games(index);
timers.setInterval(() => {
g.next().value.then((game) => {
game.time = new Date(game.time);
collection.insertOne(game, {}, (err, game) => game_debuglog('Inserted game %o', game));
})
}, 400)
}
// Maybe outsource this?
var db = database.connect();
database.connect().then((Db) => {
let collection = Db.collection('games');
collection.count(function(err, count) {
let index = (count > 0) ? count : 1;
aggregate(index, collection)
})
})
// There might be a better way to handle this;
process.on('uncaughtException', (err) => {
if (err instanceof TypeError) {
debuglog('Ended updating.');
process.exit();
}
})