mirror of
https://github.com/abocn/TelegramBot.git
synced 2025-05-09 12:05:00 +00:00
222 lines
7.3 KiB
TypeScript
222 lines
7.3 KiB
TypeScript
import { getStrings } from '../plugins/checklang';
|
|
import { isOnSpamWatch } from '../spamwatch/spamwatch';
|
|
import spamwatchMiddlewareModule from '../spamwatch/Middleware';
|
|
import { execFile } from 'child_process';
|
|
import os from 'os';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import * as ytUrl from 'youtube-url';
|
|
|
|
const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch);
|
|
|
|
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'),
|
|
};
|
|
|
|
const getYtDlpPath = () => {
|
|
const platform = os.platform();
|
|
return ytDlpPaths[platform] || ytDlpPaths.linux;
|
|
};
|
|
|
|
|
|
const ffmpegPaths = {
|
|
linux: '/usr/bin/ffmpeg',
|
|
win32: path.resolve(__dirname, '../plugins/ffmpeg/bin/ffmpeg.exe'),
|
|
};
|
|
|
|
const getFfmpegPath = () => {
|
|
const platform = os.platform();
|
|
return ffmpegPaths[platform] || ffmpegPaths.linux;
|
|
};
|
|
|
|
const downloadFromYoutube = async (command: string, args: string[]): Promise<{ stdout: string; stderr: string }> => {
|
|
return new Promise((resolve, reject) => {
|
|
execFile(command, args, (error, stdout, stderr) => {
|
|
if (error) {
|
|
reject({ error, stdout, stderr });
|
|
} else {
|
|
resolve({ stdout, stderr });
|
|
}
|
|
});
|
|
});
|
|
};
|
|
|
|
const getApproxSize = async (command: string, videoUrl: string): Promise<number> => {
|
|
let args: string[] = [];
|
|
if (fs.existsSync(path.resolve(__dirname, "../props/cookies.txt"))) {
|
|
args = [videoUrl, '--compat-opt', 'manifest-filesize-approx', '-O', 'filesize_approx', '--cookies', path.resolve(__dirname, "../props/cookies.txt")];
|
|
} else {
|
|
args = [videoUrl, '--compat-opt', 'manifest-filesize-approx', '-O', 'filesize_approx'];
|
|
}
|
|
try {
|
|
const { stdout } = await downloadFromYoutube(command, args);
|
|
const sizeInBytes = parseInt(stdout.trim(), 10);
|
|
if (!isNaN(sizeInBytes)) {
|
|
return sizeInBytes / (1024 * 1024);
|
|
} else {
|
|
return 0;
|
|
}
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export default (bot) => {
|
|
bot.command(['yt', 'ytdl', 'sdl', 'video', 'dl'], spamwatchMiddleware, async (ctx) => {
|
|
const Strings = getStrings(ctx.from.language_code);
|
|
const ytDlpPath = getYtDlpPath();
|
|
const userId: number = ctx.from.id;
|
|
const videoUrl: string = ctx.message.text.split(' ').slice(1).join(' ');
|
|
const videoUrlSafe: boolean = ytUrl.valid(videoUrl);
|
|
const randId: string = Math.random().toString(36).substring(2, 15);
|
|
const mp4File: string = `tmp/${userId}-${randId}.mp4`;
|
|
const tempMp4File: string = `tmp/${userId}-${randId}.f137.mp4`;
|
|
const tempWebmFile: string = `tmp/${userId}-${randId}.f251.webm`;
|
|
let cmdArgs: string = "";
|
|
const dlpCommand: string = ytDlpPath;
|
|
const ffmpegPath: string = getFfmpegPath();
|
|
const ffmpegArgs: string[] = ['-i', tempMp4File, '-i', tempWebmFile, '-c:v copy -c:a copy -strict -2', mp4File];
|
|
|
|
console.log(`DOWNLOADING: ${videoUrl}\nSAFE: ${videoUrlSafe}\n`)
|
|
|
|
if (!videoUrl) {
|
|
return ctx.reply(Strings.ytDownload.noLink, {
|
|
parse_mode: "Markdown",
|
|
disable_web_page_preview: true,
|
|
reply_to_message_id: ctx.message.message_id
|
|
});
|
|
} else if (!videoUrlSafe) {
|
|
return ctx.reply(Strings.ytDownload.notYtLink, {
|
|
parse_mode: "Markdown",
|
|
disable_web_page_preview: true,
|
|
reply_to_message_id: ctx.message.message_id
|
|
});
|
|
}
|
|
|
|
if (fs.existsSync(path.resolve(__dirname, "../props/cookies.txt"))) {
|
|
cmdArgs = "--max-filesize 2G --no-playlist --cookies src/props/cookies.txt --merge-output-format mp4 -o";
|
|
} else {
|
|
cmdArgs = `--max-filesize 2G --no-playlist --merge-output-format mp4 -o`;
|
|
}
|
|
|
|
try {
|
|
const downloadingMessage = await ctx.reply(Strings.ytDownload.checkingSize, {
|
|
parse_mode: 'Markdown',
|
|
reply_to_message_id: ctx.message.message_id,
|
|
});
|
|
|
|
if (fs.existsSync(ytDlpPath)) {
|
|
const approxSizeInMB = await Promise.race([
|
|
getApproxSize(ytDlpPath, videoUrl),
|
|
]);
|
|
|
|
if (approxSizeInMB > 50) {
|
|
await ctx.telegram.editMessageText(
|
|
ctx.chat.id,
|
|
downloadingMessage.message_id,
|
|
null,
|
|
Strings.ytDownload.uploadLimit, {
|
|
parse_mode: 'Markdown',
|
|
reply_to_message_id: ctx.message.message_id,
|
|
},
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
await ctx.telegram.editMessageText(
|
|
ctx.chat.id,
|
|
downloadingMessage.message_id,
|
|
null,
|
|
Strings.ytDownload.downloadingVid, {
|
|
parse_mode: 'Markdown',
|
|
reply_to_message_id: ctx.message.message_id,
|
|
},
|
|
);
|
|
|
|
const dlpArgs = [videoUrl, ...cmdArgs.split(' '), mp4File];
|
|
await downloadFromYoutube(dlpCommand, dlpArgs);
|
|
|
|
await ctx.telegram.editMessageText(
|
|
ctx.chat.id,
|
|
downloadingMessage.message_id,
|
|
null,
|
|
Strings.ytDownload.uploadingVid, {
|
|
parse_mode: 'Markdown',
|
|
reply_to_message_id: ctx.message.message_id,
|
|
},
|
|
);
|
|
|
|
if (fs.existsSync(tempMp4File)) {
|
|
await downloadFromYoutube(ffmpegPath, ffmpegArgs);
|
|
}
|
|
|
|
if (fs.existsSync(mp4File)) {
|
|
const message = Strings.ytDownload.msgDesc.replace("{userMention}", `[${ctx.from.first_name}](tg://user?id=${userId})`)
|
|
|
|
try {
|
|
await ctx.replyWithVideo({
|
|
source: mp4File
|
|
}, {
|
|
caption: message,
|
|
parse_mode: 'Markdown',
|
|
reply_to_message_id: ctx.message.message_id,
|
|
});
|
|
|
|
fs.unlinkSync(mp4File);
|
|
} catch (error) {
|
|
if (error.response.description.includes("Request Entity Too Large")) {
|
|
await ctx.telegram.editMessageText(
|
|
ctx.chat.id,
|
|
downloadingMessage.message_id,
|
|
null,
|
|
Strings.ytDownload.uploadLimit, {
|
|
parse_mode: 'Markdown',
|
|
reply_to_message_id: ctx.message.message_id,
|
|
},
|
|
);
|
|
} else {
|
|
const errMsg = Strings.ytDownload.uploadErr.replace("{error}", error)
|
|
await ctx.telegram.editMessageText(
|
|
ctx.chat.id,
|
|
downloadingMessage.message_id,
|
|
null,
|
|
errMsg, {
|
|
parse_mode: 'Markdown',
|
|
reply_to_message_id: ctx.message.message_id,
|
|
},
|
|
);
|
|
};
|
|
|
|
fs.unlinkSync(mp4File);
|
|
}
|
|
} else {
|
|
await ctx.reply(mp4File, {
|
|
parse_mode: 'Markdown',
|
|
reply_to_message_id: ctx.message.message_id,
|
|
});
|
|
}
|
|
} else {
|
|
await ctx.telegram.editMessageText(
|
|
ctx.chat.id,
|
|
downloadingMessage.message_id,
|
|
null,
|
|
Strings.ytDownload.libNotFound, {
|
|
parse_mode: 'Markdown',
|
|
reply_to_message_id: ctx.message.message_id,
|
|
},
|
|
);
|
|
}
|
|
} catch (error) {
|
|
const errMsg = Strings.ytDownload.uploadErr.replace("{error}", error)
|
|
// will no longer edit the message as the message context is not outside the try block
|
|
await ctx.reply(errMsg, {
|
|
parse_mode: 'Markdown',
|
|
reply_to_message_id: ctx.message.message_id,
|
|
},
|
|
);
|
|
}
|
|
});
|
|
}; |