-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (59 loc) · 1.64 KB
/
index.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
const { default: axios } = require('axios');
const fs = require('fs');
const { green, yellow } = require('colors');
const { exit } = require('process');
const readData = async () => {
const rawdata = await fs.promises.readFile('data.json');
return JSON.parse(rawdata);
};
const isModified = (remote, local) => {
return (
remote.headers.etag !== local.etag ||
remote.headers['last-modified'] !== local.lastModified
);
};
const updateData = (etag, lastModified) => {
const data = JSON.stringify({
etag,
lastModified,
});
fs.writeFileSync('data.json', data);
};
const downloadFile = () => {
const writer = fs.createWriteStream('download/data.zip');
return axios
.get('https://www.funrecords.de/download/newsxls.zip', {
responseType: 'stream',
})
.then(async (response) => {
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', () => {
resolve(writer.close());
});
});
});
};
setInterval(async () => {
const [remote, local] = await Promise.all([
axios.head('https://www.funrecords.de/download/newsxls.zip'),
readData(),
]);
if (isModified(remote, local)) {
updateData(remote.headers.etag, remote.headers['last-modified']);
await downloadFile();
console.log(green('Il file è stato aggiornato.'));
exit(0);
}
console.log(
yellow(
new Intl.DateTimeFormat('it-IT', {
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
hour12: false,
timeZone: 'Europe/Rome',
}).format(new Date()) + ' Il file non è ancora stato aggiornato.'
)
);
}, 3000);