-
Notifications
You must be signed in to change notification settings - Fork 1
/
application.js
164 lines (107 loc) · 3.43 KB
/
application.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
console.log(`****** STARTING UP ${new Date().toISOString()} ******`);
const {Config, saveConfig, Trakt} = require('./utils');
const {PlexQuery} = require('./plex');
const Job = require('./job');
const Path = require('path');
const FS = require('fs');
const HealtCheckJob = require('./health-check');
const HealtCheck = new HealtCheckJob({
jobschedule: Config.HEALT_CHECK_CRON
});
let _console_log = console.log;
console.log = function() {
let args = Array.prototype.slice.call(arguments, 0);
args.unshift( `(${new Date().toISOString()}) -`);
_console_log.apply(console, args);
};
let datafolder = Config.DATAFOLDER
if ( ! FS.existsSync(datafolder) ) {
FS.mkdirSync( datafolder );
}
if ( Config.ENABLE_WEBHOOK ) {
require('./webhook/webhook');
}
let libraries = Config.ConfigFile.Libraries || {};
let jobs = [];
async function loginTrakt() {
if ( FS.existsSync(Config.TraktTokenFile) ) {
let tokensraw = FS.readFileSync( Config.TraktTokenFile, {encoding: 'utf-8'});
if ( tokensraw ) {
try {
let tokens = JSON.parse(tokensraw);
tokens = await Trakt.login(tokens);
FS.writeFileSync( Config.TraktTokenFile, JSON.stringify(tokens, null, 2), 'utf-8');
} catch(e) {
Log.warn('Trakt tokens are not valid');
}
}
}
}
async function executeLoginTrakt() {
console.log('Trakt login...');
let codes = await Trakt.getCode();
console.log('go to https://trakt.tv/activate and type this code:', codes.user_code);
console.log('waiting for login...');
let tokens = await Trakt.pollAuth(codes);
FS.writeFileSync( Config.TraktTokenFile, JSON.stringify(tokens, null, 2), 'utf-8');
console.log('login successfull!');
}
async function execTrakt() {
await loginTrakt();
if ( ! Trakt.LoggedIn ) {
await executeLoginTrakt();
}
if ( ! Trakt.LoggedIn ) {
throw "Trakt is not loggedin";
}
}
async function startPlex() {
PlexQuery('/library/sections').then( (results) => {
console.log(`got directories: ${results.MediaContainer.Directory.length}`);
let directories = results.MediaContainer.Directory;
directories = directories.filter( (dir) => {
if ( Config.PLEX_SKIP_LIBRARY.indexOf(dir.key) > -1 ) {
console.log(`[WARN] Library [${dir.title} - ${dir.key}] has been marked for skip`);
return false;
}
return true;
})
for ( let dir of directories ) {
console.log(`Library [${dir.title} - ${dir.key}] will be processed`);
let lib = libraries[ dir.title ];
if ( !lib ) {
lib = libraries[ dir.title ] = {lastScan: 0};
console.log(`found new directory`);
}
lib.title = dir.title;
lib.key = dir.key;
lib.type = dir.type;
lib.jobschedule = Config.CRON;
let job = new Job( lib );
job.on('gotitems', (items) => {
let item = items.slice(0).pop();
item = (item && item.plexItem) || item;
if ( item ) {
console.log(`${job.JobName} report scanned from ${lib.lastScan} to ${item.addedAt}`);
lib.lastScan = item.addedAt;
saveConfig();
}
});
jobs.push( job );
}
}).then( () => {
Config.ConfigFile.Libraries = libraries;
// TODO: save preferences file
saveConfig();
}).then( () => {
for ( let job of jobs ) {
job.start();
}
HealtCheck.start();
});
}
async function StartApplication() {
await execTrakt();
await startPlex();
}
StartApplication()