Search in sources :

Example 6 with BasicAudioPlaylist

use of com.sedmelluq.discord.lavaplayer.track.BasicAudioPlaylist 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 7 with BasicAudioPlaylist

use of com.sedmelluq.discord.lavaplayer.track.BasicAudioPlaylist in project lavaplayer by sedmelluq.

the class YoutubeMixProvider method loadTracksAsynchronously.

private AudioPlaylist loadTracksAsynchronously(List<String> videoIds, String selectedVideoId) {
    ExecutorCompletionService<AudioItem> completion = new ExecutorCompletionService<>(mixLoadingExecutor);
    List<AudioTrack> tracks = new ArrayList<>();
    for (final String videoId : videoIds) {
        completion.submit(() -> sourceManager.loadTrackWithVideoId(videoId, true));
    }
    try {
        fetchTrackResultsFromExecutor(completion, tracks, videoIds.size());
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    AudioTrack selectedTrack = sourceManager.findSelectedTrack(tracks, selectedVideoId);
    if (tracks.isEmpty()) {
        throw new FriendlyException("No tracks from the mix loaded succesfully.", SUSPICIOUS, null);
    } else if (selectedTrack == null) {
        throw new FriendlyException("The selected track of the mix failed to load.", SUSPICIOUS, null);
    }
    return new BasicAudioPlaylist("YouTube mix", tracks, selectedTrack, false);
}
Also used : BasicAudioPlaylist(com.sedmelluq.discord.lavaplayer.track.BasicAudioPlaylist) ArrayList(java.util.ArrayList) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) AudioItem(com.sedmelluq.discord.lavaplayer.track.AudioItem) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException)

Example 8 with BasicAudioPlaylist

use of com.sedmelluq.discord.lavaplayer.track.BasicAudioPlaylist in project lavaplayer by sedmelluq.

the class YoutubeSearchProvider method extractSearchResults.

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);
    }
}
Also used : BasicAudioPlaylist(com.sedmelluq.discord.lavaplayer.track.BasicAudioPlaylist) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) IOException(java.io.IOException) Elements(org.jsoup.select.Elements)

Example 9 with BasicAudioPlaylist

use of com.sedmelluq.discord.lavaplayer.track.BasicAudioPlaylist in project lavaplayer by sedmelluq.

the class BandcampAudioSourceManager method loadAlbum.

private AudioItem loadAlbum(UrlInfo urlInfo) {
    return extractFromPage(urlInfo.fullUrl, (httpClient, 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, urlInfo.baseUrl, artist));
        }
        JsonBrowser albumInfo = readAlbumInformation(text);
        return new BasicAudioPlaylist(albumInfo.get("current").get("title").text(), tracks, null, false);
    });
}
Also used : BasicAudioPlaylist(com.sedmelluq.discord.lavaplayer.track.BasicAudioPlaylist) ArrayList(java.util.ArrayList) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) JsonBrowser(com.sedmelluq.discord.lavaplayer.tools.JsonBrowser)

Example 10 with BasicAudioPlaylist

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

Aggregations

BasicAudioPlaylist (com.sedmelluq.discord.lavaplayer.track.BasicAudioPlaylist)10 ArrayList (java.util.ArrayList)9 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)8 JsonBrowser (com.sedmelluq.discord.lavaplayer.tools.JsonBrowser)6 FriendlyException (com.sedmelluq.discord.lavaplayer.tools.FriendlyException)5 IOException (java.io.IOException)5 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)3 HttpGet (org.apache.http.client.methods.HttpGet)2 Element (org.jsoup.nodes.Element)2 YoutubeAudioSourceManager (com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeAudioSourceManager)1 HttpInterface (com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface)1 AudioItem (com.sedmelluq.discord.lavaplayer.track.AudioItem)1 AudioPlaylist (com.sedmelluq.discord.lavaplayer.track.AudioPlaylist)1 SearchResult (fredboat.db.transfer.SearchResult)1 SearchProvider (fredboat.definitions.SearchProvider)1 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)1 HttpPost (org.apache.http.client.methods.HttpPost)1 StringEntity (org.apache.http.entity.StringEntity)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1