2021-03-12 13:58:48 +00:00
|
|
|
|
const got = require('got')
|
|
|
|
|
|
|
|
|
|
// Possible values for order parameter in search
|
2021-03-13 12:18:22 +00:00
|
|
|
|
const SearchOrder = {
|
|
|
|
|
RANKING : "RANKING",
|
|
|
|
|
TRACK_ASC : "TRACK_ASC",
|
|
|
|
|
TRACK_DESC : "TRACK_DESC",
|
|
|
|
|
ARTIST_ASC : "ARTIST_ASC",
|
|
|
|
|
ARTIST_DESC : "ARTIST_DESC",
|
|
|
|
|
ALBUM_ASC : "ALBUM_ASC",
|
|
|
|
|
ALBUM_DESC : "ALBUM_DESC",
|
|
|
|
|
RATING_ASC : "RATING_ASC",
|
|
|
|
|
RATING_DESC : "RATING_DESC",
|
|
|
|
|
DURATION_ASC : "DURATION_ASC",
|
2021-03-12 13:58:48 +00:00
|
|
|
|
DURATION_DESC : "DURATION_DESC"
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-13 12:18:22 +00:00
|
|
|
|
class API{
|
2021-03-12 13:58:48 +00:00
|
|
|
|
constructor(cookie_jar, headers){
|
|
|
|
|
this.http_headers = headers
|
|
|
|
|
this.cookie_jar = cookie_jar
|
|
|
|
|
this.access_token = null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async api_call(method, args){
|
|
|
|
|
if (typeof args == "undefined") args = {}
|
2021-03-13 12:18:22 +00:00
|
|
|
|
if (this.access_token) args.access_token = this.access_token
|
|
|
|
|
let result_json;
|
2021-03-12 13:58:48 +00:00
|
|
|
|
try {
|
2021-03-13 12:18:22 +00:00
|
|
|
|
result_json = await got.get("https://api.deezer.com/" + method, {
|
2021-03-12 13:58:48 +00:00
|
|
|
|
searchParams: args,
|
|
|
|
|
cookieJar: this.cookie_jar,
|
|
|
|
|
headers: this.http_headers,
|
|
|
|
|
timeout: 30000
|
|
|
|
|
}).json()
|
2021-03-13 12:18:22 +00:00
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log(e)
|
2021-03-12 13:58:48 +00:00
|
|
|
|
await new Promise(r => setTimeout(r, 2000)) // sleep(2000ms)
|
2021-04-08 15:25:24 +00:00
|
|
|
|
return this.api_call(method, args)
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
if (result_json.error){
|
|
|
|
|
if (result_json.error.code){
|
|
|
|
|
if ([4, 700].indexOf(result_json.error.code) != -1) {
|
|
|
|
|
await new Promise(r => setTimeout(r, 5000)) // sleep(5000ms)
|
|
|
|
|
return await this.api_call(method, args)
|
|
|
|
|
}
|
|
|
|
|
if (result_json.error.code == 100) throw new ItemsLimitExceededException(`ItemsLimitExceededException: ${method} ${result_json.error.message || ""}`)
|
|
|
|
|
if (result_json.error.code == 200) throw new PermissionException(`PermissionException: ${method} ${result_json.error.message || ""}`)
|
|
|
|
|
if (result_json.error.code == 300) throw new InvalidTokenException(`InvalidTokenException: ${method} ${result_json.error.message || ""}`)
|
|
|
|
|
if (result_json.error.code == 500) throw new WrongParameterException(`ParameterException: ${method} ${result_json.error.message || ""}`)
|
|
|
|
|
if (result_json.error.code == 501) throw new MissingParameterException(`MissingParameterException: ${method} ${result_json.error.message || ""}`)
|
|
|
|
|
if (result_json.error.code == 600) throw new InvalidQueryException(`InvalidQueryException: ${method} ${result_json.error.message || ""}`)
|
|
|
|
|
if (result_json.error.code == 800) throw new DataException(`DataException: ${method} ${result_json.error.message || ""}`)
|
|
|
|
|
if (result_json.error.code == 901) throw new IndividualAccountChangedNotAllowedException(`IndividualAccountChangedNotAllowedException: ${method} ${result_json.error.message || ""}`)
|
|
|
|
|
}
|
2021-03-13 12:18:22 +00:00
|
|
|
|
throw APIError(result_json.error)
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
return result_json
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-13 12:18:22 +00:00
|
|
|
|
get_album(album_id){
|
|
|
|
|
return this.api_call(`album/${album_id}`)
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-13 12:18:22 +00:00
|
|
|
|
get_album_by_UPC(upc){
|
|
|
|
|
return this.get_album(`upc:${upc}`)
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_album_comments(album_id, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 25
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`album/${album_id}/comments`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_album_fans(album_id, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 100
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`album/${album_id}/fans`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_album_tracks(album_id, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || -1
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`album/${album_id}/tracks`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-13 12:18:22 +00:00
|
|
|
|
get_artist(artist_id){
|
|
|
|
|
return this.api_call(`artist/${artist_id}`)
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_artist_top(artist_id, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 10
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`artist/${artist_id}/top`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_artist_albums(artist_id, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || -1
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`artist/${artist_id}/albums`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_artist_comments(artist_id, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 10
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`artist/${artist_id}/comments`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_artist_fans(artist_id, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 100
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`artist/${artist_id}/fans`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_artist_related(artist_id, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 20
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`artist/${artist_id}/related`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_artist_radio(artist_id, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 25
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`artist/${artist_id}/radio`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_artist_playlists(artist_id, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || -1
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`artist/${artist_id}/playlists`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_chart(genre_id=0, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 10
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`chart/${genre_id}`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_chart_tracks(genre_id=0, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 10
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`chart/${genre_id}/tracks`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_chart_albums(genre_id=0, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 10
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`chart/${genre_id}/albums`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_chart_artists(genre_id=0, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 10
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`chart/${genre_id}/artists`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_chart_playlists(genre_id=0, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 10
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`chart/${genre_id}/playlists`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_chart_podcasts(genre_id=0, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 10
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`chart/${genre_id}/podcasts`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-13 12:18:22 +00:00
|
|
|
|
get_comment(comment_id){
|
|
|
|
|
return this.api_call(`comment/${comment_id}`)
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_editorials(options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 10
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call('editorial', {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-13 12:18:22 +00:00
|
|
|
|
get_editorial(genre_id=0){
|
|
|
|
|
return this.api_call(`editorial/${genre_id}`)
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_editorial_selection(genre_id=0, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 10
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`editorial/${genre_id}/selection`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_editorial_charts(genre_id=0, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 10
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`editorial/${genre_id}/charts`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_editorial_releases(genre_id=0, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 10
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`editorial/${genre_id}/releases`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_genres(options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 10
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call('genre', {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-13 12:18:22 +00:00
|
|
|
|
get_genre(genre_id=0){
|
|
|
|
|
return this.api_call(`genre/${genre_id}`)
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_genre_artists(genre_id=0, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 10
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`genre/${genre_id}/artists`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_genre_radios( genre_id=0, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 10
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`genre/${genre_id}/radios`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-13 12:18:22 +00:00
|
|
|
|
get_infos(){
|
|
|
|
|
return this.api_call('infos')
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-13 12:18:22 +00:00
|
|
|
|
get_options(){
|
|
|
|
|
return this.api_call('options')
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-13 12:18:22 +00:00
|
|
|
|
get_playlist(playlist_id){
|
|
|
|
|
return this.api_call(`playlist/${playlist_id}`)
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_playlist_comments(album_id, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 10
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`playlist/${album_id}/comments`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_playlist_fans(album_id, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 100
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`playlist/${album_id}/fans`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_playlist_tracks(album_id, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || -1
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`playlist/${album_id}/tracks`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_playlist_radio(album_id, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 100
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`playlist/${album_id}/radio`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_radios(options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 10
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call('radio', {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_radios_genres(options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 25
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call('radio/genres', {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_radios_top(options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 50
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call('radio/top', {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_radios_lists(options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 25
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call('radio/lists', {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-13 12:18:22 +00:00
|
|
|
|
get_radio(radio_id){
|
|
|
|
|
return this.api_call(`radio/${radio_id}`)
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_radio_tracks(radio_id, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 40
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`radio/${radio_id}/tracks`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-21 18:01:06 +00:00
|
|
|
|
_generate_search_advanced_query(filters){
|
2021-03-12 13:58:48 +00:00
|
|
|
|
let query = ""
|
2021-04-21 18:01:06 +00:00
|
|
|
|
if (filters.artist) query += `artist:"${filters.artist}" `
|
|
|
|
|
if (filters.album) query += `album:"${filters.album}" `
|
|
|
|
|
if (filters.track) query += `track:"${filters.track}" `
|
|
|
|
|
if (filters.label) query += `label:"${filters.label}" `
|
|
|
|
|
if (filters.dur_min) query += `dur_min:"${filters.dur_min}" `
|
|
|
|
|
if (filters.dur_max) query += `dur_max:"${filters.dur_max}" `
|
|
|
|
|
if (filters.bpm_min) query += `bpm_min:"${filters.bpm_min}" `
|
|
|
|
|
if (filters.bpm_max) query += `bpm_max:"${filters.bpm_max}" `
|
2021-03-12 13:58:48 +00:00
|
|
|
|
return query.trim()
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
_generate_search_args(query, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const strict = options.strict || false
|
|
|
|
|
const order = options.order || SearchOrder.RANKING
|
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 25
|
2021-03-12 13:58:48 +00:00
|
|
|
|
let args = {q: query, index, limit}
|
|
|
|
|
if (strict) args.strict = 'on'
|
|
|
|
|
if (order) args.order = order
|
|
|
|
|
return args
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
search(query, options={}){
|
|
|
|
|
const args = this._generate_search_args(query, options={})
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call('search', args)
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
advanced_search(filters, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const query = this._generate_search_advanced_query(filters)
|
2021-04-22 08:30:52 +00:00
|
|
|
|
return this.search(query, options={})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
search_album(query, options={}){
|
2021-04-24 18:21:46 +00:00
|
|
|
|
const args = this._generate_search_args(query, options)
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call('search/album', args)
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
search_artist(query, options={}){
|
2021-04-24 18:21:46 +00:00
|
|
|
|
const args = this._generate_search_args(query, options)
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call('search/artist', args)
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
search_playlist(query, options={}){
|
2021-04-24 18:21:46 +00:00
|
|
|
|
const args = this._generate_search_args(query, options)
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call('search/playlist', args)
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
search_radio(query, options={}){
|
2021-04-24 18:21:46 +00:00
|
|
|
|
const args = this._generate_search_args(query, options)
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call('search/radio', args)
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
search_track(query, options={}){
|
2021-04-24 18:21:46 +00:00
|
|
|
|
const args = this._generate_search_args(query, options)
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call('search/track', args)
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
search_user(query, options={}){
|
2021-04-24 18:21:46 +00:00
|
|
|
|
const args = this._generate_search_args(query, options)
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call('search/user', args)
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-13 12:18:22 +00:00
|
|
|
|
get_track(song_id){
|
|
|
|
|
return this.api_call(`track/${song_id}`)
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-13 12:18:22 +00:00
|
|
|
|
get_track_by_ISRC(isrc){
|
|
|
|
|
return this.get_track(`isrc:${isrc}`)
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-13 12:18:22 +00:00
|
|
|
|
get_user(user_id){
|
|
|
|
|
return this.api_call(`user/${user_id}`)
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_user_albums(user_id, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 25
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`user/${user_id}/albums`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_user_artists(user_id, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 25
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`user/${user_id}/artists`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_user_flow(user_id, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 25
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`user/${user_id}/flow`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_user_following(user_id, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 25
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`user/${user_id}/followings`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_user_followers(user_id, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 25
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`user/${user_id}/followers`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_user_playlists(user_id, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 25
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`user/${user_id}/playlists`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_user_radios(user_id, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 25
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`user/${user_id}/radios`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 08:30:52 +00:00
|
|
|
|
get_user_tracks(user_id, options={}){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
const index = options.index || 0
|
|
|
|
|
const limit = options.limit || 25
|
2021-03-13 12:18:22 +00:00
|
|
|
|
return this.api_call(`user/${user_id}/tracks`, {index, limit})
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Extra calls
|
|
|
|
|
|
|
|
|
|
async get_countries_charts(){
|
2021-04-21 18:58:19 +00:00
|
|
|
|
let temp = await this.get_user_playlists('637006841', {index:0, limit:-1})
|
|
|
|
|
let result = temp['data'].sort((a, b) => a.title.localeCompare(b.title)) // Sort all playlists
|
2021-03-12 13:58:48 +00:00
|
|
|
|
if (!result[0].title.startsWith('Top')) result.shift() // Remove loved tracks playlist
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async get_track_id_from_metadata(artist, track, album){
|
|
|
|
|
artist = artist.replace("–", "-").replace("’", "'")
|
|
|
|
|
track = track.replace("–", "-").replace("’", "'")
|
|
|
|
|
album = album.replace("–", "-").replace("’", "'")
|
|
|
|
|
|
2021-04-21 18:01:06 +00:00
|
|
|
|
let resp = await this.advanced_search({artist, track, album})
|
2021-04-09 17:27:12 +00:00
|
|
|
|
if (resp.data.length) return resp.data[0].id
|
2021-03-12 13:58:48 +00:00
|
|
|
|
|
2021-04-21 18:01:06 +00:00
|
|
|
|
resp = await this.advanced_search({artist, track})
|
2021-04-09 17:27:12 +00:00
|
|
|
|
if (resp.data.length) return resp.data[0].id
|
2021-03-12 13:58:48 +00:00
|
|
|
|
|
|
|
|
|
// Try removing version
|
|
|
|
|
if ( track.indexOf("(") != -1 && track.indexOf(")") != -1 && track.indexOf("(") < track.indexOf(")") ){
|
2021-04-21 18:01:06 +00:00
|
|
|
|
resp = await this.advanced_search({artist, track: track.split("(")[0]})
|
2021-04-09 17:27:12 +00:00
|
|
|
|
if (resp.data.length) return resp.data[0].id
|
2021-03-12 13:58:48 +00:00
|
|
|
|
} else if ( track.indexOf(" - ") != -1) {
|
2021-04-21 18:01:06 +00:00
|
|
|
|
resp = await this.advanced_search({artist, track: track.split(" - ")[0]})
|
2021-04-09 17:27:12 +00:00
|
|
|
|
if (resp.data.length) return resp.data[0].id
|
2021-03-12 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "0"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Base class for Deezer exceptions
|
2021-03-13 12:18:22 +00:00
|
|
|
|
class APIError extends Error {
|
2021-03-12 13:58:48 +00:00
|
|
|
|
constructor(message) {
|
|
|
|
|
super(message);
|
|
|
|
|
this.name = "APIError";
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-13 12:18:22 +00:00
|
|
|
|
class ItemsLimitExceededException extends APIError {
|
2021-03-12 13:58:48 +00:00
|
|
|
|
constructor(message) {
|
|
|
|
|
super(message);
|
|
|
|
|
this.name = "ItemsLimitExceededException";
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-13 12:18:22 +00:00
|
|
|
|
class PermissionException extends APIError {
|
2021-03-12 13:58:48 +00:00
|
|
|
|
constructor(message) {
|
|
|
|
|
super(message);
|
|
|
|
|
this.name = "PermissionException";
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-13 12:18:22 +00:00
|
|
|
|
class InvalidTokenException extends APIError {
|
2021-03-12 13:58:48 +00:00
|
|
|
|
constructor(message) {
|
|
|
|
|
super(message);
|
|
|
|
|
this.name = "InvalidTokenException";
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-13 12:18:22 +00:00
|
|
|
|
class WrongParameterException extends APIError {
|
2021-03-12 13:58:48 +00:00
|
|
|
|
constructor(message) {
|
|
|
|
|
super(message);
|
|
|
|
|
this.name = "WrongParameterException";
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-13 12:18:22 +00:00
|
|
|
|
class MissingParameterException extends APIError {
|
2021-03-12 13:58:48 +00:00
|
|
|
|
constructor(message) {
|
|
|
|
|
super(message);
|
|
|
|
|
this.name = "MissingParameterException";
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-13 12:18:22 +00:00
|
|
|
|
class InvalidQueryException extends APIError {
|
2021-03-12 13:58:48 +00:00
|
|
|
|
constructor(message) {
|
|
|
|
|
super(message);
|
|
|
|
|
this.name = "InvalidQueryException";
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-13 12:18:22 +00:00
|
|
|
|
class DataException extends APIError {
|
2021-03-12 13:58:48 +00:00
|
|
|
|
constructor(message) {
|
|
|
|
|
super(message);
|
|
|
|
|
this.name = "DataException";
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-13 12:18:22 +00:00
|
|
|
|
class IndividualAccountChangedNotAllowedException extends APIError {
|
2021-03-12 13:58:48 +00:00
|
|
|
|
constructor(message) {
|
|
|
|
|
super(message);
|
|
|
|
|
this.name = "IndividualAccountChangedNotAllowedException";
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-13 12:18:22 +00:00
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
SearchOrder,
|
|
|
|
|
API,
|
|
|
|
|
APIError,
|
|
|
|
|
ItemsLimitExceededException,
|
|
|
|
|
PermissionException,
|
|
|
|
|
InvalidTokenException,
|
|
|
|
|
WrongParameterException,
|
|
|
|
|
MissingParameterException,
|
|
|
|
|
InvalidQueryException,
|
|
|
|
|
DataException,
|
|
|
|
|
IndividualAccountChangedNotAllowedException
|
|
|
|
|
}
|