-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcapture.js
More file actions
62 lines (62 loc) · 1.66 KB
/
capture.js
File metadata and controls
62 lines (62 loc) · 1.66 KB
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
#!/usr/bin/env node
let activities = require("./activities.js");
const chalk = require("chalk");
var argv = require("yargs/yargs")(process.argv.slice(2)).command(
"watch",
"download datafeed every [seconds]",
(yargs) => {
yargs
.option("maxAge", {
describe: "maximum age of last feed",
default: 1000 * 60 * 1,
type: "number",
})
.option("verbose", {
alias: "v",
type: "boolean",
description: "Run with verbose logging",
default: true,
});
},
(argv) => {
if (argv.verbose)
console.info(
`Watching the datafeed. Will download every ${argv.maxAge} seconds.`
);
watch(argv.maxAge, argv.verbose);
}
).argv;
function watch(maxAge, verbose) {
setInterval(
() =>
activities.getNewest().then(
function (newest) {
let lastUpdated = new Date(newest * 1000);
let age = Date.now() - lastUpdated;
if (age > maxAge) {
if (verbose)
console.info(
`Local datafeed was last updated ${Math.round(
age / (1000 * 60)
)} minutes ago, downloading a new version`
);
activities.downloadLatestData().catch((e) => {
console.log(chalk.bgRed(e));
return;
});
} else {
if (verbose)
console.info(
`Local datafeed was last updated ${Math.round(
age / 1000
)} seconds ago - no need for a new one`
);
}
},
function (error) {
console.log(error);
}
),
maxAge
);
}