Search in sources :

Example 21 with FriendlyException

use of com.sedmelluq.discord.lavaplayer.tools.FriendlyException 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 22 with FriendlyException

use of com.sedmelluq.discord.lavaplayer.tools.FriendlyException 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 23 with FriendlyException

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

the class DefaultYoutubeTrackDetailsLoader method loadTrackInfoFromMainPage.

protected JsonBrowser loadTrackInfoFromMainPage(HttpInterface httpInterface, String videoId) throws IOException {
    String url = "https://www.youtube.com/watch?v=" + videoId + PBJ_PARAMETER + "&hl=en";
    try (CloseableHttpResponse response = httpInterface.execute(new HttpGet(url))) {
        HttpClientTools.assertSuccessWithContent(response, "video page response");
        String responseText = EntityUtils.toString(response.getEntity(), UTF_8);
        try {
            return JsonBrowser.parse(responseText);
        } catch (FriendlyException e) {
            throw e;
        } catch (Exception e) {
            throw new FriendlyException("Received unexpected response from YouTube.", SUSPICIOUS, new RuntimeException("Failed to parse: " + responseText, e));
        }
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) IOException(java.io.IOException) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException)

Example 24 with FriendlyException

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

the class NicoAudioSourceManager method checkLoggedIn.

void checkLoggedIn() {
    synchronized (loggedIn) {
        if (loggedIn.get()) {
            return;
        }
        HttpPost loginRequest = new HttpPost("https://secure.nicovideo.jp/secure/login");
        loginRequest.setEntity(new UrlEncodedFormEntity(Arrays.asList(new BasicNameValuePair("mail", email), new BasicNameValuePair("password", password)), StandardCharsets.UTF_8));
        try (HttpInterface httpInterface = getHttpInterface()) {
            try (CloseableHttpResponse response = httpInterface.execute(loginRequest)) {
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode != 302) {
                    throw new IOException("Unexpected response code " + statusCode);
                }
                Header location = response.getFirstHeader("Location");
                if (location == null || location.getValue().contains("message=")) {
                    throw new FriendlyException("Login details for NicoNico are invalid.", COMMON, null);
                }
                loggedIn.set(true);
            }
        } catch (IOException e) {
            throw new FriendlyException("Exception when trying to log into NicoNico", SUSPICIOUS, e);
        }
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpInterface(com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface) Header(org.apache.http.Header) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException)

Example 25 with FriendlyException

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

the class DefaultSoundCloudHtmlDataLoader method load.

@Override
public JsonBrowser load(HttpInterface httpInterface, String url) throws IOException {
    try (CloseableHttpResponse response = httpInterface.execute(new HttpGet(url))) {
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
            return JsonBrowser.NULL_BROWSER;
        }
        HttpClientTools.assertSuccessWithContent(response, "video page response");
        String html = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
        String rootData = DataFormatTools.extractBetween(html, JSON_RANGES);
        if (rootData == null) {
            throw new FriendlyException("This url does not appear to be a playable track.", SUSPICIOUS, ExceptionTools.throwWithDebugInfo(log, null, "No track JSON found", "html", html));
        }
        return JsonBrowser.parse(rootData);
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException)

Aggregations

FriendlyException (com.sedmelluq.discord.lavaplayer.tools.FriendlyException)62 IOException (java.io.IOException)23 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)18 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)13 AudioLoadResultHandler (com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler)12 JsonBrowser (com.sedmelluq.discord.lavaplayer.tools.JsonBrowser)12 AudioPlaylist (com.sedmelluq.discord.lavaplayer.track.AudioPlaylist)12 ArrayList (java.util.ArrayList)10 HttpGet (org.apache.http.client.methods.HttpGet)10 List (java.util.List)7 HttpInterface (com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface)6 BasicAudioPlaylist (com.sedmelluq.discord.lavaplayer.track.BasicAudioPlaylist)5 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 MediaContainerHints (com.sedmelluq.discord.lavaplayer.container.MediaContainerHints)3 PersistentHttpStream (com.sedmelluq.discord.lavaplayer.tools.io.PersistentHttpStream)3 AudioReference (com.sedmelluq.discord.lavaplayer.track.AudioReference)3 AudioTrackInfo (com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo)3 Document (org.jsoup.nodes.Document)3 Track (com.arsenarsen.lavaplayerbridge.player.Track)2