Search in sources :

Example 1 with YouTubePlaylist

use of net.robinfriedli.aiode.audio.youtube.YouTubePlaylist in project aiode by robinfriedli.

the class SearchCommand method listYouTubePlaylists.

private void listYouTubePlaylists() throws IOException {
    YouTubeService youTubeService = Aiode.get().getAudioManager().getYouTubeService();
    if (argumentSet("select")) {
        int limit = getArgumentValueWithTypeOrElse("select", Integer.class, 10);
        List<YouTubePlaylist> playlists = youTubeService.searchSeveralPlaylists(limit, getCommandInput());
        if (playlists.size() == 1) {
            listYouTubePlaylist(playlists.get(0));
        } else if (playlists.isEmpty()) {
            throw new NoResultsFoundException(String.format("No YouTube playlist found for '%s'", getCommandInput()));
        } else {
            askQuestion(playlists, YouTubePlaylist::getTitle, YouTubePlaylist::getChannelTitle);
        }
    } else {
        listYouTubePlaylist(youTubeService.searchPlaylist(getCommandInput()));
    }
}
Also used : NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) YouTubePlaylist(net.robinfriedli.aiode.audio.youtube.YouTubePlaylist) YouTubeService(net.robinfriedli.aiode.audio.youtube.YouTubeService)

Example 2 with YouTubePlaylist

use of net.robinfriedli.aiode.audio.youtube.YouTubePlaylist 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 3 with YouTubePlaylist

use of net.robinfriedli.aiode.audio.youtube.YouTubePlaylist 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 4 with YouTubePlaylist

use of net.robinfriedli.aiode.audio.youtube.YouTubePlaylist in project aiode by robinfriedli.

the class AbstractPlayableLoadingCommand method loadYouTubeList.

private void loadYouTubeList(AudioManager audioManager) throws IOException {
    YouTubeService youTubeService = audioManager.getYouTubeService();
    PlayableFactory playableFactory = audioManager.createPlayableFactory(getSpotifyService(), trackLoadingExecutor);
    if (argumentSet("select")) {
        int limit = getArgumentValueWithTypeOrElse("select", Integer.class, 10);
        List<YouTubePlaylist> playlists = youTubeService.searchSeveralPlaylists(limit, getCommandInput());
        if (playlists.size() == 1) {
            YouTubePlaylist playlist = playlists.get(0);
            List<Playable> playables = playableFactory.createPlayables(playlist);
            handleResults(playables);
            loadedYouTubePlaylist = playlist;
        } else if (playlists.isEmpty()) {
            throw new NoResultsFoundException(String.format("No YouTube playlist found for '%s'", getCommandInput()));
        } else {
            askQuestion(playlists, YouTubePlaylist::getTitle, YouTubePlaylist::getChannelTitle);
        }
    } else {
        YouTubePlaylist youTubePlaylist = youTubeService.searchPlaylist(getCommandInput());
        List<Playable> playables = playableFactory.createPlayables(youTubePlaylist);
        handleResults(playables);
        loadedYouTubePlaylist = youTubePlaylist;
    }
}
Also used : PlayableFactory(net.robinfriedli.aiode.audio.PlayableFactory) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) UrlPlayable(net.robinfriedli.aiode.audio.UrlPlayable) Playable(net.robinfriedli.aiode.audio.Playable) YouTubePlaylist(net.robinfriedli.aiode.audio.youtube.YouTubePlaylist) YouTubeService(net.robinfriedli.aiode.audio.youtube.YouTubeService)

Example 5 with YouTubePlaylist

use of net.robinfriedli.aiode.audio.youtube.YouTubePlaylist 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)

Aggregations

YouTubePlaylist (net.robinfriedli.aiode.audio.youtube.YouTubePlaylist)6 List (java.util.List)3 SpotifyTrack (net.robinfriedli.aiode.audio.spotify.SpotifyTrack)3 YouTubeVideo (net.robinfriedli.aiode.audio.youtube.YouTubeVideo)3 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)3 NoResultsFoundException (net.robinfriedli.aiode.exceptions.NoResultsFoundException)3 AlbumSimplified (se.michaelthelin.spotify.model_objects.specification.AlbumSimplified)3 PlaylistSimplified (se.michaelthelin.spotify.model_objects.specification.PlaylistSimplified)3 AudioPlaylist (com.sedmelluq.discord.lavaplayer.track.AudioPlaylist)2 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)2 Playable (net.robinfriedli.aiode.audio.Playable)2 HollowYouTubeVideo (net.robinfriedli.aiode.audio.youtube.HollowYouTubeVideo)2 YouTubeService (net.robinfriedli.aiode.audio.youtube.YouTubeService)2 StringList (net.robinfriedli.stringlist.StringList)2 Episode (se.michaelthelin.spotify.model_objects.specification.Episode)2 ShowSimplified (se.michaelthelin.spotify.model_objects.specification.ShowSimplified)2 Track (se.michaelthelin.spotify.model_objects.specification.Track)2 IOException (java.io.IOException)1 URI (java.net.URI)1 Collection (java.util.Collection)1