Search in sources :

Example 6 with FriendlyException

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

the class BeamSegmentUrlProvider method getNextSegmentUrl.

/**
 * @param httpInterface Http interface to use for requests.
 * @return The URL of the next TS segment.
 */
@Override
public String getNextSegmentUrl(HttpInterface httpInterface) {
    try {
        if (!obtainSegmentPlaylistUrl(httpInterface)) {
            return null;
        }
        List<String> segments = loadStreamSegmentsList(httpInterface, streamSegmentPlaylistUrl);
        String segment = chooseNextSegment(segments, lastSegment);
        if (segment == null) {
            return null;
        }
        lastSegment = segment;
        return createSegmentUrl(streamSegmentPlaylistUrl, segment);
    } catch (IOException e) {
        throw new FriendlyException("Failed to get next part of the stream.", SUSPICIOUS, e);
    }
}
Also used : IOException(java.io.IOException) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException)

Example 7 with FriendlyException

use of com.sedmelluq.discord.lavaplayer.tools.FriendlyException in project SkyBot by duncte123.

the class ClypitAudioSourceManager method detectContainerWithClient.

private MediaContainerDetectionResult detectContainerWithClient(HttpInterface httpInterface, AudioReference reference) throws IOException {
    try (PersistentHttpStream inputStream = new PersistentHttpStream(httpInterface, new URI(reference.identifier), Long.MAX_VALUE)) {
        int statusCode = inputStream.checkStatusCode();
        String redirectUrl = HttpClientTools.getRedirectLocation(reference.identifier, inputStream.getCurrentResponse());
        if (redirectUrl != null) {
            return new MediaContainerDetectionResult(null, new AudioReference(redirectUrl, null));
        } else if (statusCode == HttpStatus.SC_NOT_FOUND) {
            return null;
        } else if (!HttpClientTools.isSuccessWithContent(statusCode)) {
            throw new FriendlyException("That URL is not playable.", COMMON, new IllegalStateException("Status code " + statusCode));
        }
        MediaContainerHints hints = MediaContainerHints.from(getHeaderValue(inputStream.getCurrentResponse(), "Content-Type"), null);
        return MediaContainerDetection.detectContainer(reference, inputStream, hints);
    } catch (URISyntaxException e) {
        throw new FriendlyException("Not a valid URL.", COMMON, e);
    }
}
Also used : MediaContainerHints(com.sedmelluq.discord.lavaplayer.container.MediaContainerHints) MediaContainerDetectionResult(com.sedmelluq.discord.lavaplayer.container.MediaContainerDetectionResult) URISyntaxException(java.net.URISyntaxException) PersistentHttpStream(com.sedmelluq.discord.lavaplayer.tools.io.PersistentHttpStream) AudioReference(com.sedmelluq.discord.lavaplayer.track.AudioReference) URI(java.net.URI) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException)

Example 8 with FriendlyException

use of com.sedmelluq.discord.lavaplayer.tools.FriendlyException in project SkyBot by duncte123.

the class SpotifyAudioSourceManager method loadItem.

