Search in sources :

Example 26 with JsonBrowser

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

the class YoutubeAudioSourceManager method buildPlaylist.

private AudioPlaylist buildPlaylist(HttpInterface httpInterface, Document document, String selectedVideoId) throws IOException {
    boolean isAccessible = !document.select("#pl-header").isEmpty();
    if (!isAccessible) {
        if (selectedVideoId != null) {
            return null;
        } else {
            throw new FriendlyException("The playlist is private.", COMMON, null);
        }
    }
    Element container = document.select("#pl-header").first().parent();
    String playlistName = container.select(".pl-header-title").first().text();
    List<AudioTrack> tracks = new ArrayList<>();
    String loadMoreUrl = extractPlaylistTracks(container, container, tracks);
    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 (loadMoreUrl != null && ++loadCount < pageCount) {
        try (CloseableHttpResponse response = httpInterface.execute(new HttpGet("https://www.youtube.com" + loadMoreUrl))) {
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != 200) {
                throw new IOException("Invalid status code for playlist response: " + statusCode);
            }
            JsonBrowser json = JsonBrowser.parse(response.getEntity().getContent());
            String html = json.get("content_html").text();
            Element videoContainer = Jsoup.parse("<table>" + html + "</table>", "");
            String moreHtml = json.get("load_more_widget_html").text();
            Element moreContainer = moreHtml != null ? Jsoup.parse(moreHtml) : null;
            loadMoreUrl = extractPlaylistTracks(videoContainer, moreContainer, tracks);
        }
    }
    return new BasicAudioPlaylist(playlistName, tracks, findSelectedTrack(tracks, selectedVideoId), false);
}
Also used : BasicAudioPlaylist(com.sedmelluq.discord.lavaplayer.track.BasicAudioPlaylist) Element(org.jsoup.nodes.Element) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) JsonBrowser(com.sedmelluq.discord.lavaplayer.tools.JsonBrowser)

Example 27 with JsonBrowser

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

the class YoutubeAudioTrack method loadTrackFormats.

private List<YoutubeTrackFormat> loadTrackFormats(JsonBrowser info, HttpInterface httpInterface, String playerScript) throws Exception {
    JsonBrowser args = info.safeGet("args");
    String adaptiveFormats = args.safeGet("adaptive_fmts").text();
    if (adaptiveFormats != null) {
        return loadTrackFormatsFromAdaptive(adaptiveFormats);
    }
    String dashUrl = args.safeGet("dashmpd").text();
    if (dashUrl != null) {
        return loadTrackFormatsFromDash(dashUrl, httpInterface, playerScript);
    }
    String formatStreamMap = args.safeGet("url_encoded_fmt_stream_map").text();
    if (formatStreamMap != null) {
        return loadTrackFormatsFromFormatStreamMap(formatStreamMap);
    }
    throw new FriendlyException("Unable to play this YouTube track.", SUSPICIOUS, new IllegalStateException("No adaptive formats, no dash, no stream map."));
}
Also used : JsonBrowser(com.sedmelluq.discord.lavaplayer.tools.JsonBrowser) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException)

Example 28 with JsonBrowser

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

the class BandcampAudioSourceManager method loadTrack.

private AudioItem loadTrack(String trackUrl) {
    return extractFromPage(trackUrl, (httpClient, text) -> {
        String bandUrl = readBandUrl(text);
        JsonBrowser trackListInfo = readTrackListInformation(text);
        String artist = trackListInfo.get("artist").text();
        return extractTrack(trackListInfo.get("trackinfo").index(0), bandUrl, artist);
    });
}
Also used : JsonBrowser(com.sedmelluq.discord.lavaplayer.tools.JsonBrowser)

Example 29 with JsonBrowser

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

the class YoutubeSearchMusicProvider method extractMusicData.

private AudioTrack extractMusicData(JsonBrowser json, Function<AudioTrackInfo, AudioTrack> trackFactory) {
    JsonBrowser columns = json.get("musicResponsiveListItemRenderer").get("flexColumns");
    if (columns.isNull()) {
        // Somehow don't get track info, ignore
        return null;
    }
    JsonBrowser firstColumn = columns.index(0).get("musicResponsiveListItemFlexColumnRenderer").get("text").get("runs").index(0);
    String title = firstColumn.get("text").text();
    String videoId = firstColumn.get("navigationEndpoint").get("watchEndpoint").get("videoId").text();
    List<JsonBrowser> secondColumn = columns.index(1).get("musicResponsiveListItemFlexColumnRenderer").get("text").get("runs").values();
    String author = secondColumn.get(0).get("text").text();
    JsonBrowser lastElement = secondColumn.get(secondColumn.size() - 1);
    if (!lastElement.get("navigationEndpoint").isNull()) {
        // The duration element should not have this key, if it does, then duration is probably missing, so return
        return null;
    }
    long duration = DataFormatTools.durationTextToMillis(lastElement.get("text").text());
    AudioTrackInfo info = new AudioTrackInfo(title, author, duration, videoId, false, WATCH_URL_PREFIX + videoId);
    return trackFactory.apply(info);
}
Also used : AudioTrackInfo(com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo) JsonBrowser(com.sedmelluq.discord.lavaplayer.tools.JsonBrowser)

Example 30 with JsonBrowser

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

the class YoutubeSearchProvider method extractPolymerData.

private AudioTrack extractPolymerData(JsonBrowser json, Function<AudioTrackInfo, AudioTrack> trackFactory) {
    JsonBrowser renderer = json.get("videoRenderer");
    if (renderer.isNull()) {
        // Not a track, ignore
        return null;
    }
    String title = renderer.get("title").get("runs").index(0).get("text").text();
    String author = renderer.get("ownerText").get("runs").index(0).get("text").text();
    String lengthText = renderer.get("lengthText").get("simpleText").text();
    if (lengthText == null) {
        // Unknown length means this is a livestream, ignore
        return null;
    }
    long duration = DataFormatTools.durationTextToMillis(lengthText);
    String videoId = renderer.get("videoId").text();
    AudioTrackInfo info = new AudioTrackInfo(title, author, duration, videoId, false, WATCH_URL_PREFIX + videoId);
    return trackFactory.apply(info);
}
Also used : AudioTrackInfo(com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo) 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