Added property to get all tracks in playlist, album

This commit is contained in:
killer069 2022-01-03 15:02:52 +05:30
parent 2e1476745d
commit 433ec4976b
4 changed files with 93 additions and 49 deletions

View File

@ -491,6 +491,21 @@ export class DeezerAlbum {
return this; return this;
} }
/**
* Fetches all the tracks in the album and returns them
*
* ```ts
* const album = await play.deezer('album url')
*
* const tracks = await album.all_tracks()
* ```
* @returns An array of {@link DeezerTrack}
*/
async all_tracks(): Promise<DeezerTrack[]> {
await this.fetch()
return this.tracks as DeezerTrack[]
}
/** /**
* Converts instances of this class to JSON data * Converts instances of this class to JSON data
* @returns JSON data. * @returns JSON data.
@ -746,6 +761,21 @@ export class DeezerPlaylist {
return this; return this;
} }
/**
* Fetches all the tracks in the playlist and returns them
*
* ```ts
* const playlist = await play.deezer('playlist url')
*
* const tracks = await playlist.all_tracks()
* ```
* @returns An array of {@link DeezerTrack}
*/
async all_tracks(): Promise<DeezerTrack[]> {
await this.fetch()
return this.tracks as DeezerTrack[]
}
/** /**
* Converts instances of this class to JSON data * Converts instances of this class to JSON data
* @returns JSON data. * @returns JSON data.

View File

@ -314,6 +314,7 @@ export class SoundCloudPlaylist {
} }
/** /**
* Get total no. of fetched tracks * Get total no. of fetched tracks
* @see {@link SoundCloudPlaylist.all_tracks}
*/ */
get total_tracks(): number { get total_tracks(): number {
let count = 0; let count = 0;
@ -324,25 +325,19 @@ export class SoundCloudPlaylist {
return count; return count;
} }
/** /**
* Get all fetched tracks as a array. * Fetches all the tracks in the playlist and returns them
* *
* For getting all feetched tracks
*
* ```ts * ```ts
* const playlist = await play.soundcloud("playlist url") * const playlist = await play.soundcloud('playlist url')
* *
* await playlist.fetch() * const tracks = await playlist.all_tracks()
*
* const result = playlist.fetched_tracks
* ``` * ```
* @returns An array of {@link SoundCloudTrack}
*/ */
get fetched_tracks(): SoundCloudTrack[] { async all_tracks(): Promise<SoundCloudTrack[]> {
let result: SoundCloudTrack[] = []; await this.fetch()
this.tracks.forEach((track) => {
if (track instanceof SoundCloudTrack) result.push(track); return this.tracks as SoundCloudTrack[]
else return;
});
return result;
} }
/** /**
* Converts Class to JSON data * Converts Class to JSON data

View File

@ -309,21 +309,8 @@ export class SpotifyPlaylist {
return this.fetched_tracks.get(`${num}`) as SpotifyTrack[]; return this.fetched_tracks.get(`${num}`) as SpotifyTrack[];
} }
/** /**
* Spotify Playlist total no of pages in a playlist * Gets total number of pages in that playlist class.
* * @see {@link SpotifyPlaylist.all_tracks}
* For getting all songs in a playlist,
*
* ```ts
* const playlist = await play.spotify('playlist url')
*
* await playlist.fetch()
*
* const result = []
*
* for (let i = 0; i <= playlist.tota_pages; i++) {
* result.push(playlist.page(i))
* }
* ```
*/ */
get total_pages() { get total_pages() {
return this.fetched_tracks.size; return this.fetched_tracks.size;
@ -336,6 +323,25 @@ export class SpotifyPlaylist {
const page_number: number = this.total_pages; const page_number: number = this.total_pages;
return (page_number - 1) * 100 + (this.fetched_tracks.get(`${page_number}`) as SpotifyTrack[]).length; return (page_number - 1) * 100 + (this.fetched_tracks.get(`${page_number}`) as SpotifyTrack[]).length;
} }
/**
* Fetches all the tracks in the playlist and returns them
*
* ```ts
* const playlist = await play.spotify('playlist url')
*
* const tracks = await playlist.all_tracks()
* ```
* @returns An array of {@link SpotifyTrack}
*/
async all_tracks() : Promise<SpotifyTrack[]>{
await this.fetch()
const tracks : SpotifyTrack[] = []
for(const page of this.fetched_tracks.values()) tracks.push(...page);
return tracks
}
/** /**
* Converts Class to JSON * Converts Class to JSON
* @returns JSON data * @returns JSON data
@ -508,21 +514,8 @@ export class SpotifyAlbum {
return this.fetched_tracks.get(`${num}`); return this.fetched_tracks.get(`${num}`);
} }
/** /**
* Spotify Album total no of pages in a album * Gets total number of pages in that album class.
* * @see {@link SpotifyAlbum.all_tracks}
* For getting all songs in a album,
*
* ```ts
* const album = await play.spotify('album url')
*
* await album.fetch()
*
* const result = []
*
* for (let i = 0; i <= album.tota_pages; i++) {
* result.push(album.page(i))
* }
* ```
*/ */
get total_pages() { get total_pages() {
return this.fetched_tracks.size; return this.fetched_tracks.size;
@ -535,7 +528,29 @@ export class SpotifyAlbum {
const page_number: number = this.total_pages; const page_number: number = this.total_pages;
return (page_number - 1) * 100 + (this.fetched_tracks.get(`${page_number}`) as SpotifyTrack[]).length; return (page_number - 1) * 100 + (this.fetched_tracks.get(`${page_number}`) as SpotifyTrack[]).length;
} }
/**
* Fetches all the tracks in the album and returns them
*
* ```ts
* const album = await play.spotify('album url')
*
* const tracks = await album.all_tracks()
* ```
* @returns An array of {@link SpotifyTrack}
*/
async all_tracks() : Promise<SpotifyTrack[]>{
await this.fetch()
const tracks : SpotifyTrack[] = []
for( const page of this.fetched_tracks.values() ) tracks.push(...page);
return tracks
}
/**
* Converts Class to JSON
* @returns JSON data
*/
toJSON(): AlbumJSON { toJSON(): AlbumJSON {
return { return {
name: this.name, name: this.name,

View File

@ -218,6 +218,12 @@ export class YouTubePlayList {
} }
/** /**
* Fetches all the videos in the playlist and returns them * Fetches all the videos in the playlist and returns them
*
* ```ts
* const playlist = await play.playlist_info('playlist url')
*
* const videos = await playlist.all_videos()
* ```
* @returns An array of {@link YouTubeVideo} objects * @returns An array of {@link YouTubeVideo} objects
* @see {@link YouTubePlayList.fetch} * @see {@link YouTubePlayList.fetch}
*/ */
@ -226,9 +232,7 @@ export class YouTubePlayList {
const videos = []; const videos = [];
for (const [, page] of this.fetched_videos.entries()) { for (const page of this.fetched_videos.values()) videos.push(...page);
videos.push(...page);
}
return videos; return videos;
} }