com.sedmelluq.discord.lavaplayer.track.BasicAudioPlaylist Java Examples

The following examples show how to use com.sedmelluq.discord.lavaplayer.track.BasicAudioPlaylist. 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: YoutubeApiPlaylistLoader.java    From SkyBot with GNU Affero General Public License v3.0 6 votes vote down vote up
private AudioPlaylist buildPlaylist(YoutubePlaylistMetadata firstPage, String playlistId, String selectedVideoId,
                                    Function<AudioTrackInfo, AudioTrack> trackFactory) throws IOException {
    final List<AudioTrack> convertedTracks = new ArrayList<>();

    firstPage.getTracks()
        .stream()
        .map(trackFactory)
        .forEach(convertedTracks::add);

    String nextPageKey = firstPage.getNextPageKey();
    int loadCount = 0;
    final int pageCount = playlistPageCount;

    while (nextPageKey != null && ++loadCount < pageCount) {
        nextPageKey = fetchNextPage(nextPageKey, playlistId, trackFactory, convertedTracks);
    }

    return new BasicAudioPlaylist(
        firstPage.getTitle(),
        convertedTracks,
        getSelectedTrack(selectedVideoId, convertedTracks),
        false
    );
}
 
Example #2
Source File: DefaultSoundCloudPlaylistLoader.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
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);
  }
}
 
Example #3
Source File: BandcampAudioSourceManager.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
private AudioItem loadAlbum(String albumUrl) {
  return extractFromPage(albumUrl, (httpClient, text) -> {
    String bandUrl = readBandUrl(text);
    JsonBrowser trackListInfo = readTrackListInformation(text);
    String artist = trackListInfo.get("artist").text();

    List<AudioTrack> tracks = new ArrayList<>();
    for (JsonBrowser trackInfo : trackListInfo.get("trackinfo").values()) {
      tracks.add(extractTrack(trackInfo, bandUrl, artist));
    }

    JsonBrowser albumInfo = readAlbumInformation(text);
    return new BasicAudioPlaylist(albumInfo.get("album_title").text(), tracks, null, false);
  });
}
 
Example #4
Source File: SoundCloudAudioSourceManager.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
private AudioItem extractTracksFromLikedList(JsonBrowser likedTracks, UserInfo userInfo) {
  List<AudioTrack> tracks = new ArrayList<>();

  for (JsonBrowser item : likedTracks.get("collection").values()) {
    JsonBrowser trackItem = item.get("track");

    if (!trackItem.isNull() && !dataReader.isTrackBlocked(trackItem)) {
      tracks.add(loadFromTrackData(trackItem));
    }
  }

  return new BasicAudioPlaylist("Liked by " + userInfo.name, tracks, null, false);
}
 
Example #5
Source File: SoundCloudAudioSourceManager.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
private AudioItem extractTracksFromSearchResults(String query, JsonBrowser searchResults) {
  List<AudioTrack> tracks = new ArrayList<>();

  for (JsonBrowser item : searchResults.get("collection").values()) {
    if (!item.isNull()) {
      tracks.add(loadFromTrackData(item));
    }
  }

  return new BasicAudioPlaylist("Search results for: " + query, tracks, null, true);
}
 
Example #6
Source File: YoutubeSearchProvider.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
private AudioItem extractSearchResults(Document document, String query,
                                       Function<AudioTrackInfo, AudioTrack> trackFactory) {

  List<AudioTrack> tracks = new ArrayList<>();
  Elements resultsSelection = document.select("#page > #content #results");
  if (!resultsSelection.isEmpty()) {
    for (Element results : resultsSelection) {
      for (Element result : results.select(".yt-lockup-video")) {
        if (!result.hasAttr("data-ad-impressions") && result.select(".standalone-ypc-badge-renderer-label").isEmpty()) {
          extractTrackFromResultEntry(tracks, result, trackFactory);
        }
      }
    }
  } else {
    log.debug("Attempting to parse results page as polymer");
    try {
      tracks = polymerExtractTracks(document, trackFactory);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  if (tracks.isEmpty()) {
    return AudioReference.NO_TRACK;
  } else {
    return new BasicAudioPlaylist("Search results for: " + query, tracks, null, true);
  }
}
 
Example #7
Source File: DefaultYoutubePlaylistLoader.java    From lavaplayer with Apache License 2.0 4 votes vote down vote up
private AudioPlaylist buildPlaylist(HttpInterface httpInterface, JsonBrowser json, String selectedVideoId,
                                    Function<AudioTrackInfo, AudioTrack> trackFactory) throws IOException {

  JsonBrowser jsonResponse = json.index(1).get("response");

  JsonBrowser alerts = jsonResponse.get("alerts");

  if (!alerts.isNull()) {
    throw new FriendlyException(alerts.index(0).get("alertRenderer").get("text").get("simpleText").text(), 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");

  List<AudioTrack> tracks = new ArrayList<>();
  String loadMoreUrl = 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 (loadMoreUrl != null && ++loadCount < pageCount) {
    try (CloseableHttpResponse response = httpInterface.execute(new HttpGet("https://www.youtube.com" + loadMoreUrl))) {
      int statusCode = response.getStatusLine().getStatusCode();
      if (!HttpClientTools.isSuccessWithContent(statusCode)) {
        throw new IOException("Invalid status code for playlist response: " + statusCode);
      }

      JsonBrowser continuationJson = JsonBrowser.parse(response.getEntity().getContent());

      JsonBrowser playlistVideoListPage = continuationJson.index(1)
          .get("response")
          .get("continuationContents")
          .get("playlistVideoListContinuation");

      loadMoreUrl = extractPlaylistTracks(playlistVideoListPage, tracks, trackFactory);
    }
  }

  return new BasicAudioPlaylist(playlistName, tracks, findSelectedTrack(tracks, selectedVideoId), false);
}
 
Example #8
Source File: YoutubeMixProvider.java    From lavaplayer with Apache License 2.0 4 votes vote down vote up
/**
 * 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=1";

  try (CloseableHttpResponse response = httpInterface.execute(new HttpGet(mixUrl))) {
    int statusCode = response.getStatusLine().getStatusCode();
    if (!HttpClientTools.isSuccessWithContent(statusCode)) {
      throw new IOException("Invalid status code for mix response: " + statusCode);
    }

    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);
}