Skip to content

Commit 25a7611

Browse files
committed
Add requested changes v5
1 parent 387951e commit 25a7611

File tree

2 files changed

+70
-83
lines changed

2 files changed

+70
-83
lines changed

application.example.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ plugins:
5757
artistLoadLimit: 1 # The number of pages at 10 tracks each
5858
recommendationsLoadLimit: 10 # Number of tracks
5959
tidal:
60-
countryCode: "US"
60+
countryCode: "US" # the country code for accessing region-specific content on Tidal (ISO 3166-1 alpha-2).
6161
searchLimit: 6 # How many search results should be returned
6262
token: "your tidal token" # the token used for accessing the tidal api.
6363

main/src/main/java/com/github/topi314/lavasrc/tidal/TidalSourceManager.java

Lines changed: 69 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import java.io.DataInput;
2222
import java.io.IOException;
23-
import java.net.SocketTimeoutException;
2423
import java.net.URLEncoder;
2524
import java.nio.charset.StandardCharsets;
2625
import java.util.ArrayList;
@@ -128,30 +127,26 @@ private List<AudioTrack> parseTracks(JsonBrowser json) {
128127
}
129128

130129
private AudioItem getSearch(String query) throws IOException {
131-
try {
132-
String apiUrl = PUBLIC_API_BASE +
133-
"search?query=" +
134-
URLEncoder.encode(query, StandardCharsets.UTF_8) +
135-
"&offset=0&limit=" +
136-
searchLimit +
137-
"&countryCode=" +
138-
countryCode;
139-
var json = getApiResponse(apiUrl);
140-
141-
if (json.get("tracks").get("items").isNull()) {
142-
return AudioReference.NO_TRACK;
143-
}
130+
String apiUrl = PUBLIC_API_BASE +
131+
"search?query=" +
132+
URLEncoder.encode(query, StandardCharsets.UTF_8) +
133+
"&offset=0&limit=" +
134+
searchLimit +
135+
"&countryCode=" +
136+
countryCode;
137+
var json = getApiResponse(apiUrl);
144138

145-
var tracks = parseTracks(json.get("tracks").get("items"));
139+
if (json.get("tracks").get("items").isNull()) {
140+
return AudioReference.NO_TRACK;
141+
}
146142

147-
if (tracks.isEmpty()) {
148-
return AudioReference.NO_TRACK;
149-
}
143+
var tracks = parseTracks(json.get("tracks").get("items"));
150144

151-
return new BasicAudioPlaylist("Tidal Search: " + query, tracks, null, true);
152-
} catch (SocketTimeoutException e) {
145+
if (tracks.isEmpty()) {
153146
return AudioReference.NO_TRACK;
154147
}
148+
149+
return new BasicAudioPlaylist("Tidal Search: " + query, tracks, null, true);
155150
}
156151

157152
private AudioItem getRecommendations(String trackId) throws IOException {
@@ -203,73 +198,65 @@ private AudioTrack parseTrack(JsonBrowser audio) {
203198
}
204199

205200
private AudioItem getAlbumOrPlaylist(String itemId, String type, int maxPageItems) throws IOException {
206-
try {
207-
String apiUrl = PUBLIC_API_BASE +
208-
type +
209-
"s/" +
210-
itemId +
211-
"/tracks?countryCode=" +
212-
countryCode +
213-
"&limit=" +
214-
maxPageItems;
215-
var json = getApiResponse(apiUrl);
216-
217-
if (json == null || json.get("items").isNull()) {
218-
return AudioReference.NO_TRACK;
219-
}
220-
221-
var items = parseTrackItem(json);
201+
String apiUrl = PUBLIC_API_BASE +
202+
type +
203+
"s/" +
204+
itemId +
205+
"/tracks?countryCode=" +
206+
countryCode +
207+
"&limit=" +
208+
maxPageItems;
209+
var json = getApiResponse(apiUrl);
222210

223-
if (items.isEmpty()) {
224-
return AudioReference.NO_TRACK;
225-
}
226-
String itemInfoUrl = "";
227-
ExtendedAudioPlaylist.Type trackType = type.equalsIgnoreCase("playlist") ? ExtendedAudioPlaylist.Type.PLAYLIST : ExtendedAudioPlaylist.Type.ALBUM;
228-
if (trackType == ExtendedAudioPlaylist.Type.PLAYLIST) {
229-
itemInfoUrl = PUBLIC_API_BASE + "playlists/" + itemId + "?countryCode=" + countryCode;
230-
} else {
231-
itemInfoUrl = PUBLIC_API_BASE + "albums/" + itemId + "?countryCode=" + countryCode;
232-
}
211+
if (json == null || json.get("items").isNull()) {
212+
return AudioReference.NO_TRACK;
213+
}
233214

234-
var itemInfoJson = getApiResponse(itemInfoUrl);
215+
var items = parseTrackItem(json);
235216

236-
if (itemInfoJson == null) {
237-
return AudioReference.NO_TRACK;
238-
}
239-
String title = "";
240-
String artistName = "";
241-
String url = "";
242-
String coverUrl = "";
243-
long totalTracks = 0;
244-
245-
if (trackType == ExtendedAudioPlaylist.Type.PLAYLIST) {
246-
title = itemInfoJson.get("title").text();
247-
url = itemInfoJson.get("url").text();
248-
coverUrl = itemInfoJson.get("squareImage").text();
249-
artistName = itemInfoJson.get("promotedArtists").index(0).get("name").text();
250-
totalTracks = itemInfoJson.get("numberOfTracks").asLong(0);
251-
} else {
252-
title = itemInfoJson.get("title").text();
253-
url = itemInfoJson.get("url").text();
254-
coverUrl = itemInfoJson.get("cover").text();
255-
artistName = itemInfoJson.get("artists").index(0).get("name").text();
256-
totalTracks = itemInfoJson.get("numberOfTracks").asLong(0);
257-
}
258-
if (title == null || url == null) {
259-
return AudioReference.NO_TRACK;
260-
}
261-
var formattedCoverIdentifier = coverUrl.replaceAll("-", "/");
262-
var artworkUrl = "https://resources.tidal.com/images/" +
263-
formattedCoverIdentifier +
264-
"/1080x1080.jpg";
265-
return new TidalAudioPlaylist(title, items, type.equalsIgnoreCase("playlist") ? ExtendedAudioPlaylist.Type.PLAYLIST : ExtendedAudioPlaylist.Type.ALBUM, url, artworkUrl, artistName, (int) totalTracks);
266-
} catch (SocketTimeoutException e) {
267-
log.error("Socket timeout while fetching {} info for ID: {}", type, itemId, e);
268-
} catch (Exception e) {
269-
log.error("Error fetching {} info for ID: {}", type, itemId, e);
217+
if (items.isEmpty()) {
218+
return AudioReference.NO_TRACK;
270219
}
220+
String itemInfoUrl = "";
221+
ExtendedAudioPlaylist.Type trackType = type.equalsIgnoreCase("playlist") ? ExtendedAudioPlaylist.Type.PLAYLIST : ExtendedAudioPlaylist.Type.ALBUM;
222+
if (trackType == ExtendedAudioPlaylist.Type.PLAYLIST) {
223+
itemInfoUrl = PUBLIC_API_BASE + "playlists/" + itemId + "?countryCode=" + countryCode;
224+
} else {
225+
itemInfoUrl = PUBLIC_API_BASE + "albums/" + itemId + "?countryCode=" + countryCode;
226+
}
227+
228+
var itemInfoJson = getApiResponse(itemInfoUrl);
271229

272-
return AudioReference.NO_TRACK;
230+
if (itemInfoJson == null) {
231+
return AudioReference.NO_TRACK;
232+
}
233+
String title = "";
234+
String artistName = "";
235+
String url = "";
236+
String coverUrl = "";
237+
long totalTracks = 0;
238+
239+
if (trackType == ExtendedAudioPlaylist.Type.PLAYLIST) {
240+
title = itemInfoJson.get("title").text();
241+
url = itemInfoJson.get("url").text();
242+
coverUrl = itemInfoJson.get("squareImage").text();
243+
artistName = itemInfoJson.get("promotedArtists").index(0).get("name").text();
244+
totalTracks = itemInfoJson.get("numberOfTracks").asLong(0);
245+
} else {
246+
title = itemInfoJson.get("title").text();
247+
url = itemInfoJson.get("url").text();
248+
coverUrl = itemInfoJson.get("cover").text();
249+
artistName = itemInfoJson.get("artists").index(0).get("name").text();
250+
totalTracks = itemInfoJson.get("numberOfTracks").asLong(0);
251+
}
252+
if (title == null || url == null) {
253+
return AudioReference.NO_TRACK;
254+
}
255+
var formattedCoverIdentifier = coverUrl.replaceAll("-", "/");
256+
var artworkUrl = "https://resources.tidal.com/images/" +
257+
formattedCoverIdentifier +
258+
"/1080x1080.jpg";
259+
return new TidalAudioPlaylist(title, items, type.equalsIgnoreCase("playlist") ? ExtendedAudioPlaylist.Type.PLAYLIST : ExtendedAudioPlaylist.Type.ALBUM, url, artworkUrl, artistName, (int) totalTracks);
273260
}
274261

275262
public AudioItem getTrack(String trackId) throws IOException {

0 commit comments

Comments
 (0)