비버놀로지

[Spotify 스포티파이] 9. Spring Boot Items 검색 본문

OPEN API 활용

[Spotify 스포티파이] 9. Spring Boot Items 검색

KUNDUZ 2021. 3. 25. 21:44
728x90
import com.wrapper.spotify.SpotifyApi;
import com.wrapper.spotify.enums.ModelObjectType;
import com.wrapper.spotify.exceptions.SpotifyWebApiException;
import com.wrapper.spotify.model_objects.special.SearchResult;
import com.wrapper.spotify.requests.data.search.SearchItemRequest;
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 SearchItemExample {
  private static final String accessToken = "taHZ2SdB-bPA3FsK3D7ZN5npZS47cMy-IEySVEGttOhXmqaVAIo0ESvTCLjLBifhHOHOIuhFUKPW1WMDP7w6dj3MAZdWT8CLI2MkZaXbYLTeoDvXesf2eeiLYPBGdx8tIwQJKgV8XdnzH_DONk";
  private static final String q = "Abba";
  private static final String type = ModelObjectType.ARTIST.getType();	//ARTIST, TRACK, ALBUM

  private static final SpotifyApi spotifyApi = new SpotifyApi.Builder()
    .setAccessToken(accessToken)
    .build();
  private static final SearchItemRequest searchItemRequest = spotifyApi.searchItem(q, type)
//          .market(CountryCode.SE)
//          .limit(10)
//          .offset(0)
//          .includeExternal("audio")
    .build();

  public static void searchItem_Sync() {
    try {
      final SearchResult searchResult = searchItemRequest.execute();

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

  public static void searchItem_Async() {
    try {
      final CompletableFuture<SearchResult> searchResultFuture = searchItemRequest.executeAsync();

      // Thread free to do other tasks...

      // Example Only. Never block in production code.
      final SearchResult searchResult = searchResultFuture.join();

      System.out.println("Total tracks: " + searchResult.getTracks().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) {
    searchItem_Sync();
    searchItem_Async();
  }
}

위의 코드를 활용하면 item을 검색 할 수 있다.

 

위에서 type에 대해서 변경을 하면 다양하게 얻을 수 있다.

 

위의 코드를 간략히 해서 변경해 아래의 코드로 활용을 했다.

 

import com.wrapper.spotify.SpotifyApi;
import com.wrapper.spotify.enums.ModelObjectType;
import com.wrapper.spotify.exceptions.SpotifyWebApiException;
import com.wrapper.spotify.model_objects.special.SearchResult;
import com.wrapper.spotify.requests.data.search.SearchItemRequest;
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 SearchItemExample {
  private static final String accessToken = "taHZ2SdB-bPA3FsK3D7ZN5npZS47cMy-IEySVEGttOhXmqaVAIo0ESvTCLjLBifhHOHOIuhFUKPW1WMDP7w6dj3MAZdWT8CLI2MkZaXbYLTeoDvXesf2eeiLYPBGdx8tIwQJKgV8XdnzH_DONk";
  private static final String q = "Abba";
  private static final String type = ModelObjectType.ARTIST.getType();	//ARTIST, TRACK, ALBUM

  private static final SpotifyApi spotifyApi = new SpotifyApi.Builder()
    .setAccessToken(accessToken)
    .build();

  public static void searchItem_Sync() {
  SearchItemRequest searchItemRequest = spotifyApi.searchItem(q, type)
//          .market(CountryCode.SE)
//          .limit(10)
//          .offset(0)
//          .includeExternal("audio")
    .build();
    try {
      final SearchResult searchResult = searchItemRequest.execute();

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

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

위의 코드를 활용해서 결과를 출력을 할 수 있다.

728x90
Comments