Skip to content

Commit

Permalink
don't throw exceptions when no track is found
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Apr 11, 2022
1 parent aa42248 commit 2cbc066
Showing 1 changed file with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ public JsonBrowser getJson(String uri) throws IOException{
request.addHeader("Authorization", "Bearer " + this.getToken());
request.addHeader("Origin", "https://music.apple.com");
try(var response = this.httpInterfaceManager.getInterface().execute(request)){
if(response.getStatusLine().getStatusCode() == 404){
return null;
}
if(response.getStatusLine().getStatusCode() != 200){
throw new IOException("HTTP error " + response.getStatusLine().getStatusCode() + ": " + response.getStatusLine().getReasonPhrase());
}
Expand All @@ -124,11 +127,17 @@ public JsonBrowser getJson(String uri) throws IOException{

public AudioItem getSearch(String query) throws IOException{
var json = this.getJson("https://api.music.apple.com/v1/catalog/" + countryCode + "/search?term=" + URLEncoder.encode(query, StandardCharsets.UTF_8) + "&limit=" + 25);
if(json == null){
return AudioReference.NO_TRACK;
}
return new BasicAudioPlaylist("Apple Music Search: " + query, parseTracks(json.get("results").get("songs")), null, true);
}

public AudioItem getAlbum(String id) throws IOException{
var json = this.getJson("https://api.music.apple.com/v1/catalog/" + countryCode + "/albums/" + id);
if(json == null){
return AudioReference.NO_TRACK;
}

var tracks = new ArrayList<AudioTrack>();
JsonBrowser page;
Expand All @@ -146,6 +155,9 @@ public AudioItem getAlbum(String id) throws IOException{

public AudioItem getPlaylist(String id) throws IOException{
var json = this.getJson("https://api.music.apple.com/v1/catalog/" + countryCode + "/playlists/" + id);
if(json == null){
return AudioReference.NO_TRACK;
}

var tracks = new ArrayList<AudioTrack>();
JsonBrowser page;
Expand All @@ -163,11 +175,17 @@ public AudioItem getPlaylist(String id) throws IOException{

public AudioItem getArtist(String id) throws IOException{
var json = this.getJson("https://api.music.apple.com/v1/catalog/" + countryCode + "/artists/" + id + "/view/top-songs");
if(json == null){
return AudioReference.NO_TRACK;
}
return new BasicAudioPlaylist(json.get("data").index(0).get("attributes").get("artistName").text() + "'s Top Tracks", parseTracks(json), null, false);
}

public AudioItem getSong(String id) throws IOException{
var json = this.getJson("https://api.music.apple.com/v1/catalog/" + countryCode + "/songs/" + id);
if(json == null){
return AudioReference.NO_TRACK;
}
return parseTrack(json.get("data").index(0));
}

Expand Down

0 comments on commit 2cbc066

Please sign in to comment.