@Override public AudioSourceManager newSourceManagerInstance() throws Exception { YoutubeAudioSourceManager manager = new YoutubeAudioSourceManager(); manager.setPlaylistPageCount(100); return manager; } }
public AudioPlayerManager registerSourceManagers(AudioPlayerManager manager) { manager.registerSourceManager(new PlaylistImportSourceManager()); YoutubeAudioSourceManager youtubeAudioSourceManager = new YoutubeAudioSourceManager(); youtubeAudioSourceManager.configureRequests(config -> RequestConfig.copy(config) .setCookieSpec(CookieSpecs.IGNORE_COOKIES) .build()); manager.registerSourceManager(youtubeAudioSourceManager); manager.registerSourceManager(new SoundCloudAudioSourceManager()); manager.registerSourceManager(new TwitchStreamAudioSourceManager()); manager.registerSourceManager(new BandcampAudioSourceManager()); manager.registerSourceManager(new VimeoAudioSourceManager()); manager.registerSourceManager(new BeamAudioSourceManager()); manager.registerSourceManager(new LocalAudioSourceManager()); manager.registerSourceManager(new HttpAudioSourceManager()); return manager; }
/** * Registers all built-in remote audio sources to the specified player manager. Local file audio source must be * registered separately. * * @param playerManager Player manager to register the source managers to * @param containerRegistry Media container registry to be used by any probing sources. */ public static void registerRemoteSources(AudioPlayerManager playerManager, MediaContainerRegistry containerRegistry) { playerManager.registerSourceManager(new YoutubeAudioSourceManager(true)); playerManager.registerSourceManager(new SoundCloudAudioSourceManager()); playerManager.registerSourceManager(new BandcampAudioSourceManager()); playerManager.registerSourceManager(new VimeoAudioSourceManager()); playerManager.registerSourceManager(new TwitchStreamAudioSourceManager()); playerManager.registerSourceManager(new BeamAudioSourceManager()); playerManager.registerSourceManager(new HttpAudioSourceManager()); }
/** * @param httpInterface HTTP interface to use for performing any necessary request. * @param videoId ID of the video. * @param mustExist If <code>true</code>, throws an exception instead of returning <code>null</code> if the track does * not exist. * @return JSON information about the track if it exists. <code>null</code> if it does not and mustExist is * <code>false</code>. * @throws IOException On network error. */ public JsonBrowser getTrackInfoFromMainPage(HttpInterface httpInterface, String videoId, boolean mustExist) throws IOException { try (CloseableHttpResponse response = httpInterface.execute(new HttpGet(getWatchUrl(videoId)))) { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200) { throw new IOException("Invalid status code for video page response: " + statusCode); } String html = IOUtils.toString(response.getEntity().getContent(), Charset.forName(CHARSET)); String configJson = DataFormatTools.extractBetween(html, "ytplayer.config = ", ";ytplayer.load"); if (configJson != null) { return JsonBrowser.parse(configJson); } } if (determineFailureReason(httpInterface, videoId, mustExist)) { return null; } // In case main page does not give player configuration, but info page indicates an OK result, it is probably an // age-restricted video for which the complete track info can be combined from the embed page and the info page. return getTrackInfoFromEmbedPage(httpInterface, videoId); }
public void init() { AudioSourceManagers.registerRemoteSources(this); AudioSourceManagers.registerLocalSource(this); source(YoutubeAudioSourceManager.class).setPlaylistPageCount(10); }
/** * 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); } }
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); } }
private void extractTrackFromResultEntry(List<AudioTrack> tracks, Element element) { Element durationElement = element.select("[class^=video-time]").first(); Element contentElement = element.select(".yt-lockup-content").first(); String videoId = element.attr("data-context-item-id"); if (durationElement == null || contentElement == null || videoId.isEmpty()) { return; } long duration = DataFormatTools.durationTextToMillis(durationElement.text()); String title = contentElement.select(".yt-lockup-title > a").text(); String author = contentElement.select(".yt-lockup-byline > a").text(); tracks.add(sourceManager.buildTrackObject(videoId, title, author, false, duration)); }
private boolean determineFailureReason(HttpInterface httpInterface, String videoId, boolean mustExist) throws IOException { try (CloseableHttpResponse response = httpInterface.execute(new HttpGet("https://www.youtube.com/get_video_info?hl=en_GB&video_id=" + videoId))) { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200) { throw new IOException("Invalid status code for video info response: " + statusCode); } Map<String, String> format = convertToMapLayout(URLEncodedUtils.parse(response.getEntity())); return determineFailureReasonFromStatus(format.get("status"), format.get("reason"), mustExist); } }
private String extractPlaylistTracks(Element videoContainer, Element loadMoreContainer, List<AudioTrack> tracks) { for (Element video : videoContainer.select(".pl-video")) { Elements lengthElements = video.select(".timestamp span"); // If the timestamp element does not exist, it means the video is private if (!lengthElements.isEmpty()) { String videoId = video.attr("data-video-id").trim(); String title = video.attr("data-title").trim(); String author = video.select(".pl-video-owner a").text().trim(); long duration = DataFormatTools.durationTextToMillis(lengthElements.first().text()); tracks.add(buildTrackObject(videoId, title, author, false, duration)); } } if (loadMoreContainer != null) { Elements more = loadMoreContainer.select(".load-more-button"); if (!more.isEmpty()) { return more.first().attr("data-uix-load-more-href"); } } return null; }
@Override public AudioSourceManager newSourceManagerInstance() throws Exception { YoutubeAudioSourceManager manager = new YoutubeAudioSourceManager(); manager.setPlaylistPageCount(100); return manager; } }
@Override public AudioSourceManager newSourceManagerInstance() throws Exception { YoutubeAudioSourceManager manager = new YoutubeAudioSourceManager(); manager.setPlaylistPageCount(100); return manager; } }
@Override public AudioSourceManager newSourceManagerInstance() throws Exception { YoutubeAudioSourceManager manager = new YoutubeAudioSourceManager(); manager.setPlaylistPageCount(100); return manager; } }
YoutubeAudioSourceManager youtubeAudioSourceManager = new YoutubeAudioSourceManager(); if (bot.getConfigs().config.patreon) { youtubeAudioSourceManager.setPlaylistPageCount(25);