Skip to content

Commit

Permalink
skip empty playlist in track mirroring (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 authored Sep 27, 2024
1 parent ae8f3d1 commit f7fa002
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.topi314.lavasrc.applemusic.AppleMusicSourceManager;
import com.github.topi314.lavasrc.spotify.SpotifySourceManager;
import com.sedmelluq.discord.lavaplayer.track.AudioItem;
import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist;
import com.sedmelluq.discord.lavaplayer.track.AudioReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -24,7 +25,6 @@ public DefaultMirroringAudioTrackResolver(String[] providers) {

@Override
public AudioItem apply(MirroringAudioTrack mirroringAudioTrack) {
AudioItem track = AudioReference.NO_TRACK;
for (var provider : providers) {
if (provider.startsWith(SpotifySourceManager.SEARCH_PREFIX)) {
log.warn("Can not use spotify search as search provider!");
Expand All @@ -46,19 +46,22 @@ public AudioItem apply(MirroringAudioTrack mirroringAudioTrack) {
}

provider = provider.replace(MirroringAudioSourceManager.QUERY_PATTERN, getTrackTitle(mirroringAudioTrack));

AudioItem item;
try {
track = mirroringAudioTrack.loadItem(provider);
}
catch (Exception e) {
item = mirroringAudioTrack.loadItem(provider);
} catch (Exception e) {
log.error("Failed to load track from provider \"{}\"!", provider, e);
continue;
}
if (track != AudioReference.NO_TRACK) {
break;
// If the track is an empty playlist, skip the provider
if (item instanceof AudioPlaylist && ((AudioPlaylist) item).getTracks().isEmpty() || item == AudioReference.NO_TRACK) {
continue;
}
return item;
}

return track;
return AudioReference.NO_TRACK;
}

public String getTrackTitle(MirroringAudioTrack mirroringAudioTrack) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void process(LocalAudioTrackExecutor executor) throws Exception {
if (track instanceof AudioPlaylist) {
var tracks = ((AudioPlaylist) track).getTracks();
if (tracks.isEmpty()) {
throw new TrackNotFoundException();
throw new TrackNotFoundException("No tracks found in playlist or search result for track");
}
track = tracks.get(0);
}
Expand All @@ -55,7 +55,7 @@ public void process(LocalAudioTrackExecutor executor) throws Exception {
processDelegate(internalTrack, executor);
return;
}
throw new TrackNotFoundException();
throw new TrackNotFoundException("No mirror found for track");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public class TrackNotFoundException extends FriendlyException {

private static final long serialVersionUID = 6550093849278285754L;

public TrackNotFoundException() {
super("Playlist is empty", FriendlyException.Severity.COMMON, null);
public TrackNotFoundException(String msg) {
super(msg, FriendlyException.Severity.COMMON, null);
}

}

0 comments on commit f7fa002

Please sign in to comment.