Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Oct 6, 2024
1 parent f9d2892 commit d9e4800
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.yen.SpotifyPlayList.model.dto;

import com.neovisionaries.i18n.CountryCode;
import lombok.Data;
import lombok.ToString;

/**
*
* {
* acousticness: 0.359,
* analysisUrl: "https://api.spotify.com/v1/audio-analysis/7FJC2pF6zMliU7Lvk0GBDV",
* danceability: 0.337,
* durationMs: 358587,
* energy: 0.532,
* id: "7FJC2pF6zMliU7Lvk0GBDV",
* instrumentalness: 0,
* key: 6,
* liveness: 0.0827,
* loudness: -6.296,
* mode: "MAJOR",
* speechiness: 0.0345,
* tempo: 140.257,
* timeSignature: 4,
* trackHref: "https://api.spotify.com/v1/tracks/7FJC2pF6zMliU7Lvk0GBDV",
* type: "AUDIO_FEATURES",
* uri: "spotify:track:7FJC2pF6zMliU7Lvk0GBDV",
* valence: 0.336
* },
*
*/

@ToString
@Data
public class GetRecommendationsWithFeatureDto {

private int amount = 10;
private CountryCode market = CountryCode.JP;
private int maxPopularity = 100;
private int minPopularity = 0;
private String seedArtistId; // e.g. : 0LcJLqbBmaGUft1e9Mm8HV
private String seedGenres;
private String seedTrack; // e.g. 01iyCAUm8EvOFqVWYJ3dVX
private int targetPopularity = 50;
private float danceability = 0;
private float energy = 0;
private float instrumentalness = 0;
private float liveness = 0;
private float loudness = 0;
private float speechiness = 0;
private float tempo = 0;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.yen.SpotifyPlayList.service;

import com.yen.SpotifyPlayList.model.dto.GetRecommendationsDto;
import com.yen.SpotifyPlayList.model.dto.GetRecommendationsWithFeatureDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.hc.core5.http.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -47,7 +48,7 @@ public Recommendations getRecommendation(GetRecommendationsDto getRecommendation

public Recommendations getRecommendationWithPlayList(String playListId) throws SpotifyWebApiException {

GetRecommendationsDto getRecommendationsDto = new GetRecommendationsDto();
GetRecommendationsWithFeatureDto getRecommendationsWithFeatureDto = new GetRecommendationsWithFeatureDto();
Recommendations recommendations = null;

try{
Expand All @@ -59,24 +60,33 @@ public Recommendations getRecommendationWithPlayList(String playListId) throws S
// get avg value of features

// TODO : use functional way
Float energySum = null;
Float energy = null;
Float acousticness = null;
Float danceability = null;
Float liveness = null;
Float loudness = null;
Float speechiness = null;

for (AudioFeatures audioFeatures : audioFeaturesList){
energySum += audioFeatures.getEnergy();
energy += audioFeatures.getEnergy();
acousticness += audioFeatures.getAcousticness();
danceability += audioFeatures.getDanceability();
liveness += audioFeatures.getLiveness();
loudness += audioFeatures.getLoudness();
speechiness += audioFeatures.getSpeechiness();
// TODO : implement others
}

GetRecommendationsRequest recommendationsRequest = spotifyApi.getRecommendations().build();
return recommendationsRequest.execute();
// getRecommendationsWithFeatureDto
GetRecommendationsRequest getRecommendationsRequest = prepareRecommendationsWithSongFeatureRequest(getRecommendationsWithFeatureDto);
recommendations = getRecommendationsRequest.execute();
return recommendations;
}catch (Exception e){

}
return recommendations;
}

// TODO : merge below
private GetRecommendationsRequest prepareRecommendationsRequest(GetRecommendationsDto getRecommendationsDto){
return spotifyApi.getRecommendations()
.limit(getRecommendationsDto.getAmount())
Expand All @@ -90,4 +100,23 @@ private GetRecommendationsRequest prepareRecommendationsRequest(GetRecommendatio
.build();
}

private GetRecommendationsRequest prepareRecommendationsWithSongFeatureRequest(GetRecommendationsWithFeatureDto getRecommendationsWithFeatureDto){
return spotifyApi.getRecommendations()
.limit(getRecommendationsWithFeatureDto.getAmount())
.market(getRecommendationsWithFeatureDto.getMarket())
.max_popularity(getRecommendationsWithFeatureDto.getMaxPopularity())
.min_popularity(getRecommendationsWithFeatureDto.getMinPopularity())
.seed_artists(getRecommendationsWithFeatureDto.getSeedArtistId())
.seed_genres(getRecommendationsWithFeatureDto.getSeedGenres())
.seed_tracks(getRecommendationsWithFeatureDto.getSeedTrack())
.target_popularity(getRecommendationsWithFeatureDto.getTargetPopularity())
.target_danceability(getRecommendationsWithFeatureDto.getDanceability())
.target_energy(getRecommendationsWithFeatureDto.getEnergy())
.target_instrumentalness(getRecommendationsWithFeatureDto.getInstrumentalness())
.target_liveness(getRecommendationsWithFeatureDto.getLiveness())
.target_loudness(getRecommendationsWithFeatureDto.getLoudness())
.target_speechiness(getRecommendationsWithFeatureDto.getSpeechiness())
.build();
}

}

0 comments on commit d9e4800

Please sign in to comment.