diff --git a/src/main/java/play/pluv/PluvApplication.java b/src/main/java/play/pluv/PluvApplication.java index 239920a..dc4fe31 100644 --- a/src/main/java/play/pluv/PluvApplication.java +++ b/src/main/java/play/pluv/PluvApplication.java @@ -4,10 +4,12 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.ConfigurationPropertiesScan; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; +import org.springframework.scheduling.annotation.EnableAsync; @SpringBootApplication @EnableJpaAuditing(dateTimeProviderRef = "localDateTimeProvider") @ConfigurationPropertiesScan +@EnableAsync public class PluvApplication { public static void main(String[] args) { diff --git a/src/main/java/play/pluv/oauth/apple/AppleApiClient.java b/src/main/java/play/pluv/oauth/apple/AppleApiClient.java index 59d97c3..ed81043 100644 --- a/src/main/java/play/pluv/oauth/apple/AppleApiClient.java +++ b/src/main/java/play/pluv/oauth/apple/AppleApiClient.java @@ -1,6 +1,7 @@ package play.pluv.oauth.apple; import static org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE; +import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.PathVariable; @@ -11,7 +12,6 @@ import org.springframework.web.service.annotation.PostExchange; import play.pluv.oauth.apple.dto.AppleAddMusicRequest; import play.pluv.oauth.apple.dto.AppleCreatePlayListRequest; -import play.pluv.oauth.apple.dto.AppleCreatePlayListResponse; import play.pluv.oauth.apple.dto.AppleMusicSongs; import play.pluv.oauth.apple.dto.ApplePlayListMusicResponses; import play.pluv.oauth.apple.dto.ApplePlayListResponses; @@ -48,8 +48,8 @@ AppleMusicSongs searchMusicByIsrc( @RequestParam("filter[isrc]") final String isrc ); - @PostExchange(url = "https://api.music.apple.com/v1/me/library/playlists") - AppleCreatePlayListResponse createPlayList( + @PostExchange(url = "https://api.music.apple.com/v1/me/library/playlists", contentType = APPLICATION_JSON_VALUE) + String createPlayList( @RequestHeader("Authorization") final String developerToken, @RequestHeader("Music-User-Token") final String musicUserToken, @RequestBody final AppleCreatePlayListRequest request diff --git a/src/main/java/play/pluv/oauth/apple/AppleConnector.java b/src/main/java/play/pluv/oauth/apple/AppleConnector.java index 8f2c852..1855177 100644 --- a/src/main/java/play/pluv/oauth/apple/AppleConnector.java +++ b/src/main/java/play/pluv/oauth/apple/AppleConnector.java @@ -29,6 +29,7 @@ import play.pluv.music.domain.MusicId; import play.pluv.oauth.apple.dto.AppleAddMusicRequest; import play.pluv.oauth.apple.dto.AppleCreatePlayListRequest; +import play.pluv.oauth.apple.dto.ApplePlayListResponses; import play.pluv.oauth.apple.dto.AppleTokenResponse; import play.pluv.oauth.application.SocialLoginClient; import play.pluv.oauth.domain.OAuthMemberInfo; @@ -106,8 +107,15 @@ public List getMusics(final String playListId, final String music @Override public PlayListId createPlayList(final String musicUserToken, final String name) { final AppleCreatePlayListRequest request = AppleCreatePlayListRequest.from(name); - return appleApiClient.createPlayList(developerAuthorization, musicUserToken, request) - .getId(); + appleApiClient.createPlayList(developerAuthorization, musicUserToken, request); + try { + Thread.sleep(1000 * 90); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + final ApplePlayListResponses playLists = appleApiClient.getPlayList(developerAuthorization, + musicUserToken); + return playLists.recentPlayListIds(); } @Override diff --git a/src/main/java/play/pluv/oauth/apple/dto/ApplePlayListResponses.java b/src/main/java/play/pluv/oauth/apple/dto/ApplePlayListResponses.java index e8a596f..5c7f3a1 100644 --- a/src/main/java/play/pluv/oauth/apple/dto/ApplePlayListResponses.java +++ b/src/main/java/play/pluv/oauth/apple/dto/ApplePlayListResponses.java @@ -1,7 +1,9 @@ package play.pluv.oauth.apple.dto; +import static java.util.Comparator.comparing; import static play.pluv.playlist.domain.MusicStreaming.APPLE; +import java.time.LocalDateTime; import java.util.List; import java.util.Optional; import play.pluv.playlist.domain.PlayList; @@ -11,6 +13,14 @@ public record ApplePlayListResponses( List data ) { + public PlayListId recentPlayListIds() { + return data.stream() + .sorted(comparing(ApplePlayListResponse::recentUpdatedDate).reversed()) + .limit(1) + .map(ApplePlayListResponse::toPlayList) + .toList().get(0).getPlayListId(); + } + public List toPlayLists() { return data.stream() .map(ApplePlayListResponse::toPlayList) @@ -21,6 +31,10 @@ private record ApplePlayListResponse( String id, PlayListAttributes attributes ) { + private LocalDateTime recentUpdatedDate() { + return attributes.lastModifiedDate(); + } + private PlayList toPlayList() { return PlayList.builder() .playListId(new PlayListId(id, APPLE)) @@ -30,7 +44,7 @@ private PlayList toPlayList() { } private record PlayListAttributes( - Artwork artwork, String name + Artwork artwork, String name, LocalDateTime lastModifiedDate ) { public String artworkUrl() {