2023-03-05 16:17:46 +00:00
|
|
|
const fetch = require('node-fetch')
|
|
|
|
|
2023-03-03 16:44:40 +00:00
|
|
|
async function validateVideoInput(input) {
|
|
|
|
if (!input) return {
|
|
|
|
fail: true,
|
|
|
|
message: 'Missing URL'
|
|
|
|
}
|
|
|
|
|
2023-03-05 16:17:46 +00:00
|
|
|
const id = (input.trim()).match(/^https:\/\/(?:(?:www\.)?youtube\.com\/(?:watch\?v=|shorts\/)|youtu\.be\/)([A-Za-z0-9_-]{11})$/m)?.[1]
|
2023-03-03 16:44:40 +00:00
|
|
|
if (!id) return {
|
|
|
|
fail: true,
|
|
|
|
message: 'Whoops! What is that? That is not a Youtube url.'
|
|
|
|
}
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
async function validatePlaylistInput(input) {
|
|
|
|
if (!input) return {
|
|
|
|
fail: true,
|
|
|
|
message: 'Missing URL'
|
|
|
|
}
|
|
|
|
|
2023-03-05 16:17:46 +00:00
|
|
|
const id = (input.trim()).match(/^(?:https?:\/\/)?(?:www\.)?youtu(?:(?:\.be)|(?:be\.com))\/playlist\?list=([\w_-]{34})$/m)?.[1]
|
2023-03-03 16:44:40 +00:00
|
|
|
if (!id) return {
|
|
|
|
fail: true,
|
|
|
|
message: 'Whoops! What is that? That is not a Youtube Playlist.'
|
|
|
|
}
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
async function validateChannelInput(input) {
|
|
|
|
if (!input) return {
|
|
|
|
fail: true,
|
|
|
|
message: 'Missing URL'
|
|
|
|
}
|
|
|
|
|
2023-03-05 16:17:46 +00:00
|
|
|
const id = input.match(/^(?:https?:\/\/)?(?:www\.)?youtu(?:(?:\.be)|(?:be\.com))\/(?:channel\/|@)([\w-]+)/m)?.[1]
|
2023-03-03 16:44:40 +00:00
|
|
|
if (!id) return {
|
|
|
|
fail: true,
|
|
|
|
message: 'Whoops! What is that? That is not a Youtube Channel.'
|
|
|
|
}
|
|
|
|
|
2023-03-05 16:17:46 +00:00
|
|
|
if (input.includes('@')) {
|
2023-11-26 10:16:49 +00:00
|
|
|
const channelId = await (await fetch(`https://yt.jaybee.digital/api/channels?part=channels&handle=${id}`)).json()
|
|
|
|
return channelId['items'][0]['id']
|
2023-03-05 16:17:46 +00:00
|
|
|
} else {
|
|
|
|
return id
|
|
|
|
}
|
2023-03-03 16:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { validateVideoInput, validatePlaylistInput, validateChannelInput }
|