use of com.sedmelluq.discord.lavaplayer.tools.JsonBrowser in project lavaplayer by sedmelluq.
the class YoutubeMixProvider method extractPlaylistTracks.
private void extractPlaylistTracks(JsonBrowser browser, List<AudioTrack> tracks, Function<AudioTrackInfo, AudioTrack> trackFactory) {
for (JsonBrowser video : browser.values()) {
JsonBrowser renderer = video.get("playlistPanelVideoRenderer");
String title = renderer.get("title").get("simpleText").text();
String author = renderer.get("longBylineText").get("runs").index(0).get("text").text();
String durationStr = renderer.get("lengthText").get("simpleText").text();
long duration = parseDuration(durationStr);
String identifier = renderer.get("videoId").text();
String uri = "https://youtube.com/watch?v=" + identifier;
AudioTrackInfo trackInfo = new AudioTrackInfo(title, author, duration, identifier, false, uri);
tracks.add(trackFactory.apply(trackInfo));
}
}
use of com.sedmelluq.discord.lavaplayer.tools.JsonBrowser in project lavaplayer by sedmelluq.
the class YoutubeMixProvider method load.
/**
* 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_PARAMETER;
try (CloseableHttpResponse response = httpInterface.execute(new HttpGet(mixUrl))) {
HttpClientTools.assertSuccessWithContent(response, "mix response");
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);
}
use of com.sedmelluq.discord.lavaplayer.tools.JsonBrowser in project lavaplayer by sedmelluq.
the class YoutubeSearchMusicProvider method extractMusicTracks.
private List<AudioTrack> extractMusicTracks(Document document, Function<AudioTrackInfo, AudioTrack> trackFactory) throws IOException {
Matcher matcher = ytMusicDataRegex.matcher(document.outerHtml());
if (!matcher.find()) {
log.warn("Failed to match ytMusicData JSON object");
return Collections.emptyList();
}
JsonBrowser jsonBrowser = JsonBrowser.parse(matcher.group(1));
ArrayList<AudioTrack> list = new ArrayList<>();
JsonBrowser tracks = jsonBrowser.get("contents").get("sectionListRenderer").get("contents").index(0).get("musicShelfRenderer").get("contents");
if (tracks == JsonBrowser.NULL_BROWSER) {
tracks = jsonBrowser.get("contents").get("sectionListRenderer").get("contents").index(1).get("musicShelfRenderer").get("contents");
}
tracks.values().forEach(json -> {
AudioTrack track = extractMusicData(json, trackFactory);
if (track != null)
list.add(track);
});
return list;
}
use of com.sedmelluq.discord.lavaplayer.tools.JsonBrowser in project lavaplayer by sedmelluq.
the class YoutubeSearchProvider method polymerExtractTracks.
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(2));
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;
}
use of com.sedmelluq.discord.lavaplayer.tools.JsonBrowser in project lavaplayer by sedmelluq.
the class YoutubeTrackJsonData method fromPolymerPlayerInfo.
private static YoutubeTrackJsonData fromPolymerPlayerInfo(JsonBrowser playerInfo, JsonBrowser playerResponse) {
JsonBrowser args = playerInfo.get("args");
String playerScriptUrl = playerInfo.get("assets").get("js").text();
String playerResponseText = args.get("player_response").text();
if (playerResponseText == null) {
// However, if no player_response is available, use the outer playerResponse.
return new YoutubeTrackJsonData(playerResponse, args, playerScriptUrl);
}
return new YoutubeTrackJsonData(parsePlayerResponse(playerResponseText), args, playerScriptUrl);
}
Aggregations