Search in sources :

Example 1 with HttpInterface

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);
        }
    }
}
Also used : HttpInterface(com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface) MpegTsElementaryInputStream(com.sedmelluq.discord.lavaplayer.container.mpegts.MpegTsElementaryInputStream) ChainedInputStream(com.sedmelluq.discord.lavaplayer.tools.io.ChainedInputStream) PesPacketInputStream(com.sedmelluq.discord.lavaplayer.container.mpegts.PesPacketInputStream) AdtsAudioTrack(com.sedmelluq.discord.lavaplayer.container.adts.AdtsAudioTrack)

Example 2 with HttpInterface

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);
    }
}
Also used : HttpInterface(com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface) JsonBrowser(com.sedmelluq.discord.lavaplayer.tools.JsonBrowser) URISyntaxException(java.net.URISyntaxException) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) IOException(java.io.IOException) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException)

Example 3 with HttpInterface

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);
}
Also used : HttpInterface(com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) Document(org.jsoup.nodes.Document) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException)

Example 4 with HttpInterface

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);
        }
    }
}
Also used : HttpInterface(com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface)

Example 5 with HttpInterface

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);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) HttpInterface(com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Document(org.jsoup.nodes.Document) URI(java.net.URI) IOException(java.io.IOException) URIBuilder(org.apache.http.client.utils.URIBuilder)

Aggregations

HttpInterface (com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface)18 IOException (java.io.IOException)11 FriendlyException (com.sedmelluq.discord.lavaplayer.tools.FriendlyException)7 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)7 URI (java.net.URI)6 Document (org.jsoup.nodes.Document)6 HttpGet (org.apache.http.client.methods.HttpGet)5 JsonBrowser (com.sedmelluq.discord.lavaplayer.tools.JsonBrowser)3 PersistentHttpStream (com.sedmelluq.discord.lavaplayer.tools.io.PersistentHttpStream)3 URIBuilder (org.apache.http.client.utils.URIBuilder)3 MpegAudioTrack (com.sedmelluq.discord.lavaplayer.container.mpeg.MpegAudioTrack)2 URISyntaxException (java.net.URISyntaxException)2 HttpPost (org.apache.http.client.methods.HttpPost)2 AdtsAudioTrack (com.sedmelluq.discord.lavaplayer.container.adts.AdtsAudioTrack)1 Mp3AudioTrack (com.sedmelluq.discord.lavaplayer.container.mp3.Mp3AudioTrack)1 MpegTsElementaryInputStream (com.sedmelluq.discord.lavaplayer.container.mpegts.MpegTsElementaryInputStream)1 PesPacketInputStream (com.sedmelluq.discord.lavaplayer.container.mpegts.PesPacketInputStream)1 RingBufferMath (com.sedmelluq.discord.lavaplayer.tools.RingBufferMath)1 ChainedInputStream (com.sedmelluq.discord.lavaplayer.tools.io.ChainedInputStream)1 BasicAudioPlaylist (com.sedmelluq.discord.lavaplayer.track.BasicAudioPlaylist)1