This commit is contained in:
killer069 2021-10-02 13:26:50 +05:30
parent 543d0e5fd0
commit ecc50ee186
2 changed files with 16 additions and 6 deletions

View File

@ -13,10 +13,13 @@ interface PlaylistOptions {
proxy?: Proxy[]; proxy?: Proxy[];
} }
const video_id_pattern = /^[a-zA-Z\d_-]{11,12}$/;
const playlist_id_pattern = /^PL[a-zA-Z\d_-]{32}$/;
const DEFAULT_API_KEY = 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8'; const DEFAULT_API_KEY = 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8';
const video_pattern = const video_pattern =
/^((?:https?:)?\/\/)?(?:(?:www|m)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$/; /^((?:https?:)?\/\/)?(?:(?:www|m)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$/;
const playlist_pattern = /^((?:https?:)?\/\/)?(?:(?:www|m)\.)?(youtube\.com)\/(?:(playlist|watch))(.*)?((\?|\&)list=)/; const playlist_pattern =
/^((?:https?:)?\/\/)?(?:(?:www|m)\.)?(youtube\.com)\/(?:(playlist|watch))(.*)?((\?|\&)list=)PL[a-zA-Z\d_-]{32}(.*)?$/;
/** /**
* Command to validate a YouTube url * Command to validate a YouTube url
* @param url Url for validation * @param url Url for validation
@ -24,8 +27,14 @@ const playlist_pattern = /^((?:https?:)?\/\/)?(?:(?:www|m)\.)?(youtube\.com)\/(?
*/ */
export function yt_validate(url: string): 'playlist' | 'video' | false { export function yt_validate(url: string): 'playlist' | 'video' | false {
if (url.indexOf('list=') === -1) { if (url.indexOf('list=') === -1) {
if (!url.match(video_pattern)) return false; if (url.startsWith('https')) {
else return 'video'; if (url.match(video_pattern)) return 'video';
else return false;
} else {
if (url.match(video_id_pattern)) return 'video';
else if (url.match(playlist_id_pattern)) return 'playlist';
else return false;
}
} else { } else {
if (!url.match(playlist_pattern)) return false; if (!url.match(playlist_pattern)) return false;
const Playlist_id = url.split('list=')[1].split('&')[0]; const Playlist_id = url.split('list=')[1].split('&')[0];
@ -41,6 +50,7 @@ export function yt_validate(url: string): 'playlist' | 'video' | false {
* @returns ID of video or playlist. * @returns ID of video or playlist.
*/ */
export function extractID(url: string): string { export function extractID(url: string): string {
if (!yt_validate(url)) throw new Error('This is not a YouTube url or videoId or PlaylistID');
if (url.startsWith('https')) { if (url.startsWith('https')) {
if (url.indexOf('list=') === -1) { if (url.indexOf('list=') === -1) {
let video_id: string; let video_id: string;
@ -105,8 +115,8 @@ export async function video_basic_info(url: string, options: InfoOptions = {}) {
url: `https://www.youtube.com/watch?v=${vid.videoId}`, url: `https://www.youtube.com/watch?v=${vid.videoId}`,
title: vid.title, title: vid.title,
description: vid.shortDescription, description: vid.shortDescription,
durationInSec: vid.lengthSeconds, duration: vid.lengthSeconds,
durationRaw: parseSeconds(vid.lengthSeconds), duration_raw: parseSeconds(vid.lengthSeconds),
uploadedDate: microformat.publishDate, uploadedDate: microformat.publishDate,
thumbnail: vid.thumbnail.thumbnails[vid.thumbnail.thumbnails.length - 1], thumbnail: vid.thumbnail.thumbnails[vid.thumbnail.thumbnails.length - 1],
channel: { channel: {