Search in sources :

Example 21 with JsonBrowser

use of com.sedmelluq.discord.lavaplayer.tools.JsonBrowser in project lavaplayer by sedmelluq.

the class TwitchStreamSegmentUrlProvider method fetchSegmentPlaylistUrl.

@Override
protected String fetchSegmentPlaylistUrl(HttpInterface httpInterface) throws IOException {
    if (System.currentTimeMillis() < tokenExpirationTime) {
        return streamSegmentPlaylistUrl;
    }
    JsonBrowser token = loadAccessToken(httpInterface);
    HttpUriRequest request = new HttpGet(getChannelStreamsUrl(token).toString());
    ChannelStreams streams = loadChannelStreamsInfo(HttpClientTools.fetchResponseLines(httpInterface, request, "channel streams list"));
    if (streams.entries.isEmpty()) {
        throw new IllegalStateException("No streams available on channel.");
    }
    ChannelStreamInfo stream = streams.entries.get(0);
    log.debug("Chose stream with quality {} from url {}", stream.quality, stream.url);
    streamSegmentPlaylistUrl = stream.url;
    long tokenServerExpirationTime = JsonBrowser.parse(token.get(TOKEN_PARAMETER).text()).get("expires").as(Long.class) * 1000L;
    tokenExpirationTime = System.currentTimeMillis() + (tokenServerExpirationTime - streams.serverTime) - 5000;
    return streamSegmentPlaylistUrl;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpGet(org.apache.http.client.methods.HttpGet) JsonBrowser(com.sedmelluq.discord.lavaplayer.tools.JsonBrowser)

Example 22 with JsonBrowser

use of com.sedmelluq.discord.lavaplayer.tools.JsonBrowser in project lavaplayer by sedmelluq.

the class BandcampAudioSourceManager method loadTrack.

private AudioItem loadTrack(UrlInfo urlInfo) {
    return extractFromPage(urlInfo.fullUrl, (httpClient, text) -> {
        JsonBrowser trackListInfo = readTrackListInformation(text);
        String artist = trackListInfo.get("artist").safeText();
        return extractTrack(trackListInfo.get("trackinfo").index(0), urlInfo.baseUrl, artist);
    });
}
Also used : JsonBrowser(com.sedmelluq.discord.lavaplayer.tools.JsonBrowser)

Example 23 with JsonBrowser

use of com.sedmelluq.discord.lavaplayer.tools.JsonBrowser in project lavaplayer by sedmelluq.

the class DefaultSoundCloudPlaylistLoader method loadPlaylistTracks.

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;
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) JsonBrowser(com.sedmelluq.discord.lavaplayer.tools.JsonBrowser) URISyntaxException(java.net.URISyntaxException) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) IOException(java.io.IOException)

Example 24 with JsonBrowser

use of com.sedmelluq.discord.lavaplayer.tools.JsonBrowser in project lavaplayer by sedmelluq.

the class TwitchStreamSegmentUrlProvider method obtainSegmentPlaylistUrl.

private boolean obtainSegmentPlaylistUrl(HttpInterface httpInterface) throws IOException {
    if (System.currentTimeMillis() < tokenExpirationTime) {
        return true;
    }
    JsonBrowser token = loadAccessToken(httpInterface);
    HttpUriRequest request = new HttpGet(getChannelStreamsUrl(token).toString());
    ChannelStreams streams = loadChannelStreamsInfo(HttpClientTools.fetchResponseLines(httpInterface, request, "channel streams list"));
    if (streams.entries.isEmpty()) {
        throw new IllegalStateException("No streams available on channel.");
    }
    ChannelStreamInfo stream = streams.entries.get(0);
    log.debug("Chose stream with quality {} from url {}", stream.quality, stream.url);
    streamSegmentPlaylistUrl = stream.url;
    long tokenServerExpirationTime = JsonBrowser.parse(token.get(TOKEN_PARAMETER).text()).get("expires").as(Long.class) * 1000L;
    tokenExpirationTime = System.currentTimeMillis() + (tokenServerExpirationTime - streams.serverTime) - 5000;
    return true;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpGet(org.apache.http.client.methods.HttpGet) JsonBrowser(com.sedmelluq.discord.lavaplayer.tools.JsonBrowser)

Example 25 with JsonBrowser

use of com.sedmelluq.discord.lavaplayer.tools.JsonBrowser in project lavaplayer by sedmelluq.

the class YoutubeAudioSourceManager method getTrackInfoFromEmbedPage.

private JsonBrowser getTrackInfoFromEmbedPage(HttpInterface httpInterface, String videoId) throws Exception {
    JsonBrowser basicInfo = loadTrackBaseInfoFromEmbedPage(httpInterface, videoId);
    basicInfo.put("args", loadTrackArgsFromVideoInfoPage(httpInterface, videoId, basicInfo.get("sts").text()));
    return basicInfo;
}
Also used : JsonBrowser(com.sedmelluq.discord.lavaplayer.tools.JsonBrowser)

Aggregations

JsonBrowser (com.sedmelluq.discord.lavaplayer.tools.JsonBrowser)41 FriendlyException (com.sedmelluq.discord.lavaplayer.tools.FriendlyException)14 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)11 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)9 HttpGet (org.apache.http.client.methods.HttpGet)9 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)8 AudioTrackInfo (com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo)7 BasicAudioPlaylist (com.sedmelluq.discord.lavaplayer.track.BasicAudioPlaylist)6 HttpInterface (com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface)4 YoutubeTrackFormat (com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeTrackFormat)2 URISyntaxException (java.net.URISyntaxException)2 Matcher (java.util.regex.Matcher)2 NameValuePair (org.apache.http.NameValuePair)2 HttpPost (org.apache.http.client.methods.HttpPost)2 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)2 StringEntity (org.apache.http.entity.StringEntity)2 PBJ_PARAMETER (com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeHttpContextFilter.PBJ_PARAMETER)1 YoutubeTrackJsonData.fromEmbedParts (com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeTrackJsonData.fromEmbedParts)1 DataFormatTools (com.sedmelluq.discord.lavaplayer.tools.DataFormatTools)1