diff --git a/play-dl/Spotify/classes.ts b/play-dl/Spotify/classes.ts index 59ccb71..3ab4bd9 100644 --- a/play-dl/Spotify/classes.ts +++ b/play-dl/Spotify/classes.ts @@ -215,14 +215,19 @@ export class SpotifyPlaylist { * @private */ private fetched_tracks: Map; + /** + * Boolean to tell whether it is a searched result or not. + */ + private readonly search : boolean /** * Constructor for Spotify Playlist Class * @param data JSON parsed data of playlist * @param spotifyData Data about sporify token for furhter fetching. */ - constructor(data: any, spotifyData: SpotifyDataOptions) { + constructor(data: any, spotifyData: SpotifyDataOptions, search : boolean) { this.name = data.name; this.type = 'playlist'; + this.search = search this.collaborative = data.collaborative; this.description = data.description; this.url = data.external_urls.spotify; @@ -235,7 +240,7 @@ export class SpotifyPlaylist { }; this.tracksCount = Number(data.tracks.total); const videos: SpotifyTrack[] = []; - data.tracks.items.forEach((v: any) => { + if(!this.search) data.tracks.items.forEach((v: any) => { if(v.track) videos.push(new SpotifyTrack(v.track)); }); this.fetched_tracks = new Map(); @@ -249,10 +254,11 @@ export class SpotifyPlaylist { * @returns Playlist Class. */ async fetch() { + if(this.search) return this; let fetching: number; if (this.tracksCount > 1000) fetching = 1000; else fetching = this.tracksCount; - if (fetching <= 100) return; + if (fetching <= 100) return this; const work = []; for (let i = 2; i <= Math.ceil(fetching / 100); i++) { work.push( @@ -325,6 +331,7 @@ export class SpotifyPlaylist { * Spotify Playlist total no of tracks that have been fetched so far. */ get total_tracks() { + if(this.search) return this.tracksCount; const page_number: number = this.total_pages; return (page_number - 1) * 100 + (this.fetched_tracks.get(`${page_number}`) as SpotifyTrack[]).length; } @@ -401,15 +408,20 @@ export class SpotifyAlbum { * @private */ private fetched_tracks: Map; + /** + * Boolean to tell whether it is a searched result or not. + */ + private readonly search : boolean /** * Constructor for Spotify Album Class * @param data Json parsed album data * @param spotifyData Spotify credentials */ - constructor(data: any, spotifyData: SpotifyDataOptions) { + constructor(data: any, spotifyData: SpotifyDataOptions, search : boolean) { this.name = data.name; this.type = 'album'; this.id = data.id; + this.search = search this.url = data.external_urls.spotify; this.thumbnail = data.images[0]; const artists: SpotifyArtists[] = []; @@ -426,7 +438,7 @@ export class SpotifyAlbum { this.release_date_precision = data.release_date_precision; this.tracksCount = data.total_tracks; const videos: SpotifyTrack[] = []; - data.tracks.items.forEach((v: any) => { + if(!this.search) data.tracks.items.forEach((v: any) => { videos.push(new SpotifyTrack(v)); }); this.fetched_tracks = new Map(); @@ -440,10 +452,11 @@ export class SpotifyAlbum { * @returns Album Class. */ async fetch() { + if(this.search) return this let fetching: number; if (this.tracksCount > 500) fetching = 500; else fetching = this.tracksCount; - if (fetching <= 50) return; + if (fetching <= 50) return this; const work = []; for (let i = 2; i <= Math.ceil(fetching / 50); i++) { work.push( @@ -516,6 +529,7 @@ export class SpotifyAlbum { * Spotify Album total no of tracks that have been fetched so far. */ get total_tracks() { + if(this.search) return this.tracksCount const page_number: number = this.total_pages; return (page_number - 1) * 100 + (this.fetched_tracks.get(`${page_number}`) as SpotifyTrack[]).length; } @@ -534,4 +548,4 @@ export class SpotifyAlbum { tracksCount: this.tracksCount }; } -} +} \ No newline at end of file diff --git a/play-dl/Spotify/index.ts b/play-dl/Spotify/index.ts index 6bea758..7ab93ea 100644 --- a/play-dl/Spotify/index.ts +++ b/play-dl/Spotify/index.ts @@ -65,7 +65,7 @@ export async function spotify(url: string): Promise { return err; }); if (response instanceof Error) throw response; - return new SpotifyAlbum(JSON.parse(response), spotifyData); + return new SpotifyAlbum(JSON.parse(response), spotifyData, false); } else if (url.indexOf('playlist/') !== -1) { const playlistID = url.split('playlist/')[1].split('&')[0].split('?')[0]; const response = await request( @@ -79,7 +79,7 @@ export async function spotify(url: string): Promise { return err; }); if (response instanceof Error) throw response; - return new SpotifyPlaylist(JSON.parse(response), spotifyData); + return new SpotifyPlaylist(JSON.parse(response), spotifyData, false); } else throw new Error('URL is out of scope for play-dl.'); } /** @@ -158,7 +158,7 @@ export function is_expired(): boolean { else return false; } /** - * type for Spotify Class + * type for Spotify Classes */ export type Spotify = SpotifyAlbum | SpotifyPlaylist | SpotifyTrack; /** @@ -195,11 +195,11 @@ export async function sp_search( }); } else if (type === 'album') { json_data.albums.items.forEach((album: any) => { - results.push(new SpotifyAlbum(album, spotifyData)); + results.push(new SpotifyAlbum(album, spotifyData, true)); }); } else if (type === 'playlist') { json_data.playlists.items.forEach((playlist: any) => { - results.push(new SpotifyPlaylist(playlist, spotifyData)); + results.push(new SpotifyPlaylist(playlist, spotifyData, true)); }); } return results;