com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface Java Examples
The following examples show how to use
com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: YoutubeAudioSourceManager.java From kyoko with MIT License | 6 votes |
private AudioItem loadAnonymous(String videoIds) { try (HttpInterface httpInterface = getHttpInterface()) { try (CloseableHttpResponse response = httpInterface.execute(new HttpGet("https://www.youtube.com/watch_videos?video_ids=" + videoIds))) { int statusCode = response.getStatusLine().getStatusCode(); HttpClientContext context = httpInterface.getContext(); if (statusCode != 200) { throw new IOException("Invalid status code for playlist response: " + statusCode); } // youtube currently transforms watch_video links into a link with a video id and a list id. // because thats what happens, we can simply re-process with the redirected link List<URI> redirects = context.getRedirectLocations(); if (redirects != null && !redirects.isEmpty()) { return loadNonSearch(redirects.get(0).toString()); } else { throw new FriendlyException("Unable to process youtube watch_videos link", SUSPICIOUS, new IllegalStateException("Expected youtube to redirect watch_videos link to a watch?v={id}&list={list_id} link, but it did not redirect at all")); } } } catch (Exception e) { throw ExceptionTools.wrapUnfriendlyExceptions(e); } }
Example #2
Source File: HttpAudioSourceManager.java From lavaplayer with Apache License 2.0 | 6 votes |
private MediaContainerDetectionResult detectContainerWithClient(HttpInterface httpInterface, AudioReference reference) throws IOException { try (PersistentHttpStream inputStream = new PersistentHttpStream(httpInterface, new URI(reference.identifier), Units.CONTENT_LENGTH_UNKNOWN)) { int statusCode = inputStream.checkStatusCode(); String redirectUrl = HttpClientTools.getRedirectLocation(reference.identifier, inputStream.getCurrentResponse()); if (redirectUrl != null) { return refer(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 new MediaContainerDetection(containerRegistry, reference, inputStream, hints).detectContainer(); } catch (URISyntaxException e) { throw new FriendlyException("Not a valid URL.", COMMON, e); } }
Example #3
Source File: DefaultYoutubePlaylistLoader.java From lavaplayer with Apache License 2.0 | 6 votes |
@Override public AudioPlaylist load(HttpInterface httpInterface, String playlistId, String selectedVideoId, Function<AudioTrackInfo, AudioTrack> trackFactory) { HttpGet request = new HttpGet(getPlaylistUrl(playlistId) + "&pbj=1&hl=en"); try (CloseableHttpResponse response = httpInterface.execute(request)) { int statusCode = response.getStatusLine().getStatusCode(); if (!HttpClientTools.isSuccessWithContent(statusCode)) { throw new IOException("Invalid status code for playlist response: " + statusCode); } JsonBrowser json = JsonBrowser.parse(response.getEntity().getContent()); return buildPlaylist(httpInterface, json, selectedVideoId, trackFactory); } catch (IOException e) { throw new RuntimeException(e); } }
Example #4
Source File: SoundCloudClientIdTracker.java From lavaplayer with Apache License 2.0 | 6 votes |
private String findClientIdFromApplicationScript(HttpInterface httpInterface, String scriptUrl) throws IOException { try (CloseableHttpResponse response = httpInterface.execute(new HttpGet(scriptUrl))) { int statusCode = response.getStatusLine().getStatusCode(); if (!HttpClientTools.isSuccessWithContent(statusCode)) { throw new IOException("Invalid status code for application script response: " + statusCode); } String page = EntityUtils.toString(response.getEntity()); Matcher clientIdMatcher = appScriptClientIdPattern.matcher(page); if (clientIdMatcher.find()) { return clientIdMatcher.group(1); } else { throw new IllegalStateException("Could not find client ID from application script."); } } }
Example #5
Source File: BandcampAudioSourceManager.java From lavaplayer with Apache License 2.0 | 6 votes |
private AudioItem extractFromPageWithInterface(HttpInterface httpInterface, String url, AudioItemExtractor extractor) throws Exception { String responseText; try (CloseableHttpResponse response = httpInterface.execute(new HttpGet(url))) { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_NOT_FOUND) { return new AudioReference(null, null); } else if (!HttpClientTools.isSuccessWithContent(statusCode)) { throw new IOException("Invalid status code for track page: " + statusCode); } responseText = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8); } return extractor.extract(httpInterface, responseText); }
Example #6
Source File: HttpStreamTools.java From lavaplayer with Apache License 2.0 | 6 votes |
public static InputStream streamContent(HttpInterface httpInterface, HttpUriRequest request) { CloseableHttpResponse response = null; boolean success = false; try { response = httpInterface.execute(request); int statusCode = response.getStatusLine().getStatusCode(); if (!HttpClientTools.isSuccessWithContent(statusCode)) { throw new IOException("Invalid status code from " + request.getURI() + " URL: " + statusCode); } success = true; return response.getEntity().getContent(); } catch (IOException e) { throw new RuntimeException(e); } finally { if (response != null && !success) { ExceptionTools.closeWithWarnings(response); } } }
Example #7
Source File: RemoteNodeProcessor.java From lavaplayer with Apache License 2.0 | 6 votes |
private boolean processOneTick(HttpInterface httpInterface, RingBufferMath timingAverage) throws Exception { TickBuilder tickBuilder = new TickBuilder(System.currentTimeMillis()); try { if (!dispatchOneTick(httpInterface, tickBuilder)) { return false; } } finally { tickBuilder.endTime = System.currentTimeMillis(); recordTick(tickBuilder.build(), timingAverage); } long sleepDuration = Math.max((tickBuilder.startTime + 500) - tickBuilder.endTime, 10); Thread.sleep(sleepDuration); return true; }
Example #8
Source File: YoutubeApiPlaylistLoader.java From SkyBot with GNU Affero General Public License v3.0 | 6 votes |
@Override public AudioPlaylist load(HttpInterface httpInterface, String playlistId, String selectedVideoId, Function<AudioTrackInfo, AudioTrack> trackFactory) { try { final YoutubePlaylistMetadata firstPage = getPlaylistPageById(playlistId, this.apiKey, null, true); if (firstPage == null) { throw new FriendlyException("This playlist does not exist", COMMON, null); } return buildPlaylist(firstPage, playlistId, selectedVideoId, trackFactory); } catch (IOException e) { Sentry.capture(e); throw ExceptionTools.wrapUnfriendlyExceptions(e); } }
Example #9
Source File: NicoAudioTrack.java From kyoko with MIT License | 6 votes |
private String loadPlaybackUrl(HttpInterface httpInterface) throws IOException { HttpGet request = new HttpGet("http://flapi.nicovideo.jp/api/getflv/" + trackInfo.identifier); try (CloseableHttpResponse response = httpInterface.execute(request)) { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200) { throw new IOException("Unexpected status code from playback parameters page: " + statusCode); } String text = EntityUtils.toString(response.getEntity()); Map<String, String> format = convertToMapLayout(URLEncodedUtils.parse(text, StandardCharsets.UTF_8)); String url = format.get("url"); try (CloseableHttpResponse testResponse = httpInterface.execute(new HttpGet(url))) { int testStatusCode = testResponse.getStatusLine().getStatusCode(); if (testStatusCode != 200) { url = url.replace("/smile?v=", "/smile?m="); url += "low"; } } return url; } }
Example #10
Source File: NicoAudioSourceManager.java From lavaplayer with Apache License 2.0 | 6 votes |
private AudioTrack loadTrack(String videoId) { checkLoggedIn(); try (HttpInterface httpInterface = getHttpInterface()) { try (CloseableHttpResponse response = httpInterface.execute(new HttpGet("http://ext.nicovideo.jp/api/getthumbinfo/" + videoId))) { int statusCode = response.getStatusLine().getStatusCode(); if (!HttpClientTools.isSuccessWithContent(statusCode)) { throw new IOException("Unexpected response code from video info: " + statusCode); } Document document = Jsoup.parse(response.getEntity().getContent(), StandardCharsets.UTF_8.name(), "", Parser.xmlParser()); return extractTrackFromXml(videoId, document); } } catch (IOException e) { throw new FriendlyException("Error occurred when extracting video info.", SUSPICIOUS, e); } }
Example #11
Source File: DefaultSoundCloudPlaylistLoader.java From lavaplayer with Apache License 2.0 | 6 votes |
protected AudioPlaylist loadFromSet( HttpInterfaceManager httpInterfaceManager, String playlistWebUrl, Function<AudioTrackInfo, AudioTrack> trackFactory ) { try (HttpInterface httpInterface = httpInterfaceManager.getInterface()) { JsonBrowser rootData = htmlDataLoader.load(httpInterface, playlistWebUrl); JsonBrowser playlistData = dataReader.findPlaylistData(rootData); return new BasicAudioPlaylist( dataReader.readPlaylistName(playlistData), loadPlaylistTracks(httpInterface, playlistData, trackFactory), null, false ); } catch (IOException e) { throw new FriendlyException("Loading playlist from SoundCloud failed.", SUSPICIOUS, e); } }
Example #12
Source File: NicoAudioSourceManager.java From kyoko with MIT License | 6 votes |
private AudioTrack loadTrack(String videoId) { checkLoggedIn(); try (HttpInterface httpInterface = getHttpInterface()) { try (CloseableHttpResponse response = httpInterface.execute(new HttpGet("https://www.nicovideo.jp/watch/" + videoId))) { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200) { throw new IOException("Unexpected response code from video info: " + statusCode); } Document document = Jsoup.parse(response.getEntity().getContent(), StandardCharsets.UTF_8.name(), "", Parser.htmlParser()); return extractTrackFromHtml(videoId, document); } } catch (IOException e) { throw new FriendlyException("Error occurred when extracting video info.", SUSPICIOUS, e); } }
Example #13
Source File: YoutubeAudioSourceManager.java From lavaplayer with Apache License 2.0 | 6 votes |
/** * 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()) { YoutubeTrackDetails details = trackDetailsLoader.loadDetails(httpInterface, videoId); if (details == null) { if (mustExist) { throw new FriendlyException("Video unavailable", COMMON, null); } else { return AudioReference.NO_TRACK; } } return new YoutubeAudioTrack(details.getTrackInfo(), this); } catch (Exception e) { throw ExceptionTools.wrapUnfriendlyExceptions("Loading information for a YouTube track failed.", FAULT, e); } }
Example #14
Source File: YoutubeAudioSourceManager.java From kyoko with MIT License | 6 votes |
private AudioPlaylist loadPlaylistWithId(String playlistId, String selectedVideoId) { log.debug("Starting to load playlist with ID {}", playlistId); try (HttpInterface httpInterface = getHttpInterface()) { try (CloseableHttpResponse response = httpInterface.execute(new HttpGet("https://www.youtube.com/playlist?list=" + playlistId))) { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200) { throw new IOException("Invalid status code for playlist response: " + statusCode); } Document document = Jsoup.parse(response.getEntity().getContent(), CHARSET, ""); return buildPlaylist(httpInterface, document, selectedVideoId); } } catch (Exception e) { throw ExceptionTools.wrapUnfriendlyExceptions(e); } }
Example #15
Source File: HlsStreamSegmentUrlProvider.java From lavaplayer with Apache License 2.0 | 6 votes |
@Override protected String fetchSegmentPlaylistUrl(HttpInterface httpInterface) throws IOException { if (segmentPlaylistUrl != null) { return segmentPlaylistUrl; } HttpUriRequest request = new HttpGet(streamListUrl); List<ChannelStreamInfo> streams = loadChannelStreamsList(fetchResponseLines(httpInterface, request, "HLS stream list")); if (streams.isEmpty()) { throw new IllegalStateException("No streams listed in HLS stream list."); } ChannelStreamInfo stream = streams.get(0); log.debug("Chose stream with url {}", stream.quality, stream.url); segmentPlaylistUrl = stream.url; return segmentPlaylistUrl; }
Example #16
Source File: YoutubeAudioSourceManager.java From kyoko with MIT License | 6 votes |
private JsonBrowser loadTrackBaseInfoFromEmbedPage(HttpInterface httpInterface, String videoId) throws IOException { try (CloseableHttpResponse response = httpInterface.execute(new HttpGet("https://www.youtube.com/embed/" + videoId))) { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200) { throw new IOException("Invalid status code for embed video page response: " + statusCode); } String html = IOUtils.toString(response.getEntity().getContent(), Charset.forName(CHARSET)); String configJson = DataFormatTools.extractBetween(html, "'PLAYER_CONFIG': ", "});writeEmbed();"); if (configJson != null) { return JsonBrowser.parse(configJson); } } throw new FriendlyException("Track information is unavailable.", SUSPICIOUS, new IllegalStateException("Expected player config is not present in embed page.")); }
Example #17
Source File: YoutubeSearchProvider.java From lavaplayer with Apache License 2.0 | 6 votes |
/** * @param query Search query. * @return Playlist of the first page of results. */ @Override public AudioItem loadSearchResult(String query, Function<AudioTrackInfo, AudioTrack> trackFactory) { log.debug("Performing a search with query {}", query); try (HttpInterface httpInterface = httpInterfaceManager.getInterface()) { URI url = new URIBuilder("https://www.youtube.com/results").addParameter("search_query", query).build(); try (CloseableHttpResponse response = httpInterface.execute(new HttpGet(url))) { int statusCode = response.getStatusLine().getStatusCode(); if (!HttpClientTools.isSuccessWithContent(statusCode)) { throw new IOException("Invalid status code for search response: " + statusCode); } Document document = Jsoup.parse(response.getEntity().getContent(), StandardCharsets.UTF_8.name(), ""); return extractSearchResults(document, query, trackFactory); } } catch (Exception e) { throw ExceptionTools.wrapUnfriendlyExceptions(e); } }
Example #18
Source File: DefaultYoutubeTrackDetails.java From lavaplayer with Apache License 2.0 | 6 votes |
private List<YoutubeTrackFormat> loadTrackFormatsFromDash( String dashUrl, HttpInterface httpInterface, YoutubeSignatureResolver signatureResolver ) throws Exception { String resolvedDashUrl = signatureResolver.resolveDashUrl(httpInterface, getPlayerScript(), dashUrl); try (CloseableHttpResponse response = httpInterface.execute(new HttpGet(resolvedDashUrl))) { int statusCode = response.getStatusLine().getStatusCode(); if (!HttpClientTools.isSuccessWithContent(statusCode)) { throw new IOException("Invalid status code for track info page response: " + statusCode); } Document document = Jsoup.parse(response.getEntity().getContent(), StandardCharsets.UTF_8.name(), "", Parser.xmlParser()); return loadTrackFormatsFromDashDocument(document); } }
Example #19
Source File: VimeoAudioTrack.java From lavaplayer with Apache License 2.0 | 5 votes |
private String loadPlaybackUrl(HttpInterface httpInterface) throws IOException { JsonBrowser config = loadPlayerConfig(httpInterface); if (config == null) { throw new FriendlyException("Track information not present on the page.", SUSPICIOUS, null); } String trackConfigUrl = config.get("player").get("config_url").text(); JsonBrowser trackConfig = loadTrackConfig(httpInterface, trackConfigUrl); return trackConfig.get("request").get("files").get("progressive").index(0).get("url").text(); }
Example #20
Source File: SoundCloudAudioSourceManager.java From lavaplayer with Apache License 2.0 | 5 votes |
private JsonBrowser loadLikedListForUserId(HttpInterface httpInterface, UserInfo userInfo) throws IOException { URI uri = URI.create("https://api-v2.soundcloud.com/users/" + userInfo.id + "/likes?limit=200&offset=0"); try (CloseableHttpResponse response = httpInterface.execute(new HttpGet(uri))) { HttpClientTools.assertSuccessWithContent(response, "liked tracks response"); return JsonBrowser.parse(response.getEntity().getContent()); } }
Example #21
Source File: SoundCloudHelper.java From lavaplayer with Apache License 2.0 | 5 votes |
public static String loadPlaybackUrl(HttpInterface httpInterface, String jsonUrl) throws IOException { try (PersistentHttpStream stream = new PersistentHttpStream(httpInterface, URI.create(jsonUrl), null)) { if (!HttpClientTools.isSuccessWithContent(stream.checkStatusCode())) { throw new IOException("Invalid status code for soundcloud stream: " + stream.checkStatusCode()); } JsonBrowser json = JsonBrowser.parse(stream); return json.get("url").text(); } }
Example #22
Source File: SoundCloudAudioSourceManager.java From lavaplayer with Apache License 2.0 | 5 votes |
private AudioItem loadSearchResult(String query, int offset, int rawLimit) { int limit = Math.min(rawLimit, MAXIMUM_SEARCH_RESULTS); try ( HttpInterface httpInterface = getHttpInterface(); CloseableHttpResponse response = httpInterface.execute(new HttpGet(buildSearchUri(query, offset, limit))) ) { return loadSearchResultsFromResponse(response, query); } catch (IOException e) { throw new FriendlyException("Loading search results from SoundCloud failed.", SUSPICIOUS, e); } }
Example #23
Source File: YoutubeAudioTrack.java From lavaplayer with Apache License 2.0 | 5 votes |
private void processStatic(LocalAudioTrackExecutor localExecutor, HttpInterface httpInterface, FormatWithUrl format) throws Exception { try (YoutubePersistentHttpStream stream = new YoutubePersistentHttpStream(httpInterface, format.signedUrl, format.details.getContentLength())) { if (format.details.getType().getMimeType().endsWith("/webm")) { processDelegate(new MatroskaAudioTrack(trackInfo, stream), localExecutor); } else { processDelegate(new MpegAudioTrack(trackInfo, stream), localExecutor); } } }
Example #24
Source File: VimeoAudioTrack.java From lavaplayer with Apache License 2.0 | 5 votes |
@Override public void process(LocalAudioTrackExecutor localExecutor) throws Exception { try (HttpInterface httpInterface = sourceManager.getHttpInterface()) { String playbackUrl = loadPlaybackUrl(httpInterface); log.debug("Starting Vimeo track from URL: {}", playbackUrl); try (PersistentHttpStream stream = new PersistentHttpStream(httpInterface, new URI(playbackUrl), null)) { processDelegate(new MpegAudioTrack(trackInfo, stream), localExecutor); } } }
Example #25
Source File: VimeoAudioTrack.java From lavaplayer with Apache License 2.0 | 5 votes |
private JsonBrowser loadPlayerConfig(HttpInterface httpInterface) throws IOException { try (CloseableHttpResponse response = httpInterface.execute(new HttpGet(trackInfo.identifier))) { int statusCode = response.getStatusLine().getStatusCode(); if (!HttpClientTools.isSuccessWithContent(statusCode)) { throw new FriendlyException("Server responded with an error.", SUSPICIOUS, new IllegalStateException("Response code for player config is " + statusCode)); } return sourceManager.loadConfigJsonFromPageContent(IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8)); } }
Example #26
Source File: DefaultYoutubeTrackDetails.java From lavaplayer with Apache License 2.0 | 5 votes |
@Override public List<YoutubeTrackFormat> getFormats(HttpInterface httpInterface, YoutubeSignatureResolver signatureResolver) { try { return loadTrackFormats(httpInterface, signatureResolver); } catch (Exception e) { throw new RuntimeException(e); } }
Example #27
Source File: HttpAudioTrack.java From lavaplayer with Apache License 2.0 | 5 votes |
@Override public void process(LocalAudioTrackExecutor localExecutor) throws Exception { try (HttpInterface httpInterface = sourceManager.getHttpInterface()) { log.debug("Starting http track from URL: {}", trackInfo.identifier); try (PersistentHttpStream inputStream = new PersistentHttpStream(httpInterface, new URI(trackInfo.identifier), Units.CONTENT_LENGTH_UNKNOWN)) { processDelegate((InternalAudioTrack) containerTrackFactory.createTrack(trackInfo, inputStream), localExecutor); } } }
Example #28
Source File: VimeoAudioSourceManager.java From lavaplayer with Apache License 2.0 | 5 votes |
private AudioItem loadFromTrackPage(HttpInterface httpInterface, String trackUrl) throws IOException { try (CloseableHttpResponse response = httpInterface.execute(new HttpGet(trackUrl))) { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_NOT_FOUND) { return AudioReference.NO_TRACK; } else if (!HttpClientTools.isSuccessWithContent(statusCode)) { throw new FriendlyException("Server responded with an error.", SUSPICIOUS, new IllegalStateException("Response code is " + statusCode)); } return loadTrackFromPageContent(trackUrl, IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8)); } }
Example #29
Source File: VimeoAudioTrack.java From lavaplayer with Apache License 2.0 | 5 votes |
private JsonBrowser loadTrackConfig(HttpInterface httpInterface, String trackAccessInfoUrl) throws IOException { try (CloseableHttpResponse response = httpInterface.execute(new HttpGet(trackAccessInfoUrl))) { int statusCode = response.getStatusLine().getStatusCode(); if (!HttpClientTools.isSuccessWithContent(statusCode)) { throw new FriendlyException("Server responded with an error.", SUSPICIOUS, new IllegalStateException("Response code for track access info is " + statusCode)); } return JsonBrowser.parse(response.getEntity().getContent()); } }
Example #30
Source File: VimeoAudioSourceManager.java From lavaplayer with Apache License 2.0 | 5 votes |
@Override public AudioItem loadItem(DefaultAudioPlayerManager manager, AudioReference reference) { if (!trackUrlPattern.matcher(reference.identifier).matches()) { return null; } try (HttpInterface httpInterface = httpInterfaceManager.getInterface()) { return loadFromTrackPage(httpInterface, reference.identifier); } catch (IOException e) { throw new FriendlyException("Loading Vimeo track information failed.", SUSPICIOUS, e); } }