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());
}
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;
}
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();
}
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);
}
}
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;
}
Aggregations