Java Code Examples for com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface#execute()

The following examples show how to use com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface#execute() . 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: YoutubeAudioSourceManager.java    From kyoko with MIT License 6 votes vote down vote up
private JsonBrowser loadTrackBaseInfoFromEmbedPage(HttpInterface httpInterface, String videoId) throws IOException {
    try (CloseableHttpResponse response = httpInterface.execute(new HttpGet("https://www.youtube.com/embed/" + videoId))) {
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != 200) {
            throw new IOException("Invalid status code for embed video page response: " + statusCode);
        }

        String html = IOUtils.toString(response.getEntity().getContent(), Charset.forName(CHARSET));
        String configJson = DataFormatTools.extractBetween(html, "'PLAYER_CONFIG': ", "});writeEmbed();");

        if (configJson != null) {
            return JsonBrowser.parse(configJson);
        }
    }

    throw new FriendlyException("Track information is unavailable.", SUSPICIOUS,
            new IllegalStateException("Expected player config is not present in embed page."));
}
 
Example 2
Source File: DefaultYoutubePlaylistLoader.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
@Override
public AudioPlaylist load(HttpInterface httpInterface, String playlistId, String selectedVideoId,
                          Function<AudioTrackInfo, AudioTrack> trackFactory) {

  HttpGet request = new HttpGet(getPlaylistUrl(playlistId) + "&pbj=1&hl=en");

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

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

    return buildPlaylist(httpInterface, json, selectedVideoId, trackFactory);
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 3
Source File: NicoAudioTrack.java    From kyoko with MIT License 6 votes vote down vote up
private String loadPlaybackUrl(HttpInterface httpInterface) throws IOException {
    HttpGet request = new HttpGet("http://flapi.nicovideo.jp/api/getflv/" + trackInfo.identifier);

    try (CloseableHttpResponse response = httpInterface.execute(request)) {
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != 200) {
            throw new IOException("Unexpected status code from playback parameters page: " + statusCode);
        }

        String text = EntityUtils.toString(response.getEntity());
        Map<String, String> format = convertToMapLayout(URLEncodedUtils.parse(text, StandardCharsets.UTF_8));

        String url = format.get("url");
        try (CloseableHttpResponse testResponse = httpInterface.execute(new HttpGet(url))) {
            int testStatusCode = testResponse.getStatusLine().getStatusCode();
            if (testStatusCode != 200) {
                url = url.replace("/smile?v=", "/smile?m=");
                url += "low";
            }
        }
        return url;
    }
}
 
Example 4
Source File: DefaultYoutubeTrackDetails.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
private List<YoutubeTrackFormat> loadTrackFormatsFromDash(
    String dashUrl,
    HttpInterface httpInterface,
    YoutubeSignatureResolver signatureResolver
) throws Exception {
  String resolvedDashUrl = signatureResolver.resolveDashUrl(httpInterface, getPlayerScript(), dashUrl);

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

    Document document = Jsoup.parse(response.getEntity().getContent(), StandardCharsets.UTF_8.name(), "",
        Parser.xmlParser());
    return loadTrackFormatsFromDashDocument(document);
  }
}
 
Example 5
Source File: SoundCloudClientIdTracker.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
private String findClientIdFromApplicationScript(HttpInterface httpInterface, String scriptUrl) throws IOException {
  try (CloseableHttpResponse response = httpInterface.execute(new HttpGet(scriptUrl))) {
    int statusCode = response.getStatusLine().getStatusCode();
    if (!HttpClientTools.isSuccessWithContent(statusCode)) {
      throw new IOException("Invalid status code for application script response: " + statusCode);
    }

    String page = EntityUtils.toString(response.getEntity());
    Matcher clientIdMatcher = appScriptClientIdPattern.matcher(page);

    if (clientIdMatcher.find()) {
      return clientIdMatcher.group(1);
    } else {
      throw new IllegalStateException("Could not find client ID from application script.");
    }
  }
}
 
