Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,48 @@
@Slf4j
@RestController
@RequestMapping("/recommend")
@CrossOrigin(origins = "*")
public class RecommendationsController {

@Autowired
private RecommendationsService recommendationsService;

@PostMapping("/")
public ResponseEntity getRecommendation(@RequestBody GetRecommendationsDto getRecommendationsDto) {
@PostMapping
public ResponseEntity<Recommendations> getRecommendation(@RequestBody(required = false) GetRecommendationsDto getRecommendationsDto) {
try {
log.info("(getRecommendation) getRecommendationsDto = " + getRecommendationsDto.toString());
log.info("Getting recommendations with DTO: {}", getRecommendationsDto);

// If no DTO provided or no seeds set, use default values
if (getRecommendationsDto == null ||
(getRecommendationsDto.getSeedArtistId() == null &&
getRecommendationsDto.getSeedTrack() == null &&
getRecommendationsDto.getSeedGenres() == null)) {

getRecommendationsDto = new GetRecommendationsDto();
getRecommendationsDto.setAmount(10);
// Using Tame Impala as default seed artist
getRecommendationsDto.setSeedArtistId("4NHQUGzhtTLFvgF5SZesLK");
// Adding a default genre as well for better recommendations
getRecommendationsDto.setSeedGenres("alternative,indie");
}

Recommendations recommendations = recommendationsService.getRecommendation(getRecommendationsDto);
return ResponseEntity.status(HttpStatus.OK).body(recommendations);
return ResponseEntity.ok(recommendations);
} catch (Exception e) {
log.error("getRecommendation error : " + e);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
log.error("Error getting recommendations: ", e);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
}
}

@GetMapping("/playlist/{playListId}")
public ResponseEntity getRecommendationWithPlayList(@PathVariable("playListId") String playListId) {
public ResponseEntity<Recommendations> getRecommendationWithPlayList(@PathVariable("playListId") String playListId) {
try {
log.info("(getRecommendationWithPlayList) playListId = " + playListId);
log.info("Getting recommendations for playlist: {}", playListId);
Recommendations recommendations = recommendationsService.getRecommendationWithPlayList(playListId);
return ResponseEntity.status(HttpStatus.OK).body(recommendations);
return ResponseEntity.ok(recommendations);
} catch (Exception e) {
log.error("getRecommendationWithPlayList error : " + e);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
log.error("Error getting recommendations for playlist: ", e);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.yen.SpotifyPlayList.model;

import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class AudioFeatureAverages {
private double energy;
private double acousticness;
private double danceability;
private double instrumentalness;
private double liveness;
private double valence;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.yen.SpotifyPlayList.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import se.michaelthelin.spotify.model_objects.specification.Track;

import java.util.List;

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class SpotifyRecommendationsResponse {
private List<Track> tracks;
private List<RecommendationSeed> seeds;

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public static class RecommendationSeed {
private Integer afterFilteringSize;
private Integer afterRelinkingSize;
private String href;
private String id;
private Integer initialPoolSize;
private String type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.neovisionaries.i18n.CountryCode;
import lombok.Data;
import lombok.ToString;
import lombok.Builder;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

/**
* { acousticness: 0.359, analysisUrl:
Expand All @@ -12,22 +14,22 @@
* timeSignature: 4, trackHref: "https://api.spotify.com/v1/tracks/7FJC2pF6zMliU7Lvk0GBDV", type:
* "AUDIO_FEATURES", uri: "spotify:track:7FJC2pF6zMliU7Lvk0GBDV", valence: 0.336 },
*/
@ToString
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
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 double danceability = 0;
private double energy = 0;
private double instrumentalness = 0;
private double liveness = 0;
private double loudness = 0;
private double speechiness = 0;
private double tempo = 0;
private String seedArtistIds; // Multiple artists, comma-separated
private String seedTracks; // Multiple tracks, comma-separated
private float danceability = 0.5f;
private float energy = 0.5f;
private float instrumentalness = 0.0f;
private float liveness = 0.0f;
private float acousticness = 0.5f;
private float valence = 0.5f;
private float tempo = 120.0f;
}
Loading