From bbe894a66f03a044c9b8ab3013a1a11611e4a1de Mon Sep 17 00:00:00 2001 From: Supositware Date: Thu, 18 Aug 2022 02:43:08 +0200 Subject: [PATCH] Download yt-dlp --- scripts/updateytdlp.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 scripts/updateytdlp.js diff --git a/scripts/updateytdlp.js b/scripts/updateytdlp.js new file mode 100644 index 0000000..2717f52 --- /dev/null +++ b/scripts/updateytdlp.js @@ -0,0 +1,41 @@ +import fs from 'node:fs'; +import https from 'node:https'; + +if (process.platform !== 'linux' && process.argv[2] !== '-f') { + console.error('This script only download the linux version of yt-dlp. If you want to download anyway try again with -f'); + process.exit(1); +} +else if (process.platform !== 'linux' && process.argv[2] === '-f') { + console.log('Executed with -f. Reminder that this script only download the linux version of yt-dlp.'); +} + +const downloadUrl = 'https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp'; + +download(downloadUrl); + +async function download(url) { + return new Promise((resolve, reject) => { + https.get(url, (res) => { + if (res.statusCode === 301 || res.statusCode === 302) { + console.log(`yt-dlp download url: ${res.headers.location}`); + return download(res.headers.location); + } + + const tmpPath = './bin/yt-dlp.new'; + const path = './bin/yt-dlp'; + const filePath = fs.createWriteStream(tmpPath); + res.pipe(filePath); + filePath.on('finish', () => { + filePath.close(); + fs.renameSync(tmpPath, path); + fs.chmodSync('./bin/yt-dlp', '755'); + console.log('yt-dlp download finished.'); + resolve(true); + }); + filePath.on('error', (err) => { + filePath.close(); + reject(err); + }); + }); + }); +} \ No newline at end of file