|
| 1 | +package com.back.web7_9_codecrete_be.domain.artists.service.spotify.application; |
| 2 | + |
| 3 | +import com.back.web7_9_codecrete_be.domain.artists.dto.response.AlbumResponse; |
| 4 | +import com.back.web7_9_codecrete_be.domain.artists.dto.response.SpotifyArtistDetailCache; |
| 5 | +import com.back.web7_9_codecrete_be.domain.artists.dto.response.TopTrackResponse; |
| 6 | +import com.back.web7_9_codecrete_be.domain.artists.service.spotify.rate_limit.SpotifyRateLimitHandler; |
| 7 | +import com.back.web7_9_codecrete_be.global.spotify.SpotifyClient; |
| 8 | +import com.neovisionaries.i18n.CountryCode; |
| 9 | +import lombok.RequiredArgsConstructor; |
| 10 | +import lombok.extern.slf4j.Slf4j; |
| 11 | +import org.springframework.stereotype.Service; |
| 12 | +import se.michaelthelin.spotify.SpotifyApi; |
| 13 | +import se.michaelthelin.spotify.enums.AlbumType; |
| 14 | +import se.michaelthelin.spotify.model_objects.specification.AlbumSimplified; |
| 15 | +import se.michaelthelin.spotify.model_objects.specification.Image; |
| 16 | +import se.michaelthelin.spotify.model_objects.specification.Paging; |
| 17 | +import se.michaelthelin.spotify.model_objects.specification.Track; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Objects; |
| 22 | +import java.util.concurrent.Semaphore; |
| 23 | +import java.util.stream.Stream; |
| 24 | + |
| 25 | +import static java.util.stream.Collectors.toList; |
| 26 | + |
| 27 | + |
| 28 | +@Slf4j |
| 29 | +@Service |
| 30 | +@RequiredArgsConstructor |
| 31 | +// Spotify API를 통한 아티스트 상세 정보 조회 서비스 : Spotify에서 아티스트 기본 정보, Top Tracks, 앨범 목록을 조회 |
| 32 | +public class SpotifyDetailService { |
| 33 | + |
| 34 | + private final SpotifyClient spotifyClient; |
| 35 | + private final SpotifyRateLimitHandler rateLimitHandler; |
| 36 | + |
| 37 | + private final Semaphore spotifyRateLimiter = new Semaphore(1); |
| 38 | + |
| 39 | + // Spotify API에서 아티스트 상세 정보 조회 |
| 40 | + public SpotifyArtistDetailCache fetchDetailFromApi(String spotifyArtistId) { |
| 41 | + SpotifyApi api = spotifyClient.getAuthorizedApi(); |
| 42 | + |
| 43 | + // 아티스트 기본 정보 |
| 44 | + se.michaelthelin.spotify.model_objects.specification.Artist artist = rateLimitHandler.callWithRateLimitRetry(() -> { |
| 45 | + try { |
| 46 | + spotifyRateLimiter.acquire(); |
| 47 | + return api.getArtist(spotifyArtistId).build().execute(); |
| 48 | + } catch (RuntimeException e) { |
| 49 | + throw e; |
| 50 | + } catch (Exception e) { |
| 51 | + throw new RuntimeException("Exception during getArtist API call", e); |
| 52 | + } |
| 53 | + }, "getArtistDetail getArtist spotifyId=" + spotifyArtistId); |
| 54 | + |
| 55 | + // Top Tracks |
| 56 | + Track[] topTracks = safeGetTopTracks(api, spotifyArtistId); |
| 57 | + |
| 58 | + // 앨범 목록 |
| 59 | + Paging<AlbumSimplified> albums = safeGetAlbums(api, spotifyArtistId); |
| 60 | + |
| 61 | + return new SpotifyArtistDetailCache( |
| 62 | + artist.getName(), |
| 63 | + pickImageUrl(artist.getImages()), |
| 64 | + artist.getPopularity(), |
| 65 | + toTopTrackResponses(topTracks), |
| 66 | + toAlbumResponses(albums != null ? albums.getItems() : null, spotifyArtistId), |
| 67 | + albums != null ? albums.getTotal() : 0 |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + private Track[] safeGetTopTracks(SpotifyApi api, String artistId) { |
| 72 | + try { |
| 73 | + return rateLimitHandler.callWithRateLimitRetry(() -> { |
| 74 | + try { |
| 75 | + spotifyRateLimiter.acquire(); |
| 76 | + return api.getArtistsTopTracks(artistId, CountryCode.KR) |
| 77 | + .build() |
| 78 | + .execute(); |
| 79 | + } catch (RuntimeException e) { |
| 80 | + throw e; |
| 81 | + } catch (Exception e) { |
| 82 | + throw new RuntimeException("Exception during getArtistsTopTracks API call", e); |
| 83 | + } |
| 84 | + }, "safeGetTopTracks artistId=" + artistId); |
| 85 | + } catch (RuntimeException e) { |
| 86 | + return new Track[0]; |
| 87 | + } catch (Exception e) { |
| 88 | + return new Track[0]; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private Paging<AlbumSimplified> safeGetAlbums(SpotifyApi api, String artistId) { |
| 93 | + try { |
| 94 | + return rateLimitHandler.callWithRateLimitRetry(() -> { |
| 95 | + try { |
| 96 | + spotifyRateLimiter.acquire(); |
| 97 | + return api.getArtistsAlbums(artistId) |
| 98 | + .market(CountryCode.KR) |
| 99 | + .limit(20) |
| 100 | + .build() |
| 101 | + .execute(); |
| 102 | + } catch (RuntimeException e) { |
| 103 | + throw e; |
| 104 | + } catch (Exception e) { |
| 105 | + throw new RuntimeException("Exception during getArtistsAlbums API call", e); |
| 106 | + } |
| 107 | + }, "safeGetAlbums artistId=" + artistId); |
| 108 | + } catch (RuntimeException e) { |
| 109 | + return null; |
| 110 | + } catch (Exception e) { |
| 111 | + return null; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public String pickImageUrl(Image[] images) { |
| 116 | + if (images == null || images.length == 0) return null; |
| 117 | + return Arrays.stream(images) |
| 118 | + .filter(Objects::nonNull) |
| 119 | + .findFirst() |
| 120 | + .map(Image::getUrl) |
| 121 | + .orElse(null); |
| 122 | + } |
| 123 | + |
| 124 | + private List<AlbumResponse> toAlbumResponses(AlbumSimplified[] items, String artistId) { |
| 125 | + if (items == null) return List.of(); |
| 126 | + return Stream.of(items) |
| 127 | + .filter(Objects::nonNull) |
| 128 | + .filter(a -> a.getAlbumType() == AlbumType.ALBUM |
| 129 | + || a.getAlbumType() == AlbumType.SINGLE |
| 130 | + || a.getAlbumType() == AlbumType.COMPILATION) |
| 131 | + .filter(a -> { |
| 132 | + if (a.getArtists() == null) return false; |
| 133 | + return Arrays.stream(a.getArtists()) |
| 134 | + .anyMatch(ar -> ar != null && artistId != null && artistId.equals(ar.getId())); |
| 135 | + }) |
| 136 | + .map(a -> new AlbumResponse( |
| 137 | + a.getName(), |
| 138 | + a.getReleaseDate(), |
| 139 | + albumTypeToString(a.getAlbumType()), |
| 140 | + pickImageUrl(a.getImages()), |
| 141 | + a.getExternalUrls() != null ? a.getExternalUrls().get("spotify") : null |
| 142 | + )) |
| 143 | + .collect(toList()); |
| 144 | + } |
| 145 | + |
| 146 | + private String albumTypeToString(AlbumType type) { |
| 147 | + if (type == null) return null; |
| 148 | + return type.getType(); |
| 149 | + } |
| 150 | + |
| 151 | + private List<TopTrackResponse> toTopTrackResponses(Track[] tracks) { |
| 152 | + if (tracks == null) return List.of(); |
| 153 | + return Stream.of(tracks) |
| 154 | + .filter(Objects::nonNull) |
| 155 | + .map(t -> new TopTrackResponse( |
| 156 | + t.getName(), |
| 157 | + t.getExternalUrls() != null ? t.getExternalUrls().get("spotify") : null |
| 158 | + )) |
| 159 | + .collect(toList()); |
| 160 | + } |
| 161 | +} |
| 162 | + |
0 commit comments