Search in sources :

Example 6 with Track

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

use of se.michaelthelin.spotify.model_objects.specification.Track 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 8 with Track

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

the class PlayableFactory method createPlayablesFromSpotifyUrl.

private List<Playable> createPlayablesFromSpotifyUrl(URI uri, SpotifyApi spotifyApi, boolean redirectSpotify) {
    StringList pathFragments = StringList.createWithRegex(uri.getPath(), "/");
    SpotifyService spotifyService = new SpotifyService(spotifyApi);
    if (pathFragments.contains("playlist")) {
        return createPlayableForSpotifyUrlType(pathFragments, "playlist", playlistId -> {
            List<SpotifyTrack> playlistTracks = spotifyService.getPlaylistTracks(playlistId);
            return createPlayables(redirectSpotify, playlistTracks);
        }, spotifyApi);
    } else if (pathFragments.contains("track")) {
        return createPlayableForSpotifyUrlType(pathFragments, "track", trackId -> {
            Track track = spotifyApi.getTrack(trackId).build().execute();
            return Lists.newArrayList(createPlayable(redirectSpotify, track));
        }, spotifyApi);
    } else if (pathFragments.contains("episode")) {
        return createPlayableForSpotifyUrlType(pathFragments, "episode", episodeId -> {
            Episode episode = spotifyApi.getEpisode(episodeId).build().execute();
            return Lists.newArrayList(createPlayable(redirectSpotify, episode));
        }, spotifyApi);
    } else if (pathFragments.contains("album")) {
        return createPlayableForSpotifyUrlType(pathFragments, "album", albumId -> {
            List<Track> albumTracks = spotifyService.getAlbumTracks(albumId);
            return createPlayables(redirectSpotify, albumTracks);
        }, spotifyApi);
    } else if (pathFragments.contains("show")) {
        return createPlayableForSpotifyUrlType(pathFragments, "show", showId -> {
            List<Episode> showEpisodes = spotifyService.getShowEpisodes(showId);
            return createPlayables(redirectSpotify, showEpisodes);
        }, spotifyApi);
    } else {
        throw new InvalidCommandException("Detected Spotify URL but no track, playlist or album id provided.");
    }
}
Also used : StringList(net.robinfriedli.stringlist.StringList) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) YouTubePlaylist(net.robinfriedli.aiode.audio.youtube.YouTubePlaylist) SpotifyInvoker(net.robinfriedli.aiode.function.SpotifyInvoker) SpotifyTrack(net.robinfriedli.aiode.audio.spotify.SpotifyTrack) Episode(se.michaelthelin.spotify.model_objects.specification.Episode) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist) PlayableTrackWrapper(net.robinfriedli.aiode.audio.spotify.PlayableTrackWrapper) Strings(com.google.common.base.Strings) NotFoundException(se.michaelthelin.spotify.exceptions.detailed.NotFoundException) Lists(com.google.common.collect.Lists) HollowYouTubeVideo(net.robinfriedli.aiode.audio.youtube.HollowYouTubeVideo) Map(java.util.Map) YouTubeVideo(net.robinfriedli.aiode.audio.youtube.YouTubeVideo) Track(se.michaelthelin.spotify.model_objects.specification.Track) URI(java.net.URI) SpotifyTrackRedirectionRunnable(net.robinfriedli.aiode.audio.exec.SpotifyTrackRedirectionRunnable) Nullable(javax.annotation.Nullable) Collection(java.util.Collection) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) ParseException(org.apache.hc.core5.http.ParseException) SpotifyWebApiException(se.michaelthelin.spotify.exceptions.SpotifyWebApiException) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) SpotifyTrackKind(net.robinfriedli.aiode.audio.spotify.SpotifyTrackKind) StandardCharsets(java.nio.charset.StandardCharsets) AudioItem(com.sedmelluq.discord.lavaplayer.track.AudioItem) SpotifyApi(se.michaelthelin.spotify.SpotifyApi) YouTubePlaylistPopulationRunnable(net.robinfriedli.aiode.audio.exec.YouTubePlaylistPopulationRunnable) List(java.util.List) SpotifyService(net.robinfriedli.aiode.audio.spotify.SpotifyService) PlaylistSimplified(se.michaelthelin.spotify.model_objects.specification.PlaylistSimplified) URLEncodedUtils(org.apache.http.client.utils.URLEncodedUtils) AlbumSimplified(se.michaelthelin.spotify.model_objects.specification.AlbumSimplified) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) TrackLoadingExecutor(net.robinfriedli.aiode.audio.exec.TrackLoadingExecutor) NameValuePair(org.apache.http.NameValuePair) UrlTrack(net.robinfriedli.aiode.entities.UrlTrack) Collections(java.util.Collections) YouTubeService(net.robinfriedli.aiode.audio.youtube.YouTubeService) CheckedFunction(net.robinfriedli.aiode.function.CheckedFunction) Episode(se.michaelthelin.spotify.model_objects.specification.Episode) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) StringList(net.robinfriedli.stringlist.StringList) SpotifyTrack(net.robinfriedli.aiode.audio.spotify.SpotifyTrack) SpotifyService(net.robinfriedli.aiode.audio.spotify.SpotifyService) StringList(net.robinfriedli.stringlist.StringList) List(java.util.List) 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)

