diff options
Diffstat (limited to 'server/lib/schedulers/youtube-dl-update-scheduler.ts')
-rw-r--r-- | server/lib/schedulers/youtube-dl-update-scheduler.ts | 71 |
1 files changed, 40 insertions, 31 deletions
diff --git a/server/lib/schedulers/youtube-dl-update-scheduler.ts b/server/lib/schedulers/youtube-dl-update-scheduler.ts index a2d919603..24cd3f87b 100644 --- a/server/lib/schedulers/youtube-dl-update-scheduler.ts +++ b/server/lib/schedulers/youtube-dl-update-scheduler.ts | |||
@@ -7,7 +7,7 @@ import { logger } from '../../helpers/logger' | |||
7 | import * as request from 'request' | 7 | import * as request from 'request' |
8 | import { createWriteStream, writeFile } from 'fs' | 8 | import { createWriteStream, writeFile } from 'fs' |
9 | import { join } from 'path' | 9 | import { join } from 'path' |
10 | import { root } from '../../helpers/core-utils' | 10 | import { mkdirpPromise, root } from '../../helpers/core-utils' |
11 | 11 | ||
12 | export class YoutubeDlUpdateScheduler extends AbstractScheduler { | 12 | export class YoutubeDlUpdateScheduler extends AbstractScheduler { |
13 | 13 | ||
@@ -20,49 +20,58 @@ export class YoutubeDlUpdateScheduler extends AbstractScheduler { | |||
20 | } | 20 | } |
21 | 21 | ||
22 | async execute () { | 22 | async execute () { |
23 | logger.info('Updating youtubeDL binary.') | ||
24 | |||
23 | const binDirectory = join(root(), 'node_modules', 'youtube-dl', 'bin') | 25 | const binDirectory = join(root(), 'node_modules', 'youtube-dl', 'bin') |
24 | const bin = join(binDirectory, 'youtube-dl') | 26 | const bin = join(binDirectory, 'youtube-dl') |
25 | const detailsPath = join(binDirectory, 'details') | 27 | const detailsPath = join(binDirectory, 'details') |
26 | const url = 'https://yt-dl.org/downloads/latest/youtube-dl' | 28 | const url = 'https://yt-dl.org/downloads/latest/youtube-dl' |
27 | 29 | ||
28 | request.get(url, { followRedirect: false }, (err, res) => { | 30 | await mkdirpPromise(binDirectory) |
29 | if (err) { | 31 | |
30 | logger.error('Cannot update youtube-dl.', { err }) | 32 | return new Promise(res => { |
31 | return | 33 | request.get(url, { followRedirect: false }, (err, result) => { |
32 | } | 34 | if (err) { |
33 | 35 | logger.error('Cannot update youtube-dl.', { err }) | |
34 | if (res.statusCode !== 302) { | 36 | return res() |
35 | logger.error('youtube-dl update error: did not get redirect for the latest version link. Status %d', res.statusCode) | ||
36 | return | ||
37 | } | ||
38 | |||
39 | const url = res.headers.location | ||
40 | const downloadFile = request.get(url) | ||
41 | const newVersion = /yt-dl\.org\/downloads\/(\d{4}\.\d\d\.\d\d(\.\d)?)\/youtube-dl/.exec(url)[1] | ||
42 | |||
43 | downloadFile.on('response', res => { | ||
44 | if (res.statusCode !== 200) { | ||
45 | logger.error('Cannot update youtube-dl: new version response is not 200, it\'s %d.', res.statusCode) | ||
46 | return | ||
47 | } | 37 | } |
48 | 38 | ||
49 | downloadFile.pipe(createWriteStream(bin, { mode: 493 })) | 39 | if (result.statusCode !== 302) { |
50 | }) | 40 | logger.error('youtube-dl update error: did not get redirect for the latest version link. Status %d', result.statusCode) |
41 | return res() | ||
42 | } | ||
51 | 43 | ||
52 | downloadFile.on('error', err => logger.error('youtube-dl update error.', { err })) | 44 | const url = result.headers.location |
45 | const downloadFile = request.get(url) | ||
46 | const newVersion = /yt-dl\.org\/downloads\/(\d{4}\.\d\d\.\d\d(\.\d)?)\/youtube-dl/.exec(url)[ 1 ] | ||
53 | 47 | ||
54 | downloadFile.on('end', () => { | 48 | downloadFile.on('response', result => { |
55 | const details = JSON.stringify({ version: newVersion, path: bin, exec: 'youtube-dl' }) | 49 | if (result.statusCode !== 200) { |
56 | writeFile(detailsPath, details, { encoding: 'utf8' }, err => { | 50 | logger.error('Cannot update youtube-dl: new version response is not 200, it\'s %d.', result.statusCode) |
57 | if (err) { | 51 | return res() |
58 | logger.error('youtube-dl update error: cannot write details.', { err }) | ||
59 | return | ||
60 | } | 52 | } |
61 | 53 | ||
62 | logger.info('youtube-dl updated to version %s.', newVersion) | 54 | downloadFile.pipe(createWriteStream(bin, { mode: 493 })) |
63 | }) | 55 | }) |
64 | }) | ||
65 | 56 | ||
57 | downloadFile.on('error', err => { | ||
58 | logger.error('youtube-dl update error.', { err }) | ||
59 | return res() | ||
60 | }) | ||
61 | |||
62 | downloadFile.on('end', () => { | ||
63 | const details = JSON.stringify({ version: newVersion, path: bin, exec: 'youtube-dl' }) | ||
64 | writeFile(detailsPath, details, { encoding: 'utf8' }, err => { | ||
65 | if (err) { | ||
66 | logger.error('youtube-dl update error: cannot write details.', { err }) | ||
67 | return res() | ||
68 | } | ||
69 | |||
70 | logger.info('youtube-dl updated to version %s.', newVersion) | ||
71 | return res() | ||
72 | }) | ||
73 | }) | ||
74 | }) | ||
66 | }) | 75 | }) |
67 | } | 76 | } |
68 | 77 | ||