fix: yt: set cookies for check_size args

This commit is contained in:
GiovaniFZ 2025-04-14 11:50:52 -03:00
parent ff9260bebc
commit 8ec088796e
No known key found for this signature in database
GPG key ID: 63DD92181B575322

View file

@ -1,193 +1,198 @@
const { getStrings } = require('../plugins/checklang.js'); const { getStrings } = require('../plugins/checklang.js');
const { isOnSpamWatch } = require('../plugins/lib-spamwatch/spamwatch.js'); const { isOnSpamWatch } = require('../plugins/lib-spamwatch/spamwatch.js');
const spamwatchMiddleware = require('../plugins/lib-spamwatch/Middleware.js')(isOnSpamWatch); const spamwatchMiddleware = require('../plugins/lib-spamwatch/Middleware.js')(isOnSpamWatch);
const { execFile } = require('child_process'); const { execFile } = require('child_process');
const os = require('os'); const os = require('os');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const ytDlpPaths = { const ytDlpPaths = {
linux: path.resolve(__dirname, '../plugins/yt-dlp/yt-dlp'), linux: path.resolve(__dirname, '../plugins/yt-dlp/yt-dlp'),
win32: path.resolve(__dirname, '../plugins/yt-dlp/yt-dlp.exe'), win32: path.resolve(__dirname, '../plugins/yt-dlp/yt-dlp.exe'),
darwin: path.resolve(__dirname, '../plugins/yt-dlp/yt-dlp_macos'), darwin: path.resolve(__dirname, '../plugins/yt-dlp/yt-dlp_macos'),
}; };
const getYtDlpPath = () => { const getYtDlpPath = () => {
const platform = os.platform(); const platform = os.platform();
return ytDlpPaths[platform] || ytDlpPaths.linux; return ytDlpPaths[platform] || ytDlpPaths.linux;
}; };
const ffmpegPaths = { const ffmpegPaths = {
linux: '/usr/bin/ffmpeg', linux: '/usr/bin/ffmpeg',
win32: path.resolve(__dirname, '../plugins/ffmpeg/bin/ffmpeg.exe'), win32: path.resolve(__dirname, '../plugins/ffmpeg/bin/ffmpeg.exe'),
}; };
const getFfmpegPath = () => { const getFfmpegPath = () => {
const platform = os.platform(); const platform = os.platform();
return ffmpegPaths[platform] || ffmpegPaths.linux; return ffmpegPaths[platform] || ffmpegPaths.linux;
}; };
const downloadFromYoutube = async (command, args) => { const downloadFromYoutube = async (command, args) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
execFile(command, args, (error, stdout, stderr) => { execFile(command, args, (error, stdout, stderr) => {
if (error) { if (error) {
reject({ error, stdout, stderr }); reject({ error, stdout, stderr });
} else { } else {
resolve({ stdout, stderr }); resolve({ stdout, stderr });
} }
}); });
}); });
}; };
const getApproxSize = async (command, videoUrl) => { const getApproxSize = async (command, videoUrl) => {
const args = [videoUrl, '--compat-opt', 'manifest-filesize-approx', '-O', 'filesize_approx']; let args = [];
try { if (fs.existsSync(path.resolve(__dirname, "../props/cookies.txt"))) {
const { stdout } = await downloadFromYoutube(command, args); args = [videoUrl, '--compat-opt', 'manifest-filesize-approx', '-O', 'filesize_approx', '--cookies', path.resolve(__dirname, "../props/cookies.txt")];
const sizeInBytes = parseInt(stdout.trim(), 10); } else {
if (!isNaN(sizeInBytes)) { args = [videoUrl, '--compat-opt', 'manifest-filesize-approx', '-O', 'filesize_approx'];
return sizeInBytes / (1024 * 1024); }
} else { try {
return 0; const { stdout } = await downloadFromYoutube(command, args);
} const sizeInBytes = parseInt(stdout.trim(), 10);
} catch (error) { if (!isNaN(sizeInBytes)) {
throw error; return sizeInBytes / (1024 * 1024);
} } else {
}; return 0;
}
module.exports = (bot) => { } catch (error) {
bot.command(['yt', 'ytdl', 'sdl', 'video', 'dl'], spamwatchMiddleware, async (ctx) => { throw error;
const Strings = getStrings(ctx.from.language_code); }
const ytDlpPath = getYtDlpPath(); };
const userId = ctx.from.id;
const videoUrl = ctx.message.text.split(' ').slice(1).join(' '); module.exports = (bot) => {
const mp4File = `tmp/${userId}.mp4`; bot.command(['yt', 'ytdl', 'sdl', 'video', 'dl'], spamwatchMiddleware, async (ctx) => {
const tempMp4File = `tmp/${userId}.f137.mp4`; const Strings = getStrings(ctx.from.language_code);
const tempWebmFile = `tmp/${userId}.f251.webm`; const ytDlpPath = getYtDlpPath();
let cmdArgs = ""; const userId = ctx.from.id;
const dlpCommand = ytDlpPath; const videoUrl = ctx.message.text.split(' ').slice(1).join(' ');
const ffmpegPath = getFfmpegPath(); const mp4File = `tmp/${userId}.mp4`;
const ffmpegArgs = ['-i', tempMp4File, '-i', tempWebmFile, '-c:v copy -c:a copy -strict -2', mp4File]; const tempMp4File = `tmp/${userId}.f137.mp4`;
const tempWebmFile = `tmp/${userId}.f251.webm`;
if (!videoUrl) { let cmdArgs = "";
return ctx.reply(Strings.ytDownload.noLink, { const dlpCommand = ytDlpPath;
parse_mode: "Markdown", const ffmpegPath = getFfmpegPath();
disable_web_page_preview: true, const ffmpegArgs = ['-i', tempMp4File, '-i', tempWebmFile, '-c:v copy -c:a copy -strict -2', mp4File];
reply_to_message_id: ctx.message.message_id
}); if (!videoUrl) {
}; return ctx.reply(Strings.ytDownload.noLink, {
parse_mode: "Markdown",
if (fs.existsSync(path.resolve(__dirname, "../props/cookies.txt"))) { disable_web_page_preview: true,
cmdArgs = "--max-filesize 2G --no-playlist --cookies src/props/cookies.txt --merge-output-format mp4 -o"; reply_to_message_id: ctx.message.message_id
} else { });
cmdArgs = `--max-filesize 2G --no-playlist --merge-output-format mp4 -o`; };
}
if (fs.existsSync(path.resolve(__dirname, "../props/cookies.txt"))) {
try { cmdArgs = "--max-filesize 2G --no-playlist --cookies src/props/cookies.txt --merge-output-format mp4 -o";
const downloadingMessage = await ctx.reply(Strings.ytDownload.checkingSize, { } else {
parse_mode: 'Markdown', cmdArgs = `--max-filesize 2G --no-playlist --merge-output-format mp4 -o`;
reply_to_message_id: ctx.message.message_id, }
});
try {
if (fs.existsSync(ytDlpPath)) { const downloadingMessage = await ctx.reply(Strings.ytDownload.checkingSize, {
const approxSizeInMB = await Promise.race([ parse_mode: 'Markdown',
getApproxSize(ytDlpPath, videoUrl), reply_to_message_id: ctx.message.message_id,
]); });
await ctx.telegram.editMessageText( if (fs.existsSync(ytDlpPath)) {
ctx.chat.id, const approxSizeInMB = await Promise.race([
downloadingMessage.message_id, getApproxSize(ytDlpPath, videoUrl),
null, ]);
Strings.ytDownload.downloadingVid, {
parse_mode: 'Markdown', await ctx.telegram.editMessageText(
reply_to_message_id: ctx.message.message_id, ctx.chat.id,
}, downloadingMessage.message_id,
); null,
Strings.ytDownload.downloadingVid, {
const dlpArgs = [videoUrl, ...cmdArgs.split(' '), mp4File]; parse_mode: 'Markdown',
await downloadFromYoutube(dlpCommand, dlpArgs); reply_to_message_id: ctx.message.message_id,
},
await ctx.telegram.editMessageText( );
ctx.chat.id,
downloadingMessage.message_id, const dlpArgs = [videoUrl, ...cmdArgs.split(' '), mp4File];
null, await downloadFromYoutube(dlpCommand, dlpArgs);
Strings.ytDownload.uploadingVid, {
parse_mode: 'Markdown', await ctx.telegram.editMessageText(
reply_to_message_id: ctx.message.message_id, ctx.chat.id,
}, downloadingMessage.message_id,
); null,
Strings.ytDownload.uploadingVid, {
if(fs.existsSync(tempMp4File)){ parse_mode: 'Markdown',
await downloadFromYoutube(ffmpegPath, ffmpegArgs); reply_to_message_id: ctx.message.message_id,
} },
);
if (fs.existsSync(mp4File)) {
const message = Strings.ytDownload.msgDesc.replace("{userMention}", `[${ctx.from.first_name}](tg://user?id=${userId})`) if (fs.existsSync(tempMp4File)) {
await downloadFromYoutube(ffmpegPath, ffmpegArgs);
try { }
await ctx.replyWithVideo({
source: mp4File }, { if (fs.existsSync(mp4File)) {
caption: message, const message = Strings.ytDownload.msgDesc.replace("{userMention}", `[${ctx.from.first_name}](tg://user?id=${userId})`)
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id, try {
}); await ctx.replyWithVideo({
source: mp4File
fs.unlinkSync(mp4File); }, {
} catch (error) { caption: message,
if (toString(error).includes("Request Entity Too Large")) { parse_mode: 'Markdown',
await ctx.telegram.editMessageText( reply_to_message_id: ctx.message.message_id,
ctx.chat.id, });
downloadingMessage.message_id,
null, fs.unlinkSync(mp4File);
Strings.ytDownload.uploadLimit, { } catch (error) {
parse_mode: 'Markdown', if (toString(error).includes("Request Entity Too Large")) {
reply_to_message_id: ctx.message.message_id, await ctx.telegram.editMessageText(
}, ctx.chat.id,
); downloadingMessage.message_id,
} else { null,
const errMsg = Strings.ytDownload.uploadErr.replace("{error}", error) Strings.ytDownload.uploadLimit, {
await ctx.telegram.editMessageText( parse_mode: 'Markdown',
ctx.chat.id, reply_to_message_id: ctx.message.message_id,
downloadingMessage.message_id, },
null, );
errMsg, { } else {
parse_mode: 'Markdown', const errMsg = Strings.ytDownload.uploadErr.replace("{error}", error)
reply_to_message_id: ctx.message.message_id, await ctx.telegram.editMessageText(
}, ctx.chat.id,
); downloadingMessage.message_id,
}; null,
errMsg, {
fs.unlinkSync(mp4File); parse_mode: 'Markdown',
} reply_to_message_id: ctx.message.message_id,
} else { },
await ctx.reply(mp4File, { );
parse_mode: 'Markdown', };
reply_to_message_id: ctx.message.message_id,
}); fs.unlinkSync(mp4File);
} }
} else { } else {
await ctx.telegram.editMessageText( await ctx.reply(mp4File, {
ctx.chat.id, parse_mode: 'Markdown',
downloadingMessage.message_id, reply_to_message_id: ctx.message.message_id,
null, });
Strings.ytDownload.libNotFound, { }
parse_mode: 'Markdown', } else {
reply_to_message_id: ctx.message.message_id, await ctx.telegram.editMessageText(
}, ctx.chat.id,
); downloadingMessage.message_id,
} null,
} catch (error) { Strings.ytDownload.libNotFound, {
console.error(error); parse_mode: 'Markdown',
const errMsg = Strings.ytDownload.uploadErr.replace("{error}", error) reply_to_message_id: ctx.message.message_id,
await ctx.telegram.editMessageText( },
ctx.chat.id, );
downloadingMessage.message_id, }
null, } catch (error) {
errMsg, { const errMsg = Strings.ytDownload.uploadErr.replace("{error}", error)
parse_mode: 'Markdown', await ctx.telegram.editMessageText(
reply_to_message_id: ctx.message.message_id, ctx.chat.id,
}, downloadingMessage.message_id,
); null,
} errMsg, {
}); parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id,
},
);
}
});
}; };