일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- spring boot
- C++
- Spotify Api
- Computer Science
- Spring
- CS
- 회귀
- Baekjoon
- SECS/GEM
- modern c++
- linux
- python
- MYSQL
- 프로그래머스
- 스포티파이
- programmers
- 자바
- java
- 회원가입
- spotify
- SW Expert Academy
- 백준
- SECS-II
- SECS
- 파이썬
- c
- Gem
- regression
- SWEA
- Spring JPA
Archives
- Today
- Total
비버놀로지
[Spotify 스포티파이] 8. Spring Boot 가수 검색 본문
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
'OPEN API 활용' 카테고리의 다른 글
[Spotify 스포티파이] 10. Spring Boot 제목ID로 검색 (0) | 2021.03.25 |
---|---|
[Spotify 스포티파이] 9. Spring Boot Items 검색 (0) | 2021.03.25 |
[Spotify 스포티파이] 7. Spring Boot 토큰 발행 (0) | 2021.03.25 |
[Spotify 스포티파이] 6. Spring Boot 기본 Code (0) | 2021.03.25 |
[Spotify 스포티파이] 5. Spring Boot 사전준비 (0) | 2021.03.25 |
Comments