Search in sources :

Example 1 with PlayableFactory

use of net.robinfriedli.aiode.audio.PlayableFactory in project aiode by robinfriedli.

the class Playlist method getTracks.

/**
 * Returns the items in this playlist as objects supported by the {@link PlayableFactory} class. Note that getting the
 * Spotify track for a Song requires this method to be invoked with client credentials
 */
public List<Object> getTracks(SpotifyApi spotifyApi) {
    List<PlaylistItem> playlistItems = getItemsSorted();
    SpotifyTrackBulkLoadingService service = new SpotifyTrackBulkLoadingService(spotifyApi);
    Map<Object, Integer> itemsWithIndex = new HashMap<>();
    for (int i = 0; i < playlistItems.size(); i++) {
        PlaylistItem item = playlistItems.get(i);
        if (item instanceof Song) {
            String id = ((Song) item).getId();
            int finalI = i;
            service.add(createItem(id, TRACK), track -> itemsWithIndex.put(track, finalI));
        } else if (item instanceof Episode) {
            String id = ((Episode) item).getId();
            int finalI = i;
            service.add(createItem(id, EPISODE), track -> itemsWithIndex.put(track, finalI));
        } else if (item instanceof Video) {
            Video video = (Video) item;
            YouTubeVideo youtubeVideo = video.asYouTubeVideo();
            itemsWithIndex.put(youtubeVideo, i);
            String spotifyId = video.getRedirectedSpotifyId();
            if (!Strings.isNullOrEmpty(spotifyId)) {
                SpotifyItemKind kindEntity = video.getRedirectedSpotifyKind();
                SpotifyTrackKind kind = kindEntity != null ? kindEntity.asEnum() : TRACK;
                service.add(createItem(spotifyId, kind), youtubeVideo::setRedirectedSpotifyTrack);
            }
        } else if (item instanceof UrlTrack) {
            itemsWithIndex.put(item, i);
        }
    }
    service.perform();
    return itemsWithIndex.keySet().stream().sorted(Comparator.comparing(itemsWithIndex::get)).collect(Collectors.toList());
}
Also used : Size(javax.validation.constraints.Size) PlayableFactory(net.robinfriedli.aiode.audio.PlayableFactory) SpotifyTrack(net.robinfriedli.aiode.audio.spotify.SpotifyTrack) HashMap(java.util.HashMap) User(net.dv8tion.jda.api.entities.User) Strings(com.google.common.base.Strings) Table(javax.persistence.Table) Lists(com.google.common.collect.Lists) Guild(net.dv8tion.jda.api.entities.Guild) Map(java.util.Map) YouTubeVideo(net.robinfriedli.aiode.audio.youtube.YouTubeVideo) Id(javax.persistence.Id) Entity(javax.persistence.Entity) Collection(java.util.Collection) OneToMany(javax.persistence.OneToMany) Set(java.util.Set) Collectors(java.util.stream.Collectors) SpotifyTrackKind(net.robinfriedli.aiode.audio.spotify.SpotifyTrackKind) SpotifyTrackBulkLoadingService(net.robinfriedli.aiode.audio.spotify.SpotifyTrackBulkLoadingService) Serializable(java.io.Serializable) Objects(java.util.Objects) SpotifyApi(se.michaelthelin.spotify.SpotifyApi) Column(javax.persistence.Column) GenerationType(javax.persistence.GenerationType) List(java.util.List) Stream(java.util.stream.Stream) GeneratedValue(javax.persistence.GeneratedValue) SpringPropertiesConfig(net.robinfriedli.aiode.boot.SpringPropertiesConfig) Comparator(java.util.Comparator) Sets(com.google.api.client.util.Sets) HashMap(java.util.HashMap) SpotifyTrackBulkLoadingService(net.robinfriedli.aiode.audio.spotify.SpotifyTrackBulkLoadingService) YouTubeVideo(net.robinfriedli.aiode.audio.youtube.YouTubeVideo) SpotifyTrackKind(net.robinfriedli.aiode.audio.spotify.SpotifyTrackKind) YouTubeVideo(net.robinfriedli.aiode.audio.youtube.YouTubeVideo)

Example 2 with PlayableFactory

use of net.robinfriedli.aiode.audio.PlayableFactory in project aiode by robinfriedli.

the class AbstractPlayableLoadingCommand method createPlayableForTrack.

private void createPlayableForTrack(Track track, AudioManager audioManager) {
    PlayableFactory playableFactory = audioManager.createPlayableFactory(getSpotifyService(), trackLoadingExecutor);
    Playable playable = playableFactory.createPlayable(shouldRedirectSpotify(), track);
    handleResults(Lists.newArrayList(playable));
    loadedTrack = playable;
}
Also used : PlayableFactory(net.robinfriedli.aiode.audio.PlayableFactory) UrlPlayable(net.robinfriedli.aiode.audio.UrlPlayable) Playable(net.robinfriedli.aiode.audio.Playable)

Example 3 with PlayableFactory

use of net.robinfriedli.aiode.audio.PlayableFactory in project aiode by robinfriedli.

