|
20 | 20 |
|
21 | 21 | import java.io.DataInput;
|
22 | 22 | import java.io.IOException;
|
23 |
| -import java.net.SocketTimeoutException; |
24 | 23 | import java.net.URLEncoder;
|
25 | 24 | import java.nio.charset.StandardCharsets;
|
26 | 25 | import java.util.ArrayList;
|
@@ -128,30 +127,26 @@ private List<AudioTrack> parseTracks(JsonBrowser json) {
|
128 | 127 | }
|
129 | 128 |
|
130 | 129 | 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); |
144 | 138 |
|
145 |
| - var tracks = parseTracks(json.get("tracks").get("items")); |
| 139 | + if (json.get("tracks").get("items").isNull()) { |
| 140 | + return AudioReference.NO_TRACK; |
| 141 | + } |
146 | 142 |
|
147 |
| - if (tracks.isEmpty()) { |
148 |
| - return AudioReference.NO_TRACK; |
149 |
| - } |
| 143 | + var tracks = parseTracks(json.get("tracks").get("items")); |
150 | 144 |
|
151 |
| - return new BasicAudioPlaylist("Tidal Search: " + query, tracks, null, true); |
152 |
| - } catch (SocketTimeoutException e) { |
| 145 | + if (tracks.isEmpty()) { |
153 | 146 | return AudioReference.NO_TRACK;
|
154 | 147 | }
|
| 148 | + |
| 149 | + return new BasicAudioPlaylist("Tidal Search: " + query, tracks, null, true); |
155 | 150 | }
|
156 | 151 |
|
157 | 152 | private AudioItem getRecommendations(String trackId) throws IOException {
|
@@ -203,73 +198,65 @@ private AudioTrack parseTrack(JsonBrowser audio) {
|
203 | 198 | }
|
204 | 199 |
|
205 | 200 | 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); |
222 | 210 |
|
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 | + } |
233 | 214 |
|
234 |
| - var itemInfoJson = getApiResponse(itemInfoUrl); |
| 215 | + var items = parseTrackItem(json); |
235 | 216 |
|
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; |
270 | 219 | }
|
| 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); |
271 | 229 |
|
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); |
273 | 260 | }
|
274 | 261 |
|
275 | 262 | public AudioItem getTrack(String trackId) throws IOException {
|
|
0 commit comments