Example 6
Source File: HttpStreamTools.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
public static InputStream streamContent(HttpInterface httpInterface, HttpUriRequest request) {
  CloseableHttpResponse response = null;
  boolean success = false;

  try {
    response = httpInterface.execute(request);
    int statusCode = response.getStatusLine().getStatusCode();

    if (!HttpClientTools.isSuccessWithContent(statusCode)) {
      throw new IOException("Invalid status code from " + request.getURI() + " URL: " + statusCode);
    }

    success = true;
    return response.getEntity().getContent();
  } catch (IOException e) {
    throw new RuntimeException(e);
  } finally {
    if (response != null && !success) {
      ExceptionTools.closeWithWarnings(response);
    }
  }
}
 
Example 7
Source File: NicoAudioTrack.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
private void loadVideoMainPage(HttpInterface httpInterface) throws IOException {
  HttpGet request = new HttpGet("http://www.nicovideo.jp/watch/" + trackInfo.identifier);

  try (CloseableHttpResponse response = httpInterface.execute(request)) {
    int statusCode = response.getStatusLine().getStatusCode();
    if (!HttpClientTools.isSuccessWithContent(statusCode)) {
      throw new IOException("Unexpected status code from video main page: " + statusCode);
    }

    EntityUtils.consume(response.getEntity());
  }
}
 
Example 8
Source File: SoundCloudAudioSourceManager.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
private JsonBrowser loadLikedListForUserId(HttpInterface httpInterface, UserInfo userInfo) throws IOException {
  URI uri = URI.create("https://api-v2.soundcloud.com/users/" + userInfo.id + "/likes?limit=200&offset=0");

  try (CloseableHttpResponse response = httpInterface.execute(new HttpGet(uri))) {
    HttpClientTools.assertSuccessWithContent(response, "liked tracks response");
    return JsonBrowser.parse(response.getEntity().getContent());
  }
}
 
Example 9
Source File: SoundCloudAudioSourceManager.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
private UserInfo findUserIdFromLikedList(HttpInterface httpInterface, String likedListUrl) throws IOException {
  try (CloseableHttpResponse response = httpInterface.execute(new HttpGet(likedListUrl))) {
    int statusCode = response.getStatusLine().getStatusCode();

    if (statusCode == HttpStatus.SC_NOT_FOUND) {
      return null;
    } else if (!HttpClientTools.isSuccessWithContent(statusCode)) {
      throw new IOException("Invalid status code for track list response: " + statusCode);
    }

    Matcher matcher = likedUserUrnPattern.matcher(IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8));
    return matcher.find() ? new UserInfo(matcher.group(1), matcher.group(2)) : null;
  }
}
 
Example 10
Source File: DefaultYoutubeTrackDetailsLoader.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
protected Map<String, String> loadTrackArgsFromVideoInfoPage(HttpInterface httpInterface, String videoId, String sts) throws IOException {
  String videoApiUrl = "https://youtube.googleapis.com/v/" + videoId;
  String encodedApiUrl = URLEncoder.encode(videoApiUrl, UTF_8.name());
  String url = "https://www.youtube.com/get_video_info?video_id=" + videoId + "&eurl=" + encodedApiUrl +
          "hl=en_GB";

  if (sts != null) {
    url += "&sts=" + sts;
  }

  try (CloseableHttpResponse response = httpInterface.execute(new HttpGet(url))) {
    HttpClientTools.assertSuccessWithContent(response, "video info response");
    return convertToMapLayout(URLEncodedUtils.parse(response.getEntity()));
  }
}
 
Example 11
Source File: VimeoAudioSourceManager.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
private AudioItem loadFromTrackPage(HttpInterface httpInterface, String trackUrl) throws IOException {
  try (CloseableHttpResponse response = httpInterface.execute(new HttpGet(trackUrl))) {
    int statusCode = response.getStatusLine().getStatusCode();

    if (statusCode == HttpStatus.SC_NOT_FOUND) {
      return AudioReference.NO_TRACK;
    } else if (!HttpClientTools.isSuccessWithContent(statusCode)) {
      throw new FriendlyException("Server responded with an error.", SUSPICIOUS,
          new IllegalStateException("Response code is " + statusCode));
    }

    return loadTrackFromPageContent(trackUrl, IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8));
  }
}
 
