From 19ce5295b10795715a84ca845d8015f20f61ac6e Mon Sep 17 00:00:00 2001 From: Aidan Date: Tue, 29 Apr 2025 19:27:08 -0400 Subject: [PATCH] feat: add checking and logging for valid yt url --- package.json | 3 ++- src/commands/youtube.ts | 31 +++++++++++++++++++++---------- src/locales/english.json | 3 ++- src/locales/portuguese.json | 3 ++- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index db30850..d617c84 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/commands/youtube.ts b/src/commands/youtube.ts index 9fa6364..09ce859 100644 --- a/src/commands/youtube.ts +++ b/src/commands/youtube.ts @@ -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"; diff --git a/src/locales/english.json b/src/locales/english.json index 5e21a15..773c490 100644 --- a/src/locales/english.json +++ b/src/locales/english.json @@ -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}", diff --git a/src/locales/portuguese.json b/src/locales/portuguese.json index 08025e9..c7b1b4b 100644 --- a/src/locales/portuguese.json +++ b/src/locales/portuguese.json @@ -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}",