backend/utils/validate.js

53 lines
1.5 KiB
JavaScript
Raw Normal View History

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'
}
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'
}
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'
}
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.'
}
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']
} else {
return id
}
2023-03-03 16:44:40 +00:00
}
module.exports = { validateVideoInput, validatePlaylistInput, validateChannelInput }