From 5aae37bb01bc6d3e98387baf8854c500c42ccd7a Mon Sep 17 00:00:00 2001 From: killer069 <65385476+killer069@users.noreply.github.com> Date: Mon, 6 Sep 2021 10:33:37 +0530 Subject: [PATCH] Extract ID added --- docs/YouTube/README.md | 21 +++++++++++++----- play-dl/Spotify/classes.ts | 12 +++++----- play-dl/YouTube/utils/extractor.ts | 35 +++++++++++++++++++++++------- play-dl/YouTube/utils/index.ts | 2 +- play-dl/index.ts | 2 +- 5 files changed, 51 insertions(+), 21 deletions(-) diff --git a/docs/YouTube/README.md b/docs/YouTube/README.md index b036e72..42b22d6 100644 --- a/docs/YouTube/README.md +++ b/docs/YouTube/README.md @@ -27,10 +27,21 @@ if(check === "video") //URL is video url if(check === "playlist") //URL is a playlist url ``` +## Extract ID + +### extractID(url : `string`) +*This will return videoID or playlistID from a url* + +**Note :** URL like [this](https://www.youtube.com/watch?v=E2gHczUOCGI&list=PLUt3leKZfbZqLzLwcQMYPBdbe7i7KRCOP&index=2) will return a playlist ID only. + +```js +let id = extractID(url) +``` + ## Stream ### stream(url : `string`, cookie? : `string`) -*This is basic to create a youtube stream from a url.* +*This is basic to create a youtube stream from a url or videoID.* **[Cookies](https://github.com/play-dl/play-dl/discussions/34) are optional and are required for playing age restricted videos.** @@ -74,7 +85,7 @@ console.log(results[0].url); ## Video ### video_basic_info(url : `string`, cookie? : `string`) -*The basic video details `play-dl` fetches at first.* +*The basic video details `play-dl` fetches at first from url or videoID.* **[Cookies](https://github.com/play-dl/play-dl/discussions/34) are optional and are required for playing age restricted videos.** @@ -82,7 +93,7 @@ console.log(results[0].url); const video = await video_basic_info(url) ``` ### video_info(url : `string`, cookie? : `string`) -*This contains everything with deciphered formats along with `video_details`.* +*This contains everything with deciphered formats along with `video_details`. It can fetech data from url or videoID.* **[Cookies](https://github.com/play-dl/play-dl/discussions/34) are optional and are required for playing age restricted videos.** @@ -102,7 +113,7 @@ const video = await video_info(url) ## Playlist ### playlist_info(url : `string`, parseIncomplete : `boolean`) -*This fetches all details about a playlist.* +*This fetches all details about a playlist from a url or playlistID.* **parseIncomplete** is optional parameter if you want to parse playlist with hidden videos. ```js @@ -155,7 +166,7 @@ const playlist = await playlist_info(url, true) // This displays total no. of pages fetched so far. for(let i = 1; i <= playlist.total_pages; i++){ - "Your queue".push(...playlist.page(i)) + queue.push(...playlist.page(i)) } // This will push every video in that playlist to your queue ``` diff --git a/play-dl/Spotify/classes.ts b/play-dl/Spotify/classes.ts index c7ee48b..0186b20 100644 --- a/play-dl/Spotify/classes.ts +++ b/play-dl/Spotify/classes.ts @@ -28,7 +28,7 @@ interface SpotifyCopyright{ export class SpotifyVideo{ name : string; - type : "video" | "playlist" | "album" + type : "track" | "playlist" | "album" id : string; url : string; explicit : boolean; @@ -40,7 +40,7 @@ export class SpotifyVideo{ constructor(data : any){ this.name = data.name this.id = data.id - this.type = "video" + this.type = "track" this.url = data.external_urls.spotify this.explicit = data.explicit this.durationInMs = data.duration_ms @@ -83,7 +83,7 @@ export class SpotifyVideo{ export class SpotifyPlaylist{ name : string; - type : "video" | "playlist" | "album" + type : "track" | "playlist" | "album" collaborative : boolean; description : string; url : string; @@ -128,7 +128,7 @@ export class SpotifyPlaylist{ export class SpotifyAlbum{ name : string - type : "video" | "playlist" | "album" + type : "track" | "playlist" | "album" url : string thumbnail : SpotifyThumbnail artists : SpotifyArtists[] @@ -180,7 +180,7 @@ export class SpotifyAlbum{ class SpotifyTracks{ name : string; - type : "video" | "playlist" | "album" + type : "track" | "playlist" | "album" id : string; url : string; explicit : boolean; @@ -190,7 +190,7 @@ class SpotifyTracks{ constructor(data : any){ this.name = data.name this.id = data.id - this.type = "video" + this.type = "track" this.url = data.external_urls.spotify this.explicit = data.explicit this.durationInMs = data.duration_ms diff --git a/play-dl/YouTube/utils/extractor.ts b/play-dl/YouTube/utils/extractor.ts index 80effaf..849a220 100644 --- a/play-dl/YouTube/utils/extractor.ts +++ b/play-dl/YouTube/utils/extractor.ts @@ -22,12 +22,29 @@ export function yt_validate(url : string): "playlist" | "video" | boolean { } } +export function extractID(url : string): string{ + if(url.startsWith('https')){ + if(url.indexOf('list=') === -1){ + let video_id : string; + if(url.includes('youtu.be/')) video_id = url.split('youtu.be/')[1].split('/')[0] + else if(url.includes('youtube.com/embed/')) video_id = url.split('youtube.com/embed/')[1].split('/')[0] + else video_id = url.split('watch?v=')[1].split('&')[0]; + return video_id + } + else{ + return url.split('list=')[1].split('&')[0] + } + } + else return url +} + export async function video_basic_info(url : string, cookie? : string){ - if(!url.match(video_pattern)) throw new Error('This is not a YouTube URL') let video_id : string; - if(url.includes('youtu.be/')) video_id = url.split('youtu.be/')[1].split('/')[0] - else if(url.includes('youtube.com/embed/')) video_id = url.split('youtube.com/embed/')[1].split('/')[0] - else video_id = url.split('watch?v=')[1].split('&')[0]; + if(url.startsWith('https')) { + if(yt_validate(url) !== 'video') throw new Error('This is not a YouTube Watch URL') + video_id = extractID(url) + } + else video_id = url let new_url = 'https://www.youtube.com/watch?v=' + video_id let body = await url_get(new_url, { headers : (cookie) ? { 'cookie' : cookie } : {} @@ -105,10 +122,12 @@ export async function video_info(url : string, cookie? : string) { export async function playlist_info(url : string, parseIncomplete : boolean = false) { if (!url || typeof url !== "string") throw new Error(`Expected playlist url, received ${typeof url}!`); - if(url.search('(\\?|\\&)list\\=') === -1) throw new Error('This is not a PlayList URL') - - let Playlist_id = url.split('list=')[1].split('&')[0] - if(Playlist_id.length !== 34 || !Playlist_id.startsWith('PL')) throw new Error('This is not a PlayList URL') + let Playlist_id : string + if(url.startsWith('https')){ + if(yt_validate(url) !== 'playlist') throw new Error('This is not a Playlist URL') + Playlist_id = extractID(url) + } + else Playlist_id = url let new_url = `https://www.youtube.com/playlist?list=${Playlist_id}` let body = await url_get(new_url) diff --git a/play-dl/YouTube/utils/index.ts b/play-dl/YouTube/utils/index.ts index c34d395..121c72a 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, yt_validate } from './extractor' \ No newline at end of file +export { video_basic_info, video_info, playlist_info, yt_validate, extractID } from './extractor' \ No newline at end of file diff --git a/play-dl/index.ts b/play-dl/index.ts index b9833f5..20896e0 100644 --- a/play-dl/index.ts +++ b/play-dl/index.ts @@ -1,4 +1,4 @@ -export { playlist_info, video_basic_info, video_info, search, stream, stream_from_info, yt_validate } from "./YouTube"; +export { playlist_info, video_basic_info, video_info, search, stream, stream_from_info, yt_validate, extractID } from "./YouTube"; export { spotify, sp_validate } from './Spotify'