Search in sources :

Example 36 with JsonBrowser

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

the class DefaultSoundCloudPlaylistLoader method loadFromSet.

protected AudioPlaylist loadFromSet(HttpInterfaceManager httpInterfaceManager, String playlistWebUrl, Function<AudioTrackInfo, AudioTrack> trackFactory) {
    try (HttpInterface httpInterface = httpInterfaceManager.getInterface()) {
        JsonBrowser rootData = htmlDataLoader.load(httpInterface, playlistWebUrl);
        JsonBrowser playlistData = dataReader.findPlaylistData(rootData);
        return new BasicAudioPlaylist(dataReader.readPlaylistName(playlistData), loadPlaylistTracks(httpInterface, playlistData, trackFactory), null, false);
    } catch (IOException e) {
        throw new FriendlyException("Loading playlist from SoundCloud failed.", SUSPICIOUS, e);
    }
}
Also used : BasicAudioPlaylist(com.sedmelluq.discord.lavaplayer.track.BasicAudioPlaylist) HttpInterface(com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface) IOException(java.io.IOException) JsonBrowser(com.sedmelluq.discord.lavaplayer.tools.JsonBrowser) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException)

Example 37 with JsonBrowser

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

the class SoundCloudAudioSourceManager method loadFromTrackPage.

public AudioTrack loadFromTrackPage(String trackWebUrl) {
    try (HttpInterface httpInterface = getHttpInterface()) {
        JsonBrowser rootData = htmlDataLoader.load(httpInterface, trackWebUrl);
        JsonBrowser trackData = dataReader.findTrackData(rootData);
        if (trackData == null) {
            throw new FriendlyException("This track is not available", COMMON, null);
        }
        return loadFromTrackData(trackData);
    } catch (IOException e) {
        throw new FriendlyException("Loading track from SoundCloud failed.", SUSPICIOUS, e);
    }
}
Also used : HttpInterface(com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface) IOException(java.io.IOException) JsonBrowser(com.sedmelluq.discord.lavaplayer.tools.JsonBrowser) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException)

Example 38 with JsonBrowser

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

the class DefaultYoutubeTrackDetailsLoader method loadTrackArgsFromVideoInfoPage.

protected JsonBrowser loadTrackArgsFromVideoInfoPage(HttpInterface httpInterface, String videoId, String sts) throws IOException {
    String videoApiUrl = "https://youtube.googleapis.com/v/" + videoId;
    String encodedApiUrl = URLEncoder.encode(videoApiUrl, UTF_8.name());
    String url = "https://www.youtube.com/get_video_info?video_id=" + videoId + "&eurl=" + encodedApiUrl + "&hl=en_GB&html5=1&c=ANDROID&cver=16.24";
    if (sts != null) {
        url += "&sts=" + sts;
    }
    JsonBrowser values = JsonBrowser.newMap();
    try (CloseableHttpResponse response = httpInterface.execute(new HttpGet(url))) {
        HttpClientTools.assertSuccessWithContent(response, "video info response");
        for (NameValuePair pair : URLEncodedUtils.parse(response.getEntity())) {
            values.put(pair.getName(), pair.getValue());
        }
    }
    return values;
}
Also used : NameValuePair(org.apache.http.NameValuePair) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) JsonBrowser(com.sedmelluq.discord.lavaplayer.tools.JsonBrowser)

Example 39 with JsonBrowser

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

the class DefaultYoutubeTrackDetailsLoader method getUnplayableReason.

