일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 백준
- SECS
- 스포티파이
- Spring JPA
- SECS/GEM
- Gem
- 자바
- spotify
- SWEA
- 파이썬
- linux
- CS
- java
- C++
- regression
- SECS-II
- c
- spring boot
- SW Expert Academy
- Spotify Api
- 회귀
- programmers
- python
- Baekjoon
- 회원가입
- modern c++
- 프로그래머스
- Spring
- Computer Science
- MYSQL
Archives
- Today
- Total
비버놀로지
[Spotify 스포티파이] 10. Spring Boot 제목ID로 검색 본문
728x90
import com.wrapper.spotify.SpotifyApi;
import com.wrapper.spotify.exceptions.SpotifyWebApiException;
import com.wrapper.spotify.model_objects.specification.Track;
import com.wrapper.spotify.requests.data.tracks.GetTrackRequest;
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 GetTrackExample {
private static final String accessToken = "taHZ2SdB-bPA3FsK3D7ZN5npZS47cMy-IEySVEGttOhXmqaVAIo0ESvTCLjLBifhHOHOIuhFUKPW1WMDP7w6dj3MAZdWT8CLI2MkZaXbYLTeoDvXesf2eeiLYPBGdx8tIwQJKgV8XdnzH_DONk";
private static final String id = "01iyCAUm8EvOFqVWYJ3dVX";
private static final SpotifyApi spotifyApi = new SpotifyApi.Builder()
.setAccessToken(accessToken)
.build();
private static final GetTrackRequest getTrackRequest = spotifyApi.getTrack(id)
// .market(CountryCode.SE)
.build();
public static void getTrack_Sync() {
try {
final Track track = getTrackRequest.execute();
System.out.println("Name: " + track.getName());
} catch (IOException | SpotifyWebApiException | ParseException e) {
System.out.println("Error: " + e.getMessage());
}
}
public static void getTrack_Async() {
try {
final CompletableFuture<Track> trackFuture = getTrackRequest.executeAsync();
// Thread free to do other tasks...
// Example Only. Never block in production code.
final Track track = trackFuture.join();
System.out.println("Name: " + track.getName());
} 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) {
getTrack_Sync();
getTrack_Async();
}
}
위의 코드를 활용을 해서 음악에 대한 정보를 가져올 수 있다.
하지만 id를 알아야 하기 때문에 seach와 함께 이용을 해서 활용하는 경우가 많다.
import com.wrapper.spotify.SpotifyApi;
import com.wrapper.spotify.exceptions.SpotifyWebApiException;
import com.wrapper.spotify.model_objects.specification.Track;
import com.wrapper.spotify.requests.data.tracks.GetTrackRequest;
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 GetTrackExample {
private static final String accessToken = "taHZ2SdB-bPA3FsK3D7ZN5npZS47cMy-IEySVEGttOhXmqaVAIo0ESvTCLjLBifhHOHOIuhFUKPW1WMDP7w6dj3MAZdWT8CLI2MkZaXbYLTeoDvXesf2eeiLYPBGdx8tIwQJKgV8XdnzH_DONk";
private static final String id = "01iyCAUm8EvOFqVWYJ3dVX";
private static final SpotifyApi spotifyApi = new SpotifyApi.Builder()
.setAccessToken(accessToken)
.build();
public static void getTrack_Sync() {
GetTrackRequest getTrackRequest = spotifyApi.getTrack(id)
// .market(CountryCode.SE)
.build();
try {
final Track track = getTrackRequest.execute();
System.out.println("Name: " + track.getName());
} catch (IOException | SpotifyWebApiException | ParseException e) {
System.out.println("Error: " + e.getMessage());
}
}
public static void main(String[] args) {
getTrack_Sync();
}
}
코드를 간략하게 해서 활용할 수 있다.
id를 변경하면서 활용해 track에 대한 정보를 받을 수 있다.
728x90
'OPEN API 활용' 카테고리의 다른 글
[Youtube API 유튜브] 1. Youtube API 키 발급하기 (0) | 2021.04.16 |
---|---|
[Spotify 스포티파이] 11. Spring Boot 추천서비스 (0) | 2021.03.25 |
[Spotify 스포티파이] 9. Spring Boot Items 검색 (0) | 2021.03.25 |
[Spotify 스포티파이] 8. Spring Boot 가수 검색 (2) | 2021.03.25 |
[Spotify 스포티파이] 7. Spring Boot 토큰 발행 (0) | 2021.03.25 |
Comments