@lordxdd/yt-dl
Version:
YouTube-dl
41 lines (34 loc) • 1.2 kB
JavaScript
const https = require('https');
const fs = require('fs');
const os = require('os');
const path = require('path');
const BIN_DIR = path.join(__dirname, '..', 'bin');
if (!fs.existsSync(BIN_DIR)) fs.mkdirSync(BIN_DIR);
const platform = os.platform();
const isWin = platform === 'win32';
const fileName = isWin ? 'yt-dlp.exe' : 'yt-dlp';
const destPath = path.join(BIN_DIR, fileName);
const url = isWin
? 'https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp.exe'
: 'https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp';
console.log(`Downloading yt-dlp for ${platform}...`);
function download(url, dest, cb) {
https.get(url, (res) => {
if (res.statusCode === 302 || res.statusCode === 301) {
return download(res.headers.location, dest, cb);
}
const file = fs.createWriteStream(dest);
res.pipe(file);
file.on('finish', () => {
file.close(() => {
if (!isWin) fs.chmodSync(dest, 0o755);
console.log('yt-dlp downloaded.');
cb();
});
});
}).on('error', (err) => {
fs.unlinkSync(dest);
console.error(err.message);
});
}
download(url, destPath, () => {});