Search in sources :

Example 6 with JsonBrowser

use of com.sedmelluq.discord.lavaplayer.tools.JsonBrowser in project lavaplayer by sedmelluq.

the class YoutubeMixProvider method extractPlaylistTracks.

private void extractPlaylistTracks(JsonBrowser browser, List<AudioTrack> tracks, Function<AudioTrackInfo, AudioTrack> trackFactory) {
    for (JsonBrowser video : browser.values()) {
        JsonBrowser renderer = video.get("playlistPanelVideoRenderer");
        String title = renderer.get("title").get("simpleText").text();
        String author = renderer.get("longBylineText").get("runs").index(0).get("text").text();
        String durationStr = renderer.get("lengthText").get("simpleText").text();
        long duration = parseDuration(durationStr);
        String identifier = renderer.get("videoId").text();
        String uri = "https://youtube.com/watch?v=" + identifier;
        AudioTrackInfo trackInfo = new AudioTrackInfo(title, author, duration, identifier, false, uri);
        tracks.add(trackFactory.apply(trackInfo));
    }
}
Also used : AudioTrackInfo(com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo) JsonBrowser(com.sedmelluq.discord.lavaplayer.tools.JsonBrowser)

Example 7 with JsonBrowser

use of com.sedmelluq.discord.lavaplayer.tools.JsonBrowser 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 8 with JsonBrowser

use of com.sedmelluq.discord.lavaplayer.tools.JsonBrowser 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 9 with JsonBrowser

use of com.sedmelluq.discord.lavaplayer.tools.JsonBrowser 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 10 with JsonBrowser

use of com.sedmelluq.discord.lavaplayer.tools.JsonBrowser in project lavaplayer by sedmelluq.

the class YoutubeTrackJsonData method fromPolymerPlayerInfo.

private static YoutubeTrackJsonData fromPolymerPlayerInfo(JsonBrowser playerInfo, JsonBrowser playerResponse) {
    JsonBrowser args = playerInfo.get("args");
    String playerScriptUrl = playerInfo.get("assets").get("js").text();
    String playerResponseText = args.get("player_response").text();
    if (playerResponseText == null) {
        // However, if no player_response is available, use the outer playerResponse.
        return new YoutubeTrackJsonData(playerResponse, args, playerScriptUrl);
    }
    return new YoutubeTrackJsonData(parsePlayerResponse(playerResponseText), args, playerScriptUrl);
}
Also used : JsonBrowser(com.sedmelluq.discord.lavaplayer.tools.JsonBrowser)

Aggregations

JsonBrowser (com.sedmelluq.discord.lavaplayer.tools.JsonBrowser)41 FriendlyException (com.sedmelluq.discord.lavaplayer.tools.FriendlyException)14 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)11 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)9 HttpGet (org.apache.http.client.methods.HttpGet)9 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)8 AudioTrackInfo (com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo)7 BasicAudioPlaylist (com.sedmelluq.discord.lavaplayer.track.BasicAudioPlaylist)6 HttpInterface (com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface)4 YoutubeTrackFormat (com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeTrackFormat)2 URISyntaxException (java.net.URISyntaxException)2 Matcher (java.util.regex.Matcher)2 NameValuePair (org.apache.http.NameValuePair)2 HttpPost (org.apache.http.client.methods.HttpPost)2 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)2 StringEntity (org.apache.http.entity.StringEntity)2 PBJ_PARAMETER (com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeHttpContextFilter.PBJ_PARAMETER)1 YoutubeTrackJsonData.fromEmbedParts (com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeTrackJsonData.fromEmbedParts)1 DataFormatTools (com.sedmelluq.discord.lavaplayer.tools.DataFormatTools)1