diff --git a/src/commands/youtube.ts b/src/commands/youtube.ts index 94d7bdf..90e3c49 100644 --- a/src/commands/youtube.ts +++ b/src/commands/youtube.ts @@ -63,6 +63,15 @@ const getApproxSize = async (command: string, videoUrl: string): Promise } }; +const isValidUrl = (url: string): boolean => { + try { + new URL(url); + return true; + } catch { + return false; + } +}; + export default (bot) => { bot.command(['yt', 'ytdl', 'sdl', 'video', 'dl'], spamwatchMiddleware, async (ctx) => { const Strings = getStrings(ctx.from.language_code); @@ -78,8 +87,6 @@ export default (bot) => { 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}\nYOUTUBE: ${videoIsYoutube}\n`) /* for now, no checking is done for the video url @@ -93,6 +100,18 @@ export default (bot) => { }); } + // make sure its a valid url + if (!isValidUrl(videoUrl)) { + console.log("[!] Invalid URL:", videoUrl) + return ctx.reply(Strings.ytDownload.noLink, { + parse_mode: "Markdown", + disable_web_page_preview: true, + reply_to_message_id: ctx.message.message_id + }); + } + + console.log(`\nDownload Request:\nURL: ${videoUrl}\nYOUTUBE: ${videoIsYoutube}\n`) + 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 { @@ -111,6 +130,7 @@ export default (bot) => { ]); if (approxSizeInMB > 50) { + console.log("[!] Video size exceeds 50MB:", approxSizeInMB) await ctx.telegram.editMessageText( ctx.chat.id, downloadingMessage.message_id, @@ -124,6 +144,7 @@ export default (bot) => { return; } + console.log("[i] Downloading video...") await ctx.telegram.editMessageText( ctx.chat.id, downloadingMessage.message_id, @@ -137,6 +158,7 @@ export default (bot) => { const dlpArgs = [videoUrl, ...cmdArgs.split(' '), mp4File]; await downloadFromYoutube(dlpCommand, dlpArgs); + console.log("[i] Uploading video...") await ctx.telegram.editMessageText( ctx.chat.id, downloadingMessage.message_id, @@ -207,14 +229,15 @@ export default (bot) => { }, ); } + console.log("[i] Request completed\n") } catch (error) { - const errMsg = Strings.ytDownload.uploadErr.replace("{error}", error) + console.log("[!]", error.stderr) + const errMsg = Strings.ytDownload.uploadErr // 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, - }, - ); + }); } }); }; \ No newline at end of file diff --git a/src/locales/english.json b/src/locales/english.json index 00eafa5..c9ebf8f 100644 --- a/src/locales/english.json +++ b/src/locales/english.json @@ -73,7 +73,7 @@ "uploadingVid": "*Uploading video...*", "msgDesc": "{userMention}*, there is your downloaded video.*", "downloadErr": "*Error during YT video download:*\n\n`{err}`", - "uploadErr": "Error uploading file. Please try again later.\n\n{error}", + "uploadErr": "Error uploading file. Please try again later.", "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." diff --git a/src/locales/portuguese.json b/src/locales/portuguese.json index bb58896..67ef438 100644 --- a/src/locales/portuguese.json +++ b/src/locales/portuguese.json @@ -73,7 +73,7 @@ "uploadingVid": "*Enviando vídeo...*", "msgDesc": "{userMention}*, aqui está o seu vídeo baixado.*", "downloadErr": "*Erro durante o download do vídeo do YT:*\n\n`{err}`", - "uploadErr": "Erro ao enviar o arquivo. Tente novamente mais tarde.\n\n{error}", + "uploadErr": "Erro ao enviar o arquivo. Tente novamente mais tarde.", "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.*"