-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.server.js
71 lines (55 loc) · 1.84 KB
/
main.server.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
import fs from 'fs';
import path from 'path';
export default class AmazonCalendar {
static init() {
AmazonCalendar.importAmazonPrimeVideoActivity();
const watcher = fs.watch(path.join(config['folder'], 'Digital.PrimeVideo.Viewinghistory.csv'), { persistent: false }, (curr, prev) => {
watcher.close();
log('./reloading.STATS.AmazonPrime', 'boot');
AmazonCalendar.importAmazonPrimeVideoActivity();
});
}
static async insertEvent(minDate, line) {
const elements = line.split(',');
const startElements = elements[0].split(' ');
const startDateElements = startElements[0].split('/');
const start = new Date(`${startDateElements[2]}-${startDateElements[0]}-${startDateElements[1]}T${startElements[1]}Z`);
const end = new Date(start);
end.setHours(end.getHours() + 1);
if(start.getTime() <= minDate.getTime()) {
return;
}
const id = 'PrimeVideo-' + start;
const field = {
id: id,
title: 'PrimeVideo',
description: elements[12],
start,
end,
origin: ''
};
if((await Database.execQuery('SELECT id FROM calendar WHERE id = $1', [id])).rows.length === 0) {
const [query, values] = Database.buildInsertQuery('calendar', field);
await Database.execQuery(
query,
values
);
}
}
static async importAmazonPrimeVideoActivity() {
const minDate = (await Database.execQuery(
'SELECT MAX(start) as min FROM calendar WHERE title = $1', ['PrimeVideo']
)).rows[0].min;
minDate.setHours(minDate.getHours() - 6); // Safety margin
const file = fs.readFileSync(path.join(config['folder'], 'Digital.PrimeVideo.Viewinghistory.csv')).toString();
let firstLineSkipped = false;
for(const line of file.split('\n')) {
if(!firstLineSkipped || line === '') {
firstLineSkipped = true;
continue;
}
await AmazonCalendar.insertEvent(minDate, line);
}
log('Saved Amazon Activity', 'info');
}
}