Search in sources :

Example 1 with SpotifyService

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);
}
Also used : SpotifyItemKind(net.robinfriedli.aiode.entities.SpotifyItemKind) SpotifyService(net.robinfriedli.aiode.audio.spotify.SpotifyService) YouTubeService(net.robinfriedli.aiode.audio.youtube.YouTubeService) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) PlayableTrackWrapper(net.robinfriedli.aiode.audio.spotify.PlayableTrackWrapper) Playable(net.robinfriedli.aiode.audio.Playable) SpotifyTrackKind(net.robinfriedli.aiode.audio.spotify.SpotifyTrackKind) SpotifyTrack(net.robinfriedli.aiode.audio.spotify.SpotifyTrack) PlaybackHistorySource(net.robinfriedli.aiode.entities.PlaybackHistorySource)

Example 2 with SpotifyService

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.");
    }
}
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 3 with SpotifyService

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();
}
Also used : PlayableFactory(net.robinfriedli.aiode.audio.PlayableFactory) UrlPlayable(net.robinfriedli.aiode.audio.UrlPlayable) Playable(net.robinfriedli.aiode.audio.Playable) SpotifyUri(net.robinfriedli.aiode.audio.spotify.SpotifyUri) SpotifyService(net.robinfriedli.aiode.audio.spotify.SpotifyService)

Aggregations

SpotifyService (net.robinfriedli.aiode.audio.spotify.SpotifyService)3 Playable (net.robinfriedli.aiode.audio.Playable)2 PlayableTrackWrapper (net.robinfriedli.aiode.audio.spotify.PlayableTrackWrapper)2 SpotifyTrack (net.robinfriedli.aiode.audio.spotify.SpotifyTrack)2 SpotifyTrackKind (net.robinfriedli.aiode.audio.spotify.SpotifyTrackKind)2 YouTubeService (net.robinfriedli.aiode.audio.youtube.YouTubeService)2 Strings (com.google.common.base.Strings)1 Lists (com.google.common.collect.Lists)1 FriendlyException (com.sedmelluq.discord.lavaplayer.tools.FriendlyException)1 AudioItem (com.sedmelluq.discord.lavaplayer.track.AudioItem)1 AudioPlaylist (com.sedmelluq.discord.lavaplayer.track.AudioPlaylist)1 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)1 IOException (java.io.IOException)1 URI (java.net.URI)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1