Search in sources :

Example 1 with SpotifyTrackRedirectionRunnable

use of net.robinfriedli.aiode.audio.exec.SpotifyTrackRedirectionRunnable in project aiode by robinfriedli.

the class PlayableFactory method createPlayables.

/**
 * Creates Playables for a Collection of Objects; YouTube videos or Spotify Tracks.
 *
 * @param redirectSpotify if true the matching YouTube video is loaded to play the full track using
 *                        {@link YouTubeService#redirectSpotify(HollowYouTubeVideo)}, else a {@link PlayableTrackWrapper} is created to play the
 *                        preview mp3 provided by Spotify
 * @param items           the objects to create a Playable for
 * @return the created Playables
 */
public List<Playable> createPlayables(boolean redirectSpotify, Collection<?> items) {
    List<Playable> playables = Lists.newArrayList();
    List<HollowYouTubeVideo> tracksToRedirect = Lists.newArrayList();
    List<YouTubePlaylist> youTubePlaylistsToLoad = Lists.newArrayList();
    try {
        for (Object item : items) {
            if (item instanceof Playable) {
                playables.add((Playable) item);
            } else if (item instanceof Track) {
                handleTrack(SpotifyTrack.wrap((Track) item), redirectSpotify, tracksToRedirect, playables);
            } else if (item instanceof Episode) {
                handleTrack(SpotifyTrack.wrap((Episode) item), redirectSpotify, tracksToRedirect, playables);
            } else if (item instanceof SpotifyTrack) {
                handleTrack((SpotifyTrack) item, redirectSpotify, tracksToRedirect, playables);
            } else if (item instanceof UrlTrack) {
                playables.add(((UrlTrack) item).asPlayable());
            } else if (item instanceof YouTubePlaylist) {
                YouTubePlaylist youTubePlaylist = ((YouTubePlaylist) item);
                playables.addAll(youTubePlaylist.getVideos());
                youTubePlaylistsToLoad.add(youTubePlaylist);
            } else if (item instanceof PlaylistSimplified) {
                List<SpotifyTrack> t = SpotifyInvoker.create(spotifyService.getSpotifyApi()).invoke(() -> spotifyService.getPlaylistTracks((PlaylistSimplified) item));
                for (SpotifyTrack track : t) {
                    handleTrack(track, redirectSpotify, tracksToRedirect, playables);
                }
            } else if (item instanceof AlbumSimplified) {
                List<Track> t = invoker.invoke(() -> spotifyService.getAlbumTracks((AlbumSimplified) item));
                for (Track track : t) {
                    handleTrack(SpotifyTrack.wrapIfNotNull(track), redirectSpotify, tracksToRedirect, playables);
                }
            } else if (item instanceof AudioTrack) {
                playables.add(new UrlPlayable((AudioTrack) item));
            } else if (item instanceof AudioPlaylist) {
                List<Playable> convertedPlayables = ((AudioPlaylist) item).getTracks().stream().map(UrlPlayable::new).collect(Collectors.toList());
                playables.addAll(convertedPlayables);
            } else if (item != null) {
                throw new UnsupportedOperationException("Unsupported playable " + item.getClass());
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Exception while creating Playables", e);
    }
    if (!tracksToRedirect.isEmpty() || !youTubePlaylistsToLoad.isEmpty()) {
        trackLoadingExecutor.execute(new SpotifyTrackRedirectionRunnable(tracksToRedirect, youTubeService).andThen(new YouTubePlaylistPopulationRunnable(youTubePlaylistsToLoad, youTubeService)));
    }
    return playables;
}
Also used : NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) NotFoundException(se.michaelthelin.spotify.exceptions.detailed.NotFoundException) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) ParseException(org.apache.hc.core5.http.ParseException) SpotifyWebApiException(se.michaelthelin.spotify.exceptions.SpotifyWebApiException) IOException(java.io.IOException) Episode(se.michaelthelin.spotify.model_objects.specification.Episode) SpotifyTrack(net.robinfriedli.aiode.audio.spotify.SpotifyTrack) UrlTrack(net.robinfriedli.aiode.entities.UrlTrack) YouTubePlaylist(net.robinfriedli.aiode.audio.youtube.YouTubePlaylist) HollowYouTubeVideo(net.robinfriedli.aiode.audio.youtube.HollowYouTubeVideo) AlbumSimplified(se.michaelthelin.spotify.model_objects.specification.AlbumSimplified) StringList(net.robinfriedli.stringlist.StringList) List(java.util.List) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) SpotifyTrackRedirectionRunnable(net.robinfriedli.aiode.audio.exec.SpotifyTrackRedirectionRunnable) SpotifyTrack(net.robinfriedli.aiode.audio.spotify.SpotifyTrack) Track(se.michaelthelin.spotify.model_objects.specification.Track) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) UrlTrack(net.robinfriedli.aiode.entities.UrlTrack) YouTubePlaylistPopulationRunnable(net.robinfriedli.aiode.audio.exec.YouTubePlaylistPopulationRunnable) PlaylistSimplified(se.michaelthelin.spotify.model_objects.specification.PlaylistSimplified) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist)

Aggregations

AudioPlaylist (com.sedmelluq.discord.lavaplayer.track.AudioPlaylist)1 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)1 IOException (java.io.IOException)1 List (java.util.List)1 SpotifyTrackRedirectionRunnable (net.robinfriedli.aiode.audio.exec.SpotifyTrackRedirectionRunnable)1 YouTubePlaylistPopulationRunnable (net.robinfriedli.aiode.audio.exec.YouTubePlaylistPopulationRunnable)1 SpotifyTrack (net.robinfriedli.aiode.audio.spotify.SpotifyTrack)1 HollowYouTubeVideo (net.robinfriedli.aiode.audio.youtube.HollowYouTubeVideo)1 YouTubePlaylist (net.robinfriedli.aiode.audio.youtube.YouTubePlaylist)1 UrlTrack (net.robinfriedli.aiode.entities.UrlTrack)1 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)1 NoResultsFoundException (net.robinfriedli.aiode.exceptions.NoResultsFoundException)1 StringList (net.robinfriedli.stringlist.StringList)1 ParseException (org.apache.hc.core5.http.ParseException)1 SpotifyWebApiException (se.michaelthelin.spotify.exceptions.SpotifyWebApiException)1 NotFoundException (se.michaelthelin.spotify.exceptions.detailed.NotFoundException)1 AlbumSimplified (se.michaelthelin.spotify.model_objects.specification.AlbumSimplified)1 Episode (se.michaelthelin.spotify.model_objects.specification.Episode)1 PlaylistSimplified (se.michaelthelin.spotify.model_objects.specification.PlaylistSimplified)1 Track (se.michaelthelin.spotify.model_objects.specification.Track)1