Added Extract ID
This commit is contained in:
Killer069 2021-09-06 10:36:34 +05:30 committed by GitHub
commit 78e4116f54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 51 additions and 21 deletions

View File

@ -27,10 +27,21 @@ if(check === "video") //URL is video url
if(check === "playlist") //URL is a playlist 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
### stream(url : `string`, cookie? : `string`) ### 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.** **[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
### video_basic_info(url : `string`, cookie? : `string`) ### 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.** **[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) const video = await video_basic_info(url)
``` ```
### video_info(url : `string`, cookie? : `string`) ### 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.** **[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
### playlist_info(url : `string`, parseIncomplete : `boolean`) ### 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. **parseIncomplete** is optional parameter if you want to parse playlist with hidden videos.
```js ```js
@ -155,7 +166,7 @@ const playlist = await playlist_info(url, true)
// This displays total no. of pages fetched so far. // This displays total no. of pages fetched so far.
for(let i = 1; i <= playlist.total_pages; i++){ 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 } // This will push every video in that playlist to your queue
``` ```

View File

@ -28,7 +28,7 @@ interface SpotifyCopyright{
export class SpotifyVideo{ export class SpotifyVideo{
name : string; name : string;
type : "video" | "playlist" | "album" type : "track" | "playlist" | "album"
id : string; id : string;
url : string; url : string;
explicit : boolean; explicit : boolean;
@ -40,7 +40,7 @@ export class SpotifyVideo{
constructor(data : any){ constructor(data : any){
this.name = data.name this.name = data.name
this.id = data.id this.id = data.id
this.type = "video" this.type = "track"
this.url = data.external_urls.spotify this.url = data.external_urls.spotify
this.explicit = data.explicit this.explicit = data.explicit
this.durationInMs = data.duration_ms this.durationInMs = data.duration_ms
@ -83,7 +83,7 @@ export class SpotifyVideo{
export class SpotifyPlaylist{ export class SpotifyPlaylist{
name : string; name : string;
type : "video" | "playlist" | "album" type : "track" | "playlist" | "album"
collaborative : boolean; collaborative : boolean;
description : string; description : string;
url : string; url : string;
@ -128,7 +128,7 @@ export class SpotifyPlaylist{
export class SpotifyAlbum{ export class SpotifyAlbum{
name : string name : string
type : "video" | "playlist" | "album" type : "track" | "playlist" | "album"
url : string url : string
thumbnail : SpotifyThumbnail thumbnail : SpotifyThumbnail
artists : SpotifyArtists[] artists : SpotifyArtists[]
@ -180,7 +180,7 @@ export class SpotifyAlbum{
class SpotifyTracks{ class SpotifyTracks{
name : string; name : string;
type : "video" | "playlist" | "album" type : "track" | "playlist" | "album"
id : string; id : string;
url : string; url : string;
explicit : boolean; explicit : boolean;
@ -190,7 +190,7 @@ class SpotifyTracks{
constructor(data : any){ constructor(data : any){
this.name = data.name this.name = data.name
this.id = data.id this.id = data.id
this.type = "video" this.type = "track"
this.url = data.external_urls.spotify this.url = data.external_urls.spotify
this.explicit = data.explicit this.explicit = data.explicit
this.durationInMs = data.duration_ms this.durationInMs = data.duration_ms

View File

@ -22,12 +22,29 @@ export function yt_validate(url : string): "playlist" | "video" | boolean {
} }
} }
export async function video_basic_info(url : string, cookie? : string){ export function extractID(url : string): string{
if(!url.match(video_pattern)) throw new Error('This is not a YouTube URL') if(url.startsWith('https')){
if(url.indexOf('list=') === -1){
let video_id : string; let video_id : string;
if(url.includes('youtu.be/')) video_id = url.split('youtu.be/')[1].split('/')[0] 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 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]; 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){
let video_id : string;
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 new_url = 'https://www.youtube.com/watch?v=' + video_id
let body = await url_get(new_url, { let body = await url_get(new_url, {
headers : (cookie) ? { 'cookie' : cookie } : {} 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) { 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 || 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 : string
if(url.startsWith('https')){
let Playlist_id = url.split('list=')[1].split('&')[0] if(yt_validate(url) !== 'playlist') throw new Error('This is not a Playlist URL')
if(Playlist_id.length !== 34 || !Playlist_id.startsWith('PL')) 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 new_url = `https://www.youtube.com/playlist?list=${Playlist_id}`
let body = await url_get(new_url) let body = await url_get(new_url)

View File

@ -1 +1 @@
export { video_basic_info, video_info, playlist_info, yt_validate } from './extractor' export { video_basic_info, video_info, playlist_info, yt_validate, extractID } from './extractor'

View File

@ -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' export { spotify, sp_validate } from './Spotify'