feat: add checking and logging for valid yt url

This commit is contained in:
Aidan 2025-04-29 19:27:08 -04:00
parent ac7119dee5
commit 19ce5295b1
4 changed files with 27 additions and 13 deletions

View File

@ -9,6 +9,7 @@
"node-html-parser": "^7.0.1",
"nodemon": "^3.1.10",
"telegraf": "^4.16.3",
"winston": "^3.17.0"
"winston": "^3.17.0",
"youtube-url": "^0.5.0"
}
}

View File

@ -5,6 +5,7 @@ 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);
@ -66,15 +67,19 @@ 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 = ctx.from.id;
const videoUrl = ctx.message.text.split(' ').slice(1).join(' ');
const mp4File = `tmp/${userId}.mp4`;
const tempMp4File = `tmp/${userId}.f137.mp4`;
const tempWebmFile = `tmp/${userId}.f251.webm`;
let cmdArgs = "";
const dlpCommand = ytDlpPath;
const ffmpegPath = getFfmpegPath();
const ffmpegArgs = ['-i', tempMp4File, '-i', tempWebmFile, '-c:v copy -c:a copy -strict -2', mp4File];
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, {
@ -82,7 +87,13 @@ export default (bot) => {
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";

View File

@ -75,7 +75,8 @@
"uploadErr": "Error uploading file. Please try again later.\n\n{error}",
"uploadLimit": "*This video exceeds the 50 MB upload limit imposed by Telegram on our bot. Please try another video. We're doing our best to increase this limit.*",
"sizeLimitWarn": "*This video had its quality reduced because it exceeded the 50MB limit for uploads imposed by Telegram.*",
"noLink": "Please provide a link to a video to download."
"noLink": "Please provide a link to a video to download.",
"notYtLink": "Please provide a valid YouTube link to download."
},
"botUpdated": "Bot updated with success.\n\n```{result}```",
"errorUpdatingBot": "Error updating bot\n\n{error}",

View File

@ -75,7 +75,8 @@
"uploadErr": "Erro ao enviar o arquivo. Tente novamente mais tarde.\n\n{error}",
"uploadLimit": "*Este vídeo excede o limite de carregamento de 50 MB imposto pelo Telegram ao nosso bot. Por favor, tente outro vídeo. Estamos fazendo o possível para aumentar esse limite.*",
"sizeLimitWarn": "*Esse vídeo teve a qualidade reduzida por estar excedendo o limite de 50MB para uploads imposto pelo Telegram.*",
"noLink": "*Por favor, forneça um link de um vídeo para download.*"
"noLink": "*Por favor, forneça um link de um vídeo para download.*",
"notYtLink": "*Forneça um link válido do YouTube para fazer o download.*"
},
"botUpdated": "Bot atualizado com sucesso.\n\n```{result}```",
"errorUpdatingBot": "Erro ao atualizar o bot\n\n{error}",