Search in sources :

Example 1 with PlaylistSimplified

use of se.michaelthelin.spotify.model_objects.specification.PlaylistSimplified in project aiode by robinfriedli.

the class QueueCommand method getPlayablesForOption.

private List<Playable> getPlayablesForOption(Object chosenOption, PlayableFactory playableFactory) throws Exception {
    if (chosenOption instanceof Track || chosenOption instanceof YouTubeVideo || chosenOption instanceof Episode) {
        Playable track = playableFactory.createPlayable(shouldRedirectSpotify(), chosenOption);
        loadedTrack = track;
        return Collections.singletonList(track);
    } else if (chosenOption instanceof PlaylistSimplified) {
        PlaylistSimplified playlist = (PlaylistSimplified) chosenOption;
        List<SpotifyTrack> tracks = runWithCredentials(() -> getSpotifyService().getPlaylistTracks(playlist));
        List<Playable> playables = playableFactory.createPlayables(shouldRedirectSpotify(), tracks);
        loadedSpotifyPlaylist = playlist;
        return playables;
    } else if (chosenOption instanceof YouTubePlaylist) {
        YouTubePlaylist youTubePlaylist = (YouTubePlaylist) chosenOption;
        List<Playable> playables = playableFactory.createPlayables(youTubePlaylist);
        loadedYouTubePlaylist = youTubePlaylist;
        return playables;
    } else if (chosenOption instanceof AlbumSimplified) {
        AlbumSimplified album = (AlbumSimplified) chosenOption;
        List<Track> tracks = runWithCredentials(() -> getSpotifyService().getAlbumTracks(album.getId()));
        List<Playable> playables = playableFactory.createPlayables(shouldRedirectSpotify(), tracks);
        loadedAlbum = album;
        return playables;
    } else if (chosenOption instanceof AudioTrack) {
        Playable playable = playableFactory.createPlayable(shouldRedirectSpotify(), chosenOption);
        loadedAudioTrack = (AudioTrack) chosenOption;
        return Collections.singletonList(playable);
    } else if (chosenOption instanceof AudioPlaylist) {
        List<Playable> playables = playableFactory.createPlayables(shouldRedirectSpotify(), chosenOption);
        loadedAudioPlaylist = (AudioPlaylist) chosenOption;
        return playables;
    } else if (chosenOption instanceof ShowSimplified) {
        ShowSimplified show = (ShowSimplified) chosenOption;
        List<Episode> episodes = runWithCredentials(() -> getSpotifyService().getShowEpisodes(show.getId()));
        List<Playable> playables = playableFactory.createPlayables(shouldRedirectSpotify(), episodes);
        loadedShow = show;
        return playables;
    }
    throw new UnsupportedOperationException("Unsupported chosen option type: " + chosenOption.getClass());
}
Also used : ShowSimplified(se.michaelthelin.spotify.model_objects.specification.ShowSimplified) Episode(se.michaelthelin.spotify.model_objects.specification.Episode) Playable(net.robinfriedli.aiode.audio.Playable) YouTubePlaylist(net.robinfriedli.aiode.audio.youtube.YouTubePlaylist) AlbumSimplified(se.michaelthelin.spotify.model_objects.specification.AlbumSimplified) List(java.util.List) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) YouTubeVideo(net.robinfriedli.aiode.audio.youtube.YouTubeVideo) SpotifyTrack(net.robinfriedli.aiode.audio.spotify.SpotifyTrack) Track(se.michaelthelin.spotify.model_objects.specification.Track) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) PlaylistSimplified(se.michaelthelin.spotify.model_objects.specification.PlaylistSimplified) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist)

Example 2 with PlaylistSimplified

use of se.michaelthelin.spotify.model_objects.specification.PlaylistSimplified 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)

Example 3 with PlaylistSimplified

use of se.michaelthelin.spotify.model_objects.specification.PlaylistSimplified in project aiode by robinfriedli.

the class SearchCommand method withUserResponse.