Example 9 with Track

use of se.michaelthelin.spotify.model_objects.specification.Track in project spotify-web-api-java by spotify-web-api-java.

the class GetTrackExample method getTrack_Async.

public static void getTrack_Async() {
    try {
        final CompletableFuture<Track> trackFuture = getTrackRequest.executeAsync();
        // Thread free to do other tasks...
        // Example Only. Never block in production code.
        final Track track = trackFuture.join();
        System.out.println("Name: " + track.getName());
    } catch (CompletionException e) {
        System.out.println("Error: " + e.getCause().getMessage());
    } catch (CancellationException e) {
        System.out.println("Async operation cancelled.");
    }
}
Also used : CancellationException(java.util.concurrent.CancellationException) CompletionException(java.util.concurrent.CompletionException) Track(se.michaelthelin.spotify.model_objects.specification.Track)

Example 10 with Track

use of se.michaelthelin.spotify.model_objects.specification.Track in project spotify-web-api-java by spotify-web-api-java.

the class GetTrackExample method getTrack_Sync.

public static void getTrack_Sync() {
    try {
        final Track track = getTrackRequest.execute();
        System.out.println("Name: " + track.getName());
    } catch (IOException | SpotifyWebApiException | ParseException e) {
        System.out.println("Error: " + e.getMessage());
    }
}
Also used : IOException(java.io.IOException) ParseException(org.apache.hc.core5.http.ParseException) Track(se.michaelthelin.spotify.model_objects.specification.Track) SpotifyWebApiException(se.michaelthelin.spotify.exceptions.SpotifyWebApiException)

Aggregations

Track (se.michaelthelin.spotify.model_objects.specification.Track)17 List (java.util.List)8 SpotifyTrack (net.robinfriedli.aiode.audio.spotify.SpotifyTrack)8 IOException (java.io.IOException)7 StringList (net.robinfriedli.stringlist.StringList)7 Episode (se.michaelthelin.spotify.model_objects.specification.Episode)7 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)6 YouTubePlaylist (net.robinfriedli.aiode.audio.youtube.YouTubePlaylist)6 AlbumSimplified (se.michaelthelin.spotify.model_objects.specification.AlbumSimplified)6 PlaylistSimplified (se.michaelthelin.spotify.model_objects.specification.PlaylistSimplified)6 AudioPlaylist (com.sedmelluq.discord.lavaplayer.track.AudioPlaylist)5 SneakyThrows (lombok.SneakyThrows)5 YouTubeVideo (net.robinfriedli.aiode.audio.youtube.YouTubeVideo)5 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)5 NoResultsFoundException (net.robinfriedli.aiode.exceptions.NoResultsFoundException)5 AudioItem (com.sedmelluq.discord.lavaplayer.track.AudioItem)4 YouTubeService (net.robinfriedli.aiode.audio.youtube.YouTubeService)4 NoSpotifyResultsFoundException (net.robinfriedli.aiode.exceptions.NoSpotifyResultsFoundException)4 ParseException (org.apache.hc.core5.http.ParseException)4 SpotifyWebApiException (se.michaelthelin.spotify.exceptions.SpotifyWebApiException)4