Skip to content

Commit

Permalink
Add defaults and logging for missing author fields
Browse files Browse the repository at this point in the history
  • Loading branch information
devoxin committed Sep 17, 2024
1 parent 209a00e commit c5d0aab
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -95,6 +99,12 @@ protected List<AudioTrack> 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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand All @@ -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());

Expand Down Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<AudioTrack> extractSearchResultTracks(@NotNull YoutubeAudioSourceManager source,
Expand Down Expand Up @@ -49,6 +53,12 @@ protected List<AudioTrack> 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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<AudioTrack> tracks,
@NotNull YoutubeAudioSourceManager source) {
Expand Down Expand Up @@ -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());

Expand All @@ -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"),
Expand Down

0 comments on commit c5d0aab

Please sign in to comment.