From cec67eee9b3af5579a48fd426006cadfa702befc Mon Sep 17 00:00:00 2001 From: Finian Landes Date: Sat, 30 Dec 2023 17:27:11 +0100 Subject: [PATCH] Added Recommendations, All functions working, Size of arrays new parameter --- SpotifyESP32.cpp | 471 ++++++++++++++++++++--------------------- SpotifyESP32.h | 84 ++++---- Spotify_Esp32_test.ino | 91 ++++---- 3 files changed, 323 insertions(+), 323 deletions(-) diff --git a/SpotifyESP32.cpp b/SpotifyESP32.cpp index af8ca5c..9b72ec2 100644 --- a/SpotifyESP32.cpp +++ b/SpotifyESP32.cpp @@ -263,9 +263,9 @@ bool Spotify::get_token(){ http.end(); return reply; } -String Spotify::array_to_string(char** array) { +String Spotify::array_to_string(int size, char** array) { String result; - for (int i = 0; array[i] != nullptr; ++i) { + for (int i = 0; i(); - for (int i = 0; array[i] != nullptr; ++i) { + for (int i = 0; i= 0.0 && param <= 1.0; +} +bool Spotify::is_valid_value(int param) { + return param >0; +} +void Spotify::populate_float_values(std::map& float_params, recommendations& recom){ + if (is_valid_value(recom.min_acousticness)) { + float_params["min_acousticness"] = recom.min_acousticness; + } + if (is_valid_value(recom.max_acousticness)) { + float_params["max_acousticness"] = recom.max_acousticness; + } + if (is_valid_value(recom.target_acousticness)) { + float_params["target_acousticness"] = recom.target_acousticness; + } + if (is_valid_value(recom.min_danceability)) { + float_params["min_danceability"] = recom.min_danceability; + } + if (is_valid_value(recom.max_danceability)) { + float_params["max_danceability"] = recom.max_danceability; + } + if (is_valid_value(recom.target_danceability)) { + float_params["target_danceability"] = recom.target_danceability; + } + + if (is_valid_value(recom.min_duration_ms)) { + float_params["min_duration_ms"] = recom.min_duration_ms; + } + if (is_valid_value(recom.max_duration_ms)) { + float_params["max_duration_ms"] = recom.max_duration_ms; + } + if (is_valid_value(recom.target_duration_ms)) { + float_params["target_duration_ms"] = recom.target_duration_ms; + } + + if (is_valid_value(recom.min_energy)) { + float_params["min_energy"] = recom.min_energy; + } + if (is_valid_value(recom.max_energy)) { + float_params["max_energy"] = recom.max_energy; + } + if (is_valid_value(recom.target_energy)) { + float_params["target_energy"] = recom.target_energy; + } + + if (is_valid_value(recom.min_instrumentalness)) { + float_params["min_instrumentalness"] = recom.min_instrumentalness; + } + if (is_valid_value(recom.max_instrumentalness)) { + float_params["max_instrumentalness"] = recom.max_instrumentalness; + } + if (is_valid_value(recom.target_instrumentalness)) { + float_params["target_instrumentalness"] = recom.target_instrumentalness; + } + + if (is_valid_value(recom.min_key)) { + float_params["min_key"] = recom.min_key; + } + if (is_valid_value(recom.max_key)) { + float_params["max_key"] = recom.max_key; + } + if (is_valid_value(recom.target_key)) { + float_params["target_key"] = recom.target_key; + } + + if (is_valid_value(recom.min_liveness)) { + float_params["min_liveness"] = recom.min_liveness; + } + if (is_valid_value(recom.max_liveness)) { + float_params["max_liveness"] = recom.max_liveness; + } + if (is_valid_value(recom.target_liveness)) { + float_params["target_liveness"] = recom.target_liveness; + } + + if (is_valid_value(recom.min_loudness)) { + float_params["min_loudness"] = recom.min_loudness; + } + if (is_valid_value(recom.max_loudness)) { + float_params["max_loudness"] = recom.max_loudness; + } + if (is_valid_value(recom.target_loudness)) { + float_params["target_loudness"] = recom.target_loudness; + } + + if (is_valid_value(recom.min_mode)) { + float_params["min_mode"] = recom.min_mode; + } + if (is_valid_value(recom.max_mode)) { + float_params["max_mode"] = recom.max_mode; + } + if (is_valid_value(recom.target_mode)) { + float_params["target_mode"] = recom.target_mode; + } + + if (is_valid_value(recom.min_popularity)) { + float_params["min_popularity"] = recom.min_popularity; + } + if (is_valid_value(recom.max_popularity)) { + float_params["max_popularity"] = recom.max_popularity; + } + if (is_valid_value(recom.target_popularity)) { + float_params["target_popularity"] = recom.target_popularity; + } + + if (is_valid_value(recom.min_speechiness)) { + float_params["min_speechiness"] = recom.min_speechiness; + } + if (is_valid_value(recom.max_speechiness)) { + float_params["max_speechiness"] = recom.max_speechiness; + } + if (is_valid_value(recom.target_speechiness)) { + float_params["target_speechiness"] = recom.target_speechiness; + } + + if (is_valid_value(recom.min_tempo)) { + float_params["min_tempo"] = recom.min_tempo; + } + if (is_valid_value(recom.max_tempo)) { + float_params["max_tempo"] = recom.max_tempo; + } + if (is_valid_value(recom.target_tempo)) { + float_params["target_tempo"] = recom.target_tempo; + } + + if (is_valid_value(recom.min_time_signature)) { + float_params["min_time_signature"] = recom.min_time_signature; + } + if (is_valid_value(recom.max_time_signature)) { + float_params["max_time_signature"] = recom.max_time_signature; + } + if (is_valid_value(recom.target_time_signature)) { + float_params["target_time_signature"] = recom.target_time_signature; + } + + if (is_valid_value(recom.min_valence)) { + float_params["min_valence"] = recom.min_valence; + } + if (is_valid_value(recom.max_valence)) { + float_params["max_valence"] = recom.max_valence; + } + if (is_valid_value(recom.target_valence)) { + float_params["target_valence"] = recom.target_valence; + } +} +void Spotify::populate_char_values(std::map& char_params, recommendations& recom){ + if (is_valid_value(recom.seed_artists_size)) { + char_params["seed_artists"] = array_to_string(recom.seed_artists_size, recom.seed_artists); + } + if (is_valid_value(recom.seed_genres_size)) { + char_params["seed_genres"] = array_to_string(recom.seed_genres_size, recom.seed_genres); + } + if (is_valid_value(recom.seed_tracks_size)) { + char_params["seed_tracks"] = array_to_string(recom.seed_tracks_size, recom.seed_tracks); + } +} //API functions //Player response Spotify::currently_playing(){ @@ -397,9 +554,9 @@ response Spotify::get_album(String album_id){ return RestApiGet(const_cast(url.c_str())); } -response Spotify::get_albums(char ** album_ids){ +response Spotify::get_albums(int size, char ** album_ids){ String url = "https://api.spotify.com/v1/albums"; - url += "?ids=" + array_to_string(album_ids); + url += "?ids=" + array_to_string(size, album_ids); return RestApiGet(const_cast(url.c_str())); @@ -416,19 +573,19 @@ response Spotify::get_users_saved_albums(int limit, int offset){ return RestApiGet(const_cast(url.c_str())); } -response Spotify::save_albums_for_current_user(char ** album_ids){ +response Spotify::save_albums_for_current_user(int size,char ** album_ids){ char url[] = "https://api.spotify.com/v1/me/albums"; - return RestApiPut(url, array_to_json_array(album_ids)); + return RestApiPut(url, array_to_json_array(size, album_ids)); } -response Spotify::remove_users_saved_albums(char ** album_ids){ +response Spotify::remove_users_saved_albums(int size,char ** album_ids){ char url[] = "https://api.spotify.com/v1/me/albums"; - return RestApiDelete(url, array_to_json_array(album_ids)); + return RestApiDelete(url, array_to_json_array(size, album_ids)); } -response Spotify::check_useres_saved_albums(char ** album_ids){ +response Spotify::check_useres_saved_albums(int size,char ** album_ids){ String url = "https://api.spotify.com/v1/me/albums/contains"; - url += "?ids=" + array_to_string(album_ids); + url += "?ids=" + array_to_string(size, album_ids); return RestApiGet(const_cast(url.c_str())); } @@ -446,9 +603,9 @@ response Spotify::get_artist(String artist_id){ return RestApiGet(const_cast(url.c_str())); } -response Spotify::get_several_artists(char ** artist_ids){ +response Spotify::get_several_artists(int size, char ** artist_ids){ String url = "https://api.spotify.com/v1/artists"; - url += "?ids=" + array_to_string(artist_ids); + url += "?ids=" + array_to_string(size, artist_ids); return RestApiGet(const_cast(url.c_str())); } @@ -480,9 +637,9 @@ response Spotify::get_audiobook(String audiobook_id){ return RestApiGet(const_cast(url.c_str())); } -response Spotify::get_several_audiobooks(char** audiobook_ids){ +response Spotify::get_several_audiobooks(int size,char** audiobook_ids){ String url = "https://api.spotify.com/v1/audiobooks"; - url += "?ids=" + array_to_string(audiobook_ids); + url += "?ids=" + array_to_string(size, audiobook_ids); return RestApiGet(const_cast(url.c_str())); } @@ -498,21 +655,21 @@ response Spotify::get_users_audiobooks(int limit, int offset){ return RestApiGet(const_cast(url.c_str())); } -response Spotify::save_audiobooks_for_current_user(char** audiobook_ids){ +response Spotify::save_audiobooks_for_current_user(int size,char** audiobook_ids){ String url = "https://api.spotify.com/v1/me/audiobooks"; - url += "?ids=" + array_to_string(audiobook_ids); + url += "?ids=" + array_to_string(size, audiobook_ids); return RestApiPut(const_cast(url.c_str())); } -response Spotify::remove_audiobooks_for_current_user(char** audiobook_ids){ +response Spotify::remove_audiobooks_for_current_user(int size,char** audiobook_ids){ String url = "https://api.spotify.com/v1/me/audiobooks"; - url += "?ids=" + array_to_string(audiobook_ids); + url += "?ids=" + array_to_string(size, audiobook_ids); return RestApiDelete(const_cast(url.c_str())); } -response Spotify::check_users_saved_audiobooks(char** audiobook_ids){ +response Spotify::check_users_saved_audiobooks(int size,char** audiobook_ids){ String url = "https://api.spotify.com/v1/me/audiobooks/contains"; - url += "?ids=" + array_to_string(audiobook_ids); + url += "?ids=" + array_to_string(size, audiobook_ids); return RestApiGet(const_cast(url.c_str())); } @@ -539,11 +696,9 @@ response Spotify::get_chapter(String chapter_id){ return RestApiGet(const_cast(url.c_str())); } -response Spotify::get_several_chapters(char** chapter_ids){ +response Spotify::get_several_chapters(int size, char** chapter_ids){ String url = "https://api.spotify.com/v1/chapters"; - url += +"?ids=" + array_to_string(chapter_ids); - - + url += +"?ids=" + array_to_string(size, chapter_ids); return RestApiGet(const_cast(url.c_str())); } @@ -555,9 +710,9 @@ response Spotify::get_episode(String episode_id){ return RestApiGet(const_cast(url.c_str())); } -response Spotify::get_several_episodes(char** episode_ids){ +response Spotify::get_several_episodes(int size,char** episode_ids){ String url = "https://api.spotify.com/v1/episodes"; - url += "?ids=" + array_to_string(episode_ids); + url += "?ids=" + array_to_string(size, episode_ids); return RestApiGet(const_cast(url.c_str())); } @@ -567,19 +722,19 @@ response Spotify::get_users_saved_episodes(int limit, int offset){ return RestApiGet(const_cast(url.c_str())); } -response Spotify::save_episodes_for_current_user(char** episode_ids){ +response Spotify::save_episodes_for_current_user(int size,char** episode_ids){ char url[] = "https://api.spotify.com/v1/me/episodes"; - return RestApiPut(url, array_to_json_array(episode_ids)); + return RestApiPut(url, array_to_json_array(size, episode_ids)); } -response Spotify::remove_episodes_for_current_user(char** episode_ids){ +response Spotify::remove_episodes_for_current_user(int size,char** episode_ids){ char url[] = "https://api.spotify.com/v1/me/episodes"; - return RestApiDelete(url, array_to_json_array(episode_ids)); + return RestApiDelete(url, array_to_json_array(size, episode_ids)); } -response Spotify::check_users_saved_episodes(char** episode_ids){ +response Spotify::check_users_saved_episodes(int size,char** episode_ids){ String url = "https://api.spotify.com/v1/me/episodes/contains"; - url += "?ids=" + array_to_string(episode_ids); + url += "?ids=" + array_to_string(size, episode_ids); return RestApiGet(const_cast(url.c_str())); } @@ -629,40 +784,37 @@ response Spotify::get_playlist_items(String playlist_id, String fields, int limi return RestApiGet(const_cast(url.c_str())); } -response Spotify::update_playlist_items(String playlist_id, char** uris, int range_start, int insert_before, int range_length) { +response Spotify::update_playlist_items(String playlist_id,int size, char** uris, int range_length, int range_start, int insert_before) { String url = "https://api.spotify.com/v1/playlists/"; url += playlist_id + "/tracks"; + DynamicJsonDocument doc(800); - if(uris == nullptr){ - doc["range_start"] = range_start; - doc["insert_before"] = insert_before; - doc["range_length"] = range_length; - } - else{ - doc["uris"] = array_to_string(uris); - } + doc["uris"] = array_to_json_array(size, uris); + doc["range_start"] = range_start; + doc["insert_before"] = insert_before; + doc["range_length"] = range_length; + String payload; serializeJson(doc, payload); return RestApiPut(const_cast(url.c_str()), payload); } -response Spotify::add_items_to_playlist(String playlist_id, char ** uris, int position) { +response Spotify::add_items_to_playlist(String playlist_id, int size, char ** uris, int position) { String url = "https://api.spotify.com/v1/playlists/"; url += playlist_id + "/tracks"; DynamicJsonDocument doc(1000); - doc["uris"] = array_to_json_array(uris); + doc["uris"] = array_to_json_array(size, uris); doc["position"] = position; String payload; serializeJson(doc, payload); - Serial.println(payload); return RestApiPost(const_cast(url.c_str()), payload); } -response Spotify::remove_playlist_items(String playlist_id, char** uris) { +response Spotify::remove_playlist_items(String playlist_id,int size, char** uris) { String url = "https://api.spotify.com/v1/playlists/"; url += playlist_id + "/tracks"; - return RestApiDelete(const_cast(url.c_str()), array_to_json_array(uris)); + return RestApiDelete(const_cast(url.c_str()), array_to_json_array(size, uris)); } response Spotify::get_current_users_playlists(int limit, int offset) { String url = "https://api.spotify.com/v1/me/playlists"; @@ -733,9 +885,9 @@ response Spotify::get_show(String show_id) { return RestApiGet(const_cast(url.c_str())); } -response Spotify::get_several_shows(char ** show_ids) { +response Spotify::get_several_shows(int size,char ** show_ids) { String url = "https://api.spotify.com/v1/shows"; - url += "?ids=" + array_to_string(show_ids); + url += "?ids=" + array_to_string(size, show_ids); return RestApiGet(const_cast(url.c_str())); } @@ -751,21 +903,21 @@ response Spotify::get_users_saved_shows(int limit, int offset) { return RestApiGet(const_cast(url.c_str())); } -response Spotify::save_shows_for_current_user(char ** show_ids) { +response Spotify::save_shows_for_current_user(int size,char ** show_ids) { String url = "https://api.spotify.com/v1/me/shows"; - url += "?ids=" + array_to_string(show_ids); + url += "?ids=" + array_to_string(size, show_ids); return RestApiPut(const_cast(url.c_str())); } -response Spotify::remove_shows_for_current_user(char ** show_ids) { +response Spotify::remove_shows_for_current_user(int size,char ** show_ids) { String url = "https://api.spotify.com/v1/me/shows"; - url += "?ids=" + array_to_string(show_ids); + url += "?ids=" + array_to_string(size, show_ids); return RestApiDelete(const_cast(url.c_str())); } -response Spotify::check_users_saved_shows(char ** show_ids) { +response Spotify::check_users_saved_shows(int size,char ** show_ids) { String url = "https://api.spotify.com/v1/me/shows/contains"; - url += "?ids=" + array_to_string(show_ids); + url += "?ids=" + array_to_string(size, show_ids); return RestApiGet(const_cast(url.c_str())); } @@ -777,9 +929,9 @@ response Spotify::get_track(String track_id) { return RestApiGet(const_cast(url.c_str())); } -response Spotify::get_several_tracks(char ** track_ids) { +response Spotify::get_several_tracks(int size,char ** track_ids) { String url = "https://api.spotify.com/v1/tracks"; - url += "?ids=" + array_to_string(track_ids); + url += "?ids=" + array_to_string(size, track_ids); return RestApiGet(const_cast(url.c_str())); } @@ -789,25 +941,25 @@ response Spotify::get_user_saved_tracks(int limit, int offset) { return RestApiGet(const_cast(url.c_str())); } -response Spotify::save_tracks_for_current_user(char ** track_ids) { +response Spotify::save_tracks_for_current_user(int size,char ** track_ids) { char url[] = "https://api.spotify.com/v1/me/tracks"; - return RestApiPut(url, array_to_json_array(track_ids)); + return RestApiPut(url, array_to_json_array(size, track_ids)); } -response Spotify::remove_user_saved_tracks(char ** track_ids) { +response Spotify::remove_user_saved_tracks(int size,char ** track_ids) { char url[] = "https://api.spotify.com/v1/me/tracks"; - return RestApiDelete(url, array_to_json_array(track_ids)); + return RestApiDelete(url, array_to_json_array(size, track_ids)); } -response Spotify::check_user_saved_tracks(char ** track_ids) { +response Spotify::check_user_saved_tracks(int size,char ** track_ids) { String url = "https://api.spotify.com/v1/me/tracks/contains"; - url += "?ids=" + array_to_string(track_ids); + url += "?ids=" + array_to_string(size, track_ids); return RestApiGet(const_cast(url.c_str())); } -response Spotify::get_tracks_audio_features(char ** track_ids) { +response Spotify::get_tracks_audio_features(int size,char ** track_ids) { String url = "https://api.spotify.com/v1/audio-features"; - url += "?ids=" + array_to_string(track_ids); + url += "?ids=" + array_to_string(size, track_ids); return RestApiGet(const_cast(url.c_str())); } @@ -823,15 +975,15 @@ response Spotify::get_track_audio_analysis(String track_id) { return RestApiGet(const_cast(url.c_str())); } -response Spotify::get_recommendations(int limit, recommendations recommendations){ +response Spotify::get_recommendations(recommendations& recom, int limit){ String url = "https://api.spotify.com/v1/recommendations"; url += "?limit=" + String(limit); - std::map string_params; + std::map char_params; std::map float_params; - populate_float_values(float_params, recommendations); - populate_string_values(string_params, recommendations); + populate_float_values(float_params, recom); + populate_char_values(char_params, recom); - for (const auto& param : string_params) { + for (const auto& param : char_params) { url += "&" + param.first + "=" + param.second; } @@ -841,164 +993,7 @@ response Spotify::get_recommendations(int limit, recommendations recommendations return RestApiGet(const_cast(url.c_str())); } -bool Spotify::is_valid_value(float param) { - return param >= 0.0 && param <= 1.0; -} -bool Spotify::is_valid_value(const String& param) { - return !param.isEmpty(); -} -void Spotify::populate_float_values(std::map& float_params, recommendations recommendations){ - if (is_valid_value(recommendations.min_acousticness)) { - float_params["min_acousticness"] = recommendations.min_acousticness; - } - if (is_valid_value(recommendations.max_acousticness)) { - float_params["max_acousticness"] = recommendations.max_acousticness; - } - if (is_valid_value(recommendations.target_acousticness)) { - float_params["target_acousticness"] = recommendations.target_acousticness; - } - if (is_valid_value(recommendations.min_danceability)) { - float_params["min_danceability"] = recommendations.min_danceability; - } - if (is_valid_value(recommendations.max_danceability)) { - float_params["max_danceability"] = recommendations.max_danceability; - } - if (is_valid_value(recommendations.target_danceability)) { - float_params["target_danceability"] = recommendations.target_danceability; - } - - if (is_valid_value(recommendations.min_duration_ms)) { - float_params["min_duration_ms"] = recommendations.min_duration_ms; - } - if (is_valid_value(recommendations.max_duration_ms)) { - float_params["max_duration_ms"] = recommendations.max_duration_ms; - } - if (is_valid_value(recommendations.target_duration_ms)) { - float_params["target_duration_ms"] = recommendations.target_duration_ms; - } - - if (is_valid_value(recommendations.min_energy)) { - float_params["min_energy"] = recommendations.min_energy; - } - if (is_valid_value(recommendations.max_energy)) { - float_params["max_energy"] = recommendations.max_energy; - } - if (is_valid_value(recommendations.target_energy)) { - float_params["target_energy"] = recommendations.target_energy; - } - - if (is_valid_value(recommendations.min_instrumentalness)) { - float_params["min_instrumentalness"] = recommendations.min_instrumentalness; - } - if (is_valid_value(recommendations.max_instrumentalness)) { - float_params["max_instrumentalness"] = recommendations.max_instrumentalness; - } - if (is_valid_value(recommendations.target_instrumentalness)) { - float_params["target_instrumentalness"] = recommendations.target_instrumentalness; - } - - if (is_valid_value(recommendations.min_key)) { - float_params["min_key"] = recommendations.min_key; - } - if (is_valid_value(recommendations.max_key)) { - float_params["max_key"] = recommendations.max_key; - } - if (is_valid_value(recommendations.target_key)) { - float_params["target_key"] = recommendations.target_key; - } - - if (is_valid_value(recommendations.min_liveness)) { - float_params["min_liveness"] = recommendations.min_liveness; - } - if (is_valid_value(recommendations.max_liveness)) { - float_params["max_liveness"] = recommendations.max_liveness; - } - if (is_valid_value(recommendations.target_liveness)) { - float_params["target_liveness"] = recommendations.target_liveness; - } - - if (is_valid_value(recommendations.min_loudness)) { - float_params["min_loudness"] = recommendations.min_loudness; - } - if (is_valid_value(recommendations.max_loudness)) { - float_params["max_loudness"] = recommendations.max_loudness; - } - if (is_valid_value(recommendations.target_loudness)) { - float_params["target_loudness"] = recommendations.target_loudness; - } - - if (is_valid_value(recommendations.min_mode)) { - float_params["min_mode"] = recommendations.min_mode; - } - if (is_valid_value(recommendations.max_mode)) { - float_params["max_mode"] = recommendations.max_mode; - } - if (is_valid_value(recommendations.target_mode)) { - float_params["target_mode"] = recommendations.target_mode; - } - - if (is_valid_value(recommendations.min_popularity)) { - float_params["min_popularity"] = recommendations.min_popularity; - } - if (is_valid_value(recommendations.max_popularity)) { - float_params["max_popularity"] = recommendations.max_popularity; - } - if (is_valid_value(recommendations.target_popularity)) { - float_params["target_popularity"] = recommendations.target_popularity; - } - - if (is_valid_value(recommendations.min_speechiness)) { - float_params["min_speechiness"] = recommendations.min_speechiness; - } - if (is_valid_value(recommendations.max_speechiness)) { - float_params["max_speechiness"] = recommendations.max_speechiness; - } - if (is_valid_value(recommendations.target_speechiness)) { - float_params["target_speechiness"] = recommendations.target_speechiness; - } - - if (is_valid_value(recommendations.min_tempo)) { - float_params["min_tempo"] = recommendations.min_tempo; - } - if (is_valid_value(recommendations.max_tempo)) { - float_params["max_tempo"] = recommendations.max_tempo; - } - if (is_valid_value(recommendations.target_tempo)) { - float_params["target_tempo"] = recommendations.target_tempo; - } - - if (is_valid_value(recommendations.min_time_signature)) { - float_params["min_time_signature"] = recommendations.min_time_signature; - } - if (is_valid_value(recommendations.max_time_signature)) { - float_params["max_time_signature"] = recommendations.max_time_signature; - } - if (is_valid_value(recommendations.target_time_signature)) { - float_params["target_time_signature"] = recommendations.target_time_signature; - } - - if (is_valid_value(recommendations.min_valence)) { - float_params["min_valence"] = recommendations.min_valence; - } - if (is_valid_value(recommendations.max_valence)) { - float_params["max_valence"] = recommendations.max_valence; - } - if (is_valid_value(recommendations.target_valence)) { - float_params["target_valence"] = recommendations.target_valence; - } -} -void Spotify::populate_string_values(std::map& string_params, recommendations recommendations){ - if (is_valid_value(recommendations.seed_artists)) { - string_params["seed_artists"] = recommendations.seed_artists; - } - if (is_valid_value(recommendations.seed_genres)) { - string_params["seed_genres"] = recommendations.seed_genres; - } - if (is_valid_value(recommendations.seed_tracks)) { - string_params["seed_tracks"] = recommendations.seed_tracks; - } -} //Users response Spotify::get_current_user_profile() { char url[] = "https://api.spotify.com/v1/me"; @@ -1036,27 +1031,27 @@ response Spotify::get_followed_artists(String type, String after, int limit) { return RestApiGet(const_cast(url.c_str())); } -response Spotify::follow_artists_or_users(String type, char** artist_user_ids) { +response Spotify::follow_artists_or_users(String type,int size, char** artist_user_ids) { String url = "https://api.spotify.com/v1/me/following"; url += "?type=" + type; - return RestApiPut(const_cast(url.c_str()), array_to_json_array(artist_user_ids)); + return RestApiPut(const_cast(url.c_str()), array_to_json_array(size, artist_user_ids)); } -response Spotify::unfollow_artists_or_users(String type, char** artist_user_ids) { +response Spotify::unfollow_artists_or_users(String type,int size, char** artist_user_ids) { String url = "https://api.spotify.com/v1/me/following"; url += "?type=" + type; - return RestApiDelete(const_cast(url.c_str()), array_to_json_array(artist_user_ids)); + return RestApiDelete(const_cast(url.c_str()), array_to_json_array(size, artist_user_ids)); } -response Spotify::check_if_user_follows_artists_or_users(String type, char** artist_user_ids) { +response Spotify::check_if_user_follows_artists_or_users(String type,int size, char** artist_user_ids) { String url = "https://api.spotify.com/v1/me/following/contains"; - url += "?type=" + type + "&ids=" + array_to_string(artist_user_ids); + url += "?type=" + type + "&ids=" + array_to_string(size, artist_user_ids); return RestApiGet(const_cast(url.c_str())); } -response Spotify::check_if_users_follow_playlist(String playlist_id, char** user_ids) { +response Spotify::check_if_users_follow_playlist(String playlist_id,int size, char** user_ids) { String url = "https://api.spotify.com/v1/playlists/"; - url += playlist_id + "/followers/contains?ids=" + array_to_string(user_ids); + url += playlist_id + "/followers/contains?ids=" + array_to_string(size, user_ids); return RestApiGet(const_cast(url.c_str())); } @@ -1136,6 +1131,6 @@ String Spotify::convert_id_to_uri(String id, String type){ void print_response(response response_obj){ Serial.print("Status: "); Serial.println(response_obj.status_code); - /*Serial.print("Reply: "); - Serial.println(response_obj.reply);*/ + Serial.print("Reply: "); + Serial.println(response_obj.reply); } \ No newline at end of file diff --git a/SpotifyESP32.h b/SpotifyESP32.h index 8bdde3e..d0536c3 100644 --- a/SpotifyESP32.h +++ b/SpotifyESP32.h @@ -34,9 +34,12 @@ typedef struct{ } response; struct recommendations { - String seed_artists; - String seed_genres; - String seed_tracks; + char** seed_artists; + int seed_artists_size = 0; + char** seed_genres; + int seed_genres_size = 0; + char** seed_tracks; + int seed_tracks_size = 0; float min_acousticness = -1.0; float max_acousticness = -1.0; float target_acousticness = -1.0; @@ -106,40 +109,40 @@ class Spotify { response add_to_queue(String context_uri); //Albums response get_album(String album_id); - response get_albums(char** album_ids); + response get_albums(int size, char** album_ids); response get_album_tracks(String album_id, int limit = 10, int offset = 0); response get_users_saved_albums(int limit = 10, int offset = 0); - response save_albums_for_current_user(char** album_ids); - response remove_users_saved_albums(char** album_ids); - response check_useres_saved_albums(char** album_ids); + response save_albums_for_current_user(int size, char** album_ids); + response remove_users_saved_albums(int size, char** album_ids); + response check_useres_saved_albums(int size, char** album_ids); response get_new_releases(String country, int limit = 10, int offset = 0); //Artists response get_artist(String artist_id); - response get_several_artists(char** artist_ids); + response get_several_artists(int size, char** artist_ids); response get_artist_albums(String artist_id, String include_groups, int limit = 10, int offset = 0); response get_artist_top_tracks(String artist_id, String country); response get_artist_related_artist(String artist_id); //Audiobooks (Only Available in US, UK, Canada, Ireland, New Zealand and Australia) response get_audiobook(String audiobook_id); - response get_several_audiobooks(char** audiobook_ids); + response get_several_audiobooks(int size, char** audiobook_ids); response get_audiobook_chapters(String audiobook_id, int limit = 10, int offset = 0); response get_users_audiobooks(int limit = 10, int offset = 0); - response save_audiobooks_for_current_user(char** audiobook_ids); - response remove_audiobooks_for_current_user(char** audiobook_ids); - response check_users_saved_audiobooks(char** audiobook_ids); + response save_audiobooks_for_current_user(int size, char** audiobook_ids); + response remove_audiobooks_for_current_user(int size, char** audiobook_ids); + response check_users_saved_audiobooks(int size, char** audiobook_ids); //Categories response get_several_browse_categories(String country, String locale, int limit = 10, int offset = 0); response get_single_browse_category(String category_id, String country, String locale); //Chapters (Only Available in US, UK, Canada, Ireland, New Zealand and Australia) response get_chapter(String chapter_id); - response get_several_chapters(char** chapter_ids); + response get_several_chapters(int size, char** chapter_ids); //Episodes response get_episode(String episode_id); - response get_several_episodes(char** episode_ids); + response get_several_episodes(int size, char** episode_ids); response get_users_saved_episodes(int limit = 10, int offset = 0); - response save_episodes_for_current_user(char** episode_ids); - response remove_episodes_for_current_user(char** episode_ids); - response check_users_saved_episodes(char** episode_ids); + response save_episodes_for_current_user(int size, char** episode_ids); + response remove_episodes_for_current_user(int size, char** episode_ids); + response check_users_saved_episodes(int size, char** episode_ids); //Genres response get_available_genre_seeds(); //Markets @@ -148,9 +151,9 @@ class Spotify { response get_playlist(String playlist_id, String fields); response change_playlist_details(String playlist_id, String name, bool is_public, bool is_collaborative, String description); response get_playlist_items(String playlist_id, String fields, int limit = 10, int offset = 0); - response update_playlist_items(String playlist_id, char** uris = nullptr, int range_start = 0, int insert_before = 0, int range_length = 1); - response add_items_to_playlist(String playlist_id, char** uris, int position = 0); - response remove_playlist_items(String playlist_id, char** uris); + response update_playlist_items(String playlist_id, int size, char** uris = nullptr, int range_length = 1, int range_start = 0, int insert_before = 1 ); + response add_items_to_playlist(String playlist_id, int size, char** uris, int position = 0); + response remove_playlist_items(String playlist_id, int size, char** uris); response get_current_users_playlists(int limit = 10, int offset = 0); response get_user_playlists(String user_id,int limit = 10, int offset = 0); response create_playlist(String user_id, String name, bool is_public, bool is_collaborative, String description); @@ -162,27 +165,24 @@ class Spotify { response search(String q, String type, int limit = 10, int offset = 0); //Shows response get_show(String show_id); - response get_several_shows(char** show_ids); + response get_several_shows(int size, char** show_ids); response get_show_episodes(String show_id, int limit = 10, int offset = 0); response get_users_saved_shows(int limit = 10, int offset = 0); - response save_shows_for_current_user(char** show_ids); - response remove_shows_for_current_user(char** show_ids); - response check_users_saved_shows(char** show_ids); + response save_shows_for_current_user(int size, char** show_ids); + response remove_shows_for_current_user(int size, char** show_ids); + response check_users_saved_shows(int size, char** show_ids); //Tracks response get_track(String track_id); - response get_several_tracks(char** track_ids); + response get_several_tracks(int size, char** track_ids); response get_user_saved_tracks(int limit = 10, int offset = 0); - response save_tracks_for_current_user(char** track_ids); - response remove_user_saved_tracks(char** track_ids); - response check_user_saved_tracks(char** track_ids); - response get_tracks_audio_features(char** track_ids); + response save_tracks_for_current_user(int size, char** track_ids); + response remove_user_saved_tracks(int size, char** track_ids); + response check_user_saved_tracks(int size, char** track_ids); + response get_tracks_audio_features(int size, char** track_ids); response get_track_audio_features(String track_id); response get_track_audio_analysis(String track_id); - response get_recommendations(int limit = 10, recommendations recommendations); - bool is_valid_value(float param); - bool is_valid_value(const String& param); - void populate_string_values(std::map& map, recommendations recommendations); - void populate_float_values(std::map& map, recommendations recommendations); + response get_recommendations(recommendations& recom, int limit = 10); + //Users response get_current_user_profile(); response get_user_top_items(String type, String time_range = "medium_term", int limit = 10, int offset = 0); @@ -190,10 +190,10 @@ class Spotify { response follow_playlist(String playlist_id, bool is_public); response unfollow_playlist(String playlist_id); response get_followed_artists(String after, String type = "artist", int limit = 10); - response follow_artists_or_users(String type, char** artist_user_ids); - response unfollow_artists_or_users(String type, char** artist_user_ids); - response check_if_user_follows_artists_or_users(String type, char** artist_user_ids); - response check_if_users_follow_playlist(String playlist_id, char** user_ids); + response follow_artists_or_users(String type, int size, char** artist_user_ids); + response unfollow_artists_or_users(String type, int size, char** artist_user_ids); + response check_if_user_follows_artists_or_users(String type, int size, char** artist_user_ids); + response check_if_users_follow_playlist(String playlist_id, int size, char** user_ids); //Simplified versions of the above String current_track_name(); @@ -213,8 +213,12 @@ class Spotify { response RestApiPut(char rest_url[100], String payload = ""); response RestApiPost(char rest_url[100], String payload = ""); response RestApiDelete(char rest_url[100], String payload = ""); - String array_to_string(char** array); - String array_to_json_array(char**); + String array_to_string(int size, char** array); + String array_to_json_array(int size, char** array); + bool is_valid_value(float param); + bool is_valid_value(int param); + void populate_char_values(std::map& map, recommendations& recom); + void populate_float_values(std::map& map, recommendations& recom); char* _refresh_token; char* _redirect_uri; char* _client_id; diff --git a/Spotify_Esp32_test.ino b/Spotify_Esp32_test.ino index 32bf75f..a8eb3cc 100644 --- a/Spotify_Esp32_test.ino +++ b/Spotify_Esp32_test.ino @@ -15,16 +15,17 @@ char* refresh_token="AQAichrGMfjjDHYWIAENJYyWoi_KpzLZ93_HSS30J8zULeuHRPmF9-Wh3aS using namespace Spotify_types; -Spotify sp(refresh_token, redirect_uri, client_id, client_secret, true); +Spotify sp(refresh_token, redirect_uri, client_id, client_secret, false); + String song_id = "2NTCi4wGypj56t843jb3Mt"; String track_id = "2NTCi4wGypj56t843jb3Mt"; char* track_ids[] = {"7ouMYWpwJ422jRcDASZB7P","4VqPOruhp5EdPBeR92t6lQ","2takcwOaAZWiXQijPHIx7B"}; String album_id = "7iLuHJkrb9KHPkMgddYigh"; -char* album_ids[] = {"7iLuHJkrb9KHPkMgddYigh","4KAtLRVIfB0bKnRY01dveY","2SxoeF005n621Jca66RRdu", nullptr}; +char* album_ids[] = {"7iLuHJkrb9KHPkMgddYigh","4KAtLRVIfB0bKnRY01dveY","2SxoeF005n621Jca66RRdu"}; String artist_id = "7orlzf5LTqSnCzURkZFebN"; -char* artist_ids[] = {"7orlzf5LTqSnCzURkZFebN","0lNJF6sbrXXPubqKkkyK23","3JsMj0DEzyWc0VDlHuy9Bx",nullptr}; +char* artist_ids[] = {"7orlzf5LTqSnCzURkZFebN","0lNJF6sbrXXPubqKkkyK23","3JsMj0DEzyWc0VDlHuy9Bx"}; String audiobook_id = "7iHfbu1YPACw6oZPAFJtqe"; char* audiobook_ids[] = {"7iHfbu1YPACw6oZPAFJtqe","1HGw3J3NxZO1TP1BTtVhpZ","7iHfbu1YPACw6oZPAFJtqe"}; @@ -52,16 +53,7 @@ char* artist_user_ids[] = {"2CIMQHirSU0MQqyYHq0eOx","57dN52uHvrHOxijzpIgu3E","1v void setup() { Serial.begin(115200); connectToWifi(); - //Create a list of track uris from track ids- - char* track_uri[] = {strdup(sp.convert_id_to_uri(song_id, TYPE_TRACK).c_str())}; - Serial.println(track_uri[0]); - Serial.println("update_playlist_items: "); - print_response(sp.update_playlist_items(playlist_id, track_uri)); - - test_playlist(); - test_shows(); - test_tracks(); - test_users(); + } void loop() { // put your main code here, to run repeatedly: @@ -135,7 +127,7 @@ void test_albums(){ print_response(sp.get_album(album_id)); Serial.print("Get Albums: "); - print_response(sp.get_albums(album_ids)); + print_response(sp.get_albums(3,album_ids)); Serial.print("Get Album Tracks: "); print_response(sp.get_album_tracks(album_id, 1, 0)); @@ -144,13 +136,13 @@ void test_albums(){ print_response(sp.get_users_saved_albums(1, 0)); Serial.print("Remove Saved Albums: "); - print_response(sp.remove_users_saved_albums(album_ids)); + print_response(sp.remove_users_saved_albums(3,album_ids)); Serial.print("Save Albums: "); - print_response(sp.save_albums_for_current_user(album_ids)); + print_response(sp.save_albums_for_current_user(3,album_ids)); Serial.print("Check if Album Saved: "); - print_response(sp.check_useres_saved_albums(album_ids)); + print_response(sp.check_useres_saved_albums(3,album_ids)); Serial.print("Get Releases: "); print_response(sp.get_new_releases("CH", 1, 0)); @@ -160,7 +152,7 @@ void test_artist(){ Serial.println("Get Artist: "); print_response(sp.get_artist(artist_id)); Serial.println("Get Artists: "); - print_response(sp.get_several_artists(artist_ids)); + print_response(sp.get_several_artists(3,artist_ids)); Serial.println("Get Artist Albums: "); print_response(sp.get_artist_albums(artist_id, GROUP_ALBUM +","+GROUP_SINGLE, 1, 0)); Serial.println("Get Artist top Tracks: "); @@ -174,7 +166,7 @@ void test_audiobooks(){ Serial.println("Get Audibook: "); print_response(sp.get_audiobook(audiobook_id)); Serial.println("Get Audibooks: "); - print_response(sp.get_several_audiobooks(audiobook_ids)); + print_response(sp.get_several_audiobooks(3,audiobook_ids)); //500 Serial.println("Get Audibook Chapters: "); print_response(sp.get_audiobook_chapters(audiobook_id, 1, 0)); @@ -182,19 +174,18 @@ void test_audiobooks(){ Serial.println("Get Users Audibooks: "); print_response(sp.get_users_audiobooks(0, 1)); Serial.println("Save Audibook for user: "); - print_response(sp.save_audiobooks_for_current_user(audiobook_ids)); + print_response(sp.save_audiobooks_for_current_user(3,audiobook_ids)); Serial.println("Check audiobooks: "); - print_response(sp.check_users_saved_audiobooks(audiobook_ids)); + print_response(sp.check_users_saved_audiobooks(3,audiobook_ids)); Serial.println("Remove Audiobooks: "); - print_response(sp.remove_audiobooks_for_current_user(audiobook_ids)); + print_response(sp.remove_audiobooks_for_current_user(3,audiobook_ids)); } //Working, only available in US, UK, New Zealand, Australia void test_chapters(){ - //404 Serial.print("get chapter: "); print_response(sp.get_chapter(chapter_id)); Serial.print("get chapter: "); - print_response(sp.get_several_chapters(chapter_ids)); + print_response(sp.get_several_chapters(3,chapter_ids)); } //working void test_categories(){ @@ -210,19 +201,19 @@ void test_episodes(){ print_response(sp.get_episode(episode_id)); Serial.println("get_several_episodes: "); - print_response(sp.get_several_episodes(episode_ids)); + print_response(sp.get_several_episodes(2,episode_ids)); Serial.println("get_users_saved_episodes: "); print_response(sp.get_users_saved_episodes()); Serial.println("save_episodes_for_current_user: "); - print_response(sp.save_episodes_for_current_user(episode_ids)); + print_response(sp.save_episodes_for_current_user(2,episode_ids)); Serial.println("check_users_saved_episodes: "); - print_response(sp.check_users_saved_episodes(episode_ids)); + print_response(sp.check_users_saved_episodes(2,episode_ids)); Serial.println("remove_episodes_for_current_user: "); - print_response(sp.remove_episodes_for_current_user(episode_ids)); + print_response(sp.remove_episodes_for_current_user(2,episode_ids)); } @@ -249,12 +240,15 @@ void test_playlist(){ String track_uri_str = sp.convert_id_to_uri(song_id, TYPE_TRACK); char* track_uri[] = {strdup(track_uri_str.c_str())}; + Serial.println("update_playlist_items: "); - print_response(sp.update_playlist_items(playlist_id, track_uri )); + print_response(sp.update_playlist_items(playlist_id,1, track_uri)); + Serial.println("add_items_to_playlist: "); - print_response(sp.add_items_to_playlist(playlist_id, track_uri, 1)); + print_response(sp.add_items_to_playlist(playlist_id,1, track_uri, 1)); + Serial.println("remove_playlist_items: "); - print_response(sp.remove_playlist_items(playlist_id, track_uri)); + print_response(sp.remove_playlist_items(playlist_id,1, track_uri)); Serial.println("get_current_users_playlists: "); print_response(sp.get_current_users_playlists()); @@ -264,7 +258,7 @@ void test_playlist(){ Serial.println("create_playlist: "); print_response(sp.create_playlist(user_id, "Test", true, false, "no")); - + Serial.println("get_featured_playlists: "); print_response(sp.get_featured_playlists(country, locale, "")); @@ -289,7 +283,7 @@ void test_shows(){ print_response(sp.get_show(show_id)); Serial.println("get_several_shows: "); - print_response(sp.get_several_shows(show_ids)); + print_response(sp.get_several_shows(2,show_ids)); Serial.println("get_show_episodes: "); print_response(sp.get_show_episodes(show_id)); @@ -298,13 +292,13 @@ void test_shows(){ print_response(sp.get_users_saved_shows()); Serial.println("save_shows_for_current_user: "); - print_response(sp.save_shows_for_current_user(show_ids)); + print_response(sp.save_shows_for_current_user(2,show_ids)); Serial.println("check_users_saved_shows: "); - print_response(sp.check_users_saved_shows(show_ids)); + print_response(sp.check_users_saved_shows(2,show_ids)); Serial.println("remove_shows_for_current_user: "); - print_response(sp.remove_shows_for_current_user(show_ids)); + print_response(sp.remove_shows_for_current_user(2,show_ids)); } @@ -314,28 +308,35 @@ void test_tracks(){ print_response(sp.get_track(track_id)); Serial.println("get_several_tracks: "); - print_response(sp.get_several_tracks(track_ids)); + print_response(sp.get_several_tracks(3,track_ids)); Serial.println("get_user_saved_tracks: "); print_response(sp.get_user_saved_tracks()); Serial.println("save_tracks_for_current_user: "); - print_response(sp.save_tracks_for_current_user(track_ids)); + print_response(sp.save_tracks_for_current_user(3,track_ids)); Serial.println("remove_user_saved_tracks: "); - print_response(sp.remove_user_saved_tracks(track_ids)); + print_response(sp.remove_user_saved_tracks(3,track_ids)); Serial.println("check_user_saved_tracks: "); - print_response(sp.check_user_saved_tracks(track_ids)); + print_response(sp.check_user_saved_tracks(3,track_ids)); Serial.println("get_tracks_audio_features: "); - print_response(sp.get_tracks_audio_features(track_ids)); + print_response(sp.get_tracks_audio_features(3,track_ids)); Serial.println("get_track_audio_features: "); print_response(sp.get_track_audio_features(track_id)); Serial.println("get_track_audio_analysis: "); print_response(sp.get_track_audio_analysis(track_id)); + char* track_seeds[] = {strdup(track_id.c_str())}; + recommendations recom; + recom.seed_tracks = track_seeds; + recom.seed_tracks_size = 1; + recom.max_energy = 0.5; + Serial.println("get_recommendations: "); + print_response(sp.get_recommendations(recom, 1)); } //working void test_users(){ @@ -358,16 +359,16 @@ void test_users(){ print_response(sp.get_followed_artists("")); Serial.println("follow_artists_or_users: "); - print_response(sp.follow_artists_or_users(TYPE_ARTIST, artist_user_ids)); + print_response(sp.follow_artists_or_users(TYPE_ARTIST, 3,artist_user_ids)); Serial.println("unfollow_artists_or_users: "); - print_response(sp.unfollow_artists_or_users(TYPE_ARTIST, artist_user_ids)); + print_response(sp.unfollow_artists_or_users(TYPE_ARTIST, 3,artist_user_ids)); Serial.println("check_if_user_follows_artists_or_users: "); - print_response(sp.check_if_user_follows_artists_or_users(TYPE_ARTIST, artist_user_ids)); + print_response(sp.check_if_user_follows_artists_or_users(TYPE_ARTIST,3, artist_user_ids)); Serial.println("check_if_users_follow_playlist: "); - print_response(sp.check_if_users_follow_playlist(playlist_id, user_ids)); + print_response(sp.check_if_users_follow_playlist(playlist_id, 3, user_ids)); }