@Override
public void withUserResponse(Object chosenOption) throws Exception {
    if (chosenOption instanceof Collection) {
        throw new InvalidCommandException("Cannot select more than one result");
    }
    if (chosenOption instanceof PlaylistSimplified) {
        PlaylistSimplified playlist = (PlaylistSimplified) chosenOption;
        List<SpotifyTrack> tracks = runWithCredentials(() -> getSpotifyService().getPlaylistTracks(playlist));
        listTracks(tracks, playlist.getName(), playlist.getOwner().getDisplayName(), null, "playlist/" + playlist.getId());
    } else if (chosenOption instanceof YouTubePlaylist) {
        listYouTubePlaylist((YouTubePlaylist) chosenOption);
    } else if (chosenOption instanceof YouTubeVideo) {
        listYouTubeVideo((YouTubeVideo) chosenOption);
    } else if (chosenOption instanceof AlbumSimplified) {
        AlbumSimplified album = (AlbumSimplified) chosenOption;
        List<SpotifyTrack> tracks = runWithCredentials(() -> getSpotifyService().getAlbumTracks(album.getId())).stream().filter(Objects::nonNull).map(SpotifyTrack::wrap).collect(Collectors.toList());
        listTracks(tracks, album.getName(), null, StringList.create(album.getArtists(), ArtistSimplified::getName).toSeparatedString(", "), "album/" + album.getId());
    } else if (chosenOption instanceof ShowSimplified) {
        ShowSimplified show = (ShowSimplified) chosenOption;
        List<SpotifyTrack> tracks = runWithCredentials(() -> getSpotifyService().getShowEpisodes(show.getId())).stream().filter(Objects::nonNull).map(SpotifyTrack::wrap).collect(Collectors.toList());
        listTracks(tracks, show.getName(), show.getPublisher(), null, "show/" + show.getId());
    }
}
Also used : ShowSimplified(se.michaelthelin.spotify.model_objects.specification.ShowSimplified) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) SpotifyTrack(net.robinfriedli.aiode.audio.spotify.SpotifyTrack) YouTubePlaylist(net.robinfriedli.aiode.audio.youtube.YouTubePlaylist) Objects(java.util.Objects) Collection(java.util.Collection) AlbumSimplified(se.michaelthelin.spotify.model_objects.specification.AlbumSimplified) StringList(net.robinfriedli.stringlist.StringList) List(java.util.List) YouTubeVideo(net.robinfriedli.aiode.audio.youtube.YouTubeVideo) PlaylistSimplified(se.michaelthelin.spotify.model_objects.specification.PlaylistSimplified)

Example 4 with PlaylistSimplified

use of se.michaelthelin.spotify.model_objects.specification.PlaylistSimplified in project aiode by robinfriedli.

the class SearchCommand method listSpotifyList.

