diff --git a/springSpotifyPlayList/backend/SpotifyPlayList/src/test/java/com/yen/SpotifyPlayList/GetSongFeatureTest.java b/springSpotifyPlayList/backend/SpotifyPlayList/src/test/java/com/yen/SpotifyPlayList/GetSongFeatureTest.java deleted file mode 100644 index 55179185b..000000000 --- a/springSpotifyPlayList/backend/SpotifyPlayList/src/test/java/com/yen/SpotifyPlayList/GetSongFeatureTest.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.yen.SpotifyPlayList; - -// https://github.com/spotify-web-api-java/spotify-web-api-java/blob/master/examples/data/tracks/GetAudioFeaturesForTrackExample.java - -import org.junit.jupiter.api.Test; -import se.michaelthelin.spotify.SpotifyApi; -import se.michaelthelin.spotify.exceptions.SpotifyWebApiException; -import se.michaelthelin.spotify.model_objects.specification.AudioFeatures; -import se.michaelthelin.spotify.requests.data.tracks.GetAudioFeaturesForTrackRequest; - -import java.io.IOException; -import java.text.ParseException; - -public class GetSongFeatureTest { - - final String accessToken = "BQCVX_9iFkOgTNsyrl2l3Xw7BczyFbmugxUUC5Lm0nhTyw02D9RNKzNixqbiEv7o8YK8N7VYH3MyUutbT_uHY6aFb5cAnsolKzOtjP2hgGbjEaMGGPE"; - String trackId = "6oHUMXEKpnXOrs13VoGsuF"; - - @Test - public void test_getSong_feature() { - - try { - final SpotifyApi spotifyApi = new SpotifyApi.Builder() - .setAccessToken(accessToken) - .build(); - final GetAudioFeaturesForTrackRequest getAudioFeaturesForTrackRequest = spotifyApi - .getAudioFeaturesForTrack(trackId) - .build(); - - final AudioFeatures audioFeatures = getAudioFeaturesForTrackRequest.execute(); - - System.out.println(">>> ID: " + audioFeatures.getId()); - - /** - * example output: - * - * - * >>> audioFeatures: AudioFeatures(acousticness=0.443, analysisUrl=https://api.spotify.com/v1/audio-analysis/6oHUMXEKpnXOrs13VoGsuF, danceability=0.658, durationMs=225387, energy=0.86, id=6oHUMXEKpnXOrs13VoGsuF, instrumentalness=0.0, key=5, liveness=0.304, loudness=-2.898, mode=MINOR, speechiness=0.0272, tempo=88.032, timeSignature=4, trackHref=https://api.spotify.com/v1/tracks/6oHUMXEKpnXOrs13VoGsuF, type=AUDIO_FEATURES, uri=spotify:track:6oHUMXEKpnXOrs13VoGsuF, valence=0.644) - * - * - */ - System.out.println(">>> audioFeatures: " + audioFeatures); - - } catch (IOException | SpotifyWebApiException e) { - System.out.println("Error: " + e.getMessage()); - } catch (org.apache.hc.core5.http.ParseException e) { - throw new RuntimeException(e); - } - - } -} diff --git a/springSpotifyPlayList/backend/SpotifyPlayList/src/test/java/com/yen/SpotifyPlayList/SongFeatureTest.java b/springSpotifyPlayList/backend/SpotifyPlayList/src/test/java/com/yen/SpotifyPlayList/SongFeatureTest.java new file mode 100644 index 000000000..30b5fa760 --- /dev/null +++ b/springSpotifyPlayList/backend/SpotifyPlayList/src/test/java/com/yen/SpotifyPlayList/SongFeatureTest.java @@ -0,0 +1,105 @@ +package com.yen.SpotifyPlayList; + +import org.junit.jupiter.api.Test; +import se.michaelthelin.spotify.SpotifyApi; +import se.michaelthelin.spotify.exceptions.SpotifyWebApiException; +import se.michaelthelin.spotify.model_objects.specification.*; +import se.michaelthelin.spotify.requests.data.playlists.GetPlaylistsItemsRequest; +import se.michaelthelin.spotify.requests.data.tracks.GetAudioFeaturesForTrackRequest; + +import java.io.IOException; +import java.util.concurrent.CompletableFuture; + +public class SongFeatureTest { + + final String accessToken = "BQCVX_9iFkOgTNsyrl2l3Xw7BczyFbmugxUUC5Lm0nhTyw02D9RNKzNixqbiEv7o8YK8N7VYH3MyUutbT_uHY6aFb5cAnsolKzOtjP2hgGbjEaMGGPE"; + String trackId = "6oHUMXEKpnXOrs13VoGsuF"; + + String playListId = "6fbBvdE95blBGc3ekFMzh1"; + + // // https://github.com/spotify-web-api-java/spotify-web-api-java/blob/master/examples/data/tracks/GetAudioFeaturesForTrackExample.java + @Test + public void test_getSong_feature() { + + try { + final SpotifyApi spotifyApi = new SpotifyApi.Builder() + .setAccessToken(accessToken) + .build(); + final GetAudioFeaturesForTrackRequest getAudioFeaturesForTrackRequest = spotifyApi + .getAudioFeaturesForTrack(trackId) + .build(); + + final AudioFeatures audioFeatures = getAudioFeaturesForTrackRequest.execute(); + + System.out.println(">>> ID: " + audioFeatures.getId()); + + /** + * example output: + * + * >>> audioFeatures: AudioFeatures(acousticness=0.443, analysisUrl=https://api.spotify.com/v1/audio-analysis/6oHUMXEKpnXOrs13VoGsuF, danceability=0.658, durationMs=225387, energy=0.86, id=6oHUMXEKpnXOrs13VoGsuF, instrumentalness=0.0, key=5, liveness=0.304, loudness=-2.898, mode=MINOR, speechiness=0.0272, tempo=88.032, timeSignature=4, trackHref=https://api.spotify.com/v1/tracks/6oHUMXEKpnXOrs13VoGsuF, type=AUDIO_FEATURES, uri=spotify:track:6oHUMXEKpnXOrs13VoGsuF, valence=0.644) + */ + System.out.println(">>> audioFeatures: " + audioFeatures); + + } catch (IOException | SpotifyWebApiException e) { + System.out.println("Error: " + e.getMessage()); + } catch (org.apache.hc.core5.http.ParseException e) { + throw new RuntimeException(e); + } + + } + + // https://github.com/spotify-web-api-java/spotify-web-api-java/blob/master/examples/data/playlists/GetPlaylistsItemsExample.java + + @Test + public void test_getSongFeatureList_from_playList(){ + + try { + final SpotifyApi spotifyApi = new SpotifyApi.Builder() + .setAccessToken(accessToken) + .build(); + + final GetPlaylistsItemsRequest getPlaylistsItemsRequest = spotifyApi + .getPlaylistsItems(playListId) +// .fields("description") +// .limit(10) +// .offset(0) +// .market(CountryCode.SE) +// .additionalTypes("track,episode") + .build(); + + + final CompletableFuture> pagingFuture = getPlaylistsItemsRequest.executeAsync(); + + // Thread free to do other tasks... + + // Example Only. Never block in production code. + final Paging playlistTrackPaging = pagingFuture.join(); + + System.out.println("Total: " + playlistTrackPaging.getTotal()); + //System.out.println("Track's first artist: " + ((Track) playlistTrackPaging.getItems()[0].getTrack()).getArtists()[0]); + //System.out.println("Episode's show: " + ((Episode) playlistTrackPaging.getItems()[0].getTrack()).getShow()); + + + PlaylistTrack[] playlistTracks = playlistTrackPaging.getItems(); + for (PlaylistTrack track: playlistTracks){ + + String songId = track.getTrack().getId(); + //System.out.println(">>> name = " + track.getTrack().getName() + ", id = " + songId); + // get song feature + final GetAudioFeaturesForTrackRequest getAudioFeaturesForTrackRequest = spotifyApi + .getAudioFeaturesForTrack(songId) + .build(); + + final AudioFeatures audioFeatures = getAudioFeaturesForTrackRequest.execute(); + + System.out.println(">>> name = " + track.getTrack().getName() + ", id = " + songId + ", audioFeatures = " + audioFeatures); + + } + + } catch (Exception e) { + System.out.println("Error: " + e.getMessage()); + } + + } + +}