Search in sources :

Example 1 with JsonBrowser

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

the class SoundCloudAudioSourceManager method handlePlaylistTracksResponse.

private List<AudioTrack> handlePlaylistTracksResponse(HttpResponse response, String playlistWebUrl, List<String> trackIds) throws IOException {
    List<AudioTrack> tracks = new ArrayList<>();
    int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode != 200) {
        throw new IOException("Invalid status code for track list response: " + statusCode);
    }
    JsonBrowser trackList = JsonBrowser.parse(response.getEntity().getContent());
    int blockedCount = 0;
    for (JsonBrowser trackInfoJson : trackList.values()) {
        if ("BLOCK".equals(trackInfoJson.get("policy").text())) {
            blockedCount++;
        } else {
            tracks.add(buildAudioTrack(trackInfoJson, null));
        }
    }
    if (blockedCount > 0) {
        log.debug("In soundcloud playlist {}, {} tracks were omitted because they are blocked.", playlistWebUrl, blockedCount);
    }
    sortPlaylistTracks(tracks, trackIds);
    return tracks;
}
Also used : ArrayList(java.util.ArrayList) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) IOException(java.io.IOException) JsonBrowser(com.sedmelluq.discord.lavaplayer.tools.JsonBrowser)

Example 2 with JsonBrowser

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

the class TwitchStreamAudioSourceManager method loadItem.

@Override
public AudioItem loadItem(DefaultAudioPlayerManager manager, AudioReference reference) {
    String streamName = getChannelIdentifierFromUrl(reference.identifier);
    if (streamName == null) {
        return null;
    }
    JsonBrowser channelInfo = fetchStreamChannelInfo(streamName);
    if (channelInfo == null) {
        return AudioReference.NO_TRACK;
    } else {
        final String displayName = channelInfo.get("display_name").text();
        final String status = channelInfo.get("status").text();
        return new TwitchStreamAudioTrack(new AudioTrackInfo(status, displayName, Long.MAX_VALUE, reference.identifier, true, reference.identifier), this);
    }
}
Also used : AudioTrackInfo(com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo) JsonBrowser(com.sedmelluq.discord.lavaplayer.tools.JsonBrowser)

Example 3 with JsonBrowser

use of com.sedmelluq.discord.lavaplayer.tools.JsonBrowser 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 4 with JsonBrowser

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

the class BandcampAudioSourceManager method loadAlbum.

private AudioItem loadAlbum(String albumUrl) {
    return extractFromPage(albumUrl, (httpClient, text) -> {
        String bandUrl = readBandUrl(text);
        JsonBrowser trackListInfo = readTrackListInformation(text);
        String artist = trackListInfo.get("artist").text();
        List<AudioTrack> tracks = new ArrayList<>();
        for (JsonBrowser trackInfo : trackListInfo.get("trackinfo").values()) {
            tracks.add(extractTrack(trackInfo, bandUrl, artist));
        }
        JsonBrowser albumInfo = readAlbumInformation(text);
        return new BasicAudioPlaylist(albumInfo.get("album_title").text(), tracks, null, false);
    });
}
Also used : BasicAudioPlaylist(com.sedmelluq.discord.lavaplayer.track.BasicAudioPlaylist) ArrayList(java.util.ArrayList) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) JsonBrowser(com.sedmelluq.discord.lavaplayer.tools.JsonBrowser)

Example 5 with JsonBrowser

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

the class BeamAudioSourceManager method loadItem.

@Override
public AudioItem loadItem(DefaultAudioPlayerManager manager, AudioReference reference) {
    String streamName = getChannelNameFromUrl(reference.identifier);
    if (streamName == null) {
        return null;
    }
    JsonBrowser channelInfo = fetchStreamChannelInfo(streamName);
    if (channelInfo == null) {
        return AudioReference.NO_TRACK;
    } else {
        String displayName = channelInfo.get("name").text();
        String id = channelInfo.get("id").text();
        if (displayName == null || id == null) {
            throw new IllegalStateException("Expected id and name fields from Beam channel info.");
        }
        return new BeamAudioTrack(new AudioTrackInfo(displayName, streamName, Long.MAX_VALUE, id + "|" + streamName + "|" + reference.identifier, true, "https://beam.pro/" + streamName), this);
    }
}
Also used : AudioTrackInfo(com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo) 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