@Override
public AudioItem loadItem(DefaultAudioPlayerManager manager, AudioReference reference) {
    if (isSpotifyAlbum(reference.identifier)) {
        if (youtube == null)
            return null;
        Matcher res = SPOTIFY_ALBUM_REGEX.matcher(reference.identifier);
        if (res.matches()) {
            try {
                final List<AudioTrack> playList = new ArrayList<>();
                final Album album = api.getAlbum(res.group(res.groupCount())).build().get();
                for (SimpleTrack t : album.getTracks().getItems()) {
                    List<SearchResult> results = searchYoutube(album.getArtists().get(0).getName() + " - " + t.getName());
                    playList.addAll(doThingWithPlaylist(results));
                }
                return new BasicAudioPlaylist(album.getName(), playList, playList.get(0), false);
            } catch (Exception e) {
                // logger.error("Something went wrong!", e);
                throw new FriendlyException(e.getMessage(), FriendlyException.Severity.FAULT, e);
            // return null;
            }
        }
    } else if (isSpotifyPlaylist(reference.identifier)) {
        if (youtube == null)
            return null;
        Matcher res = SPOTIFY_PLAYLIST_REGEX.matcher(reference.identifier);
        if (res.matches()) {
            try {
                final List<AudioTrack> finalPlaylist = new ArrayList<>();
                final Playlist spotifyPlaylist = api.getPlaylist(res.group(res.groupCount() - 1), res.group(res.groupCount())).build().get();
                for (PlaylistTrack playlistTrack : spotifyPlaylist.getTracks().getItems()) {
                    List<SearchResult> results = searchYoutube(playlistTrack.getTrack().getArtists().get(0).getName() + " - " + playlistTrack.getTrack().getName());
                    finalPlaylist.addAll(doThingWithPlaylist(results));
                }
                return new BasicAudioPlaylist(spotifyPlaylist.getName(), finalPlaylist, finalPlaylist.get(0), false);
            } catch (Exception e) {
                // logger.error("Something went wrong!", e);
                throw new FriendlyException(e.getMessage(), FriendlyException.Severity.FAULT, e);
            // return null;
            }
        }
    } else if (isSpotyfyTrack(reference.identifier)) {
        if (youtube == null)
            return null;
        Matcher res = SPOTIFY_TRACK_REGEX.matcher(reference.identifier);
        if (res.matches()) {
            try {
                final Track track = api.getTrack(res.group(res.groupCount())).build().get();
                List<SearchResult> results = searchYoutube(track.getArtists().get(0).getName() + " - " + track.getName());
                Video v = getVideoById(results.get(0).getId().getVideoId());
                return new SpotifyAudioTrack(new AudioTrackInfo(v.getSnippet().getTitle(), v.getSnippet().getChannelId(), toLongDuration(v.getContentDetails().getDuration()), v.getId(), false, "https://youtube.com/watch?v=" + v.getId()), youtubeAudioSourceManager);
            // return youtubeSearchProvider.loadSearchResult(track.getArtists().get(0).getName() + " - "+ track.getName());
            } catch (Exception e) {
                // logger.error("Something went wrong!", e);
                throw new FriendlyException(e.getMessage(), FriendlyException.Severity.FAULT, e);
            // return null;
            }
        }
    }
    /*if(isSpotifyAlbum(reference.identifier)) {
            if(this.youtubeAudioSourceManager == null)
                return null;
            Matcher res = SPOTIFY_ALBUM_REGEX.matcher(reference.identifier);
            if (res.matches()) {

                try {
                    final List<AudioTrack> playList = new ArrayList<>();
                    final Album album = api.getAlbum(res.group(res.groupCount())).build().get();
                    for(SimpleTrack t : album.getTracks().getItems()){
                        String fakeUrl = album.getArtists().get(0).getName() + " - "+ t.getName();
                        List<AudioTrack> tracks = ((AudioPlaylist)youtubeSearchProvider.loadSearchResult(fakeUrl)).getTracks();
                        if(tracks.size() > 0)
                            playList.add(tracks.get(0));
                    }
                    return new BasicAudioPlaylist(album.getName(), playList, playList.get(0), false);
                } catch (Exception e) {
                    //logger.error("Something went wrong!", e);
                    throw new FriendlyException(e.getMessage(), FriendlyException.Severity.FAULT, e);
                    //return null;
                }
            }
        } else if(isSpotifyPlaylist(reference.identifier)) {
            if(this.youtubeAudioSourceManager == null)
                return null;
            Matcher res = SPOTIFY_PLAYLIST_REGEX.matcher(reference.identifier);
            if (res.matches()) {

                try {
                    final List<AudioTrack> finalPlaylist = new ArrayList<>();
                    final Playlist spotifyPlaylist = api.getPlaylist(res.group(res.groupCount()-1), res.group(res.groupCount())).build().get();
                    for(PlaylistTrack playlistTrack : spotifyPlaylist.getTracks().getItems()){
                        String fakeUrl = playlistTrack.getTrack().getArtists().get(0).getName() + " - " + playlistTrack.getTrack().getName();
                        System.out.println(fakeUrl);
                        List<AudioTrack> tracks = ((AudioPlaylist)youtubeSearchProvider.loadSearchResult(fakeUrl)).getTracks();
                        if(tracks.size() > 0)
                            finalPlaylist.add(tracks.get(0));
                    }
                    return new BasicAudioPlaylist(spotifyPlaylist.getName(), finalPlaylist, finalPlaylist.get(0), false);
                } catch (Exception e) {
                    //logger.error("Something went wrong!", e);
                    throw new FriendlyException(e.getMessage(), FriendlyException.Severity.FAULT, e);
                    //return null;
                }
            }
        } else if(isSpotyfyTrack(reference.identifier)) {
            if(this.youtubeAudioSourceManager == null)
                return null;
            Matcher res = SPOTIFY_TRACK_REGEX.matcher(reference.identifier);
            if (res.matches()) {

                try {
                    final Track track = api.getTrack(res.group(res.groupCount())).build().get();
                    return youtubeSearchProvider.loadSearchResult(track.getArtists().get(0).getName() + " - "+ track.getName());
                } catch (Exception e) {
                    //logger.error("Something went wrong!", e);
                    throw new FriendlyException(e.getMessage(), FriendlyException.Severity.FAULT, e);
                    //return null;
                }
            }
        }*/
    return null;
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) SearchResult(com.google.api.services.youtube.model.SearchResult) GeneralSecurityException(java.security.GeneralSecurityException) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) IOException(java.io.IOException) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) Video(com.google.api.services.youtube.model.Video) ArrayList(java.util.ArrayList) List(java.util.List)

