Skip to content

Commit

Permalink
Replace usage of JsonBrowser#textOrDefault with DataFormatTools#defau…
Browse files Browse the repository at this point in the history
…ltOnNull

textOrDefault is non-standard and breaks compatibility with completely standard v1 Lavaplayer forks.
  • Loading branch information
devoxin committed Jul 10, 2024
1 parent 632134c commit 652385e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.lavalink.youtube.clients;

import com.sedmelluq.discord.lavaplayer.tools.DataFormatTools;
import com.sedmelluq.discord.lavaplayer.tools.JsonBrowser;
import com.sedmelluq.discord.lavaplayer.tools.Units;
import com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface;
Expand Down Expand Up @@ -66,8 +67,8 @@ protected void extractPlaylistTracks(@NotNull JsonBrowser json,
if (!authorJson.isNull()) {
String videoId = item.get("videoId").text();
JsonBrowser titleField = item.get("title");
String title = titleField.get("simpleText").textOrDefault(titleField.get("runs").index(0).get("text").text());
String author = authorJson.get("runs").index(0).get("text").textOrDefault("Unknown artist");
String title = DataFormatTools.defaultOnNull(titleField.get("simpleText").text(), titleField.get("runs").index(0).get("text").text());
String author = DataFormatTools.defaultOnNull(authorJson.get("runs").index(0).get("text").text(), "Unknown artist");
long duration = Units.secondsToMillis(item.get("lengthSeconds").asLong(Units.DURATION_SEC_UNKNOWN));

tracks.add(buildAudioTrack(source, track, title, author, duration, videoId, false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,11 @@ protected String extractPlaylistError(@NotNull JsonBrowser json) {

if ("ERROR".equals(type)) {
JsonBrowser textObject = alertInner.get("text");
String runs = textObject.get("runs").values().stream()
.map(run -> run.get("text").text())
.collect(Collectors.joining());

return textObject.get("simpleText")
.textOrDefault(textObject.get("runs").values().stream()
.map(run -> run.get("text").text())
.collect(Collectors.joining())
);
return DataFormatTools.defaultOnNull(textObject.get("simpleText").text(), runs);
}
}
}
Expand Down Expand Up @@ -270,8 +269,8 @@ protected void extractPlaylistTracks(@NotNull JsonBrowser json,
if (!item.get("isPlayable").isNull() && !authorJson.isNull()) {
String videoId = item.get("videoId").text();
JsonBrowser titleField = item.get("title");
String title = titleField.get("simpleText").textOrDefault(titleField.get("runs").index(0).get("text").text());
String author = authorJson.get("runs").index(0).get("text").textOrDefault("Unknown artist");
String title = DataFormatTools.defaultOnNull(titleField.get("simpleText").text(), titleField.get("runs").index(0).get("text").text());
String author = DataFormatTools.defaultOnNull(authorJson.get("runs").index(0).get("text").text(), "Unknown artist");
long duration = Units.secondsToMillis(item.get("lengthSeconds").asLong(Units.DURATION_SEC_UNKNOWN));

tracks.add(buildAudioTrack(source, item, title, author, duration, videoId, false));
Expand All @@ -287,11 +286,11 @@ protected AudioTrack extractAudioTrack(@NotNull JsonBrowser json,

String videoId = json.get("videoId").text();
JsonBrowser titleJson = json.get("title");
String title = titleJson.get("runs").index(0).get("text").textOrDefault(titleJson.get("simpleText").text());
String title = DataFormatTools.defaultOnNull(titleJson.get("runs").index(0).get("text").text(), titleJson.get("simpleText").text());
String author = json.get("longBylineText").get("runs").index(0).get("text").text();

JsonBrowser durationJson = json.get("lengthText");
String durationText = durationJson.get("runs").index(0).get("text").textOrDefault(durationJson.get("simpleText").text());
String durationText = DataFormatTools.defaultOnNull(durationJson.get("runs").index(0).get("text").text(), durationJson.get("simpleText").text());

long duration = DataFormatTools.durationTextToMillis(durationText);
return buildAudioTrack(source, json, title, author, duration, videoId, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ protected void extractPlaylistTracks(@NotNull JsonBrowser json,
if (!item.get("isPlayable").isNull() && !authorJson.isNull()) {
String videoId = item.get("videoId").text();
JsonBrowser titleField = item.get("title");
String title = titleField.get("simpleText").textOrDefault(titleField.get("runs").index(0).get("text").text());
String author = authorJson.get("runs").index(0).get("text").textOrDefault("Unknown artist");
String title = DataFormatTools.defaultOnNull(titleField.get("simpleText").text(), titleField.get("runs").index(0).get("text").text());
String author = DataFormatTools.defaultOnNull(authorJson.get("runs").index(0).get("text").text(), "Unknown artist");
long duration = Units.secondsToMillis(item.get("lengthSeconds").asLong(Units.DURATION_SEC_UNKNOWN));
String thumbnailUrl = ThumbnailTools.getYouTubeThumbnail(item, videoId);

Expand All @@ -63,11 +63,11 @@ protected AudioTrack extractAudioTrack(@NotNull JsonBrowser json,

String videoId = json.get("videoId").text();
JsonBrowser titleJson = json.get("title");
String title = titleJson.get("runs").index(0).get("text").textOrDefault(titleJson.get("simpleText").text());
String title = DataFormatTools.defaultOnNull(titleJson.get("runs").index(0).get("text").text(), titleJson.get("simpleText").text());
String author = json.get("longBylineText").get("runs").index(0).get("text").text();

JsonBrowser durationJson = json.get("lengthText");
String durationText = durationJson.get("runs").index(0).get("text").textOrDefault(durationJson.get("simpleText").text());
String durationText = DataFormatTools.defaultOnNull(durationJson.get("runs").index(0).get("text").text(), durationJson.get("simpleText").text());

long duration = DataFormatTools.durationTextToMillis(durationText);
String thumbnailUrl = ThumbnailTools.getYouTubeThumbnail(json, videoId);
Expand Down

0 comments on commit 652385e

Please sign in to comment.