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);
|
2024-09-28 18:44:00 -03:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2024-09-28 18:44:00 -03:00
|
|
|
async function downloadFromYoutube(command, args) {
|
2024-09-28 17:24:27 -03:00
|
|
|
return new Promise((resolve, reject) => {
|
2024-09-28 18:44:00 -03:00
|
|
|
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 videoFormat = "-f bestvideo+bestaudio";
|
2024-09-28 18:44:00 -03:00
|
|
|
const dlpCommand = ytDlpPath;
|
|
|
|
const dlpArgs = [videoUrl, videoFormat, ...cmdArgs.split(' '), mp4File];
|
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 17:24:27 -03:00
|
|
|
|
2024-09-28 21:35:05 -03:00
|
|
|
if (approxSizeInMB >= 50) {
|
|
|
|
await ctx.telegram.editMessageText(
|
|
|
|
ctx.chat.id,
|
|
|
|
downloadingMessage.message_id,
|
|
|
|
null,
|
|
|
|
strings.ytUploadLimit, {
|
|
|
|
parse_mode: 'Markdown',
|
|
|
|
reply_to_message_id: ctx.message.message_id,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
await downloadFromYoutube(dlpCommand, dlpArgs);
|
2024-09-28 17:24:27 -03:00
|
|
|
|
2024-09-28 21:35:05 -03:00
|
|
|
await ctx.telegram.editMessageText(
|
|
|
|
ctx.chat.id,
|
|
|
|
downloadingMessage.message_id,
|
|
|
|
null,
|
|
|
|
strings.ytUploading, {
|
|
|
|
parse_mode: 'Markdown',
|
|
|
|
reply_to_message_id: ctx.message.message_id,
|
|
|
|
});
|
2024-09-28 17:24:27 -03:00
|
|
|
|
2024-09-28 21:35:05 -03:00
|
|
|
if (fs.existsSync(mp4File)) {
|
|
|
|
const message = strings.ytUploadDesc
|
|
|
|
.replace("{userId}", userId)
|
|
|
|
.replace("{userName}", ctx.from.first_name);
|
2024-09-28 19:10:53 -03:00
|
|
|
|
2024-09-28 21:35:05 -03:00
|
|
|
try {
|
|
|
|
await ctx.replyWithVideo({
|
|
|
|
source: mp4File,
|
|
|
|
caption: `${message}`,
|
|
|
|
parse_mode: 'Markdown',
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
await ctx.reply(`\`${error}\``, {
|
2024-09-28 19:10:53 -03:00
|
|
|
parse_mode: 'Markdown',
|
|
|
|
reply_to_message_id: ctx.message.message_id,
|
|
|
|
});
|
2024-09-28 21:35:05 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-28 19:10:53 -03:00
|
|
|
} catch (downloadError) {
|
2024-09-28 17:24:27 -03:00
|
|
|
fs.unlinkSync(mp4File);
|
|
|
|
const message = strings.ytDownloadErr
|
2024-09-28 21:35:05 -03:00
|
|
|
.replace("{err}", downloadError.error ? downloadError.error.message : 'Unknown error')
|
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
|
|
|
};
|