일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 암호화폐투자
- Digital Marketing
- 자바
- Computer Science
- 암호화폐시장
- spring boot
- java
- Spring JPA
- SECS/GEM
- Gem
- 비트코인
- SECS
- Baekjoon
- Cars
- Investing
- programmers
- coins
- c
- ai이미지변환
- 프로그래머스
- 지브리필터
- python
- C++
- 스테이블코인
- CS
- 파이썬
- 백준
- finance & economics
- 암호화폐
Archives
- Today
- Total
비버놀로지
[Spotify 스포티파이] 9. Spring Boot Items 검색 본문
반응형
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();
}
}
위의 코드를 활용해서 결과를 출력을 할 수 있다.
반응형
'OPEN API 활용' 카테고리의 다른 글
[Spotify 스포티파이] 11. Spring Boot 추천서비스 (0) | 2021.03.25 |
---|---|
[Spotify 스포티파이] 10. Spring Boot 제목ID로 검색 (0) | 2021.03.25 |
[Spotify 스포티파이] 8. Spring Boot 가수 검색 (2) | 2021.03.25 |
[Spotify 스포티파이] 7. Spring Boot 토큰 발행 (0) | 2021.03.25 |
[Spotify 스포티파이] 6. Spring Boot 기본 Code (0) | 2021.03.25 |
Comments