From a90a35343a7a671163e5864743e2261cbb86f5dc Mon Sep 17 00:00:00 2001 From: unknown <89595418+unknownsrc@users.noreply.github.com> Date: Sun, 5 Mar 2023 17:17:46 +0100 Subject: [PATCH] changing regex, allowing @ for channels --- utils/validate.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/utils/validate.js b/utils/validate.js index 22dba36..f8edd8e 100644 --- a/utils/validate.js +++ b/utils/validate.js @@ -1,10 +1,12 @@ +const fetch = require('node-fetch') + async function validateVideoInput(input) { if (!input) return { fail: true, message: 'Missing URL' } - const id = input.match(/https:\/\/(?:(?:www\.)?youtube\.com\/(?:watch\?v=|shorts\/)|youtu\.be\/)([A-Za-z0-9_-]{11})/)?.[1] + const id = (input.trim()).match(/^https:\/\/(?:(?:www\.)?youtube\.com\/(?:watch\?v=|shorts\/)|youtu\.be\/)([A-Za-z0-9_-]{11})$/m)?.[1] if (!id) return { fail: true, message: 'Whoops! What is that? That is not a Youtube url.' @@ -19,7 +21,7 @@ async function validatePlaylistInput(input) { message: 'Missing URL' } - const id = input.match(/^.*(youtu.be\/|list=)([^#\&\?]*).*/)?.[2] + const id = (input.trim()).match(/^(?:https?:\/\/)?(?:www\.)?youtu(?:(?:\.be)|(?:be\.com))\/playlist\?list=([\w_-]{34})$/m)?.[1] if (!id) return { fail: true, message: 'Whoops! What is that? That is not a Youtube Playlist.' @@ -34,13 +36,18 @@ async function validateChannelInput(input) { message: 'Missing URL' } - const id = input.match(/https?:\/\/(?:www\.)?youtube\.com\/(?:channel\/(.*?)\/featured|(?:.*\/)*(.+))/)?.[2] + const id = input.match(/^(?:https?:\/\/)?(?:www\.)?youtu(?:(?:\.be)|(?:be\.com))\/(?:channel\/|@)([\w-]+)/m)?.[1] if (!id) return { fail: true, message: 'Whoops! What is that? That is not a Youtube Channel.' } - return id + if (input.includes('@')) { + const channelId = await (await fetch(`https://yt.jaybee.digital/api/channels?part=channels&handle=${id}`)).json() + return channelId['items'][0]['id'] + } else { + return id + } } module.exports = { validateVideoInput, validatePlaylistInput, validateChannelInput } \ No newline at end of file