Fixed Spotify Playlist and Album search issues

This commit is contained in:
killer069 2021-12-15 13:11:16 +05:30
parent 2958e65728
commit 6fbb0e96de
3 changed files with 30 additions and 176 deletions

View File

@ -215,14 +215,19 @@ export class SpotifyPlaylist {
* @private * @private
*/ */
private fetched_tracks: Map<string, SpotifyTrack[]>; private fetched_tracks: Map<string, SpotifyTrack[]>;
/**
* Boolean to tell whether it is a searched result or not.
*/
private readonly search : boolean
/** /**
* Constructor for Spotify Playlist Class * Constructor for Spotify Playlist Class
* @param data JSON parsed data of playlist * @param data JSON parsed data of playlist
* @param spotifyData Data about sporify token for furhter fetching. * @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.name = data.name;
this.type = 'playlist'; this.type = 'playlist';
this.search = search
this.collaborative = data.collaborative; this.collaborative = data.collaborative;
this.description = data.description; this.description = data.description;
this.url = data.external_urls.spotify; this.url = data.external_urls.spotify;
@ -235,7 +240,7 @@ export class SpotifyPlaylist {
}; };
this.tracksCount = Number(data.tracks.total); this.tracksCount = Number(data.tracks.total);
const videos: SpotifyTrack[] = []; 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)); if(v.track) videos.push(new SpotifyTrack(v.track));
}); });
this.fetched_tracks = new Map(); this.fetched_tracks = new Map();
@ -249,10 +254,11 @@ export class SpotifyPlaylist {
* @returns Playlist Class. * @returns Playlist Class.
*/ */
async fetch() { async fetch() {
if(this.search) return this;
let fetching: number; let fetching: number;
if (this.tracksCount > 1000) fetching = 1000; if (this.tracksCount > 1000) fetching = 1000;
else fetching = this.tracksCount; else fetching = this.tracksCount;
if (fetching <= 100) return; if (fetching <= 100) return this;
const work = []; const work = [];
for (let i = 2; i <= Math.ceil(fetching / 100); i++) { for (let i = 2; i <= Math.ceil(fetching / 100); i++) {
work.push( work.push(
@ -325,6 +331,7 @@ export class SpotifyPlaylist {
* Spotify Playlist total no of tracks that have been fetched so far. * Spotify Playlist total no of tracks that have been fetched so far.
*/ */
get total_tracks() { get total_tracks() {
if(this.search) return this.tracksCount;
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;
} }
@ -401,15 +408,20 @@ export class SpotifyAlbum {
* @private * @private
*/ */
private fetched_tracks: Map<string, SpotifyTrack[]>; private fetched_tracks: Map<string, SpotifyTrack[]>;
/**
* Boolean to tell whether it is a searched result or not.
*/
private readonly search : boolean
/** /**
* Constructor for Spotify Album Class * Constructor for Spotify Album Class
* @param data Json parsed album data * @param data Json parsed album data
* @param spotifyData Spotify credentials * @param spotifyData Spotify credentials
*/ */
constructor(data: any, spotifyData: SpotifyDataOptions) { constructor(data: any, spotifyData: SpotifyDataOptions, search : boolean) {
this.name = data.name; this.name = data.name;
this.type = 'album'; this.type = 'album';
this.id = data.id; this.id = data.id;
this.search = search
this.url = data.external_urls.spotify; this.url = data.external_urls.spotify;
this.thumbnail = data.images[0]; this.thumbnail = data.images[0];
const artists: SpotifyArtists[] = []; const artists: SpotifyArtists[] = [];
@ -426,7 +438,7 @@ export class SpotifyAlbum {
this.release_date_precision = data.release_date_precision; this.release_date_precision = data.release_date_precision;
this.tracksCount = data.total_tracks; this.tracksCount = data.total_tracks;
const videos: SpotifyTrack[] = []; const videos: SpotifyTrack[] = [];
data.tracks.items.forEach((v: any) => { if(!this.search) data.tracks.items.forEach((v: any) => {
videos.push(new SpotifyTrack(v)); videos.push(new SpotifyTrack(v));
}); });
this.fetched_tracks = new Map(); this.fetched_tracks = new Map();
@ -440,10 +452,11 @@ export class SpotifyAlbum {
* @returns Album Class. * @returns Album Class.
*/ */
async fetch() { async fetch() {
if(this.search) return this
let fetching: number; let fetching: number;
if (this.tracksCount > 500) fetching = 500; if (this.tracksCount > 500) fetching = 500;
else fetching = this.tracksCount; else fetching = this.tracksCount;
if (fetching <= 50) return; if (fetching <= 50) return this;
const work = []; const work = [];
for (let i = 2; i <= Math.ceil(fetching / 50); i++) { for (let i = 2; i <= Math.ceil(fetching / 50); i++) {
work.push( work.push(
@ -516,6 +529,7 @@ export class SpotifyAlbum {
* Spotify Album total no of tracks that have been fetched so far. * Spotify Album total no of tracks that have been fetched so far.
*/ */
get total_tracks() { get total_tracks() {
if(this.search) return this.tracksCount
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;
} }
@ -535,158 +549,3 @@ export class SpotifyAlbum {
}; };
} }
} }
export class SpotifySearchPlaylist{
/**
* Spotify Playlist Name
*/
name: string;
/**
* Spotify Class type. == "playlist"
*/
type: 'track' | 'playlist' | 'album';
/**
* Spotify Playlist collaborative boolean.
*/
collaborative: boolean;
/**
* Spotify Playlist Description
*/
description: string;
/**
* Spotify Playlist URL
*/
url: string;
/**
* Spotify Playlist ID
*/
id: string;
/**
* Spotify Playlist Thumbnail Data
*/
thumbnail: SpotifyThumbnail;
/**
* Spotify Playlist Owner Artist data
*/
owner: SpotifyArtists;
/**
* Spotify Playlist total tracks Count
*/
tracksCount: number;
constructor(data : any){
this.name = data.name;
this.type = 'playlist';
this.collaborative = data.collaborative;
this.description = data.description;
this.url = data.external_urls.spotify;
this.id = data.id;
this.thumbnail = data.images[0];
this.owner = {
name: data.owner.display_name,
url: data.owner.external_urls.spotify,
id: data.owner.id
};
this.tracksCount = Number(data.tracks.total);
}
/**
* Converts Class to JSON
* @returns JSON data
*/
toJSON(): PlaylistJSON {
return {
name: this.name,
collaborative: this.collaborative,
description: this.description,
url: this.url,
id: this.id,
thumbnail: this.thumbnail,
owner: this.owner,
tracksCount: this.tracksCount
};
}
}
export class SpotifySearchAlbum{
/**
* Spotify Album Name
*/
name: string;
/**
* Spotify Class type. == "album"
*/
type: 'track' | 'playlist' | 'album';
/**
* Spotify Album url
*/
url: string;
/**
* Spotify Album id
*/
id: string;
/**
* Spotify Album Thumbnail data
*/
thumbnail: SpotifyThumbnail;
/**
* Spotify Album artists [ array ]
*/
artists: SpotifyArtists[];
/**
* Spotify Album copyright data [ array ]
*/
copyrights: SpotifyCopyright[];
/**
* Spotify Album Release date
*/
release_date: string;
/**
* Spotify Album Release Date **precise**
*/
release_date_precision: string;
/**
* Spotify Album total no of tracks
*/
tracksCount: number;
/**
* Spotify Album Spotify data
*
* @private
*/
constructor(data : any){
this.name = data.name;
this.type = 'album';
this.id = data.id;
this.url = data.external_urls.spotify;
this.thumbnail = data.images[0];
const artists: SpotifyArtists[] = [];
data.artists.forEach((v: any) => {
artists.push({
name: v.name,
id: v.id,
url: v.external_urls.spotify
});
});
this.artists = artists;
this.copyrights = data.copyrights;
this.release_date = data.release_date;
this.release_date_precision = data.release_date_precision;
this.tracksCount = data.total_tracks;
}
toJSON(): AlbumJSON {
return {
name: this.name,
id: this.id,
type: this.type,
url: this.url,
thumbnail: this.thumbnail,
artists: this.artists,
copyrights: this.copyrights,
release_date: this.release_date,
release_date_precision: this.release_date_precision,
tracksCount: this.tracksCount
};
}
}

View File

@ -1,5 +1,5 @@
import { request } from '../Request'; import { request } from '../Request';
import { SpotifyAlbum, SpotifyPlaylist, SpotifySearchAlbum, SpotifySearchPlaylist, SpotifyTrack } from './classes'; import { SpotifyAlbum, SpotifyPlaylist, SpotifyTrack } from './classes';
import { existsSync, readFileSync, writeFileSync } from 'node:fs'; import { existsSync, readFileSync, writeFileSync } from 'node:fs';
let spotifyData: SpotifyDataOptions; let spotifyData: SpotifyDataOptions;
@ -65,7 +65,7 @@ export async function spotify(url: string): Promise<Spotify> {
return err; return err;
}); });
if (response instanceof Error) throw response; 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) { } else if (url.indexOf('playlist/') !== -1) {
const playlistID = url.split('playlist/')[1].split('&')[0].split('?')[0]; const playlistID = url.split('playlist/')[1].split('&')[0].split('?')[0];
const response = await request( const response = await request(
@ -79,7 +79,7 @@ export async function spotify(url: string): Promise<Spotify> {
return err; return err;
}); });
if (response instanceof Error) throw response; 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.'); } else throw new Error('URL is out of scope for play-dl.');
} }
/** /**
@ -161,10 +161,6 @@ export function is_expired(): boolean {
* type for Spotify Classes * type for Spotify Classes
*/ */
export type Spotify = SpotifyAlbum | SpotifyPlaylist | SpotifyTrack; export type Spotify = SpotifyAlbum | SpotifyPlaylist | SpotifyTrack;
/**
* type for Spotify Searched Classes
*/
export type SpotifySearch = SpotifyTrack | SpotifySearchPlaylist | SpotifySearchAlbum
/** /**
* Function for searching songs on Spotify * Function for searching songs on Spotify
* @param query searching query * @param query searching query
@ -176,8 +172,8 @@ export async function sp_search(
query: string, query: string,
type: 'album' | 'playlist' | 'track', type: 'album' | 'playlist' | 'track',
limit: number = 10 limit: number = 10
): Promise<SpotifySearch[]> { ): Promise<Spotify[]> {
const results: SpotifySearch[] = []; const results: Spotify[] = [];
if (!spotifyData) throw new Error('Spotify Data is missing\nDid you forgot to do authorization ?'); if (!spotifyData) throw new Error('Spotify Data is missing\nDid you forgot to do authorization ?');
if (query.length === 0) throw new Error('Pass some query to search.'); if (query.length === 0) throw new Error('Pass some query to search.');
if (limit > 50 || limit < 0) throw new Error(`You crossed limit range of Spotify [ 0 - 50 ]`); if (limit > 50 || limit < 0) throw new Error(`You crossed limit range of Spotify [ 0 - 50 ]`);
@ -199,11 +195,11 @@ export async function sp_search(
}); });
} else if (type === 'album') { } else if (type === 'album') {
json_data.albums.items.forEach((album: any) => { json_data.albums.items.forEach((album: any) => {
results.push(new SpotifySearchAlbum(album)); results.push(new SpotifyAlbum(album, spotifyData, true));
}); });
} else if (type === 'playlist') { } else if (type === 'playlist') {
json_data.playlists.items.forEach((playlist: any) => { json_data.playlists.items.forEach((playlist: any) => {
results.push(new SpotifySearchPlaylist(playlist)); results.push(new SpotifyPlaylist(playlist, spotifyData, true));
}); });
} }
return results; return results;

View File

@ -19,8 +19,7 @@ export {
SpotifyAlbum, SpotifyAlbum,
SpotifyPlaylist, SpotifyPlaylist,
SpotifyTrack, SpotifyTrack,
Spotify, Spotify
SpotifySearch
} from './Spotify'; } from './Spotify';
export { export {
soundcloud, soundcloud,
@ -73,7 +72,7 @@ import {
SoundCloud, SoundCloud,
Spotify Spotify
} from '.'; } from '.';
import { SpotifyAuthorize, SpotifySearch, sp_search } from './Spotify'; import { SpotifyAuthorize, sp_search } from './Spotify';
import { check_id, so_search, stream as so_stream, stream_from_info as so_stream_info } from './SoundCloud'; import { check_id, so_search, stream as so_stream, stream_from_info as so_stream_info } from './SoundCloud';
import { stream as yt_stream, StreamOptions, stream_from_info as yt_stream_info } from './YouTube/stream'; import { stream as yt_stream, StreamOptions, stream_from_info as yt_stream_info } from './YouTube/stream';
import { SoundCloudPlaylist, SoundCloudTrack } from './SoundCloud/classes'; import { SoundCloudPlaylist, SoundCloudTrack } from './SoundCloud/classes';
@ -214,7 +213,7 @@ export async function search(query: string, options?: SearchOptions): Promise<Yo
export async function search( export async function search(
query: string, query: string,
options: SearchOptions = {} options: SearchOptions = {}
): Promise<YouTube[] | SpotifySearch[] | SoundCloud[] | Deezer[]> { ): Promise<YouTube[] | Spotify[] | SoundCloud[] | Deezer[]> {
if (!options.source) options.source = { youtube: 'video' }; if (!options.source) options.source = { youtube: 'video' };
query = encodeURIComponent(query); query = encodeURIComponent(query);
if (options.source.youtube) return await yt_search(query, { limit: options.limit, type: options.source.youtube }); if (options.source.youtube) return await yt_search(query, { limit: options.limit, type: options.source.youtube });