diff --git a/deezer/api.js b/deezer/api.js index c3f3688..947fea5 100644 --- a/deezer/api.js +++ b/deezer/api.js @@ -66,15 +66,21 @@ class API{ return this.get_album(`upc:${upc}`) } - get_album_comments(album_id, index=0, limit=10){ + get_album_comments(album_id, options){ + const index = options.index || 0 + const limit = options.limit || 25 return this.api_call(`album/${album_id}/comments`, {index, limit}) } - get_album_fans(album_id, index=0, limit=100){ + get_album_fans(album_id, options){ + const index = options.index || 0 + const limit = options.limit || 100 return this.api_call(`album/${album_id}/fans`, {index, limit}) } - get_album_tracks(album_id, index=0, limit=-1){ + get_album_tracks(album_id, options){ + const index = options.index || 0 + const limit = options.limit || -1 return this.api_call(`album/${album_id}/tracks`, {index, limit}) } @@ -82,55 +88,81 @@ class API{ return this.api_call(`artist/${artist_id}`) } - get_artist_top(artist_id, index=0, limit=10){ + get_artist_top(artist_id, options){ + const index = options.index || 0 + const limit = options.limit || 10 return this.api_call(`artist/${artist_id}/top`, {index, limit}) } - get_artist_albums(artist_id, index=0, limit=-1){ + get_artist_albums(artist_id, options){ + const index = options.index || 0 + const limit = options.limit || -1 return this.api_call(`artist/${artist_id}/albums`, {index, limit}) } - get_artist_comments(artist_id, index=0, limit=10){ + get_artist_comments(artist_id, options){ + const index = options.index || 0 + const limit = options.limit || 10 return this.api_call(`artist/${artist_id}/comments`, {index, limit}) } - get_artist_fans(artist_id, index=0, limit=100){ + get_artist_fans(artist_id, options){ + const index = options.index || 0 + const limit = options.limit || 100 return this.api_call(`artist/${artist_id}/fans`, {index, limit}) } - get_artist_related(artist_id, index=0, limit=20){ + get_artist_related(artist_id, options){ + const index = options.index || 0 + const limit = options.limit || 20 return this.api_call(`artist/${artist_id}/related`, {index, limit}) } - get_artist_radio(artist_id, index=0, limit=25){ + get_artist_radio(artist_id, options){ + const index = options.index || 0 + const limit = options.limit || 25 return this.api_call(`artist/${artist_id}/radio`, {index, limit}) } - get_artist_playlists(artist_id, index=0, limit=-1){ + get_artist_playlists(artist_id, options){ + const index = options.index || 0 + const limit = options.limit || -1 return this.api_call(`artist/${artist_id}/playlists`, {index, limit}) } - get_chart(genre_id=0, index=0, limit=10){ + get_chart(genre_id=0, options){ + const index = options.index || 0 + const limit = options.limit || 10 return this.api_call(`chart/${genre_id}`, {index, limit}) } - get_chart_tracks(genre_id=0, index=0, limit=10){ + get_chart_tracks(genre_id=0, options){ + const index = options.index || 0 + const limit = options.limit || 10 return this.api_call(`chart/${genre_id}/tracks`, {index, limit}) } - get_chart_albums(genre_id=0, index=0, limit=10){ + get_chart_albums(genre_id=0, options){ + const index = options.index || 0 + const limit = options.limit || 10 return this.api_call(`chart/${genre_id}/albums`, {index, limit}) } - get_chart_artists(genre_id=0, index=0, limit=10){ + get_chart_artists(genre_id=0, options){ + const index = options.index || 0 + const limit = options.limit || 10 return this.api_call(`chart/${genre_id}/artists`, {index, limit}) } - get_chart_playlists(genre_id=0, index=0, limit=10){ + get_chart_playlists(genre_id=0, options){ + const index = options.index || 0 + const limit = options.limit || 10 return this.api_call(`chart/${genre_id}/playlists`, {index, limit}) } - get_chart_podcasts(genre_id=0, index=0, limit=10){ + get_chart_podcasts(genre_id=0, options){ + const index = options.index || 0 + const limit = options.limit || 10 return this.api_call(`chart/${genre_id}/podcasts`, {index, limit}) } @@ -138,7 +170,9 @@ class API{ return this.api_call(`comment/${comment_id}`) } - get_editorials(index=0, limit=10){ + get_editorials(options){ + const index = options.index || 0 + const limit = options.limit || 10 return this.api_call('editorial', {index, limit}) } @@ -146,19 +180,27 @@ class API{ return this.api_call(`editorial/${genre_id}`) } - get_editorial_selection(genre_id=0, index=0, limit=10){ + get_editorial_selection(genre_id=0, options){ + const index = options.index || 0 + const limit = options.limit || 10 return this.api_call(`editorial/${genre_id}/selection`, {index, limit}) } - get_editorial_charts(genre_id=0, index=0, limit=10){ + get_editorial_charts(genre_id=0, options){ + const index = options.index || 0 + const limit = options.limit || 10 return this.api_call(`editorial/${genre_id}/charts`, {index, limit}) } - get_editorial_releases(genre_id=0, index=0, limit=10){ + get_editorial_releases(genre_id=0, options){ + const index = options.index || 0 + const limit = options.limit || 10 return this.api_call(`editorial/${genre_id}/releases`, {index, limit}) } - get_genres(index=0, limit=10){ + get_genres(options){ + const index = options.index || 0 + const limit = options.limit || 10 return this.api_call('genre', {index, limit}) } @@ -166,11 +208,15 @@ class API{ return this.api_call(`genre/${genre_id}`) } - get_genre_artists(genre_id=0, index=0, limit=10){ + get_genre_artists(genre_id=0, options){ + const index = options.index || 0 + const limit = options.limit || 10 return this.api_call(`genre/${genre_id}/artists`, {index, limit}) } - get_genre_radios( genre_id=0, index=0, limit=10){ + get_genre_radios( genre_id=0, options){ + const index = options.index || 0 + const limit = options.limit || 10 return this.api_call(`genre/${genre_id}/radios`, {index, limit}) } @@ -186,35 +232,51 @@ class API{ return this.api_call(`playlist/${playlist_id}`) } - get_playlist_comments(album_id, index=0, limit=10){ + get_playlist_comments(album_id, options){ + const index = options.index || 0 + const limit = options.limit || 10 return this.api_call(`playlist/${album_id}/comments`, {index, limit}) } - get_playlist_fans(album_id, index=0, limit=100){ + get_playlist_fans(album_id, options){ + const index = options.index || 0 + const limit = options.limit || 100 return this.api_call(`playlist/${album_id}/fans`, {index, limit}) } - get_playlist_tracks(album_id, index=0, limit=-1){ + get_playlist_tracks(album_id, options){ + const index = options.index || 0 + const limit = options.limit || -1 return this.api_call(`playlist/${album_id}/tracks`, {index, limit}) } - get_playlist_radio(album_id, index=0, limit=100){ + get_playlist_radio(album_id, options){ + const index = options.index || 0 + const limit = options.limit || 100 return this.api_call(`playlist/${album_id}/radio`, {index, limit}) } - get_radios(index=0, limit=10){ + get_radios(options){ + const index = options.index || 0 + const limit = options.limit || 10 return this.api_call('radio', {index, limit}) } - get_radios_genres(index=0, limit=25){ + get_radios_genres(options){ + const index = options.index || 0 + const limit = options.limit || 25 return this.api_call('radio/genres', {index, limit}) } - get_radios_top(index=0, limit=50){ + get_radios_top(options){ + const index = options.index || 0 + const limit = options.limit || 50 return this.api_call('radio/top', {index, limit}) } - get_radios_lists(index=0, limit=25){ + get_radios_lists(options){ + const index = options.index || 0 + const limit = options.limit || 25 return this.api_call('radio/lists', {index, limit}) } @@ -222,67 +284,73 @@ class API{ return this.api_call(`radio/${radio_id}`) } - get_radio_tracks(radio_id, index=0, limit=40){ + get_radio_tracks(radio_id, options){ + const index = options.index || 0 + const limit = options.limit || 40 return this.api_call(`radio/${radio_id}/tracks`, {index, limit}) } - _generate_search_advanced_query(artist="", album="", track="", label="", dur_min=0, dur_max=0, bpm_min=0, bpm_max=0){ + _generate_search_advanced_query(filters){ let query = "" - if (artist != "") query += `artist:"${artist}" ` - if (album != "") query += `album:"${album}" ` - if (track != "") query += `track:"${track}" ` - if (label != "") query += `label:"${label}" ` - if (dur_min != 0) query += `dur_min:"${dur_min}" ` - if (dur_max != 0) query += `dur_max:"${dur_max}" ` - if (bpm_min != 0) query += `bpm_min:"${bpm_min}" ` - if (bpm_max != 0) query += `bpm_max:"${bpm_max}" ` + 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}" ` return query.trim() } - _generate_search_args(query, strict=false, order, index=0, limit=25){ + _generate_search_args(query, options){ + const strict = options.strict || false + const order = options.order || SearchOrder.RANKING + const index = options.index || 0 + const limit = options.limit || 25 let args = {q: query, index, limit} if (strict) args.strict = 'on' if (order) args.order = order return args } - search(query, strict=false, order, index=0, limit=25){ - const args = this._generate_search_args(query, strict, order, index, limit) + search(query, options){ + const args = this._generate_search_args(query, options) return this.api_call('search', args) } - advanced_search(artist="", album="", track="", label="", dur_min=0, dur_max=0, bpm_min=0, bpm_max=0, strict=false, order, index=0, limit=25){ - const query = this._generate_search_advanced_query(artist, album, track, label, dur_min, dur_max, bpm_min, bpm_max) - return this.search(query, strict, order, index, limit) + advanced_search(filters, options){ + const query = this._generate_search_advanced_query(filters) + return this.search(query, options) } - search_album(query, strict=false, order, index=0, limit=25){ - const args = this._generate_search_args(query, strict, order, index, limit) + search_album(query, options){ + const args = this._generate_search_args(query, options) return this.api_call('search/album', args) } - search_artist(query, strict=false, order, index=0, limit=25){ - const args = this._generate_search_args(query, strict, order, index, limit) + search_artist(query, options){ + const args = this._generate_search_args(query, options) return this.api_call('search/artist', args) } - search_playlist(query, strict=false, order, index=0, limit=25){ - const args = this._generate_search_args(query, strict, order, index, limit) + search_playlist(query, options){ + const args = this._generate_search_args(query, options) return this.api_call('search/playlist', args) } - search_radio(query, strict=false, order, index=0, limit=25){ - const args = this._generate_search_args(query, strict, order, index, limit) + search_radio(query, options){ + const args = this._generate_search_args(query, options) return this.api_call('search/radio', args) } - search_track(query, strict=false, order, index=0, limit=25){ - const args = this._generate_search_args(query, strict, order, index, limit) + search_track(query, options){ + const args = this._generate_search_args(query, options) return this.api_call('search/track', args) } - search_user(query, strict=false, order, index=0, limit=25){ - const args = this._generate_search_args(query, strict, order, index, limit) + search_user(query, options){ + const args = this._generate_search_args(query, options) return this.api_call('search/user', args) } @@ -298,42 +366,58 @@ class API{ return this.api_call(`user/${user_id}`) } - get_user_albums(user_id, index=0, limit=25){ + get_user_albums(user_id, options){ + const index = options.index || 0 + const limit = options.limit || 25 return this.api_call(`user/${user_id}/albums`, {index, limit}) } - get_user_artists(user_id, index=0, limit=25){ + get_user_artists(user_id, options){ + const index = options.index || 0 + const limit = options.limit || 25 return this.api_call(`user/${user_id}/artists`, {index, limit}) } - get_user_flow(user_id, index=0, limit=25){ + get_user_flow(user_id, options){ + const index = options.index || 0 + const limit = options.limit || 25 return this.api_call(`user/${user_id}/flow`, {index, limit}) } - get_user_following(user_id, index=0, limit=25){ + get_user_following(user_id, options){ + const index = options.index || 0 + const limit = options.limit || 25 return this.api_call(`user/${user_id}/followings`, {index, limit}) } - get_user_followers(user_id, index=0, limit=25){ + get_user_followers(user_id, options){ + const index = options.index || 0 + const limit = options.limit || 25 return this.api_call(`user/${user_id}/followers`, {index, limit}) } - get_user_playlists(user_id, index=0, limit=25){ + get_user_playlists(user_id, options){ + const index = options.index || 0 + const limit = options.limit || 25 return this.api_call(`user/${user_id}/playlists`, {index, limit}) } - get_user_radios(user_id, index=0, limit=25){ + get_user_radios(user_id, options){ + const index = options.index || 0 + const limit = options.limit || 25 return this.api_call(`user/${user_id}/radios`, {index, limit}) } - get_user_tracks(user_id, index=0, limit=25){ + get_user_tracks(user_id, options){ + const index = options.index || 0 + const limit = options.limit || 25 return this.api_call(`user/${user_id}/tracks`, {index, limit}) } // Extra calls async get_countries_charts(){ - let temp = await this.get_user_playlists('637006841', 0, -1)['data'] + let temp = await this.get_user_playlists('637006841', {index:0, limit:-1})['data'] let result = temp.sort((a, b) => a.title.localeCompare(b.title)) // Sort all playlists if (!result[0].title.startsWith('Top')) result.shift() // Remove loved tracks playlist return result @@ -345,18 +429,18 @@ class API{ track = track.replace("–", "-").replace("’", "'") album = album.replace("–", "-").replace("’", "'") - let resp = await this.advanced_search(artist, track, album) + let resp = await this.advanced_search({artist, track, album}) if (resp.data.length) return resp.data[0].id - resp = await this.advanced_search(artist, track) + resp = await this.advanced_search({artist, track}) if (resp.data.length) return resp.data[0].id // Try removing version if ( track.indexOf("(") != -1 && track.indexOf(")") != -1 && track.indexOf("(") < track.indexOf(")") ){ - resp = await this.advanced_search(artist, track.split("(")[0],) + resp = await this.advanced_search({artist, track: track.split("(")[0]}) if (resp.data.length) return resp.data[0].id } else if ( track.indexOf(" - ") != -1) { - resp = await this.advanced_search(artist, track.split(" - ")[0]) + resp = await this.advanced_search({artist, track: track.split(" - ")[0]}) if (resp.data.length) return resp.data[0].id } diff --git a/deezer/gw.js b/deezer/gw.js index 7c32176..d6c16d9 100644 --- a/deezer/gw.js +++ b/deezer/gw.js @@ -76,7 +76,8 @@ class GW{ return this.api_call('deezer.getUserData') } - get_user_profile_page(user_id, tab, limit=10){ + get_user_profile_page(user_id, tab, options){ + const limit = options.limit || 10 return this.api_call('deezer.pageProfile', {user_id, tab, nb: limit}) } @@ -148,7 +149,8 @@ class GW{ }) } - async get_artist_top_tracks(art_id, limit=100){ + async get_artist_top_tracks(art_id, options){ + const limit = options.limit || 100 let tracks_array = [] let body = await this.api_call('artist.getTopTrack', {art_id, nb: limit}) body.data.forEach(track => { @@ -158,7 +160,9 @@ class GW{ return tracks_array } - get_artist_discography(art_id, index=0, limit=25){ + get_artist_discography(art_id, options){ + const index = options.index || 0 + const limit = options.limit || 25 return this.api_call('album.getDiscography', { art_id, discography_mode:"all", @@ -316,7 +320,9 @@ class GW{ }) } - search_music(query, type, index=0, limit=10){ + search_music(query, type, options){ + const index = options.index || 0 + const limit = options.limit || 10 return this.api_call('search.music', { query, filter: "ALL", @@ -328,7 +334,8 @@ class GW{ // Extra calls - async get_artist_discography_tabs(art_id, limit=100){ + async get_artist_discography_tabs(art_id, options){ + const limit = options.limit || 100 let index = 0 let releases = [] let result = {all: []} @@ -382,7 +389,8 @@ class GW{ return body } - async get_user_playlists(user_id, limit=25){ + async get_user_playlists(user_id, options){ + const limit = options.limit || 25 let user_profile_page = await this.get_user_profile_page(user_id, 'playlists', limit) let blog_name = user_profile_page.DATA.USER.BLOG_NAME || "Unknown" let data = user_profile_page.TAB.playlists.data @@ -393,7 +401,8 @@ class GW{ return result } - async get_user_albums(user_id, limit=25){ + async get_user_albums(user_id, options){ + const limit = options.limit || 25 let data = await this.get_user_profile_page(user_id, 'albums', limit).TAB.albums.data let result = [] data.forEach(album => { @@ -402,7 +411,8 @@ class GW{ return result } - async get_user_artists(user_id, limit=25){ + async get_user_artists(user_id, options){ + const limit = options.limit || 25 let data = this.get_user_profile_page(user_id, 'artists', limit).TAB.artists.data let result = [] data.forEach(artist => { @@ -411,7 +421,8 @@ class GW{ return result } - async get_user_tracks(user_id, limit=25){ + async get_user_tracks(user_id, options){ + const limit = options.limit || 25 let data = this.get_user_profile_page(user_id, 'loved', limit).TAB.loved.data let result = [] data.forEach(track => { diff --git a/package-lock.json b/package-lock.json index eb70081..3cfbcf8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { "name": "deezer-js", - "version": "0.0.4", + "version": "0.0.5", "lockfileVersion": 2, "requires": true, "packages": { "": { - "version": "0.0.4", + "version": "0.0.5", "license": "GPL-3.0-or-later", "dependencies": { "got": "^11.8.2", diff --git a/package.json b/package.json index 9bee652..8698a71 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "deezer-js", - "version": "0.0.4", + "version": "0.0.5", "description": "A wrapper for all Deezer's APIs", "main": "deezer/index.js", "scripts": {