-
Notifications
You must be signed in to change notification settings - Fork 0
/
updatememberlist.js
79 lines (73 loc) · 1.92 KB
/
updatememberlist.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import fs from "fs";
import config from "config";
import Logger from "./src/logger.js";
import { parse } from "csv-parse";
import axios from "axios";
const logger = new Logger({process: "updatememberlist"});
const tempFile = "temp/members.csv";
const membersFile = "members.csv";
const key = config.get("members.querykey");
/**
* Download file to temporary file for checking
* This prevents a 404/500 page replacing the memberlist
*/
axios
.get(`https://members.hacman.org.uk/api/keyfobs/csv?api_token=${key}`, {
responseType: "stream",
})
.then((response) => {
if (response.status != 200) {
logger.info({
action: "DOWNLOAD",
message: "Memberlist response wasn't a 200",
});
throw "BadStatus"
}
response.data.pipe(fs.createWriteStream(tempFile)).on('close', () => {
verifyFile();
});
}).catch(e => {
// If we can't get a new memberlist, oh well.
// access.js will report if the memberlist goes too stale.
console.log(e);
});
/**
* Verify what we downloaded looks legit
* Can't be 0 length (blank page means bad api key)
* Must parse as a CSV
*/
const verifyFile = () => {
fs.readFile(tempFile, "utf8", (err, data) => {
if (err) {
logger.info({
action: "READ",
message: "Couldn't read temporary members file",
});
return;
}
if (data.length == 0) {
logger.info({
action: "LENGTH",
message: "Memberlist length was 0 (incorrect API key?)",
});
return;
}
parse(data, function (err, records) {
if (err) {
logger.info({
action: "PARSE",
message: `Error parsing new members file: ${err}`,
});
return;
}
fs.writeFile(membersFile, data, (err) => {
if (err) {
logger.info({
action: "WRITE",
message: "Error writing new members file",
});
}
});
});
});
};