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