NewPipeExtractor/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubePlaylistExtractor.java

328 lines
13 KiB
Java
Raw Normal View History

2018-05-08 21:19:03 +02:00
package org.schabi.newpipe.extractor.services.youtube.extractors;
2020-02-17 18:58:12 +01:00
import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;
2020-04-15 14:09:46 +02:00
import org.schabi.newpipe.extractor.Page;
2018-09-04 03:37:31 +02:00
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.downloader.Downloader;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
2020-05-13 17:26:07 +02:00
import org.schabi.newpipe.extractor.localization.DateWrapper;
import org.schabi.newpipe.extractor.localization.TimeAgoParser;
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper;
2020-05-13 17:26:07 +02:00
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeStreamLinkHandlerFactory;
2018-03-01 01:02:43 +01:00
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
2020-05-13 17:26:07 +02:00
import org.schabi.newpipe.extractor.stream.StreamInfoItemExtractor;
2018-02-24 22:20:50 +01:00
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
2020-05-13 17:26:07 +02:00
import org.schabi.newpipe.extractor.stream.StreamType;
import org.schabi.newpipe.extractor.utils.Utils;
import java.io.IOException;
import javax.annotation.Nonnull;
2020-05-13 17:26:07 +02:00
import javax.annotation.Nullable;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.fixThumbnailUrl;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonResponse;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getUrlFromNavigationEndpoint;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
2020-02-27 17:39:23 +01:00
@SuppressWarnings("WeakerAccess")
public class YoutubePlaylistExtractor extends PlaylistExtractor {
private JsonArray initialAjaxJson;
2020-02-17 18:58:12 +01:00
private JsonObject initialData;
private JsonObject playlistInfo;
public YoutubePlaylistExtractor(StreamingService service, ListLinkHandler linkHandler) {
super(service, linkHandler);
2017-08-06 22:20:15 +02:00
}
2017-08-06 22:20:15 +02:00
@Override
2017-11-28 13:37:01 +01:00
public void onFetchPage(@Nonnull Downloader downloader) throws IOException, ExtractionException {
2020-02-26 14:45:50 +01:00
final String url = getUrl() + "&pbj=1";
initialAjaxJson = getJsonResponse(url, getExtractorLocalization());
2020-02-26 14:45:50 +01:00
initialData = initialAjaxJson.getObject(1).getObject("response");
YoutubeParsingHelper.defaultAlertsCheck(initialData);
2020-02-17 18:58:12 +01:00
playlistInfo = getPlaylistInfo();
}
private JsonObject getUploaderInfo() throws ParsingException {
2020-07-06 20:23:41 +02:00
final JsonArray items = initialData.getObject("sidebar").getObject("playlistSidebarRenderer").getArray("items");
2020-04-16 16:08:14 +02:00
JsonObject videoOwner = items.getObject(1).getObject("playlistSidebarSecondaryInfoRenderer").getObject("videoOwner");
if (videoOwner.has("videoOwnerRenderer")) {
return videoOwner.getObject("videoOwnerRenderer");
}
2020-02-17 18:58:12 +01:00
// we might want to create a loop here instead of using duplicated code
2020-04-16 16:08:14 +02:00
videoOwner = items.getObject(items.size()).getObject("playlistSidebarSecondaryInfoRenderer").getObject("videoOwner");
if (videoOwner.has("videoOwnerRenderer")) {
return videoOwner.getObject("videoOwnerRenderer");
2020-02-17 18:58:12 +01:00
}
throw new ParsingException("Could not get uploader info");
}
private JsonObject getPlaylistInfo() throws ParsingException {
try {
return initialData.getObject("sidebar").getObject("playlistSidebarRenderer").getArray("items")
.getObject(0).getObject("playlistSidebarPrimaryInfoRenderer");
} catch (Exception e) {
throw new ParsingException("Could not get PlaylistInfo", e);
}
}
@Nonnull
@Override
2017-08-11 03:23:09 +02:00
public String getName() throws ParsingException {
2020-07-06 20:23:41 +02:00
final String name = getTextFromObject(playlistInfo.getObject("title"));
2021-02-07 22:42:21 +01:00
if (!isNullOrEmpty(name)) return name;
2020-04-16 16:08:14 +02:00
return initialData.getObject("microformat").getObject("microformatDataRenderer").getString("title");
}
@Override
2017-08-08 23:36:11 +02:00
public String getThumbnailUrl() throws ParsingException {
2020-04-16 16:08:14 +02:00
String url = playlistInfo.getObject("thumbnailRenderer").getObject("playlistVideoThumbnailRenderer")
.getObject("thumbnail").getArray("thumbnails").getObject(0).getString("url");
2020-02-27 19:08:46 +01:00
if (isNullOrEmpty(url)) {
2020-04-16 16:08:14 +02:00
url = initialData.getObject("microformat").getObject("microformatDataRenderer").getObject("thumbnail")
.getArray("thumbnails").getObject(0).getString("url");
2020-02-27 19:08:46 +01:00
if (isNullOrEmpty(url)) throw new ParsingException("Could not get playlist thumbnail");
2020-02-27 19:08:46 +01:00
}
2020-02-29 17:18:50 +01:00
return fixThumbnailUrl(url);
}
@Override
2018-03-12 16:14:33 +01:00
public String getBannerUrl() {
2020-04-16 16:08:14 +02:00
// Banner can't be handled by frontend right now.
// Whoever is willing to implement this should also implement it in the frontend.
2020-04-16 16:08:14 +02:00
return "";
}
@Override
public String getUploaderUrl() throws ParsingException {
try {
2020-02-27 17:39:23 +01:00
return getUrlFromNavigationEndpoint(getUploaderInfo().getObject("navigationEndpoint"));
} catch (Exception e) {
throw new ParsingException("Could not get playlist uploader url", e);
}
}
@Override
public String getUploaderName() throws ParsingException {
try {
2020-02-27 17:39:23 +01:00
return getTextFromObject(getUploaderInfo().getObject("title"));
} catch (Exception e) {
throw new ParsingException("Could not get playlist uploader name", e);
}
}
@Override
public String getUploaderAvatarUrl() throws ParsingException {
try {
2020-07-06 20:23:41 +02:00
final String url = getUploaderInfo().getObject("thumbnail").getArray("thumbnails").getObject(0).getString("url");
2020-02-27 17:39:23 +01:00
return fixThumbnailUrl(url);
} catch (Exception e) {
throw new ParsingException("Could not get playlist uploader avatar", e);
}
}
@Override
2017-08-06 22:20:15 +02:00
public long getStreamCount() throws ParsingException {
try {
2020-07-06 20:23:41 +02:00
final String viewsText = getTextFromObject(getPlaylistInfo().getArray("stats").getObject(0));
2020-02-17 18:58:12 +01:00
return Long.parseLong(Utils.removeNonDigitCharacters(viewsText));
} catch (Exception e) {
2017-08-06 22:20:15 +02:00
throw new ParsingException("Could not get video count from playlist", e);
}
}
@Nonnull
@Override
2020-05-13 17:26:07 +02:00
public String getSubChannelName() {
return "";
}
@Nonnull
@Override
2020-05-13 17:26:07 +02:00
public String getSubChannelUrl() {
return "";
}
@Nonnull
@Override
2020-05-13 17:26:07 +02:00
public String getSubChannelAvatarUrl() {
return "";
}
2017-11-25 02:03:30 +01:00
@Nonnull
@Override
2020-02-25 09:07:22 +01:00
public InfoItemsPage<StreamInfoItem> getInitialPage() {
2020-05-13 17:26:07 +02:00
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
2020-04-15 14:09:46 +02:00
Page nextPage = null;
2020-05-13 17:26:07 +02:00
final JsonArray contents = initialData.getObject("contents").getObject("twoColumnBrowseResultsRenderer")
.getArray("tabs").getObject(0).getObject("tabRenderer").getObject("content")
.getObject("sectionListRenderer").getArray("contents").getObject(0)
2020-05-13 17:26:07 +02:00
.getObject("itemSectionRenderer").getArray("contents");
if (contents.getObject(0).has("playlistSegmentRenderer")) {
for (final Object segment : contents) {
if (((JsonObject) segment).getObject("playlistSegmentRenderer").has("trailer")) {
collectTrailerFrom(collector, ((JsonObject) segment));
} else if (((JsonObject) segment).getObject("playlistSegmentRenderer").has("videoList")) {
collectStreamsFrom(collector, ((JsonObject) segment).getObject("playlistSegmentRenderer")
.getObject("videoList").getObject("playlistVideoListRenderer").getArray("contents"));
}
}
return new InfoItemsPage<>(collector, null);
2020-05-13 17:26:07 +02:00
} else if (contents.getObject(0).has("playlistVideoListRenderer")) {
final JsonObject videos = contents.getObject(0).getObject("playlistVideoListRenderer");
final JsonArray videosArray = videos.getArray("contents");
collectStreamsFrom(collector, videosArray);
nextPage = getNextPageFrom(videosArray);
2020-05-13 17:26:07 +02:00
}
2020-04-15 14:09:46 +02:00
return new InfoItemsPage<>(collector, nextPage);
}
@Override
2020-04-15 14:09:46 +02:00
public InfoItemsPage<StreamInfoItem> getPage(final Page page) throws IOException, ExtractionException {
if (page == null || isNullOrEmpty(page.getUrl())) {
throw new IllegalArgumentException("Page doesn't contain an URL");
}
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
2020-04-15 14:09:46 +02:00
final JsonArray ajaxJson = getJsonResponse(page.getUrl(), getExtractorLocalization());
2018-03-01 01:02:43 +01:00
final JsonArray continuation = ajaxJson.getObject(1)
.getObject("response")
.getArray("onResponseReceivedActions")
.getObject(0)
.getObject("appendContinuationItemsAction")
.getArray("continuationItems");
2018-03-01 01:02:43 +01:00
collectStreamsFrom(collector, continuation);
2018-03-01 01:02:43 +01:00
return new InfoItemsPage<>(collector, getNextPageFrom(continuation));
2018-02-26 15:55:27 +01:00
}
private Page getNextPageFrom(final JsonArray contents) {
if (isNullOrEmpty(contents)) {
2020-04-15 14:09:46 +02:00
return null;
}
final JsonObject lastElement = contents.getObject(contents.size() - 1);
if (lastElement.has("continuationItemRenderer")) {
final String continuation = lastElement
.getObject("continuationItemRenderer")
.getObject("continuationEndpoint")
.getObject("continuationCommand")
.getString("token");
return new Page("https://www.youtube.com/browse_ajax?continuation=" + continuation);
} else {
return null;
}
}
2020-05-13 17:26:07 +02:00
private void collectStreamsFrom(final StreamInfoItemsCollector collector, final JsonArray videos) {
final TimeAgoParser timeAgoParser = getTimeAgoParser();
2020-07-06 20:23:41 +02:00
for (final Object video : videos) {
2020-04-16 16:08:14 +02:00
if (((JsonObject) video).has("playlistVideoRenderer")) {
2020-02-23 13:48:54 +01:00
collector.commit(new YoutubeStreamInfoItemExtractor(((JsonObject) video).getObject("playlistVideoRenderer"), timeAgoParser) {
@Override
public long getViewCount() {
return -1;
}
2020-02-23 13:48:54 +01:00
});
}
}
}
2020-05-13 17:26:07 +02:00
private void collectTrailerFrom(final StreamInfoItemsCollector collector,
final JsonObject segment) {
collector.commit(new StreamInfoItemExtractor() {
@Override
public String getName() throws ParsingException {
return getTextFromObject(segment.getObject("playlistSegmentRenderer")
.getObject("title"));
}
@Override
public String getUrl() throws ParsingException {
return YoutubeStreamLinkHandlerFactory.getInstance()
.fromId(segment.getObject("playlistSegmentRenderer").getObject("trailer")
.getObject("playlistVideoPlayerRenderer").getString("videoId"))
.getUrl();
}
@Override
public String getThumbnailUrl() {
final JsonArray thumbnails = initialAjaxJson.getObject(1).getObject("playerResponse")
.getObject("videoDetails").getObject("thumbnail").getArray("thumbnails");
// the last thumbnail is the one with the highest resolution
final String url = thumbnails.getObject(thumbnails.size() - 1).getString("url");
return fixThumbnailUrl(url);
2020-05-13 17:26:07 +02:00
}
@Override
public StreamType getStreamType() {
return StreamType.VIDEO_STREAM;
}
@Override
public boolean isAd() {
return false;
}
@Override
public long getDuration() throws ParsingException {
return YoutubeParsingHelper.parseDurationString(
getTextFromObject(segment.getObject("playlistSegmentRenderer")
.getObject("segmentAnnotation")).split("")[0]);
}
@Override
public long getViewCount() {
return -1;
}
@Override
public String getUploaderName() throws ParsingException {
return YoutubePlaylistExtractor.this.getUploaderName();
}
@Override
public String getUploaderUrl() throws ParsingException {
return YoutubePlaylistExtractor.this.getUploaderUrl();
}
@Nullable
@Override
public String getTextualUploadDate() {
return null;
}
@Nullable
@Override
public DateWrapper getUploadDate() {
return null;
}
});
}
}