kowalski/commands/youtube.js

164 lines
4.9 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'),
2024-09-28 17:24:27 -03:00
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-30 07:42:19 -03:00
const getYtDlpPath = () => {
2024-09-28 17:24:27 -03:00
const platform = os.platform();
return ytDlpPaths[platform] || ytDlpPaths.linux;
};
2024-09-28 17:24:27 -03:00
2024-09-30 07:42:19 -03:00
const downloadFromYoutube = async (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-30 07:42:19 -03:00
};
2024-09-26 17:08:30 -03:00
2024-09-30 07:42:19 -03:00
const getApproxSize = async (command, videoUrl) => {
2024-09-28 21:35:05 -03:00
const args = [videoUrl, '--compat-opt', 'manifest-filesize-approx', '-O', 'filesize_approx'];
2024-09-30 07:42:19 -03:00
try {
const { stdout } = await downloadFromYoutube(command, args);
const sizeInBytes = parseInt(stdout.trim(), 10);
if (!isNaN(sizeInBytes)) {
return sizeInBytes / (1024 * 1024);
} else {
throw new Error('Invalid size received from yt-dlp');
}
} catch (error) {
throw error;
}
};
2024-09-28 21:35:05 -03:00
2024-09-30 07:42:19 -03:00
const timeoutPromise = (timeout) => {
2024-09-29 23:09:44 -03:00
return new Promise((_, reject) => {
setTimeout(() => {
reject(new Error('Timeout: Check took too long'));
}, timeout);
});
2024-09-30 07:42:19 -03:00
};
2024-09-28 21:35:05 -03:00
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-30 07:42:19 -03:00
const strings = getStrings(ctx.from.language_code);
2024-09-28 17:24:27 -03:00
const ytDlpPath = getYtDlpPath();
const userId = ctx.from.id;
const videoUrl = ctx.message.text.split(' ').slice(1).join(' ');
const mp4File = `tmp/${userId}.mp4`;
2024-09-30 08:55:12 -03:00
const cmdArgs = "--max-filesize 2G --no-playlist --cookies props/cookies.txt --merge-output-format mp4 -o";
const dlpCommand = ytDlpPath;
2024-09-28 17:24:27 -03:00
2024-09-30 07:42:19 -03:00
try {
const downloadingMessage = await ctx.reply(strings.ytCheckingSize, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id,
});
2024-09-28 17:24:27 -03:00
2024-09-30 07:42:19 -03:00
if (fs.existsSync(ytDlpPath)) {
2024-09-29 23:09:44 -03:00
const approxSizeInMB = await Promise.race([
2024-10-19 11:32:46 -03:00
getApproxSize(ytDlpPath, videoUrl)
2024-09-29 23:09:44 -03:00
]);
2024-09-30 07:42:19 -03:00
let videoFormat = approxSizeInMB >= 50 ? '-f best' : "-f bestvideo+bestaudio";
2024-09-29 23:09:44 -03:00
await ctx.telegram.editMessageText(
ctx.chat.id,
downloadingMessage.message_id,
null,
2024-09-30 07:42:19 -03:00
strings.ytDownloading, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id,
},
);
2024-09-29 23:09:44 -03:00
2024-10-19 12:49:56 -03:00
const ffmpegArgs = '&& which ffmpeg';
const dlpArgs = [videoUrl, videoFormat, ...cmdArgs.split(' '), mp4File, ffmpegArgs];
2024-09-29 23:09:44 -03:00
await downloadFromYoutube(dlpCommand, dlpArgs);
await ctx.telegram.editMessageText(
ctx.chat.id,
downloadingMessage.message_id,
null,
2024-09-30 07:42:19 -03:00
strings.ytUploading, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id,
},
);
2024-09-29 23:09:44 -03:00
2024-10-19 12:49:56 -03:00
if(fs.existsSync)
2024-09-29 23:09:44 -03:00
if (fs.existsSync(mp4File)) {
2024-09-30 07:42:19 -03:00
const message = strings.ytUploadDesc
2024-09-29 23:09:44 -03:00
.replace("{userId}", userId)
.replace("{userName}", ctx.from.first_name);
try {
await ctx.replyWithVideo({
source: mp4File,
caption: `${message}`,
parse_mode: 'Markdown',
});
if (approxSizeInMB >= 50) {
await ctx.telegram.editMessageText(
ctx.chat.id,
downloadingMessage.message_id,
null,
2024-09-30 07:42:19 -03:00
strings.ytUploadLimit2, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id,
},
);
2024-09-29 23:09:44 -03:00
}
fs.unlinkSync(mp4File);
} catch (error) {
await ctx.reply(`\`${error.message}\``, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id,
});
fs.unlinkSync(mp4File);
}
} else {
2024-09-30 07:42:19 -03:00
await ctx.reply(`\`${error.message}\``, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id,
});
2024-09-29 23:09:44 -03:00
}
2024-09-30 07:42:19 -03:00
} else {
await ctx.telegram.editMessageText(
ctx.chat.id,
downloadingMessage.message_id,
null,
strings.ytFileErr, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id,
},
);
2024-09-29 23:09:44 -03:00
}
2024-09-30 07:42:19 -03:00
} catch (error) {
console.error(error);
await ctx.reply(`\`${error.message}\``, {
2024-09-28 17:24:27 -03:00
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id,
});
2024-09-30 07:42:19 -03:00
}
2024-09-28 17:24:27 -03:00
});
2024-09-30 07:42:19 -03:00
};