From 382447f39763166d06489891d2696e3bf7c96f69 Mon Sep 17 00:00:00 2001 From: killer069 <65385476+killer069@users.noreply.github.com> Date: Tue, 31 Aug 2021 10:16:14 +0530 Subject: [PATCH] More Validation methods added --- README.md | 12 ++++++++++++ package-lock.json | 4 ++-- package.json | 2 +- play-dl/YouTube/utils/extractor.ts | 10 ++++++++++ play-dl/YouTube/utils/index.ts | 2 +- play-dl/index.ts | 2 +- 6 files changed, 27 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 44b8d01..fce99c9 100644 --- a/README.md +++ b/README.md @@ -33,12 +33,24 @@ const results = await youtube.search('post malone sunflower', options); ``` # Validate + ### validate( url : `string` ) *Much faster and easier way to validate url.* ```js if(validate(url)) // Will return true if url is a YouTube url ``` +### validate_playlist( url : `string` ) + *Much faster and easier way to validate url.* +```js +if(validate_playlist(url)) // Will return true if url is a YouTube Playlist url +``` + + > Want to Check both, use this + ```js +if(validate(url) || validate_playlist(url)) // This will check both and if anyone is true, it will execute the below function + ``` + # Stream ### stream(url : `string`) diff --git a/package-lock.json b/package-lock.json index 6cf2b55..cb2f904 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "play-dl", - "version": "0.6.9", + "version": "0.7.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "play-dl", - "version": "0.6.9", + "version": "0.7.0", "license": "MIT", "dependencies": { "got": "^11.8.2" diff --git a/package.json b/package.json index 59111b9..0c7335f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "play-dl", - "version": "0.6.9", + "version": "0.7.0", "description": "YouTube, SoundCloud, Spotify streaming for discord.js bots", "main": "dist/index.js", "typings": "dist/index.d.ts", diff --git a/play-dl/YouTube/utils/extractor.ts b/play-dl/YouTube/utils/extractor.ts index 9bc406c..51862cc 100644 --- a/play-dl/YouTube/utils/extractor.ts +++ b/play-dl/YouTube/utils/extractor.ts @@ -5,12 +5,22 @@ import { PlayList } from '../classes/Playlist' const DEFAULT_API_KEY = "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8"; const video_pattern = /^((?:https?:)?\/\/)?(?:(?:www|m)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$/; +const playlist_pattern = /^((?:https?:)?\/\/)?(?:(?:www|m)\.)?(youtube\.com)\/(?:(playlist|watch))(.*)?((\?|\&)list=)/ export function validate(url : string): boolean{ if(!url.match(video_pattern)) return false else return true } +export function validate_playlist(url : string): boolean{ + if(!url.match(playlist_pattern)) return false + let Playlist_id = url.split('list=')[1].split('&')[0] + if(Playlist_id.length !== 34 || !Playlist_id.startsWith('PL')){ + return false + } + return true +} + export async function video_basic_info(url : string){ if(!url.match(video_pattern)) throw new Error('This is not a YouTube URL') let video_id : string; diff --git a/play-dl/YouTube/utils/index.ts b/play-dl/YouTube/utils/index.ts index c87c6e8..2f9c0c1 100644 --- a/play-dl/YouTube/utils/index.ts +++ b/play-dl/YouTube/utils/index.ts @@ -1 +1 @@ -export { video_basic_info, video_info, playlist_info, validate } from './extractor' \ No newline at end of file +export { video_basic_info, video_info, playlist_info, validate, validate_playlist } from './extractor' \ No newline at end of file diff --git a/play-dl/index.ts b/play-dl/index.ts index 03737c2..384515b 100644 --- a/play-dl/index.ts +++ b/play-dl/index.ts @@ -1 +1 @@ -export { playlist_info, video_basic_info, video_info, search, stream, stream_from_info, validate } from "./YouTube"; \ No newline at end of file +export { playlist_info, video_basic_info, video_info, search, stream, stream_from_info, validate, validate_playlist } from "./YouTube"; \ No newline at end of file