-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
462 lines (407 loc) · 19.3 KB
/
helpers.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
const SonarrAPI = require('./sonarr');
const fs = require('fs');
const diskusage = require('diskusage-ng');
const https = require('https');
const Url = require('url').URL
const child_process = require('child_process');
const Queue = require('promise-queue')
const FileSync = require('lowdb/adapters/FileSync')
// shell-escape package annoyingly doesn't check if string
const shellescape = (a => (b) => a(b.map(c => c + '')))(require('shell-escape'));
const SONARR_OPTIONS = {
hostname: process.env.SONARR_HOST,
apiKey: process.env.SONARR_KEY,
port: 443,
ssl: true,
urlBase: ''
};
const DEBUG = process.env.DEBUG && process.env.DEBUG > 0;
const DEBUG_QUALITY = 'worstvideo+worstaudio/worst';
const EXPECTED_QUALITY = 'bestvideo[height<=1080]+bestaudio/best[height<=1080]'
const MIN_DISK_SPACE = 10 * 1024 * 1024 * 1024; // 10GB in bytes
function genericShellQueue(queue) {
return function (cmd, resultCallback) {
queue.add(() => {
return new Promise((resolve, reject) => {
if (DEBUG) console.debug(cmd);
child_process.exec(
cmd,
(error, stdout, stderr) => {
resultCallback(error, stdout, stderr);
resolve(stdout);
}
);
})
}).then(() => {}).catch((e) => { console.log('SHELL CATCH: ' + e)})
};
}
const downloadQueue = new Queue(2, Infinity);
let last_disk_availible = -1;
let historyDb = require('lowdb')(new FileSync('/config/history.json'));
module.exports = function (argv) {
const fileDestination = argv.output || '';
const webDestination = argv.http || '';
if (DEBUG) {
console.debug('DEBUG enabled.');
console.debug('Sonarr config: ' + JSON.stringify(SONARR_OPTIONS));
}
let self;
let sonarr = new SonarrAPI(SONARR_OPTIONS);
if (!(SONARR_OPTIONS.hostname && SONARR_OPTIONS.apiKey && SONARR_OPTIONS.port)) {
console.log('Warning: Missing sonarr details');
}
historyDb.defaults({ downloaded: [] });
// Cache sonarr requests
let episodeList = {};
function queueDownload(cmd, resultCallback) {
downloadQueue.add(() => {
return new Promise((resolve, reject) => {
diskusage(fileDestination, function(err, usage) {
if (err) return console.log(err);
if (DEBUG) console.debug(cmd);
if (usage.available > MIN_DISK_SPACE) {
last_disk_availible = usage.available;
child_process.exec(
cmd,
(error, stdout, stderr) => {
resultCallback(error, stdout, stderr);
resolve(stdout);
}
);
} else {
if (DEBUG) console.debug('Disk limit reached.');
}
});
})
}).then(() => {}).catch((e) => { console.log('SHELL CATCH: ' + e)})
}
function getProcessedFilename(filename, url, limiter) {
return new Promise((resolve) => {
self.queueShell(shellescape([
'yt-dlp',
'--no-warnings',
'--no-progress',
'--no-color',
'--cookies',
'/cookies.txt',
'--ignore-errors',
'--youtube-skip-dash-manifest',
'--get-filename',
'-f', (DEBUG ? DEBUG_QUALITY : limiter),
'--merge-output-format', 'mkv',
'--remux-video', 'mkv',
'-o', filename + '.mkv',
url
]), (error, stdout) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
let filename = (stdout.toString() || '').trim();
// Even when saying remux everything to mkv, yt-dlp outputs other extensions for single format sources for '--get-filename'
filename = filename.replace(/\.[^\.]+$/, '.mkv');
resolve(filename);
});
});
}
function downloadVideo(url, filename, limiter) {
if (last_disk_availible >= 0 && last_disk_availible < MIN_DISK_SPACE) {
// Don't brother. Disk is already full
return Promise.resolve(false);
}
return new Promise((downloaded) => {
let downloadUrl = new Url(url);
https.get(url, (res) => {
if(res.statusCode === 301 || res.statusCode === 302) {
if (res.headers.location) {
if (response.headers.location.match(/^http/)) {
let newUrl = new Url(response.headers.location);
downloadUrl.host = newUrl.host;
downloadUrl.path = newUrl.path;
} else {
downloadUrl.path = response.headers.location;
}
}
}
if (res.statusCode >= 200 && res.statusCode < 400) {
getProcessedFilename(filename, downloadUrl, limiter).then((properFilename) => {
if (properFilename) {
let outputFile = fileDestination + properFilename;
queueDownload(shellescape([
'yt-dlp',
'--add-metadata',
'-f', (DEBUG ? DEBUG_QUALITY : limiter),
'--all-subs',
'-c',
'--embed-subs',
'--no-mtime',
'--cookies',
'/cookies.txt',
'--youtube-skip-dash-manifest',
'--merge-output-format', 'mkv',
'--remux-video', 'mkv',
'-o', outputFile,
downloadUrl
]), (error) => {
if (error) {
console.log(error);
downloaded(false);
return;
}
fs.access(outputFile, fs.constants.R_OK, (error) => {
if (!error) {
fs.unlink(outputFile + '.torrent', () => {});
let now = Date.now() / 1000;
fs.utimes(outputFile, now, now, (err) => {
// Don't really care if date change fails. Updating just so cron doesn't clean up too early
self.queueShell(shellescape([
'mktorrent',
'-p',
'-t', 1,
'-c', 'Made with Sonarr Archiver',
'-a', 'udp://127.0.0.1',
'-w', webDestination + properFilename,
'-o', outputFile + '.torrent',
outputFile
]), (error) => {
if (error) {
console.error(error);
downloaded(false);
return
}
downloaded(properFilename + '.torrent');
});
});
}
});
});
}
}).catch((error) => {
console.log(error);
downloaded(false);
});
} else {
console.error(url + ' ' + res.statusCode);
downloaded(false);
}
});
});
}
self = {
getHistory: function(id) {
let items = historyDb.get('downloaded').filter({'id': id}).value();
if (items.length > 0) {
return items[0];
}
return null;
},
addHistory: function(id, url, resolution, filename) {
if (historyDb.get('downloaded').filter({'id': id}).value().length < 1) {
historyDb.defaults({ downloaded: [] }).get('downloaded').push({
id: id,
url: url,
resolution: resolution,
downloadTime: Date.now() / 1000,
filename: filename
}).write();
} else {
self.updateHistory(id, {
url: url,
resolution: resolution,
downloadTime: Date.now() / 1000,
filename: filename
});
}
},
// TODO: History management should REALLY be it's own thing
updateHistory: function(id, fields) {
let items = historyDb.get('downloaded').filter({'id': id}).value();
if (items.length > 0) {
historyDb.get('downloaded').find({'id': id}).assign(fields).write();
return items.length;
}
return 0;
},
youtubeDl: function (id, url, output, quality, resolution, limiter) {
quality = quality || 'WEBRip';
limiter = limiter || EXPECTED_QUALITY;
let filename;
const afterDownload = (torrentFile) => {
if (torrentFile) {
self.addHistory(id, url, resolution, filename)
if (!DEBUG) {
let req = https.request((SONARR_OPTIONS.ssl ? 'https://' : 'http://') + SONARR_OPTIONS.hostname + '/api/v3/release/push', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Api-Key': SONARR_OPTIONS.apiKey
}
}, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
console.log(data);
// Sonarr pretty prints by default. No need to store whitespace
self.updateHistory(id, {'sonarr_response': JSON.stringify(JSON.parse(data))});
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
req.write(JSON.stringify({
title: filename,
downloadUrl: webDestination + encodeURIComponent(torrentFile),
protocol:"torrent",
publishDate: (new Date()).toISOString()
}));
req.end();
} else {
self.updateHistory(id, {'sonarr_response': 'DEBUG_MODE'});
}
}
};
if (!resolution) { // TODO: Is there a better way? Maybe within an earlier JSON feed?
self.queueShell(
// Ask youtube-dl what resolution it's going to download
'yt-dlp --no-warnings --no-progress --no-color --cookies /cookies.txt --youtube-skip-dash-manifest --ignore-errors -f \'' + limiter + '\' --get-filename -o \'%(height)s\' \'' + url + '\'',
(error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
let string = stdout.toString();
if (string) {
resolution = string.trim() + 'p';
filename = (output + '.English.' + resolution + '.' + quality).replace('(', '').replace(')', '');
downloadVideo(url, filename, limiter).then(afterDownload);
}
}
);
} else {
filename = (output + '.English.' + resolution + '.' + quality).replace('(', '').replace(')', '');
downloadVideo(url, filename, limiter).then(afterDownload);
}
},
init: function () {
historyDb.read();
if (typeof historyDb.get('downloaded').value() === 'undefined') {
historyDb.set('downloaded', []).write();
}
},
filteredSonarrFind: function(showId, episodeSlug, filter) {
return new Promise((resolve) => {
self.findSonarrDetails(showId, '').then((x) => {
if (!episodeSlug) {
resolve(filter(Object.values(episodeList[showId])));
} else if (typeof filter === 'function') {
resolve(filter(episodeSlug in episodeList[showId] ? episodeList[showId][episodeSlug] : []));
} else {
resolve(false);
}
})
});
},
findSonarrDetails: function (showId, episodeSlug) {
if (!(showId in episodeList)) {
episodeList[showId] = false;
sonarr.get('series', {}).then(
serieses => sonarr.get("episode", { "seriesId": showId }).then(function (result) {
let series = null;
serieses.forEach(value => {
if (value.id == showId) {
series = value;
}
});
episodes = {};
let item, episode;
for (item of result) {
if (item && item.monitored) {
let compareSlug = self.toCompareSlug(item.title);
// "TBA" effectively means Sonarr doesn't have correct data yet.
// Would a show ever have an episode with the slug of "tba"? Let's hope not.
if (compareSlug == 'tba') {
continue;
}
episode = {
seriesSlug: series.sortTitle.toLowerCase(),
title: item.title,
season: item.seasonNumber,
episode: item.episodeNumber
};
if (!(compareSlug in episodes)) {
episodes[compareSlug] = [];
}
//Different seasons may have create duplicate slugs. Example show "Great Canadian Baking"
episodes[compareSlug].push(episode);
}
}
episodeList[showId] = episodes;
// Does it have a file?
// Has the cuttoff been met?
// pass back to download
}).catch((reason) => {
console.log(reason)
})).catch((reason) => {
console.log(reason)
});
}
return new Promise((resolve) => {
let loopId = setInterval(() => {
if (showId in episodeList && episodeList[showId]) {
clearInterval(loopId);
if (episodeSlug in episodeList[showId]) {
if (episodeList[showId][episodeSlug] && episodeList[showId][episodeSlug].length === 1) {
resolve(episodeList[showId][episodeSlug][0]);
} else {
resolve(false);
}
} else {
resolve(false);
}
}
}, 100);
});
},
toCompareSlug: function(input) {
input = input || '';
return input.trim().normalize('NFKD').normalize('NFD').replace(/[\u0300-\u036f]/g, '').toLowerCase().replace(/[^a-z0-9 -]+/g, ' ').replace(/\s+/g, '-').replace(/\-+/g, '-').replace(/\~/g, '_').replace(/\_+/g, '_');
},
queueShell: genericShellQueue(new Queue(2, Infinity)),
getFileName: function(showId, title, filter) {
return new Promise((resolve) => {
let compareSlug = self.toCompareSlug(title);
let detailPromise;
if (filter && typeof filter === 'function') {
detailPromise = self.filteredSonarrFind(showId, compareSlug, filter);
} else {
detailPromise = self.findSonarrDetails(showId, compareSlug);
}
detailPromise.catch((reason) => {
console.log('failed');
console.log(reason)
}).then((episode) => {
if (episode) {
// let title = episode.title.toLowerCase().replace(/[\[\]\/\?<>\~\\:\*\|\'\":,]/g, '').replace(/\s+/g, '.');
let showTitle = episode.seriesSlug.replace(/[^a-z0-9\s]/g, '').replace(/\s+/g, '.');
resolve(showTitle + '.S' + ('' + episode.season).padStart(2, '0') + 'E' + ('' + episode.episode).padStart(2, '0'));
} else {
resolve('');
}
}).catch(() => resolve(''));
});
},
// Stolen from https://medium.com/@oldwestaction/randomness-is-hard-e085decbcbb2
shuffleArray: (deck) => {
for (var i = deck.length - 1; i > 0; i--) {
const swapIndex = Math.floor(Math.random() * (i + 1))
const currentCard = deck[i]
const cardToSwap = deck[swapIndex]
deck[i] = cardToSwap
deck[swapIndex] = currentCard
}
return deck
}
};
return self;
}