use of com.sedmelluq.discord.lavaplayer.track.AudioTrack in project lavaplayer by sedmelluq.
the class Main method loadAndPlay.
private void loadAndPlay(final TextChannel channel, final String trackUrl) {
GuildMusicManager musicManager = getGuildAudioPlayer(channel.getGuild());
playerManager.loadItemOrdered(musicManager, trackUrl, new AudioLoadResultHandler() {
@Override
public void trackLoaded(AudioTrack track) {
channel.sendMessage("Adding to queue " + track.getInfo().title).queue();
play(channel.getGuild(), musicManager, track);
}
@Override
public void playlistLoaded(AudioPlaylist playlist) {
AudioTrack firstTrack = playlist.getSelectedTrack();
if (firstTrack == null) {
firstTrack = playlist.getTracks().get(0);
}
channel.sendMessage("Adding to queue " + firstTrack.getInfo().title + " (first track of playlist " + playlist.getName() + ")").queue();
play(channel.getGuild(), musicManager, firstTrack);
}
@Override
public void noMatches() {
channel.sendMessage("Nothing found by " + trackUrl).queue();
}
@Override
public void loadFailed(FriendlyException exception) {
channel.sendMessage("Could not play: " + exception.getMessage()).queue();
}
});
}
use of com.sedmelluq.discord.lavaplayer.track.AudioTrack 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.track.AudioTrack 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.track.AudioTrack 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.track.AudioTrack 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);
}
Aggregations