Java Code Examples for com.sedmelluq.discord.lavaplayer.tools.JsonBrowser#parse()
The following examples show how to use
com.sedmelluq.discord.lavaplayer.tools.JsonBrowser#parse() .
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 |
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 |
@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: YoutubeSearchProvider.java From lavaplayer with Apache License 2.0 | 6 votes |
private List<AudioTrack> polymerExtractTracks(Document document, Function<AudioTrackInfo, AudioTrack> trackFactory) throws IOException { // Match the JSON from the HTML. It should be within a script tag Matcher matcher = polymerInitialDataRegex.matcher(document.outerHtml()); if (!matcher.find()) { log.warn("Failed to match ytInitialData JSON object"); return Collections.emptyList(); } JsonBrowser jsonBrowser = JsonBrowser.parse(matcher.group(1)); ArrayList<AudioTrack> list = new ArrayList<>(); jsonBrowser.get("contents") .get("twoColumnSearchResultsRenderer") .get("primaryContents") .get("sectionListRenderer") .get("contents") .index(0) .get("itemSectionRenderer") .get("contents") .values() .forEach(json -> { AudioTrack track = extractPolymerData(json, trackFactory); if (track != null) list.add(track); }); return list; }
Example 4
Source File: DefaultSoundCloudHtmlDataLoader.java From lavaplayer with Apache License 2.0 | 6 votes |
@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 = extractJsonFromHtml(html); if (rootData == null) { throw new FriendlyException("This url does not appear to be a playable track.", SUSPICIOUS, null); } return JsonBrowser.parse(rootData); } }
Example 5
Source File: HttpClientTools.java From lavaplayer with Apache License 2.0 | 5 votes |
/** * Executes an HTTP request and returns the response as a JsonBrowser instance. * * @param httpInterface HTTP interface to use for the request. * @param request Request to perform. * @return Response as a JsonBrowser instance. null in case of 404. * @throws IOException On network error or for non-200 response code. */ public static JsonBrowser fetchResponseAsJson(HttpInterface httpInterface, HttpUriRequest request) throws IOException { try (CloseableHttpResponse response = httpInterface.execute(request)) { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_NOT_FOUND) { return null; } else if (!isSuccessWithContent(statusCode)) { throw new FriendlyException("Server responded with an error.", SUSPICIOUS, new IllegalStateException("Response code from channel info is " + statusCode)); } return JsonBrowser.parse(response.getEntity().getContent()); } }
Example 6
Source File: YoutubeAudioSourceManager.java From kyoko with MIT License | 5 votes |
/** * @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 7
Source File: DefaultYoutubeTrackDetailsLoader.java From lavaplayer with Apache License 2.0 | 5 votes |
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 8
Source File: SoundCloudHelper.java From lavaplayer with Apache License 2.0 | 5 votes |
public static String loadPlaybackUrl(HttpInterface httpInterface, String jsonUrl) throws IOException { try (PersistentHttpStream stream = new PersistentHttpStream(httpInterface, URI.create(jsonUrl), null)) { if (!HttpClientTools.isSuccessWithContent(stream.checkStatusCode())) { throw new IOException("Invalid status code for soundcloud stream: " + stream.checkStatusCode()); } JsonBrowser json = JsonBrowser.parse(stream); return json.get("url").text(); } }
Example 9
Source File: SoundCloudAudioSourceManager.java From lavaplayer with Apache License 2.0 | 5 votes |
private AudioItem loadSearchResultsFromResponse(HttpResponse response, String query) throws IOException { try { JsonBrowser searchResults = JsonBrowser.parse(response.getEntity().getContent()); return extractTracksFromSearchResults(query, searchResults); } finally { EntityUtils.consumeQuietly(response.getEntity()); } }
Example 10
Source File: SoundCloudAudioSourceManager.java From lavaplayer with Apache License 2.0 | 5 votes |
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 11
Source File: VimeoAudioSourceManager.java From lavaplayer with Apache License 2.0 | 5 votes |
JsonBrowser loadConfigJsonFromPageContent(String content) throws IOException { String configText = DataFormatTools.extractBetween(content, "window.vimeo.clip_page_config = ", "\n"); if (configText != null) { return JsonBrowser.parse(configText); } return null; }
Example 12
Source File: VimeoAudioTrack.java From lavaplayer with Apache License 2.0 | 5 votes |
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: BandcampAudioSourceManager.java From lavaplayer with Apache License 2.0 | 5 votes |
JsonBrowser readTrackListInformation(String text) throws IOException { String trackInfoJson = DataFormatTools.extractBetween(text, "var TralbumData = ", "};"); if (trackInfoJson == null) { throw new FriendlyException("Track information not found on the Bandcamp page.", SUSPICIOUS, null); } trackInfoJson = trackInfoJson.replace("\" + \"", "") + "};"; return JsonBrowser.parse(trackInfoJson + "};"); }
Example 14
Source File: BandcampAudioSourceManager.java From lavaplayer with Apache License 2.0 | 5 votes |
private JsonBrowser readAlbumInformation(String text) throws IOException { String albumInfoJson = DataFormatTools.extractBetween(text, "var EmbedData = ", "};"); if (albumInfoJson == null) { throw new FriendlyException("Album information not found on the Bandcamp page.", SUSPICIOUS, null); } albumInfoJson = albumInfoJson.replace("\" + \"", "") + "};"; return JsonBrowser.parse(albumInfoJson); }
Example 15
Source File: TwitchStreamSegmentUrlProvider.java From lavaplayer with Apache License 2.0 | 5 votes |
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 16
Source File: DefaultSoundCloudPlaylistLoader.java From lavaplayer with Apache License 2.0 | 4 votes |
protected List<AudioTrack> loadPlaylistTracks( HttpInterface httpInterface, JsonBrowser playlistData, Function<AudioTrackInfo, AudioTrack> trackFactory ) throws IOException { String playlistId = dataReader.readPlaylistIdentifier(playlistData); List<String> trackIds = dataReader.readPlaylistTracks(playlistData).stream() .map(dataReader::readTrackId) .collect(Collectors.toList()); int numTrackIds = trackIds.size(); List<JsonBrowser> trackDataList = new ArrayList<>(); for (int i = 0; i < numTrackIds; i += 50) { int last = Math.min(i + 50, numTrackIds); List<String> trackIdSegment = trackIds.subList(i, last); try (CloseableHttpResponse response = httpInterface.execute(new HttpGet(buildTrackListUrl(trackIdSegment)))) { HttpClientTools.assertSuccessWithContent(response, "track list response"); JsonBrowser trackList = JsonBrowser.parse(response.getEntity().getContent()); trackDataList.addAll(trackList.values()); } } sortPlaylistTracks(trackDataList, trackIds); int blockedCount = 0; List<AudioTrack> tracks = new ArrayList<>(); for (JsonBrowser trackData : trackDataList) { if (dataReader.isTrackBlocked(trackData)) { blockedCount++; } else { try { tracks.add(trackFactory.apply(dataReader.readTrackInfo( trackData, formatHandler.buildFormatIdentifier( formatHandler.chooseBestFormat(dataReader.readTrackFormats(trackData)) ) ))); } catch (Exception e) { log.error("In soundcloud playlist {}, failed to load track", playlistId, e); } } } if (blockedCount > 0) { log.debug("In soundcloud playlist {}, {} tracks were omitted because they are blocked.", playlistId, blockedCount); } return tracks; }
Example 17
Source File: DefaultYoutubePlaylistLoader.java From lavaplayer with Apache License 2.0 | 4 votes |
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 18
Source File: YoutubeMixProvider.java From lavaplayer with Apache License 2.0 | 4 votes |
/** * 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); }
Example 19
Source File: DefaultYoutubeTrackDetails.java From lavaplayer with Apache License 2.0 | 4 votes |
private List<YoutubeTrackFormat> loadTrackFormats( HttpInterface httpInterface, YoutubeSignatureResolver signatureResolver ) throws Exception { JsonBrowser args = info.get("args"); String adaptiveFormats = args.get("adaptive_fmts").text(); if (adaptiveFormats != null) { return loadTrackFormatsFromAdaptive(adaptiveFormats); } String playerResponse = args.get("player_response").text(); if (playerResponse != null) { JsonBrowser playerData = JsonBrowser.parse(playerResponse); JsonBrowser streamingData = playerData.get("streamingData"); boolean isLive = playerData.get("videoDetails").get("isLive").asBoolean(false); if (!streamingData.isNull()) { List<YoutubeTrackFormat> formats = loadTrackFormatsFromStreamingData(streamingData.get("formats"), isLive); formats.addAll(loadTrackFormatsFromStreamingData(streamingData.get("adaptiveFormats"), isLive)); if (!formats.isEmpty()) { return formats; } } } String dashUrl = args.get("dashmpd").text(); if (dashUrl != null) { return loadTrackFormatsFromDash(dashUrl, httpInterface, signatureResolver); } String formatStreamMap = args.get("url_encoded_fmt_stream_map").text(); if (formatStreamMap != null) { return loadTrackFormatsFromFormatStreamMap(formatStreamMap); } log.warn("Video {} with no detected format field, arguments are: {}", videoId, args.format()); throw new FriendlyException("Unable to play this YouTube track.", SUSPICIOUS, new IllegalStateException("No adaptive formats, no dash, no stream map.")); }
Example 20
Source File: YoutubeAudioSourceManager.java From kyoko with MIT License | 4 votes |
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); }