Search in sources :

Example 11 with Episode

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

the class PlayableFactory method createPlayable.

/**
 * Create a single playable for any url. If the url is either an open.spotify or YouTube URL this extracts the ID
 * and uses the familiar methods to load the Playables, otherwise this method uses the {@link AudioTrackLoader}
 * to load the {@link AudioTrack}s using lavaplayer and wraps them in {@link UrlPlayable}s
 */
@Nullable
public Playable createPlayable(String url, SpotifyApi spotifyApi, boolean redirectSpotify) throws IOException {
    URI uri;
    try {
        uri = URI.create(url);
    } catch (IllegalArgumentException e) {
        throw new InvalidCommandException("'" + url + "' is not a valid URL");
    }
    if (uri.getHost().contains("youtube.com")) {
        Map<String, String> parameterMap = getParameterMap(uri);
        String videoId = parameterMap.get("v");
        if (videoId != null) {
            return youTubeService.getVideoForId(videoId);
        } else {
            throw new IllegalArgumentException("Detected YouTube URL but no video id provided");
        }
    } else if (uri.getHost().equals("youtu.be")) {
        String[] parts = uri.getPath().split("/");
        return youTubeService.requireVideoForId(parts[parts.length - 1]);
    } else if (uri.getHost().equals("open.spotify.com")) {
        StringList pathFragments = StringList.createWithRegex(uri.getPath(), "/");
        SpotifyTrackKind kind;
        String trackId;
        if (pathFragments.contains("track")) {
            trackId = pathFragments.tryGet(pathFragments.indexOf("track") + 1);
            kind = SpotifyTrackKind.TRACK;
        } else if (pathFragments.contains("episode")) {
            trackId = pathFragments.tryGet(pathFragments.indexOf("episode") + 1);
            kind = SpotifyTrackKind.EPISODE;
        } else {
            throw new IllegalArgumentException("Detected Spotify URL but no track id provided");
        }
        if (trackId == null) {
            throw new InvalidCommandException("No track id provided");
        }
        try {
            String accessToken = spotifyApi.clientCredentials().build().execute().getAccessToken();
            spotifyApi.setAccessToken(accessToken);
            if (kind == SpotifyTrackKind.TRACK) {
                Track track = spotifyService.getTrack(trackId);
                return createPlayable(redirectSpotify, track);
            } else // noinspection ConstantConditions
            if (kind == SpotifyTrackKind.EPISODE) {
                Episode episode = spotifyService.getEpisode(trackId);
                return createPlayable(redirectSpotify, episode);
            } else {
                throw new UnsupportedOperationException("unsupported open.spotify URL kind: " + kind);
            }
        } catch (IOException | SpotifyWebApiException | ParseException e) {
            throw new RuntimeException("Exception during Spotify request", e);
        } finally {
            spotifyApi.setAccessToken(null);
        }
    } else {
        AudioItem audioItem = audioTrackLoader.loadByIdentifier(uri.toString());
        if (audioItem instanceof AudioTrack) {
            return new UrlPlayable((AudioTrack) audioItem);
        } else if (audioItem != null) {
            throw new IllegalArgumentException("Loading provided url did not result in an AudioTrack but " + audioItem.getClass().toString());
        } else {
            return null;
        }
    }
}
Also used : StringList(net.robinfriedli.stringlist.StringList) IOException(java.io.IOException) URI(java.net.URI) AudioItem(com.sedmelluq.discord.lavaplayer.track.AudioItem) SpotifyWebApiException(se.michaelthelin.spotify.exceptions.SpotifyWebApiException) Episode(se.michaelthelin.spotify.model_objects.specification.Episode) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) SpotifyTrackKind(net.robinfriedli.aiode.audio.spotify.SpotifyTrackKind) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) ParseException(org.apache.hc.core5.http.ParseException) 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) Nullable(javax.annotation.Nullable)

Aggregations

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