Search in sources :

Example 16 with JsonBrowser

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

Example 17 with JsonBrowser

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

the class DefaultYoutubeTrackDetailsLoader method load.

private YoutubeTrackDetails load(HttpInterface httpInterface, String videoId, boolean requireFormats) throws IOException {
    JsonBrowser mainInfo = loadTrackInfoFromMainPage(httpInterface, videoId);
    try {
        YoutubeTrackJsonData initialData = loadBaseResponse(mainInfo, httpInterface, videoId, requireFormats);
        if (initialData == null) {
            return null;
        }
        YoutubeTrackJsonData finalData = augmentWithPlayerScript(initialData, httpInterface, requireFormats);
        return new DefaultYoutubeTrackDetails(videoId, finalData);
    } catch (FriendlyException e) {
        throw e;
    } catch (Exception e) {
        throw throwWithDebugInfo(log, e, "Error when extracting data", "mainJson", mainInfo.format());
    }
}
Also used : JsonBrowser(com.sedmelluq.discord.lavaplayer.tools.JsonBrowser) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) IOException(java.io.IOException) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException)

Example 18 with JsonBrowser

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

the class DefaultYoutubeTrackDetailsLoader method loadBaseResponse.

protected YoutubeTrackJsonData loadBaseResponse(JsonBrowser mainInfo, HttpInterface httpInterface, String videoId, boolean requireFormats) throws IOException {
    YoutubeTrackJsonData data = YoutubeTrackJsonData.fromMainResult(mainInfo);
    InfoStatus status = checkPlayabilityStatus(data.playerResponse);
    if (status == InfoStatus.DOES_NOT_EXIST) {
        return null;
    }
    if (status == InfoStatus.CONTENT_CHECK_REQUIRED) {
        JsonBrowser trackInfo = loadTrackInfoWithContentVerifyRequest(httpInterface, videoId);
        return YoutubeTrackJsonData.fromMainResult(trackInfo);
    }
    if (requireFormats && status == InfoStatus.REQUIRES_LOGIN) {
        JsonBrowser basicInfo = loadTrackBaseInfoFromEmbedPage(httpInterface, videoId);
        return fromEmbedParts(basicInfo, loadTrackArgsFromVideoInfoPage(httpInterface, videoId, basicInfo.get("sts").text()));
    } else {
        return data;
    }
}
Also used : JsonBrowser(com.sedmelluq.discord.lavaplayer.tools.JsonBrowser)

Example 19 with JsonBrowser

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

the class DefaultSoundCloudDataReader method readTrackFormats.

@Override
public List<SoundCloudTrackFormat> readTrackFormats(JsonBrowser trackData) {
    List<SoundCloudTrackFormat> formats = new ArrayList<>();
    String trackId = readTrackId(trackData);
    if (trackId.isEmpty()) {
        log.warn("Track data {} missing track ID: {}.", trackId, trackData.format());
    }
    for (JsonBrowser transcoding : trackData.get("media").get("transcodings").values()) {
        JsonBrowser format = transcoding.get("format");
        String protocol = format.get("protocol").safeText();
        String mimeType = format.get("mime_type").safeText();
        if (!protocol.isEmpty() && !mimeType.isEmpty()) {
            String lookupUrl = transcoding.get("url").safeText();
            if (!lookupUrl.isEmpty()) {
                formats.add(new DefaultSoundCloudTrackFormat(trackId, protocol, mimeType, lookupUrl));
            } else {
                log.warn("Transcoding of {} missing url: {}.", trackId, transcoding.format());
            }
        } else {
            log.warn("Transcoding of {} missing protocol/mimetype: {}.", trackId, transcoding.format());
        }
    }
    return formats;
}
Also used : ArrayList(java.util.ArrayList) JsonBrowser(com.sedmelluq.discord.lavaplayer.tools.JsonBrowser)

Example 20 with JsonBrowser

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

the class TwitchStreamAudioSourceManager method loadItem.

@Override
public AudioItem loadItem(AudioPlayerManager manager, AudioReference reference) {
    String streamName = getChannelIdentifierFromUrl(reference.identifier);
    if (streamName == null) {
        return null;
    }
    JsonBrowser accessToken = fetchAccessToken(streamName);
    if (accessToken == null || accessToken.get("token").isNull()) {
        return AudioReference.NO_TRACK;
    }
    String channelId;
    try {
        JsonBrowser token = JsonBrowser.parse(accessToken.get("token").text());
        channelId = token.get("channel_id").text();
    } catch (IOException e) {
        return null;
    }
    JsonBrowser channelInfo = fetchStreamChannelInfo(channelId);
    if (channelInfo == null || channelInfo.get("stream").isNull()) {
        return AudioReference.NO_TRACK;
    } else {
        /*
      --- HELIX STUFF
      //Retrieve the data value list; this will have only one element since we're getting only one stream's information
      List<JsonBrowser> dataList = channelInfo.get("data").values();

      //The value list is empty if the stream is offline, even when hosting another channel
      if (dataList.size() == 0){
          return null;
      }

      //The first one has the title of the broadcast
      JsonBrowser channelData = dataList.get(0);
      String status = channelData.get("title").text();
       */
        JsonBrowser channelData = channelInfo.get("stream").get("channel");
        String status = channelData.get("status").text();
        return new TwitchStreamAudioTrack(new AudioTrackInfo(status, streamName, Units.DURATION_MS_UNKNOWN, reference.identifier, true, reference.identifier), this);
    }
}
Also used : AudioTrackInfo(com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo) IOException(java.io.IOException) 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