protected String getUnplayableReason(JsonBrowser statusBlock) {
    JsonBrowser playerErrorMessage = statusBlock.get("errorScreen").get("playerErrorMessageRenderer");
    String unplayableReason = statusBlock.get("reason").text();
    if (!playerErrorMessage.get("subreason").isNull()) {
        JsonBrowser subreason = playerErrorMessage.get("subreason");
        if (!subreason.get("simpleText").isNull()) {
            unplayableReason = subreason.get("simpleText").text();
        } else if (!subreason.get("runs").isNull() && subreason.get("runs").isList()) {
            StringBuilder reasonBuilder = new StringBuilder();
            subreason.get("runs").values().forEach(item -> reasonBuilder.append(item.get("text").text()).append('\n'));
            unplayableReason = reasonBuilder.toString();
        }
    }
    return unplayableReason;
}
Also used : FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) HttpPost(org.apache.http.client.methods.HttpPost) COMMON(com.sedmelluq.discord.lavaplayer.tools.FriendlyException.Severity.COMMON) HttpClientTools(com.sedmelluq.discord.lavaplayer.tools.io.HttpClientTools) Logger(org.slf4j.Logger) UTF_8(java.nio.charset.StandardCharsets.UTF_8) LoggerFactory(org.slf4j.LoggerFactory) ExceptionTools(com.sedmelluq.discord.lavaplayer.tools.ExceptionTools) StringEntity(org.apache.http.entity.StringEntity) HttpInterface(com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface) IOException(java.io.IOException) YoutubeTrackJsonData.fromEmbedParts(com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeTrackJsonData.fromEmbedParts) PBJ_PARAMETER(com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeHttpContextFilter.PBJ_PARAMETER) EntityUtils(org.apache.http.util.EntityUtils) ExceptionTools.throwWithDebugInfo(com.sedmelluq.discord.lavaplayer.tools.ExceptionTools.throwWithDebugInfo) URLEncoder(java.net.URLEncoder) DataFormatTools(com.sedmelluq.discord.lavaplayer.tools.DataFormatTools) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) URLEncodedUtils(org.apache.http.client.utils.URLEncodedUtils) SUSPICIOUS(com.sedmelluq.discord.lavaplayer.tools.FriendlyException.Severity.SUSPICIOUS) NameValuePair(org.apache.http.NameValuePair) JsonBrowser(com.sedmelluq.discord.lavaplayer.tools.JsonBrowser) JsonBrowser(com.sedmelluq.discord.lavaplayer.tools.JsonBrowser)

Example 40 with JsonBrowser

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

the class DefaultYoutubePlaylistLoader method extractPlaylistTracks.

private String extractPlaylistTracks(JsonBrowser playlistVideoList, List<AudioTrack> tracks, Function<AudioTrackInfo, AudioTrack> trackFactory) {
    if (playlistVideoList.isNull())
        return null;
    final List<JsonBrowser> playlistTrackEntries = playlistVideoList.values();
    for (JsonBrowser track : playlistTrackEntries) {
        JsonBrowser item = track.get("playlistVideoRenderer");
        JsonBrowser shortBylineText = item.get("shortBylineText");
        // If the shortBylineText property does not exist, it means the Track is Region blocked
        if (!item.get("isPlayable").isNull() && !shortBylineText.isNull()) {
            String videoId = item.get("videoId").text();
            JsonBrowser titleField = item.get("title");
            String title = Optional.ofNullable(titleField.get("simpleText").text()).orElse(titleField.get("runs").index(0).get("text").text());
            String author = shortBylineText.get("runs").index(0).get("text").text();
            JsonBrowser lengthSeconds = item.get("lengthSeconds");
            long duration = Units.secondsToMillis(lengthSeconds.asLong(Units.DURATION_SEC_UNKNOWN));
            AudioTrackInfo info = new AudioTrackInfo(title, author, duration, videoId, false, "https://www.youtube.com/watch?v=" + videoId);
            tracks.add(trackFactory.apply(info));
        }
    }
    JsonBrowser continuations = playlistTrackEntries.get(playlistTrackEntries.size() - 1).get("continuationItemRenderer").get("continuationEndpoint").get("continuationCommand");
    String continuationsToken;
    if (!continuations.isNull()) {
        continuationsToken = continuations.get("token").text();
        return continuationsToken;
    }
    return null;
}
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