Java Code Examples for com.sedmelluq.discord.lavaplayer.tools.JsonBrowser#values()

The following examples show how to use com.sedmelluq.discord.lavaplayer.tools.JsonBrowser#values() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: YoutubeMixProvider.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
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));
  }
}
 
Example 2
Source File: DefaultSoundCloudDataReader.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
protected JsonBrowser findEntryOfKind(JsonBrowser data, String kind) {
  for (JsonBrowser value : data.values()) {
    for (JsonBrowser entry : value.get("data").values()) {
      if (entry.isMap() && kind.equals(entry.get("kind").text())) {
        return entry;
      }
    }
  }

  return null;
}
 
Example 3
Source File: DefaultYoutubePlaylistLoader.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
private String extractPlaylistTracks(JsonBrowser playlistVideoList, List<AudioTrack> tracks,
                                     Function<AudioTrackInfo, AudioTrack> trackFactory) {

  JsonBrowser trackArray = playlistVideoList.get("contents");

  if (trackArray.isNull()) return null;

  for (JsonBrowser track : trackArray.values()) {
    JsonBrowser item = track.get("playlistVideoRenderer");

    JsonBrowser shortBylineText = item.get("shortBylineText");

    // If the isPlayable property does not exist, it means the video is removed or private
    // 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();
      String title = item.get("title").get("simpleText").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 = playlistVideoList.get("continuations");

  if (!continuations.isNull()) {
    String continuationsToken = continuations.index(0).get("nextContinuationData").get("continuation").text();
    return "/browse_ajax?continuation=" + continuationsToken + "&ctoken=" + continuationsToken + "&hl=en";
  }

  return null;
}
 
Example 4
Source File: DefaultYoutubeTrackDetails.java    From lavaplayer with Apache License 2.0 4 votes vote down vote up
private List<YoutubeTrackFormat> loadTrackFormatsFromStreamingData(JsonBrowser formats, boolean isLive) {
  List<YoutubeTrackFormat> tracks = new ArrayList<>();
  boolean anyFailures = false;

  if (!formats.isNull() && formats.isList()) {
    for (JsonBrowser formatJson : formats.values()) {
      String cipher = formatJson.get("cipher").text();

      if (cipher == null) {
        cipher = formatJson.get("signatureCipher").text();
      }

      Map<String, String> cipherInfo = cipher != null
          ? decodeUrlEncodedItems(cipher, true)
          : Collections.emptyMap();

      try {
        long contentLength = formatJson.get("contentLength").asLong(CONTENT_LENGTH_UNKNOWN);

        if (contentLength == CONTENT_LENGTH_UNKNOWN && !isLive) {
          log.debug("Track not a live stream, but no contentLength in format {}, skipping", formatJson.format());
          continue;
        }

        tracks.add(new YoutubeTrackFormat(
            ContentType.parse(formatJson.get("mimeType").text()),
            formatJson.get("bitrate").asLong(Units.BITRATE_UNKNOWN),
            contentLength,
            cipherInfo.getOrDefault("url", formatJson.get("url").text()),
            cipherInfo.get("s"),
            cipherInfo.getOrDefault("sp", DEFAULT_SIGNATURE_KEY)
        ));
      } catch (RuntimeException e) {
        anyFailures = true;
        log.debug("Failed to parse format {}, skipping", formatJson, e);
      }
    }
  }

  if (tracks.isEmpty() && anyFailures) {
    log.warn("In streamingData adaptive formats {}, all formats either failed to load or were skipped due to missing " +
        "fields", formats.format());
  }

  return tracks;
}