Search in sources :

Example 11 with FriendlyException

use of com.sedmelluq.discord.lavaplayer.tools.FriendlyException in project FredBoat by Frederikam.

the class SpotifyPlaylistSourceManager method loadItem.

@Override
public AudioItem loadItem(final DefaultAudioPlayerManager manager, final AudioReference ar) {
    String[] data = parse(ar.identifier);
    if (data == null)
        return null;
    final String spotifyUser = data[0];
    final String spotifyListId = data[1];
    PlaylistInfo plData;
    try {
        plData = spotifyAPIWrapper.getPlaylistDataBlocking(spotifyUser, spotifyListId);
    } catch (Exception e) {
        log.warn("Could not retrieve playlist " + spotifyListId + " of user " + spotifyUser, e);
        throw new FriendlyException("Couldn't load playlist. Either Spotify is down or the playlist does not exist.", FriendlyException.Severity.COMMON, e);
    }
    String playlistName = plData.getName();
    if (playlistName == null || "".equals(playlistName))
        playlistName = "Spotify Playlist";
    int tracksTotal = plData.getTotalTracks();
    final List<AudioTrack> trackList = new ArrayList<>();
    final List<String> trackListSearchTerms;
    try {
        trackListSearchTerms = spotifyAPIWrapper.getPlaylistTracksSearchTermsBlocking(spotifyUser, spotifyListId);
    } catch (Exception e) {
        log.warn("Could not retrieve tracks for playlist " + spotifyListId + " of user " + spotifyUser, e);
        throw new FriendlyException("Couldn't load playlist. Either Spotify is down or the playlist does not exist.", FriendlyException.Severity.COMMON, e);
    }
    log.info("Retrieved playlist data for " + playlistName + " from Spotify, loading up " + tracksTotal + " tracks");
    // build a task list
    List<CompletableFuture<AudioTrack>> taskList = new ArrayList<>();
    for (final String s : trackListSearchTerms) {
        // remove all punctuation
        final String query = s.replaceAll(TrackSearcher.PUNCTUATION_REGEX, "");
        CompletableFuture<AudioTrack> f = CompletableFuture.supplyAsync(() -> searchSingleTrack(query), loader);
        taskList.add(f);
    }
    // build a tracklist from that task list
    for (CompletableFuture<AudioTrack> futureTrack : taskList) {
        try {
            final AudioTrack audioItem = futureTrack.get();
            if (audioItem == null) {
                // skip the track if we couldn't find it
                continue;
            }
            trackList.add(audioItem);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            break;
        } catch (ExecutionException ignored) {
        // this is fine, loop will go for the next item
        }
    }
    return new BasicAudioPlaylist(playlistName, trackList, null, true);
}
Also used : ArrayList(java.util.ArrayList) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) IOException(java.io.IOException) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) PlaylistInfo(fredboat.audio.queue.PlaylistInfo)

Example 12 with FriendlyException

use of com.sedmelluq.discord.lavaplayer.tools.FriendlyException in project FredBoat by Frederikam.

the class SpotifyPlaylistSourceManager method getPlaylistDataBlocking.

@Override
public PlaylistInfo getPlaylistDataBlocking(String identifier) {
    String[] data = parse(identifier);
    if (data == null)
        return null;
    final String spotifyUser = data[0];
    final String spotifyListId = data[1];
    try {
        return spotifyAPIWrapper.getPlaylistDataBlocking(spotifyUser, spotifyListId);
    } catch (Exception e) {
        log.warn("Could not retrieve playlist " + spotifyListId + " of user " + spotifyUser, e);
        throw new FriendlyException("Couldn't load playlist. Either Spotify is down or the playlist does not exist.", FriendlyException.Severity.COMMON, e);
    }
}
Also used : FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) IOException(java.io.IOException) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException)

Example 13 with FriendlyException

use of com.sedmelluq.discord.lavaplayer.tools.FriendlyException in project PorkBot by DaMatrix.

the class AudioUtils method loadAndPlay.

