kowalski/commands/youtube.js

141 lines
4.2 KiB
JavaScript
Raw Normal View History

2024-09-28 17:24:27 -03:00
const { getStrings } = require('../plugins/checklang.js');
const { isOnSpamWatch } = require('../plugins/lib-spamwatch/spamwatch.js');
const spamwatchMiddleware = require('../plugins/lib-spamwatch/Middleware.js')(isOnSpamWatch);
const { execFile } = require('child_process');
2024-09-28 17:24:27 -03:00
const os = require('os');
2024-09-26 21:56:50 -03:00
const fs = require('fs');
2024-09-28 17:24:27 -03:00
const path = require('path');
2024-09-26 17:08:30 -03:00
2024-09-28 17:24:27 -03:00
const ytDlpPaths = {
linux: path.resolve(__dirname, '../plugins/yt-dlp/yt-dlp'),
win32: path.resolve(__dirname, '../plugins/yt-dlp/yt-dlp.exe'),
darwin: path.resolve(__dirname, '../plugins/yt-dlp/yt-dlp_macos'),
};
2024-09-26 21:56:50 -03:00
2024-09-28 17:24:27 -03:00
function getYtDlpPath() {
const platform = os.platform();
return ytDlpPaths[platform] || ytDlpPaths.linux;
};
async function downloadFromYoutube(command, args) {
2024-09-28 17:24:27 -03:00
return new Promise((resolve, reject) => {
execFile(command, args, (error, stdout, stderr) => {
2024-09-28 17:24:27 -03:00
if (error) {
reject({ error, stdout, stderr });
} else {
resolve({ stdout, stderr });
}
2024-09-26 17:08:30 -03:00
});
2024-09-28 17:24:27 -03:00
});
};
2024-09-26 17:08:30 -03:00
2024-09-28 21:35:05 -03:00
async function getApproxSize(command, videoUrl) {
const args = [videoUrl, '--compat-opt', 'manifest-filesize-approx', '-O', 'filesize_approx'];
return new Promise((resolve, reject) => {
execFile(command, args, (error, stdout, stderr) => {
if (error) {
reject({ error, stdout, stderr });
} else {
const sizeInBytes = parseInt(stdout.trim(), 10);
if (!isNaN(sizeInBytes)) {
const sizeInMB = sizeInBytes / (1024 * 1024);
resolve(sizeInMB);
} else {
reject(new Error('Invalid size received from yt-dlp'));
}
}
});
});
};
2024-09-26 17:08:30 -03:00
module.exports = (bot) => {
2024-09-28 21:35:05 -03:00
bot.command(['yt', 'ytdl'], spamwatchMiddleware, async (ctx) => {
2024-09-28 17:24:27 -03:00
const strings = getStrings(ctx.from.language_code);
const ytDlpPath = getYtDlpPath();
const userId = ctx.from.id;
const videoUrl = ctx.message.text.split(' ').slice(1).join(' ');
const mp4File = `tmp/${userId}.mp4`;
const cmdArgs = "--max-filesize 2G --no-playlist --merge-output-format mp4 -o";
const dlpCommand = ytDlpPath;
2024-09-28 17:24:27 -03:00
const downloadingMessage = await ctx.reply(strings.ytDownloading, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id,
});
try {
2024-09-28 21:35:05 -03:00
const approxSizeInMB = await getApproxSize(ytDlpPath, videoUrl);
2024-09-28 22:01:14 -03:00
let videoFormat = "";
2024-09-28 17:24:27 -03:00
2024-09-28 21:35:05 -03:00
if (approxSizeInMB >= 50) {
2024-09-28 22:01:14 -03:00
videoFormat = `-f best`;
2024-09-28 21:35:05 -03:00
} else {
2024-09-28 22:01:14 -03:00
videoFormat = "-f bestvideo+bestaudio";
}
const dlpArgs = [videoUrl, videoFormat, ...cmdArgs.split(' '), mp4File];
await downloadFromYoutube(dlpCommand, dlpArgs);
await ctx.telegram.editMessageText(
ctx.chat.id,
downloadingMessage.message_id,
null,
strings.ytUploading, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id,
});
if (fs.existsSync(mp4File)) {
const message = strings.ytUploadDesc
.replace("{userId}", userId)
.replace("{userName}", ctx.from.first_name);
try {
await ctx.replyWithVideo({
source: mp4File,
caption: `${message}`,
parse_mode: 'Markdown',
});
await ctx.telegram.editMessageText(
ctx.chat.id,
downloadingMessage.message_id,
null,
strings.ytUploadLimit2, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id,
});
fs.unlinkSync(mp4File);
} catch (error) {
await ctx.reply(`\`${error}\``, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id,
});
2024-09-28 21:35:05 -03:00
}
}
2024-09-28 22:01:14 -03:00
} catch (error) {
2024-09-28 17:24:27 -03:00
fs.unlinkSync(mp4File);
2024-09-28 22:01:14 -03:00
let errStatus = "";
if (error == "Error: 413: Request Entity Too Large") {
errStatus = Strings.ytUploadLimit;
} else {
errStatus = error.error ? error.error.message : 'Unknown error';
}
2024-09-28 17:24:27 -03:00
const message = strings.ytDownloadErr
2024-09-28 22:01:14 -03:00
.replace("{err}", errStatus)
2024-09-28 17:24:27 -03:00
.replace("{userName}", ctx.from.first_name);
2024-09-28 19:10:53 -03:00
await ctx.reply(message, {
2024-09-28 17:24:27 -03:00
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id,
});
}
});
2024-09-28 19:10:53 -03:00
};