Example 12
Source File: VimeoAudioTrack.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
private JsonBrowser loadTrackConfig(HttpInterface httpInterface, String trackAccessInfoUrl) throws IOException {
  try (CloseableHttpResponse response = httpInterface.execute(new HttpGet(trackAccessInfoUrl))) {
    int statusCode = response.getStatusLine().getStatusCode();

    if (!HttpClientTools.isSuccessWithContent(statusCode)) {
      throw new FriendlyException("Server responded with an error.", SUSPICIOUS,
          new IllegalStateException("Response code for track access info is " + statusCode));
    }

    return JsonBrowser.parse(response.getEntity().getContent());
  }
}
 
Example 13
Source File: VimeoAudioTrack.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
private JsonBrowser loadPlayerConfig(HttpInterface httpInterface) throws IOException {
  try (CloseableHttpResponse response = httpInterface.execute(new HttpGet(trackInfo.identifier))) {
    int statusCode = response.getStatusLine().getStatusCode();

    if (!HttpClientTools.isSuccessWithContent(statusCode)) {
      throw new FriendlyException("Server responded with an error.", SUSPICIOUS,
          new IllegalStateException("Response code for player config is " + statusCode));
    }

    return sourceManager.loadConfigJsonFromPageContent(IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8));
  }
}
 
Example 14
Source File: NicoAudioTrack.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
private String loadPlaybackUrl(HttpInterface httpInterface) throws IOException {
  HttpGet request = new HttpGet("http://flapi.nicovideo.jp/api/getflv/" + trackInfo.identifier);

  try (CloseableHttpResponse response = httpInterface.execute(request)) {
    int statusCode = response.getStatusLine().getStatusCode();
    if (!HttpClientTools.isSuccessWithContent(statusCode)) {
      throw new IOException("Unexpected status code from playback parameters page: " + statusCode);
    }

    String text = EntityUtils.toString(response.getEntity());
    Map<String, String> format = convertToMapLayout(URLEncodedUtils.parse(text, StandardCharsets.UTF_8));

    return format.get("url");
  }
}
 
Example 15
Source File: YoutubeAudioSourceManager.java    From kyoko with MIT License 5 votes vote down vote up
/**
 * @param httpInterface HTTP interface to use for performing any necessary request.
 * @param videoId       ID of the video.
 * @param mustExist     If <code>true</code>, throws an exception instead of returning <code>null</code> if the track does
 *                      not exist.
 * @return JSON information about the track if it exists. <code>null</code> if it does not and mustExist is
 * <code>false</code>.
 * @throws IOException On network error.
 */
public JsonBrowser getTrackInfoFromMainPage(HttpInterface httpInterface, String videoId, boolean mustExist) throws IOException {
    try (CloseableHttpResponse response = httpInterface.execute(new HttpGet(getWatchUrl(videoId)))) {
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != 200) {
            throw new IOException("Invalid status code for video page response: " + statusCode);
        }

        String html = IOUtils.toString(response.getEntity().getContent(), Charset.forName(CHARSET));
        String configJson = DataFormatTools.extractBetween(html, "ytplayer.config = ", ";ytplayer.load");

        if (configJson != null) {
            return JsonBrowser.parse(configJson);
        } else {
            if (html.contains("player-age-gate-content\">")) {
                // In case main page does not give player configuration, but info page indicates an OK result, it is probably an
                // age-restricted video for which the complete track info can be combined from the embed page and the info page.
                return getTrackInfoFromEmbedPage(httpInterface, videoId);
            }
        }
    }

    if (determineFailureReason(httpInterface, videoId, mustExist)) {
        return null;
    }

    log.debug("Falling back to embed...");
    return getTrackInfoFromEmbedPage(httpInterface, videoId);
}
 
