Search in sources :

Example 26 with AudioTrack

use of com.sedmelluq.discord.lavaplayer.track.AudioTrack in project lavaplayer by sedmelluq.

the class Main method loadAndPlay.

private void loadAndPlay(final TextChannel channel, final String trackUrl) {
    GuildMusicManager musicManager = getGuildAudioPlayer(channel.getGuild());
    playerManager.loadItemOrdered(musicManager, trackUrl, new AudioLoadResultHandler() {

        @Override
        public void trackLoaded(AudioTrack track) {
            channel.sendMessage("Adding to queue " + track.getInfo().title).queue();
            play(channel.getGuild(), musicManager, track);
        }

        @Override
        public void playlistLoaded(AudioPlaylist playlist) {
            AudioTrack firstTrack = playlist.getSelectedTrack();
            if (firstTrack == null) {
                firstTrack = playlist.getTracks().get(0);
            }
            channel.sendMessage("Adding to queue " + firstTrack.getInfo().title + " (first track of playlist " + playlist.getName() + ")").queue();
            play(channel.getGuild(), musicManager, firstTrack);
        }

        @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) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException)

Example 27 with AudioTrack

use of com.sedmelluq.discord.lavaplayer.track.AudioTrack in project lavaplayer by sedmelluq.

the class YoutubeMixProvider method load.

/**
 * Loads tracks from mix in parallel into a playlist entry.
 *
 * @param mixId ID of the mix
 * @param selectedVideoId Selected track, {@link AudioPlaylist#getSelectedTrack()} will return this.
 * @return Playlist of the tracks in the mix.
 */
public AudioPlaylist load(HttpInterface httpInterface, String mixId, String selectedVideoId, Function<AudioTrackInfo, AudioTrack> trackFactory) {
    String playlistTitle = "YouTube mix";
    List<AudioTrack> tracks = new ArrayList<>();
    String mixUrl = "https://www.youtube.com/watch?v=" + selectedVideoId + "&list=" + mixId + PBJ_PARAMETER;
    try (CloseableHttpResponse response = httpInterface.execute(new HttpGet(mixUrl))) {
        HttpClientTools.assertSuccessWithContent(response, "mix response");
        JsonBrowser body = JsonBrowser.parse(response.getEntity().getContent());
        JsonBrowser playlist = body.index(3).get("response").get("contents").get("twoColumnWatchNextResults").get("playlist").get("playlist");
        JsonBrowser title = playlist.get("title");
        if (!title.isNull()) {
            playlistTitle = title.text();
        }
        extractPlaylistTracks(playlist.get("contents"), tracks, trackFactory);
    } catch (IOException e) {
        throw new FriendlyException("Could not read mix page.", SUSPICIOUS, e);
    }
    if (tracks.isEmpty()) {
        throw new FriendlyException("Could not find tracks from mix.", SUSPICIOUS, null);
    }
    AudioTrack selectedTrack = findSelectedTrack(tracks, selectedVideoId);
    return new BasicAudioPlaylist(playlistTitle, tracks, selectedTrack, false);
}
Also used : BasicAudioPlaylist(com.sedmelluq.discord.lavaplayer.track.BasicAudioPlaylist) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) IOException(java.io.IOException) JsonBrowser(com.sedmelluq.discord.lavaplayer.tools.JsonBrowser) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException)

Example 28 with AudioTrack

use of com.sedmelluq.discord.lavaplayer.track.AudioTrack in project lavaplayer by sedmelluq.

the class YoutubeSearchMusicProvider method extractMusicTracks.

private List<AudioTrack> extractMusicTracks(Document document, Function<AudioTrackInfo, AudioTrack> trackFactory) throws IOException {
    Matcher matcher = ytMusicDataRegex.matcher(document.outerHtml());
    if (!matcher.find()) {
        log.warn("Failed to match ytMusicData JSON object");
        return Collections.emptyList();
    }
    JsonBrowser jsonBrowser = JsonBrowser.parse(matcher.group(1));
    ArrayList<AudioTrack> list = new ArrayList<>();
    JsonBrowser tracks = jsonBrowser.get("contents").get("sectionListRenderer").get("contents").index(0).get("musicShelfRenderer").get("contents");
    if (tracks == JsonBrowser.NULL_BROWSER) {
        tracks = jsonBrowser.get("contents").get("sectionListRenderer").get("contents").index(1).get("musicShelfRenderer").get("contents");
    }
    tracks.values().forEach(json -> {
        AudioTrack track = extractMusicData(json, trackFactory);
        if (track != null)
            list.add(track);
    });
    return list;
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) JsonBrowser(com.sedmelluq.discord.lavaplayer.tools.JsonBrowser)

Example 29 with AudioTrack

use of com.sedmelluq.discord.lavaplayer.track.AudioTrack in project lavaplayer by sedmelluq.

the class YoutubeSearchProvider method polymerExtractTracks.

