From a0de902935752cb5b95b9d585b79cc2e03a4a11b Mon Sep 17 00:00:00 2001 From: Supositware Date: Mon, 20 Mar 2023 03:58:21 +0100 Subject: [PATCH] Moved download function to its own file --- scripts/downloadutils.js | 34 ++++++++++++++++++++++++++++++++++ scripts/updateytdlp.js | 32 ++------------------------------ 2 files changed, 36 insertions(+), 30 deletions(-) create mode 100644 scripts/downloadutils.js diff --git a/scripts/downloadutils.js b/scripts/downloadutils.js new file mode 100644 index 0000000..6e158c0 --- /dev/null +++ b/scripts/downloadutils.js @@ -0,0 +1,34 @@ +import fs from 'node:fs'; +import https from 'node:https'; + +export default { + download, +}; + +async function download(url, output) { + return new Promise((resolve, reject) => { + https.get(url, (res) => { + if (res.statusCode === 301 || res.statusCode === 302) { + console.log(`${output} download url: ${res.headers.location}`); + return download(res.headers.location, output); + } + + const path = output; + const tmpPath = `${output}.new`; + + const filePath = fs.createWriteStream(tmpPath); + res.pipe(filePath); + filePath.on('finish', () => { + filePath.close(); + fs.renameSync(tmpPath, path); + fs.chmodSync(path, '755'); + console.log(`${url} download finished.`); + resolve(true); + }); + filePath.on('error', (err) => { + filePath.close(); + reject(err); + }); + }); + }); +} \ No newline at end of file diff --git a/scripts/updateytdlp.js b/scripts/updateytdlp.js index 2717f52..076146a 100644 --- a/scripts/updateytdlp.js +++ b/scripts/updateytdlp.js @@ -1,5 +1,4 @@ -import fs from 'node:fs'; -import https from 'node:https'; +import utils from './downloadutils.js'; 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'); @@ -11,31 +10,4 @@ else if (process.platform !== 'linux' && process.argv[2] === '-f') { 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 +utils.download(downloadUrl, './bin/yt-dlp');