private void listSpotifyList() throws Exception {
    String commandBody = getCommandInput();
    if (commandBody.isBlank()) {
        throw new InvalidCommandException("Command may not be empty when searching spotify lists");
    }
    List<PlaylistSimplified> playlists;
    int limit = getArgumentValueWithTypeOrElse("select", Integer.class, 20);
    if (argumentSet("own")) {
        playlists = runWithLogin(() -> getSpotifyService().searchOwnPlaylist(getCommandInput(), limit));
    } else {
        playlists = runWithCredentials(() -> getSpotifyService().searchPlaylist(getCommandInput(), limit));
    }
    if (playlists.size() == 1) {
        PlaylistSimplified playlist = playlists.get(0);
        List<SpotifyTrack> tracks = runWithCredentials(() -> getSpotifyService().getPlaylistTracks(playlist));
        listTracks(tracks, playlist.getName(), playlist.getOwner().getDisplayName(), null, "playlist/" + playlist.getId());
    } else if (playlists.isEmpty()) {
        throw new NoSpotifyResultsFoundException(String.format("No Spotify playlist found for '%s'", getCommandInput()));
    } else {
        askQuestion(playlists, PlaylistSimplified::getName, p -> p.getOwner().getDisplayName());
    }
}
Also used : PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem) StringList(net.robinfriedli.stringlist.StringList) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) YouTubePlaylist(net.robinfriedli.aiode.audio.youtube.YouTubePlaylist) SpotifyTrack(net.robinfriedli.aiode.audio.spotify.SpotifyTrack) Session(org.hibernate.Session) Episode(se.michaelthelin.spotify.model_objects.specification.Episode) Callable(java.util.concurrent.Callable) ErrorResponse(net.dv8tion.jda.api.requests.ErrorResponse) User(net.dv8tion.jda.api.entities.User) Util(net.robinfriedli.aiode.util.Util) Playlist(net.robinfriedli.aiode.entities.Playlist) AbstractSourceDecidingCommand(net.robinfriedli.aiode.command.commands.AbstractSourceDecidingCommand) YouTubeVideo(net.robinfriedli.aiode.audio.youtube.YouTubeVideo) SearchEngine(net.robinfriedli.aiode.util.SearchEngine) Track(se.michaelthelin.spotify.model_objects.specification.Track) CommandContribution(net.robinfriedli.aiode.entities.xml.CommandContribution) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) UnavailableResourceException(net.robinfriedli.aiode.exceptions.UnavailableResourceException) ArtistSimplified(se.michaelthelin.spotify.model_objects.specification.ArtistSimplified) CommandManager(net.robinfriedli.aiode.command.CommandManager) Collection(java.util.Collection) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) IOException(java.io.IOException) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Collectors(java.util.stream.Collectors) NoSpotifyResultsFoundException(net.robinfriedli.aiode.exceptions.NoSpotifyResultsFoundException) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) Aiode(net.robinfriedli.aiode.Aiode) ShowSimplified(se.michaelthelin.spotify.model_objects.specification.ShowSimplified) URLEncoder(java.net.URLEncoder) List(java.util.List) SpringPropertiesConfig(net.robinfriedli.aiode.boot.SpringPropertiesConfig) CommandContext(net.robinfriedli.aiode.command.CommandContext) PlaylistSimplified(se.michaelthelin.spotify.model_objects.specification.PlaylistSimplified) EmbedTable(net.robinfriedli.aiode.util.EmbedTable) AlbumSimplified(se.michaelthelin.spotify.model_objects.specification.AlbumSimplified) YouTubeService(net.robinfriedli.aiode.audio.youtube.YouTubeService) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) SpotifyTrack(net.robinfriedli.aiode.audio.spotify.SpotifyTrack) NoSpotifyResultsFoundException(net.robinfriedli.aiode.exceptions.NoSpotifyResultsFoundException) PlaylistSimplified(se.michaelthelin.spotify.model_objects.specification.PlaylistSimplified)

Aggregations

List (java.util.List)4 SpotifyTrack (net.robinfriedli.aiode.audio.spotify.SpotifyTrack)4 YouTubePlaylist (net.robinfriedli.aiode.audio.youtube.YouTubePlaylist)4 AlbumSimplified (se.michaelthelin.spotify.model_objects.specification.AlbumSimplified)4 PlaylistSimplified (se.michaelthelin.spotify.model_objects.specification.PlaylistSimplified)4 YouTubeVideo (net.robinfriedli.aiode.audio.youtube.YouTubeVideo)3 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)3 StringList (net.robinfriedli.stringlist.StringList)3 Episode (se.michaelthelin.spotify.model_objects.specification.Episode)3 ShowSimplified (se.michaelthelin.spotify.model_objects.specification.ShowSimplified)3 Track (se.michaelthelin.spotify.model_objects.specification.Track)3 AudioPlaylist (com.sedmelluq.discord.lavaplayer.track.AudioPlaylist)2 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)2 IOException (java.io.IOException)2 Collection (java.util.Collection)2 Objects (java.util.Objects)2 NoResultsFoundException (net.robinfriedli.aiode.exceptions.NoResultsFoundException)2 URLEncoder (java.net.URLEncoder)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Callable (java.util.concurrent.Callable)1