Example 9 with FriendlyException

use of com.sedmelluq.discord.lavaplayer.tools.FriendlyException in project FredBoat by Frederikam.

the class HttpSourceManager method checkHtmlResponse.

private MediaContainerDetectionResult checkHtmlResponse(AudioReference reference, PersistentHttpStream stream, MediaContainerHints hints) {
    StringWriter writer = new StringWriter();
    Matcher mimeMatcher = CHARSET_PATTERN.matcher(hints.mimeType);
    String charset = mimeMatcher.find() ? mimeMatcher.group(1) : null;
    try {
        // reset position to start of stream to get full html content
        stream.seek(0L);
        if (charset != null)
            IOUtils.copy(stream, writer, charset);
        else
            IOUtils.copy(stream, writer, StandardCharsets.UTF_8);
    } catch (IOException ex) {
        throw new FriendlyException("Could not read HTML body", SUSPICIOUS, ex);
    }
    String htmlBody = writer.toString();
    Matcher matcher = PLAYLIST_PATTERN.matcher(htmlBody);
    if (matcher.find()) {
        return detectContainer(resolve(reference, matcher.group(1)), true);
    }
    return null;
}
Also used : StringWriter(java.io.StringWriter) Matcher(java.util.regex.Matcher) IOException(java.io.IOException) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException)

Example 10 with FriendlyException

use of com.sedmelluq.discord.lavaplayer.tools.FriendlyException in project FredBoat by Frederikam.

the class PlaylistImportSourceManager method loadAndParseTrackIds.

private List<String> loadAndParseTrackIds(String serviceName, String pasteId) {
    String response;
    try {
        response = BotController.Companion.getHTTP().get(PasteServiceConstants.PASTE_SERVICE_URLS.get(serviceName) + pasteId).asString();
    } catch (IOException ex) {
        throw new FriendlyException("Couldn't load playlist. Either " + serviceName + " is down or the playlist does not exist.", FriendlyException.Severity.FAULT, ex);
    }
    String[] unfiltered = response.split("\\s");
    ArrayList<String> filtered = new ArrayList<>();
    for (String str : unfiltered) {
        if (!str.equals("")) {
            filtered.add(str);
        }
    }
    return filtered;
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException)

Aggregations

FriendlyException (com.sedmelluq.discord.lavaplayer.tools.FriendlyException)62 IOException (java.io.IOException)23 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)18 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)13 AudioLoadResultHandler (com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler)12 JsonBrowser (com.sedmelluq.discord.lavaplayer.tools.JsonBrowser)12 AudioPlaylist (com.sedmelluq.discord.lavaplayer.track.AudioPlaylist)12 ArrayList (java.util.ArrayList)10 HttpGet (org.apache.http.client.methods.HttpGet)10 List (java.util.List)7 HttpInterface (com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface)6 BasicAudioPlaylist (com.sedmelluq.discord.lavaplayer.track.BasicAudioPlaylist)5 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 MediaContainerHints (com.sedmelluq.discord.lavaplayer.container.MediaContainerHints)3 PersistentHttpStream (com.sedmelluq.discord.lavaplayer.tools.io.PersistentHttpStream)3 AudioReference (com.sedmelluq.discord.lavaplayer.track.AudioReference)3 AudioTrackInfo (com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo)3 Document (org.jsoup.nodes.Document)3 Track (com.arsenarsen.lavaplayerbridge.player.Track)2