the class AbstractPlayableLoadingCommand method loadUrlItems.

private void loadUrlItems(AudioManager audioManager, AudioPlayback playback) throws IOException {
    PlayableFactory playableFactory = audioManager.createPlayableFactory(getSpotifyService(), trackLoadingExecutor);
    List<Playable> playables = playableFactory.createPlayables(getCommandInput(), getContext().getSpotifyApi(), shouldRedirectSpotify());
    if (playables.isEmpty()) {
        throw new NoResultsFoundException("Result is empty!");
    }
    handleResults(playables);
    loadedAmount = playables.size();
}
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)

Example 4 with PlayableFactory

use of net.robinfriedli.aiode.audio.PlayableFactory in project aiode by robinfriedli.

the class AbstractPlayableLoadingCommand method loadSpotifyShow.

private void loadSpotifyShow(AudioManager audioManager) throws Exception {
    int limit = getArgumentValueWithTypeOrElse("select", Integer.class, 20);
    Callable<List<ShowSimplified>> albumLoadCallable = () -> getSpotifyService().searchShow(getCommandInput(), argumentSet("own"), limit);
    List<ShowSimplified> shows;
    if (argumentSet("own")) {
        shows = runWithLogin(albumLoadCallable);
    } else {
        shows = runWithCredentials(albumLoadCallable);
    }
    if (shows.size() == 1) {
        ShowSimplified show = shows.get(0);
        List<Episode> episodes = runWithCredentials(() -> getSpotifyService().getShowEpisodes(show.getId()));
        PlayableFactory playableFactory = audioManager.createPlayableFactory(getSpotifyService(), trackLoadingExecutor);
        List<Playable> playables = playableFactory.createPlayables(shouldRedirectSpotify(), episodes);
        handleResults(playables);
        loadedShow = show;
    } else if (shows.isEmpty()) {
        throw new NoSpotifyResultsFoundException(String.format("No shows found for '%s'", getCommandInput()));
    } else {
        askQuestion(shows, ShowSimplified::getName, ShowSimplified::getPublisher);
    }
}
Also used : Episode(se.michaelthelin.spotify.model_objects.specification.Episode) PlayableFactory(net.robinfriedli.aiode.audio.PlayableFactory) ShowSimplified(se.michaelthelin.spotify.model_objects.specification.ShowSimplified) UrlPlayable(net.robinfriedli.aiode.audio.UrlPlayable) Playable(net.robinfriedli.aiode.audio.Playable) StringList(net.robinfriedli.stringlist.StringList) List(java.util.List) NoSpotifyResultsFoundException(net.robinfriedli.aiode.exceptions.NoSpotifyResultsFoundException)

Example 5 with PlayableFactory

use of net.robinfriedli.aiode.audio.PlayableFactory in project aiode by robinfriedli.

the class AbstractPlayableLoadingCommand method createPlayableForEpisode.

private void createPlayableForEpisode(Episode episode, AudioManager audioManager) {
    PlayableFactory playableFactory = audioManager.createPlayableFactory(getSpotifyService(), trackLoadingExecutor);
    Playable playable = playableFactory.createPlayable(shouldRedirectSpotify(), episode);
    handleResults(Lists.newArrayList(playable));
    loadedTrack = playable;
}
Also used : PlayableFactory(net.robinfriedli.aiode.audio.PlayableFactory) UrlPlayable(net.robinfriedli.aiode.audio.UrlPlayable) Playable(net.robinfriedli.aiode.audio.Playable)

Aggregations

PlayableFactory (net.robinfriedli.aiode.audio.PlayableFactory)13 Playable (net.robinfriedli.aiode.audio.Playable)12 UrlPlayable (net.robinfriedli.aiode.audio.UrlPlayable)8 NoResultsFoundException (net.robinfriedli.aiode.exceptions.NoResultsFoundException)5 AudioManager (net.robinfriedli.aiode.audio.AudioManager)4 List (java.util.List)3 Guild (net.dv8tion.jda.api.entities.Guild)3 Lists (com.google.common.collect.Lists)2 AudioPlayer (com.sedmelluq.discord.lavaplayer.player.AudioPlayer)2 AudioItem (com.sedmelluq.discord.lavaplayer.track.AudioItem)2 AudioPlaylist (com.sedmelluq.discord.lavaplayer.track.AudioPlaylist)2 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)2 Collection (java.util.Collection)2 Aiode (net.robinfriedli.aiode.Aiode)2 AudioPlayback (net.robinfriedli.aiode.audio.AudioPlayback)2 AudioQueue (net.robinfriedli.aiode.audio.AudioQueue)2 AudioTrackLoader (net.robinfriedli.aiode.audio.AudioTrackLoader)2 BlockingTrackLoadingExecutor (net.robinfriedli.aiode.audio.exec.BlockingTrackLoadingExecutor)2 SpotifyService (net.robinfriedli.aiode.audio.spotify.SpotifyService)2 SpotifyUri (net.robinfriedli.aiode.audio.spotify.SpotifyUri)2