비버놀로지

[Spotify 스포티파이] 8. Spring Boot 가수 검색 본문

OPEN API 활용

[Spotify 스포티파이] 8. Spring Boot 가수 검색

KUNDUZ 2021. 3. 25. 21:34
728x90
import com.wrapper.spotify.SpotifyApi;
import com.wrapper.spotify.exceptions.SpotifyWebApiException;
import com.wrapper.spotify.model_objects.specification.Artist;
import com.wrapper.spotify.model_objects.specification.Paging;
import com.wrapper.spotify.requests.data.search.simplified.SearchArtistsRequest;
import org.apache.hc.core5.http.ParseException;

import java.io.IOException;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;

public class SearchArtistsExample {
  private static final String accessToken = "taHZ2SdB-bPA3FsK3D7ZN5npZS47cMy-IEySVEGttOhXmqaVAIo0ESvTCLjLBifhHOHOIuhFUKPW1WMDP7w6dj3MAZdWT8CLI2MkZaXbYLTeoDvXesf2eeiLYPBGdx8tIwQJKgV8XdnzH_DONk";
  private static final String q = "Abba";

  private static final SpotifyApi spotifyApi = new SpotifyApi.Builder()
    .setAccessToken(accessToken)
    .build();
  private static final SearchArtistsRequest searchArtistsRequest = spotifyApi.searchArtists(q)
//          .market(CountryCode.SE)
//          .limit(10)
//          .offset(0)
//          .includeExternal("audio")
    .build();

  public static void searchArtists_Sync() {
    try {
      final Paging<Artist> artistPaging = searchArtistsRequest.execute();

      System.out.println("Total: " + artistPaging.getTotal());
    } catch (IOException | SpotifyWebApiException | ParseException e) {
      System.out.println("Error: " + e.getMessage());
    }
  }

  public static void searchArtists_Async() {
    try {
      final CompletableFuture<Paging<Artist>> pagingFuture = searchArtistsRequest.executeAsync();

      // Thread free to do other tasks...

      // Example Only. Never block in production code.
      final Paging<Artist> artistPaging = pagingFuture.join();

      System.out.println("Total: " + artistPaging.getTotal());
    } catch (CompletionException e) {
      System.out.println("Error: " + e.getCause().getMessage());
    } catch (CancellationException e) {
      System.out.println("Async operation cancelled.");
    }
  }

  public static void main(String[] args) {
    searchArtists_Sync();
    searchArtists_Async();
  }
}

위의 코드를 활용을 하면 가수를 통해서 음악을 가져 올 수 있다.

 

위의 코드를 조금 변형을 해서 사용하면 조금더 간단하게 사용할 수 있다.

 

import com.wrapper.spotify.SpotifyApi;
import com.wrapper.spotify.exceptions.SpotifyWebApiException;
import com.wrapper.spotify.model_objects.specification.Artist;
import com.wrapper.spotify.model_objects.specification.Paging;
import com.wrapper.spotify.requests.data.search.simplified.SearchArtistsRequest;
import org.apache.hc.core5.http.ParseException;

import java.io.IOException;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;

public class SearchArtistsExample {
    static String accessToken = "taHZ2SdB-bPA3FsK3D7ZN5npZS47cMy-IEySVEGttOhXmqaVAIo0ESvTCLjLBifhHOHOIuhFUKPW1WMDP7w6dj3MAZdWT8CLI2MkZaXbYLTeoDvXesf2eeiLYPBGdx8tIwQJKgV8XdnzH_DONk";
    static String q = "Abba";	
    
    static SpotifyApi spotifyApi = new SpotifyApi.Builder()
    .setAccessToken(accessToken)
    .build();

    public static ArtistSimplified searchTracks_Sync(String q) {
		SearchTracksRequest searchTracksRequest = spotifyApi.searchTracks(q)
				.market(CountryCode.US)
		          .limit(10)
//		          .offset(0)
//		          .includeExternal("audio")
				.build();
		String preview = "";
		Track track=null;
		ArtistSimplified artist=null;
		try {
			final Paging<Track> trackPaging = searchTracksRequest.execute();

//			System.out.println("Total: " + trackPaging.getTotal());
			track=trackPaging.getItems()[0];	//해당가수의 첫번째 음악
			System.out.println("제목 : "+track.getName());
			System.out.println("가수 : "+track.getArtists()[0].getName());
            
			artist=trackPaging.getItems()[0].getArtists()[0];	//해당 노래를 부르는 메인 가수
			preview = trackPaging.getItems()[0].getPreviewUrl();	//미리듣기
			System.out.println("미리듣기 : " +preview);
		} catch (IOException | SpotifyWebApiException | ParseException e) {
			System.out.println("Error: " + e.getMessage());
		}
		return artist;
	}
    public static void main(String[] args) {
    ArtistSimplified artist=searchArtists_Sync("Abba");
  }
 }

위의 코드를 사용해서 좀더 편하게 가수를 조회할 수 있다.

 

다양한 option을 통해서 다양하게 출력해 줄 수 있다.

 

 

728x90
Comments