use of se.michaelthelin.spotify.model_objects.specification.AlbumSimplified 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());
}
use of se.michaelthelin.spotify.model_objects.specification.AlbumSimplified 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;
}
use of se.michaelthelin.spotify.model_objects.specification.AlbumSimplified in project aiode by robinfriedli.
the class AbstractPlayableLoadingCommand method loadSpotifyAlbum.
private void loadSpotifyAlbum(AudioManager audioManager) throws Exception {
int limit = getArgumentValueWithTypeOrElse("select", Integer.class, 20);
Callable<List<AlbumSimplified>> albumLoadCallable = () -> getSpotifyService().searchAlbum(getCommandInput(), argumentSet("own"), limit);
List<AlbumSimplified> albums;
if (argumentSet("own")) {
albums = runWithLogin(albumLoadCallable);
} else {
albums = runWithCredentials(albumLoadCallable);
}
if (albums.size() == 1) {
AlbumSimplified album = albums.get(0);
List<Track> tracks = runWithCredentials(() -> getSpotifyService().getAlbumTracks(album.getId()));
PlayableFactory playableFactory = audioManager.createPlayableFactory(getSpotifyService(), trackLoadingExecutor);
List<Playable> playables = playableFactory.createPlayables(shouldRedirectSpotify(), tracks);
handleResults(playables);
loadedAlbum = album;
} else if (albums.isEmpty()) {
throw new NoSpotifyResultsFoundException(String.format("No albums found for '%s'", getCommandInput()));
} else {
askQuestion(albums, AlbumSimplified::getName, album -> StringList.create(album.getArtists(), ArtistSimplified::getName).toSeparatedString(", "));
}
}
use of se.michaelthelin.spotify.model_objects.specification.AlbumSimplified 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());
}
}
use of se.michaelthelin.spotify.model_objects.specification.AlbumSimplified in project aiode by robinfriedli.
the class SearchCommand method listSpotifyAlbum.
private void listSpotifyAlbum() throws Exception {
Integer limit = getArgumentValueWithTypeOrElse("select", Integer.class, 20);
Callable<List<AlbumSimplified>> loadAlbumsCallable = () -> getSpotifyService().searchAlbum(getCommandInput(), argumentSet("own"), limit);
List<AlbumSimplified> albums;
if (argumentSet("own")) {
albums = runWithLogin(loadAlbumsCallable);
} else {
albums = runWithCredentials(loadAlbumsCallable);
}
if (albums.size() == 1) {
AlbumSimplified album = albums.get(0);
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 (albums.isEmpty()) {
throw new NoSpotifyResultsFoundException(String.format("No album found for '%s'", getCommandInput()));
} else {
askQuestion(albums, AlbumSimplified::getName, album -> StringList.create(album.getArtists(), ArtistSimplified::getName).toSeparatedString(", "));
}
}
Aggregations