get track info from public api
This commit is contained in:
parent
268b317201
commit
f79fa66832
30
src/api.ts
30
src/api.ts
|
@ -11,6 +11,7 @@ import type {
|
||||||
discographyType,
|
discographyType,
|
||||||
profileType,
|
profileType,
|
||||||
searchType,
|
searchType,
|
||||||
|
trackTypePublicApi,
|
||||||
} from './types';
|
} from './types';
|
||||||
|
|
||||||
// expire cache in 60 minutes
|
// expire cache in 60 minutes
|
||||||
|
@ -44,6 +45,34 @@ export const request = async (body: object, method: string) => {
|
||||||
throw new Error(errorMessage);
|
throw new Error(errorMessage);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make post requests to deezer public api
|
||||||
|
* @param {Object} body post body
|
||||||
|
* @param {String} method request method
|
||||||
|
*/
|
||||||
|
export const requestPublicApi = async (slug: string) => {
|
||||||
|
const cache = lru.get(slug);
|
||||||
|
if (cache) {
|
||||||
|
return cache;
|
||||||
|
}
|
||||||
|
|
||||||
|
const {data} = await axios.get('https://api.deezer.com' + slug);
|
||||||
|
|
||||||
|
if (data.error) {
|
||||||
|
const errorMessage = Object.entries(data.error).join(', ');
|
||||||
|
throw new Error(errorMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
lru.set(slug, data);
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {String} sng_id song id
|
||||||
|
*/
|
||||||
|
export const getTrackInfoPublicApi = (sng_id: string): Promise<trackTypePublicApi> =>
|
||||||
|
requestPublicApi('/track/' + sng_id);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {String} sng_id song id
|
* @param {String} sng_id song id
|
||||||
*/
|
*/
|
||||||
|
@ -85,6 +114,7 @@ export const getPlaylistTracks = async (playlist_id: string): Promise<playlistTr
|
||||||
});
|
});
|
||||||
return playlistTracks;
|
return playlistTracks;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {String} art_id artist id
|
* @param {String} art_id artist id
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -91,3 +91,60 @@ export interface trackType extends songType {
|
||||||
FALLBACK?: songType;
|
FALLBACK?: songType;
|
||||||
TRACK_POSITION?: number;
|
TRACK_POSITION?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface contributorsPublicApi {
|
||||||
|
id: number; // 27
|
||||||
|
name: string; // 'Daft Punk'
|
||||||
|
link: string; // 'https://www.deezer.com/artist/27'
|
||||||
|
share: string; // 'https://www.deezer.com/artist/27?utm_source=deezer&utm_content=artist-27&utm_term=0_1614937516&utm_medium=web'
|
||||||
|
picture: string; // 'https://api.deezer.com/artist/27/image'
|
||||||
|
picture_small: string; // 'https://e-cdns-images.dzcdn.net/images/artist/f2bc007e9133c946ac3c3907ddc5d2ea/56x56-000000-80-0-0.jpg'
|
||||||
|
picture_medium: string; // 'https://e-cdns-images.dzcdn.net/images/artist/f2bc007e9133c946ac3c3907ddc5d2ea/250x250-000000-80-0-0.jpg'
|
||||||
|
picture_big: string; // 'https://e-cdns-images.dzcdn.net/images/artist/f2bc007e9133c946ac3c3907ddc5d2ea/500x500-000000-80-0-0.jpg'
|
||||||
|
picture_xl: string; // 'https://e-cdns-images.dzcdn.net/images/artist/f2bc007e9133c946ac3c3907ddc5d2ea/1000x1000-000000-80-0-0.jpg'
|
||||||
|
radio: boolean;
|
||||||
|
tracklist: string; // 'https://api.deezer.com/artist/27/top?limit=50'
|
||||||
|
type: string; // 'artist'
|
||||||
|
role: string; // 'Main'
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface trackTypePublicApi {
|
||||||
|
id: string; // '3135556';
|
||||||
|
readable: boolean;
|
||||||
|
title: string; // 'Harder, Better, Faster, Stronger'
|
||||||
|
title_short: string; // 'Harder, Better, Faster, Stronger'
|
||||||
|
title_version?: string; // ''
|
||||||
|
isrc: string; // 'GBDUW0000059'
|
||||||
|
link: string; // 'https://www.deezer.com/track/3135556'
|
||||||
|
share: string; // 'https://www.deezer.com/track/3135556?utm_source=deezer&utm_content=track-3135556&utm_term=0_1614937516&utm_medium=web'
|
||||||
|
duration: number; // '224'
|
||||||
|
track_position: number; // 4
|
||||||
|
disk_number: number; // 1
|
||||||
|
rank: string; // '956167'
|
||||||
|
release_date: string; // '2001-03-07'
|
||||||
|
explicit_lyrics: boolean;
|
||||||
|
explicit_content_lyrics: number; // 0
|
||||||
|
explicit_content_cover: number; // 0
|
||||||
|
preview: string; // 'https://cdns-preview-d.dzcdn.net/stream/c-deda7fa9316d9e9e880d2c6207e92260-8.mp3'
|
||||||
|
bpm: number; // 123.4
|
||||||
|
gain: number; // -12.4
|
||||||
|
available_countries: string[];
|
||||||
|
contributors: contributorsPublicApi[];
|
||||||
|
md5_image: string; // '2e018122cb56986277102d2041a592c8'
|
||||||
|
artist: contributorsPublicApi;
|
||||||
|
album: {
|
||||||
|
id: string; // '302127'
|
||||||
|
title: string; // 'Discovery';
|
||||||
|
link: string; // 'https://www.deezer.com/album/302127'
|
||||||
|
cover: string; // 'https://api.deezer.com/album/302127/image'
|
||||||
|
cover_small: string; // 'https://e-cdns-images.dzcdn.net/images/cover/2e018122cb56986277102d2041a592c8/56x56-000000-80-0-0.jpg'
|
||||||
|
cover_medium: string; // 'https://e-cdns-images.dzcdn.net/images/cover/2e018122cb56986277102d2041a592c8/250x250-000000-80-0-0.jpg'
|
||||||
|
cover_big: string; // 'https://e-cdns-images.dzcdn.net/images/cover/2e018122cb56986277102d2041a592c8/500x500-000000-80-0-0.jpg'
|
||||||
|
cover_xl: string; // 'https://e-cdns-images.dzcdn.net/images/cover/2e018122cb56986277102d2041a592c8/1000x1000-000000-80-0-0.jpg'
|
||||||
|
md5_image: string; // '2e018122cb56986277102d2041a592c8'
|
||||||
|
release_date: string; // '2001-03-07';
|
||||||
|
tracklist: string; // 'https://api.deezer.com/album/302127/tracks'
|
||||||
|
type: 'album';
|
||||||
|
};
|
||||||
|
type: 'track';
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue