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;
}
/**
* 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
* @returns JSON data.
@ -746,6 +761,21 @@ export class DeezerPlaylist {
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
* @returns JSON data.

View File

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

View File

@ -309,21 +309,8 @@ export class SpotifyPlaylist {
return this.fetched_tracks.get(`${num}`) as SpotifyTrack[];
}
/**
* Spotify Playlist total no of pages in a playlist
*
* 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))
* }
* ```
* Gets total number of pages in that playlist class.
* @see {@link SpotifyPlaylist.all_tracks}
*/
get total_pages() {
return this.fetched_tracks.size;
@ -336,6 +323,25 @@ export class SpotifyPlaylist {
const page_number: number = this.total_pages;
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
* @returns JSON data
@ -508,21 +514,8 @@ export class SpotifyAlbum {
return this.fetched_tracks.get(`${num}`);
}
/**
* Spotify Album total no of pages in a album
*
* 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))
* }
* ```
* Gets total number of pages in that album class.
* @see {@link SpotifyAlbum.all_tracks}
*/
get total_pages() {
return this.fetched_tracks.size;
@ -535,7 +528,29 @@ export class SpotifyAlbum {
const page_number: number = this.total_pages;
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 {
return {
name: this.name,

View File

@ -218,6 +218,12 @@ export class YouTubePlayList {
}
/**
* 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
* @see {@link YouTubePlayList.fetch}
*/
@ -226,9 +232,7 @@ export class YouTubePlayList {
const videos = [];
for (const [, page] of this.fetched_videos.entries()) {
videos.push(...page);
}
for (const page of this.fetched_videos.values()) videos.push(...page);
return videos;
}