Skip to content

Commit

Permalink
Reduce MusicWithThumbnail class size further.
Browse files Browse the repository at this point in the history
  • Loading branch information
devoxin committed May 9, 2024
1 parent 936782e commit 15f1efa
Showing 1 changed file with 2 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -1,70 +1,13 @@
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.ThumbnailTools;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;
import dev.lavalink.youtube.YoutubeAudioSourceManager;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class MusicWithThumbnail extends Music {
@Override
@NotNull
protected List<AudioTrack> extractSearchResultTracks(@NotNull YoutubeAudioSourceManager source,
@NotNull JsonBrowser json) {
List<AudioTrack> tracks = new ArrayList<>();

for (JsonBrowser track : json.values()) {
JsonBrowser thumbnail = track.get("musicResponsiveListItemRenderer").get("thumbnail").get("musicThumbnailRenderer");
JsonBrowser columns = track.get("musicResponsiveListItemRenderer").get("flexColumns");

if (columns.isNull()) {
continue;
}

JsonBrowser metadata = columns.index(0)
.get("musicResponsiveListItemFlexColumnRenderer")
.get("text")
.get("runs")
.index(0);

String title = metadata.get("text").text();
String videoId = metadata.get("navigationEndpoint").get("watchEndpoint").get("videoId").text();

if (videoId == null) {
// If the track is not available on YouTube Music, videoId will be empty
continue;
}

List<JsonBrowser> runs = columns.index(1)
.get("musicResponsiveListItemFlexColumnRenderer")
.get("text")
.get("runs")
.values();

String author = runs.get(0).get("text").text();
JsonBrowser lastElement = runs.get(runs.size() - 1);

if (!lastElement.get("navigationEndpoint").isNull()) {
// The duration element should not have this key. If it does,
// then duration is probably missing.
continue;
}

long duration = DataFormatTools.durationTextToMillis(lastElement.get("text").text());
String thumbnailUrl = ThumbnailTools.getYouTubeMusicThumbnail(thumbnail, videoId);

AudioTrackInfo info = new AudioTrackInfo(title, author, duration, videoId, false, WATCH_URL + videoId, thumbnailUrl, null);
tracks.add(source.buildAudioTrack(info));
}

return tracks;
}

@Override
@NotNull
public AudioTrack buildAudioTrack(@NotNull YoutubeAudioSourceManager source,
Expand All @@ -74,7 +17,8 @@ public AudioTrack buildAudioTrack(@NotNull YoutubeAudioSourceManager source,
long duration,
@NotNull String videoId,
boolean isStream) {
String thumbnail = ThumbnailTools.getYouTubeMusicThumbnail(json, videoId);
JsonBrowser thumbnailJson = json.get("musicResponsiveListItemRenderer").get("thumbnail").get("musicThumbnailRenderer");
String thumbnail = ThumbnailTools.getYouTubeMusicThumbnail(thumbnailJson, videoId);
return source.buildAudioTrack(new AudioTrackInfo(title, author, duration, videoId, isStream, WATCH_URL + videoId, thumbnail, null));
}
}

0 comments on commit 15f1efa

Please sign in to comment.