Example 16
Source File: TwitchStreamSegmentUrlProvider.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
private JsonBrowser loadAccessToken(HttpInterface httpInterface) throws IOException {
  HttpUriRequest request = createSegmentGetRequest("https://api.twitch.tv/api/channels/" + channelName +
      "/access_token?adblock=false&need_https=true&platform=web&player_type=site");

  try (CloseableHttpResponse response = httpInterface.execute(request)) {
    int statusCode = response.getStatusLine().getStatusCode();
    if (!HttpClientTools.isSuccessWithContent(statusCode)) {
      throw new IOException("Unexpected response code from access token request: " + statusCode);
    }

    return JsonBrowser.parse(response.getEntity().getContent());
  }
}
 
Example 17
Source File: DefaultYoutubeTrackDetailsLoader.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
protected JsonBrowser loadTrackBaseInfoFromEmbedPage(HttpInterface httpInterface, String videoId) throws IOException {
  try (CloseableHttpResponse response = httpInterface.execute(new HttpGet("https://www.youtube.com/embed/" + videoId))) {
    HttpClientTools.assertSuccessWithContent(response, "embed video page response");

    String html = EntityUtils.toString(response.getEntity(), UTF_8);
    String configJson = DataFormatTools.extractBetween(html, "'PLAYER_CONFIG': ", "});writeEmbed();");

    if (configJson != null) {
      return JsonBrowser.parse(configJson);
    }
  }

  throw new FriendlyException("Track information is unavailable.", SUSPICIOUS,
          new IllegalStateException("Expected player config is not present in embed page."));
}
 
Example 18
Source File: YoutubeAudioSourceManager.java    From kyoko with MIT License 5 votes vote down vote up
private boolean determineFailureReason(HttpInterface httpInterface, String videoId, boolean mustExist) throws IOException {
    try (CloseableHttpResponse response = httpInterface.execute(new HttpGet("https://www.youtube.com/get_video_info?hl=en_GB&video_id=" + videoId))) {
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != 200) {
            throw new IOException("Invalid status code for video info response: " + statusCode);
        }

        Map<String, String> format = convertToMapLayout(URLEncodedUtils.parse(response.getEntity()));
        return determineFailureReasonFromStatus(format.get("status"), format.get("reason"), mustExist);
    }
}
 
Example 19
Source File: RemoteNodeProcessor.java    From lavaplayer with Apache License 2.0 4 votes vote down vote up
private boolean dispatchOneTick(HttpInterface httpInterface, TickBuilder tickBuilder) throws Exception {
  boolean success = false;
  HttpPost post = new HttpPost("http://" + nodeAddress + "/tick");

  abandonedTrackManager.distribute(Collections.singletonList(this));

  ByteArrayEntity entity = new ByteArrayEntity(buildRequestBody());
  post.setEntity(entity);

  tickBuilder.requestSize = (int) entity.getContentLength();

  CloseableHttpResponse response = httpInterface.execute(post);

  try {
    tickBuilder.responseCode = response.getStatusLine().getStatusCode();
    if (tickBuilder.responseCode != HttpStatus.SC_OK) {
      throw new IOException("Returned an unexpected response code " + tickBuilder.responseCode);
    }

    if (connectionState.compareAndSet(ConnectionState.PENDING.id(), ConnectionState.ONLINE.id())) {
      log.info("Node {} came online.", nodeAddress);
    } else if (connectionState.get() != ConnectionState.ONLINE.id()) {
      log.warn("Node {} received successful response, but had already lost control of its tracks.", nodeAddress);
      return false;
    }

    lastAliveTime = System.currentTimeMillis();

    if (!handleResponseBody(response.getEntity().getContent(), tickBuilder)) {
      return false;
    }

    success = true;
  } finally {
    if (!success) {
      ExceptionTools.closeWithWarnings(response);
    } else {
      ExceptionTools.closeWithWarnings(response.getEntity().getContent());
    }
  }

  return true;
}
 
Example 20
Source File: YoutubeAudioSourceManager.java    From kyoko with MIT License 4 votes vote down vote up
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);
}