use of net.robinfriedli.aiode.audio.spotify.SpotifyService in project aiode by robinfriedli.
the class ChartsCommand method getTrackForRecord.
private Playable getTrackForRecord(Object[] record, Session session) throws Exception {
long sourceEntityPk = (Long) record[0];
PlaybackHistorySource sourceEntity = session.load(PlaybackHistorySource.class, sourceEntityPk);
Playable.Source source = sourceEntity.asEnum();
String id = (String) record[1];
Long spotifyItemKindPk = (Long) record[3];
SpotifyItemKind spotifyItemKind = spotifyItemKindPk != null ? session.load(SpotifyItemKind.class, spotifyItemKindPk) : null;
switch(source) {
case SPOTIFY:
return runWithCredentials(() -> {
if (spotifyItemKind == null) {
throw new IllegalStateException("spotifyItemKind cannot be null for PlaybackHistory entries of source SPOTIFY");
}
SpotifyTrackKind kind = spotifyItemKind.asEnum();
SpotifyService spotifyService = getSpotifyService();
SpotifyTrack track = kind.loadSingleItem(spotifyService, id);
if (track == null) {
return null;
}
return new PlayableTrackWrapper(track);
});
case YOUTUBE:
YouTubeService youTubeService = Aiode.get().getAudioManager().getYouTubeService();
try {
return youTubeService.getVideoForId(id);
} catch (FriendlyException e) {
return null;
}
case URL:
return playableFactory.createPlayable(id, getContext().getSpotifyApi(), false);
}
throw new UnsupportedOperationException("Unsupported source " + sourceEntity);
}
use of net.robinfriedli.aiode.audio.spotify.SpotifyService 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.");
}
}
use of net.robinfriedli.aiode.audio.spotify.SpotifyService in project aiode by robinfriedli.
the class AbstractPlayableLoadingCommand method loadSpotifyUri.
private void loadSpotifyUri(AudioManager audioManager) throws Exception {
SpotifyUri spotifyUri = SpotifyUri.parse(getCommandInput());
SpotifyService spotifyService = getContext().getSpotifyService();
PlayableFactory playableFactory = audioManager.createPlayableFactory(getSpotifyService(), trackLoadingExecutor);
List<Playable> playables = spotifyUri.loadPlayables(playableFactory, spotifyService, shouldRedirectSpotify(), mayInterrupt);
handleResults(playables);
loadedAmount = playables.size();
}
Aggregations