private List<AudioTrack> polymerExtractTracks(Document document, Function<AudioTrackInfo, AudioTrack> trackFactory) throws IOException {
    // Match the JSON from the HTML. It should be within a script tag
    Matcher matcher = polymerInitialDataRegex.matcher(document.outerHtml());
    if (!matcher.find()) {
        log.warn("Failed to match ytInitialData JSON object");
        return Collections.emptyList();
    }
    JsonBrowser jsonBrowser = JsonBrowser.parse(matcher.group(2));
    ArrayList<AudioTrack> list = new ArrayList<>();
    jsonBrowser.get("contents").get("twoColumnSearchResultsRenderer").get("primaryContents").get("sectionListRenderer").get("contents").index(0).get("itemSectionRenderer").get("contents").values().forEach(json -> {
        AudioTrack track = extractPolymerData(json, trackFactory);
        if (track != null)
            list.add(track);
    });
    return list;
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) JsonBrowser(com.sedmelluq.discord.lavaplayer.tools.JsonBrowser)

Example 30 with AudioTrack

use of com.sedmelluq.discord.lavaplayer.track.AudioTrack in project lavaplayer by sedmelluq.

the class DefaultYoutubePlaylistLoader method buildPlaylist.

private AudioPlaylist buildPlaylist(HttpInterface httpInterface, JsonBrowser json, String selectedVideoId, Function<AudioTrackInfo, AudioTrack> trackFactory) throws IOException {
    JsonBrowser jsonResponse = json.index(1).get("response");
    String errorAlertMessage = findErrorAlert(jsonResponse);
    if (errorAlertMessage != null) {
        throw new FriendlyException(errorAlertMessage, COMMON, null);
    }
    JsonBrowser info = jsonResponse.get("sidebar").get("playlistSidebarRenderer").get("items").index(0).get("playlistSidebarPrimaryInfoRenderer");
    String playlistName = info.get("title").get("runs").index(0).get("text").text();
    JsonBrowser playlistVideoList = jsonResponse.get("contents").get("twoColumnBrowseResultsRenderer").get("tabs").index(0).get("tabRenderer").get("content").get("sectionListRenderer").get("contents").index(0).get("itemSectionRenderer").get("contents").index(0).get("playlistVideoListRenderer").get("contents");
    List<AudioTrack> tracks = new ArrayList<>();
    String continuationsToken = extractPlaylistTracks(playlistVideoList, tracks, trackFactory);
    int loadCount = 0;
    int pageCount = playlistPageCount;
    // Also load the next pages, each result gives us a JSON with separate values for list html and next page loader html
    while (continuationsToken != null && ++loadCount < pageCount) {
        HttpPost post = new HttpPost(REQUEST_URL);
        StringEntity payload = new StringEntity(String.format(REQUEST_PAYLOAD, continuationsToken), "UTF-8");
        post.setEntity(payload);
        try (CloseableHttpResponse response = httpInterface.execute(post)) {
            HttpClientTools.assertSuccessWithContent(response, "playlist response");
            JsonBrowser continuationJson = JsonBrowser.parse(response.getEntity().getContent());
            JsonBrowser playlistVideoListPage = continuationJson.index(1).get("response").get("continuationContents").get("playlistVideoListContinuation");
            if (playlistVideoListPage.isNull()) {
                playlistVideoListPage = continuationJson.get("onResponseReceivedActions").index(0).get("appendContinuationItemsAction").get("continuationItems");
            }
            continuationsToken = extractPlaylistTracks(playlistVideoListPage, tracks, trackFactory);
        }
    }
    return new BasicAudioPlaylist(playlistName, tracks, findSelectedTrack(tracks, selectedVideoId), false);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) BasicAudioPlaylist(com.sedmelluq.discord.lavaplayer.track.BasicAudioPlaylist) ArrayList(java.util.ArrayList) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) JsonBrowser(com.sedmelluq.discord.lavaplayer.tools.JsonBrowser) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException)

Aggregations

AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)85 FriendlyException (com.sedmelluq.discord.lavaplayer.tools.FriendlyException)26 ArrayList (java.util.ArrayList)23 AudioPlaylist (com.sedmelluq.discord.lavaplayer.track.AudioPlaylist)21 AudioLoadResultHandler (com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler)18 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)17 AudioTrackInfo (com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo)11 List (java.util.List)10 JsonBrowser (com.sedmelluq.discord.lavaplayer.tools.JsonBrowser)9 IOException (java.io.IOException)9 BasicAudioPlaylist (com.sedmelluq.discord.lavaplayer.track.BasicAudioPlaylist)8 TimeUnit (java.util.concurrent.TimeUnit)7 GuildMessageReceivedEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)7 YoutubeAudioSourceManager (com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeAudioSourceManager)6 Optional (java.util.Optional)6 MessageBuilder (net.dv8tion.jda.core.MessageBuilder)6 Permission (net.dv8tion.jda.core.Permission)6 AudioPlayer (com.sedmelluq.discord.lavaplayer.player.AudioPlayer)5 InternalAudioTrack (com.sedmelluq.discord.lavaplayer.track.InternalAudioTrack)5 Slf4j (lombok.extern.slf4j.Slf4j)5