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 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; }
/** * 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); } }