use of com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface in project lavaplayer by sedmelluq.
the class M3uStreamAudioTrack method process.
@Override
public void process(LocalAudioTrackExecutor localExecutor) throws Exception {
try (final HttpInterface httpInterface = getHttpInterface()) {
try (ChainedInputStream chainedInputStream = new ChainedInputStream(() -> segmentUrlProvider.getNextSegmentStream(httpInterface))) {
MpegTsElementaryInputStream elementaryInputStream = new MpegTsElementaryInputStream(chainedInputStream, ADTS_ELEMENTARY_STREAM);
PesPacketInputStream pesPacketInputStream = new PesPacketInputStream(elementaryInputStream);
processDelegate(new AdtsAudioTrack(trackInfo, pesPacketInputStream), localExecutor);
}
}
}
use of com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface in project lavaplayer by sedmelluq.
the class YoutubeAudioSourceManager method loadTrackWithVideoId.
/**
* Loads a single track from video ID.
*
* @param videoId ID of the YouTube video.
* @param mustExist True if it should throw an exception on missing track, otherwise returns AudioReference.NO_TRACK.
* @return Loaded YouTube track.
*/
public AudioItem loadTrackWithVideoId(String videoId, boolean mustExist) {
try (HttpInterface httpInterface = getHttpInterface()) {
JsonBrowser info = getTrackInfoFromMainPage(httpInterface, videoId, mustExist);
if (info == null) {
return AudioReference.NO_TRACK;
}
JsonBrowser args = info.get("args");
if ("fail".equals(args.get("status").text())) {
throw new FriendlyException(args.get("reason").text(), COMMON, null);
}
boolean isStream = "1".equals(args.get("live_playback").text());
long duration = isStream ? Long.MAX_VALUE : args.get("length_seconds").as(Long.class) * 1000;
return buildTrackObject(videoId, args.get("title").text(), args.get("author").text(), isStream, duration);
} catch (Exception e) {
throw ExceptionTools.wrapUnfriendlyExceptions("Loading information for a YouTube track failed.", FAULT, e);
}
}
use of com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface in project lavaplayer by sedmelluq.
the class YoutubeMixProvider method loadMixWithId.
/**
* 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 loadMixWithId(String mixId, String selectedVideoId) {
List<String> videoIds = new ArrayList<>();
try (HttpInterface httpInterface = sourceManager.getHttpInterface()) {
String mixUrl = "https://www.youtube.com/watch?v=" + selectedVideoId + "&list=" + mixId;
try (CloseableHttpResponse response = httpInterface.execute(new HttpGet(mixUrl))) {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200) {
throw new IOException("Invalid status code for mix response: " + statusCode);
}
Document document = Jsoup.parse(response.getEntity().getContent(), StandardCharsets.UTF_8.name(), "");
extractVideoIdsFromMix(document, videoIds);
}
} catch (IOException e) {
throw new FriendlyException("Could not read mix page.", SUSPICIOUS, e);
}
if (videoIds.isEmpty()) {
throw new FriendlyException("Could not find tracks from mix.", SUSPICIOUS, null);
}
return loadTracksAsynchronously(videoIds, selectedVideoId);
}
use of com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface in project lavaplayer by sedmelluq.
the class YoutubeAudioTrack method process.
@Override
public void process(LocalAudioTrackExecutor localExecutor) throws Exception {
try (HttpInterface httpInterface = sourceManager.getHttpInterface()) {
FormatWithUrl format = loadBestFormatWithUrl(httpInterface);
log.debug("Starting track from URL: {}", format.signedUrl);
if (trackInfo.isStream) {
processStream(localExecutor, format);
} else {
processStatic(localExecutor, httpInterface, format);
}
}
}
use of com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface in project lavaplayer by sedmelluq.
the class YoutubeSearchMusicProvider method loadSearchMusicResult.
/**
* @param query Search query.
* @return Playlist of the first page of music results.
*/
@Override
public AudioItem loadSearchMusicResult(String query, Function<AudioTrackInfo, AudioTrack> trackFactory) {
log.debug("Performing a search music with query {}", query);
try (HttpInterface httpInterface = httpInterfaceManager.getInterface()) {
URI url = new URIBuilder("https://music.youtube.com/youtubei/v1/search").addParameter("alt", "json").addParameter("key", YT_MUSIC_KEY).build();
HttpPost post = new HttpPost(url);
StringEntity payload = new StringEntity(String.format(YT_MUSIC_PAYLOAD, query.replace("\"", "\\\"")), "UTF-8");
post.setHeader("Referer", "music.youtube.com");
post.setEntity(payload);
try (CloseableHttpResponse response = httpInterface.execute(post)) {
HttpClientTools.assertSuccessWithContent(response, "search music response");
Document document = Jsoup.parse(response.getEntity().getContent(), StandardCharsets.UTF_8.name(), "");
return extractSearchResults(document, query, trackFactory);
}
} catch (Exception e) {
throw ExceptionTools.wrapUnfriendlyExceptions(e);
}
}
Aggregations