Skip to content

Commit

Permalink
Provide default value for missing author field
Browse files Browse the repository at this point in the history
For some obscure reason, YouTube do not return an author field for select tracks so we substitute what would be a null value with a placeholder of Unknown artist (consistent with the default value of AudioTrackInfoBuilder#UNKNOWN_ARTIST).
  • Loading branch information
devoxin committed Nov 7, 2023
1 parent 3c30693 commit 4a9a442
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -167,9 +166,8 @@ private String extractPlaylistTracks(JsonBrowser playlistVideoList, List<AudioTr
if (!item.get("isPlayable").isNull() && !shortBylineText.isNull()) {
String videoId = item.get("videoId").text();
JsonBrowser titleField = item.get("title");
String title = Optional.ofNullable(titleField.get("simpleText").text())
.orElse(titleField.get("runs").index(0).get("text").text());
String author = shortBylineText.get("runs").index(0).get("text").text();
String title = titleField.get("simpleText").textOrDefault(titleField.get("runs").index(0).get("text").text());
String author = shortBylineText.get("runs").index(0).get("text").textOrDefault("Unknown artist");
JsonBrowser lengthSeconds = item.get("lengthSeconds");
long duration = Units.secondsToMillis(lengthSeconds.asLong(Units.DURATION_SEC_UNKNOWN));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ public String text() {
return null;
}

public String textOrDefault(String defaultValue) {
String value = text();
return value != null ? value : defaultValue;
}

public boolean asBoolean(boolean defaultValue) {
if (node != null) {
if (node.isBoolean()) {
Expand Down

0 comments on commit 4a9a442

Please sign in to comment.