public static void loadAndPlay(final TextChannel channel, final String trackUrl, final Member user, final boolean notify) {
    GuildAudioInfo musicManager = getGuildAudioPlayer(channel.getGuild(), true, user, channel);
    playerManager.loadItemOrdered(musicManager, trackUrl, new AudioLoadResultHandler() {

        @Override
        public void trackLoaded(AudioTrack track) {
            if (notify)
                channel.sendMessage("Adding to queue: " + track.getInfo().title).queue();
            play(channel.getGuild(), musicManager, track, user, channel);
            synchronized (trackUrl) {
                trackUrl.notifyAll();
            }
        }

        @Override
        public void playlistLoaded(AudioPlaylist playlist) {
            List<AudioTrack> tracks = playlist.getTracks();
            if (tracks == null || tracks.size() == 0) {
                channel.sendMessage("Empty or invalid playlist, not loading");
            }
            if (notify)
                channel.sendMessage("Adding " + tracks.size() + " tracks from playlist " + playlist.getName() + " to queue").queue();
            playList(channel.getGuild(), musicManager, tracks, user, channel);
            synchronized (trackUrl) {
                trackUrl.notifyAll();
            }
        }

        @Override
        public void noMatches() {
            channel.sendMessage("Nothing found by " + trackUrl).queue();
        }

        @Override
        public void loadFailed(FriendlyException exception) {
            channel.sendMessage("Could not play: " + exception.getMessage()).queue();
        }
    });
}
Also used : AudioLoadResultHandler(com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) List(java.util.List) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException)

Example 14 with FriendlyException

use of com.sedmelluq.discord.lavaplayer.tools.FriendlyException in project BoltBot by DiscordBolt.

the class VoiceManager method getSongTitle.

public String getSongTitle(String songURL) {
    Semaphore s = new Semaphore(0);
    Info title = new Info();
    playerManager.loadItem(songURL, new AudioLoadResultHandler() {

        @Override
        public void trackLoaded(AudioTrack track) {
            title.title = track.getInfo().title;
            s.release();
        }

        @Override
        public void playlistLoaded(AudioPlaylist playlist) {
            s.release();
        }

        @Override
        public void noMatches() {
            s.release();
        }

        @Override
        public void loadFailed(FriendlyException exception) {
            s.release();
        }
    });
    s.acquireUninterruptibly();
    return title.title;
}
Also used : AudioLoadResultHandler(com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) Semaphore(java.util.concurrent.Semaphore) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException)

Example 15 with FriendlyException

use of com.sedmelluq.discord.lavaplayer.tools.FriendlyException in project Ardent by adamint.

the class TrackScheduler method onException.

private void onException(AudioPlayer player, AudioTrack track, FriendlyException exception) {
    manager.setCurrentlyPlaying(null);
    manager.nextTrack();
    try {
        manager.getChannel().sendMessage("I wasn't able to play that track, skipping... **Reason: **" + exception.getLocalizedMessage()).queue();
    } catch (Exception ex) {
        new BotException(ex);
    }
}
Also used : BotException(tk.ardentbot.core.misc.logging.BotException) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) BotException(tk.ardentbot.core.misc.logging.BotException)

Aggregations

FriendlyException (com.sedmelluq.discord.lavaplayer.tools.FriendlyException)62 IOException (java.io.IOException)23 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)18 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)13 AudioLoadResultHandler (com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler)12 JsonBrowser (com.sedmelluq.discord.lavaplayer.tools.JsonBrowser)12 AudioPlaylist (com.sedmelluq.discord.lavaplayer.track.AudioPlaylist)12 ArrayList (java.util.ArrayList)10 HttpGet (org.apache.http.client.methods.HttpGet)10 List (java.util.List)7 HttpInterface (com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface)6 BasicAudioPlaylist (com.sedmelluq.discord.lavaplayer.track.BasicAudioPlaylist)5 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 MediaContainerHints (com.sedmelluq.discord.lavaplayer.container.MediaContainerHints)3 PersistentHttpStream (com.sedmelluq.discord.lavaplayer.tools.io.PersistentHttpStream)3 AudioReference (com.sedmelluq.discord.lavaplayer.track.AudioReference)3 AudioTrackInfo (com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo)3 Document (org.jsoup.nodes.Document)3 Track (com.arsenarsen.lavaplayerbridge.player.Track)2