From c5d0aab260f19e743e6549010989e06ddc5dd05a Mon Sep 17 00:00:00 2001 From: devoxin Date: Tue, 17 Sep 2024 22:49:48 +0100 Subject: [PATCH] Add defaults and logging for missing author fields --- .../dev/lavalink/youtube/clients/AndroidMusic.java | 8 ++++++++ .../lavalink/youtube/clients/TvHtml5Embedded.java | 1 - .../youtube/clients/skeleton/MusicClient.java | 10 ++++++++++ .../youtube/clients/skeleton/NonMusicClient.java | 11 ++++++++++- .../clients/skeleton/ThumbnailMusicClient.java | 10 ++++++++++ .../clients/skeleton/ThumbnailNonMusicClient.java | 14 ++++++++++++++ 6 files changed, 52 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/dev/lavalink/youtube/clients/AndroidMusic.java b/common/src/main/java/dev/lavalink/youtube/clients/AndroidMusic.java index f0eef69..f744c19 100644 --- a/common/src/main/java/dev/lavalink/youtube/clients/AndroidMusic.java +++ b/common/src/main/java/dev/lavalink/youtube/clients/AndroidMusic.java @@ -10,12 +10,15 @@ import dev.lavalink.youtube.YoutubeAudioSourceManager; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; public class AndroidMusic extends Android { + private static final Logger log = LoggerFactory.getLogger(AndroidMusic.class); public static String CLIENT_VERSION = "7.11.50"; public static ClientConfig BASE_CONFIG = new ClientConfig() @@ -94,6 +97,11 @@ protected AudioTrack extractAudioTrack(@NotNull JsonBrowser json, @NotNull Youtu String title = DataFormatTools.defaultOnNull(titleJson.get("runs").index(0).get("text").text(), titleJson.get("simpleText").text()); String author = secondaryJson.index(0).get("text").text(); + if (author == null) { + log.debug("Author field is null, json: {}", json.format()); + author = "Unknown artist"; + } + JsonBrowser durationJson = secondaryJson.index(2); String durationText = DataFormatTools.defaultOnNull(durationJson.get("text").text(), durationJson.get("runs").index(0).get("text").text()); diff --git a/common/src/main/java/dev/lavalink/youtube/clients/TvHtml5Embedded.java b/common/src/main/java/dev/lavalink/youtube/clients/TvHtml5Embedded.java index 9c6c86e..bf4e91b 100644 --- a/common/src/main/java/dev/lavalink/youtube/clients/TvHtml5Embedded.java +++ b/common/src/main/java/dev/lavalink/youtube/clients/TvHtml5Embedded.java @@ -72,7 +72,6 @@ protected void extractPlaylistTracks(@NotNull JsonBrowser json, 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)); } } diff --git a/common/src/main/java/dev/lavalink/youtube/clients/skeleton/MusicClient.java b/common/src/main/java/dev/lavalink/youtube/clients/skeleton/MusicClient.java index 1676cc6..aa13061 100644 --- a/common/src/main/java/dev/lavalink/youtube/clients/skeleton/MusicClient.java +++ b/common/src/main/java/dev/lavalink/youtube/clients/skeleton/MusicClient.java @@ -14,6 +14,8 @@ import org.apache.http.entity.StringEntity; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.ArrayList; @@ -23,6 +25,8 @@ * The base class for a client that can be used with music.youtube.com. */ public abstract class MusicClient implements Client { + private static final Logger log = LoggerFactory.getLogger(MusicClient.class); + @NotNull protected abstract ClientConfig getBaseClientConfig(@NotNull HttpInterface httpInterface); @@ -95,6 +99,12 @@ protected List extractSearchResultTracks(@NotNull YoutubeAudioSource .values(); String author = runs.get(0).get("text").text(); + + if (author == null) { + log.debug("Author field is null, client: {}, json: {}", getIdentifier(), json.format()); + author = "Unknown artist"; + } + JsonBrowser lastElement = runs.get(runs.size() - 1); if (!lastElement.get("navigationEndpoint").isNull()) { diff --git a/common/src/main/java/dev/lavalink/youtube/clients/skeleton/NonMusicClient.java b/common/src/main/java/dev/lavalink/youtube/clients/skeleton/NonMusicClient.java index f306721..182748f 100644 --- a/common/src/main/java/dev/lavalink/youtube/clients/skeleton/NonMusicClient.java +++ b/common/src/main/java/dev/lavalink/youtube/clients/skeleton/NonMusicClient.java @@ -279,7 +279,6 @@ protected void extractPlaylistTracks(@NotNull JsonBrowser json, 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)); } } @@ -296,6 +295,11 @@ protected AudioTrack extractAudioTrack(@NotNull JsonBrowser json, 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(); + if (author == null) { + log.debug("Author field is null, client: {}, json: {}", getIdentifier(), json.format()); + author = "Unknown artist"; + } + JsonBrowser durationJson = json.get("lengthText"); String durationText = DataFormatTools.defaultOnNull(durationJson.get("runs").index(0).get("text").text(), durationJson.get("simpleText").text()); @@ -329,6 +333,11 @@ public AudioItem loadVideo(@NotNull YoutubeAudioSourceManager source, String title = videoDetails.get("title").text(); String author = videoDetails.get("author").text(); + if (author == null) { + log.debug("Author field is null, client: {}, json: {}", getIdentifier(), json.format()); + author = "Unknown artist"; + } + TemporalInfo temporalInfo = TemporalInfo.fromRawData( !playabilityStatus.get("liveStreamability").isNull(), videoDetails.get("lengthSeconds"), diff --git a/v2/src/main/java/dev/lavalink/youtube/clients/skeleton/ThumbnailMusicClient.java b/v2/src/main/java/dev/lavalink/youtube/clients/skeleton/ThumbnailMusicClient.java index eef2818..5141d4a 100644 --- a/v2/src/main/java/dev/lavalink/youtube/clients/skeleton/ThumbnailMusicClient.java +++ b/v2/src/main/java/dev/lavalink/youtube/clients/skeleton/ThumbnailMusicClient.java @@ -6,6 +6,8 @@ import com.sedmelluq.discord.lavaplayer.track.*; import dev.lavalink.youtube.YoutubeAudioSourceManager; import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.List; @@ -14,6 +16,8 @@ * This class is deprecated. */ public abstract class ThumbnailMusicClient extends MusicClient { + private static final Logger log = LoggerFactory.getLogger(ThumbnailMusicClient.class); + @Override @NotNull protected List extractSearchResultTracks(@NotNull YoutubeAudioSourceManager source, @@ -49,6 +53,12 @@ protected List extractSearchResultTracks(@NotNull YoutubeAudioSource .values(); String author = runs.get(0).get("text").text(); + + if (author == null) { + log.debug("Author field is null, client: {}, json: {}", getIdentifier(), json.format()); + author = "Unknown artist"; + } + JsonBrowser lastElement = runs.get(runs.size() - 1); if (!lastElement.get("navigationEndpoint").isNull()) { diff --git a/v2/src/main/java/dev/lavalink/youtube/clients/skeleton/ThumbnailNonMusicClient.java b/v2/src/main/java/dev/lavalink/youtube/clients/skeleton/ThumbnailNonMusicClient.java index 971959b..1cc4ba0 100644 --- a/v2/src/main/java/dev/lavalink/youtube/clients/skeleton/ThumbnailNonMusicClient.java +++ b/v2/src/main/java/dev/lavalink/youtube/clients/skeleton/ThumbnailNonMusicClient.java @@ -13,6 +13,8 @@ import dev.lavalink.youtube.track.TemporalInfo; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.List; @@ -24,6 +26,8 @@ * method instead. */ public abstract class ThumbnailNonMusicClient extends NonMusicClient { + private static final Logger log = LoggerFactory.getLogger(ThumbnailNonMusicClient.class); + protected void extractPlaylistTracks(@NotNull JsonBrowser json, @NotNull List tracks, @NotNull YoutubeAudioSourceManager source) { @@ -66,6 +70,11 @@ protected AudioTrack extractAudioTrack(@NotNull JsonBrowser json, 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(); + if (author == null) { + log.debug("Author field is null, client: {}, json: {}", getIdentifier(), json.format()); + author = "Unknown artist"; + } + JsonBrowser durationJson = json.get("lengthText"); String durationText = DataFormatTools.defaultOnNull(durationJson.get("runs").index(0).get("text").text(), durationJson.get("simpleText").text()); @@ -87,6 +96,11 @@ public AudioItem loadVideo(@NotNull YoutubeAudioSourceManager source, String title = videoDetails.get("title").text(); String author = videoDetails.get("author").text(); + if (author == null) { + log.debug("Author field is null, client: {}, json: {}", getIdentifier(), json.format()); + author = "Unknown artist"; + } + TemporalInfo temporalInfo = TemporalInfo.fromRawData( !playabilityStatus.get("liveStreamability").isNull(), videoDetails.get("lengthSeconds"),