]>
Commit | Line | Data |
---|---|---|
ce32426b | 1 | // Thanks: https://github.com/przemyslawpluta/node-youtube-dl/blob/master/lib/downloader.js |
299474e8 | 2 | // We rewrote it to avoid sync calls |
ce32426b C |
3 | |
4 | import { AbstractScheduler } from './abstract-scheduler' | |
5 | import { SCHEDULER_INTERVALS_MS } from '../../initializers' | |
6 | import { logger } from '../../helpers/logger' | |
7 | import * as request from 'request' | |
62689b94 | 8 | import { createWriteStream, ensureDir, writeFile } from 'fs-extra' |
ce32426b | 9 | import { join } from 'path' |
62689b94 | 10 | import { root } from '../../helpers/core-utils' |
ce32426b C |
11 | |
12 | export class YoutubeDlUpdateScheduler extends AbstractScheduler { | |
13 | ||
14 | private static instance: AbstractScheduler | |
15 | ||
16 | protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.youtubeDLUpdate | |
17 | ||
18 | private constructor () { | |
19 | super() | |
20 | } | |
21 | ||
22 | async execute () { | |
4f1f6f03 C |
23 | logger.info('Updating youtubeDL binary.') |
24 | ||
ce32426b C |
25 | const binDirectory = join(root(), 'node_modules', 'youtube-dl', 'bin') |
26 | const bin = join(binDirectory, 'youtube-dl') | |
27 | const detailsPath = join(binDirectory, 'details') | |
28 | const url = 'https://yt-dl.org/downloads/latest/youtube-dl' | |
29 | ||
62689b94 | 30 | await ensureDir(binDirectory) |
4f1f6f03 C |
31 | |
32 | return new Promise(res => { | |
33 | request.get(url, { followRedirect: false }, (err, result) => { | |
34 | if (err) { | |
35 | logger.error('Cannot update youtube-dl.', { err }) | |
36 | return res() | |
ce32426b C |
37 | } |
38 | ||
4f1f6f03 C |
39 | if (result.statusCode !== 302) { |
40 | logger.error('youtube-dl update error: did not get redirect for the latest version link. Status %d', result.statusCode) | |
41 | return res() | |
42 | } | |
ce32426b | 43 | |
4f1f6f03 C |
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 ] | |
ce32426b | 47 | |
4f1f6f03 C |
48 | downloadFile.on('response', result => { |
49 | if (result.statusCode !== 200) { | |
50 | logger.error('Cannot update youtube-dl: new version response is not 200, it\'s %d.', result.statusCode) | |
51 | return res() | |
ce32426b C |
52 | } |
53 | ||
4f1f6f03 | 54 | downloadFile.pipe(createWriteStream(bin, { mode: 493 })) |
ce32426b | 55 | }) |
ce32426b | 56 | |
4f1f6f03 C |
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 | }) | |
ce32426b C |
75 | }) |
76 | } | |
77 | ||
78 | static get Instance () { | |
79 | return this.instance || (this.instance = new this()) | |
80 | } | |
81 | } |