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

230 lines
9.6 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;
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;
import org.schabi.newpipe.extractor.localization.TimeAgoParser;
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper;
2018-03-01 01:02:43 +01:00
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
2018-02-24 22:20:50 +01:00
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
import org.schabi.newpipe.extractor.utils.Utils;
import java.io.IOException;
import javax.annotation.Nonnull;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.fixThumbnailUrl;
2020-02-29 16:42:04 +01:00
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getJsonResponse;
2020-02-27 17:39:23 +01:00
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getTextFromObject;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getUrlFromNavigationEndpoint;
@SuppressWarnings("WeakerAccess")
public class YoutubePlaylistExtractor extends PlaylistExtractor {
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";
2020-02-29 16:55:07 +01:00
final JsonArray ajaxJson = getJsonResponse(url, getExtractorLocalization());
2020-02-26 14:45:50 +01:00
initialData = ajaxJson.getObject(1).getObject("response");
YoutubeParsingHelper.defaultAlertsCheck(initialData);
2020-02-17 18:58:12 +01:00
playlistInfo = getPlaylistInfo();
}
private JsonObject getUploaderInfo() throws ParsingException {
JsonArray items = initialData.getObject("sidebar").getObject("playlistSidebarRenderer").getArray("items");
try {
JsonObject uploaderInfo = items.getObject(1).getObject("playlistSidebarSecondaryInfoRenderer")
.getObject("videoOwner").getObject("videoOwnerRenderer");
if (uploaderInfo != null) {
return uploaderInfo;
}
} catch (Exception ignored) {}
// we might want to create a loop here instead of using duplicated code
try {
JsonObject uploaderInfo = items.getObject(items.size()).getObject("playlistSidebarSecondaryInfoRenderer")
.getObject("videoOwner").getObject("videoOwnerRenderer");
if (uploaderInfo != null) {
return uploaderInfo;
}
} catch (Exception e) {
throw new ParsingException("Could not get uploader info", e);
}
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);
}
}
2018-02-26 15:55:27 +01:00
@Override
2020-02-25 09:07:22 +01:00
public String getNextPageUrl() {
return getNextPageUrlFrom(initialData.getObject("contents").getObject("twoColumnBrowseResultsRenderer")
.getArray("tabs").getObject(0).getObject("tabRenderer").getObject("content")
.getObject("sectionListRenderer").getArray("contents").getObject(0)
.getObject("itemSectionRenderer").getArray("contents").getObject(0)
.getObject("playlistVideoListRenderer").getArray("continuations"));
}
@Nonnull
@Override
2017-08-11 03:23:09 +02:00
public String getName() throws ParsingException {
try {
2020-02-27 17:39:23 +01:00
String name = getTextFromObject(playlistInfo.getObject("title"));
2020-02-17 18:58:12 +01:00
if (name != null) return name;
} catch (Exception ignored) {}
try {
return initialData.getObject("microformat").getObject("microformatDataRenderer").getString("title");
} catch (Exception e) {
throw new ParsingException("Could not get playlist name", e);
}
}
@Override
2017-08-08 23:36:11 +02:00
public String getThumbnailUrl() throws ParsingException {
2020-02-27 19:08:46 +01:00
String url = null;
try {
2020-02-27 19:08:46 +01:00
url = playlistInfo.getObject("thumbnailRenderer").getObject("playlistVideoThumbnailRenderer")
2020-02-17 18:58:12 +01:00
.getObject("thumbnail").getArray("thumbnails").getObject(0).getString("url");
} catch (Exception ignored) {}
2020-02-27 19:08:46 +01:00
if (url == null) {
try {
2020-02-29 17:18:50 +01:00
url = initialData.getObject("microformat").getObject("microformatDataRenderer").getObject("thumbnail")
2020-02-27 19:08:46 +01:00
.getArray("thumbnails").getObject(0).getString("url");
} catch (Exception ignored) {}
2020-02-29 17:18:50 +01:00
if (url == null) 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() {
return ""; // Banner can't be handled by frontend right now.
// Whoever is willing to implement this should also implement it in the frontend.
}
@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-02-27 17:39:23 +01:00
String url = getUploaderInfo().getObject("thumbnail").getArray("thumbnails").getObject(0).getString("url");
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-02-27 17:39:23 +01:00
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);
}
}
2017-11-25 02:03:30 +01:00
@Nonnull
@Override
2020-02-25 09:07:22 +01:00
public InfoItemsPage<StreamInfoItem> getInitialPage() {
2018-02-24 22:20:50 +01:00
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
JsonArray videos = initialData.getObject("contents").getObject("twoColumnBrowseResultsRenderer")
.getArray("tabs").getObject(0).getObject("tabRenderer").getObject("content")
.getObject("sectionListRenderer").getArray("contents").getObject(0)
.getObject("itemSectionRenderer").getArray("contents").getObject(0)
.getObject("playlistVideoListRenderer").getArray("contents");
collectStreamsFrom(collector, videos);
return new InfoItemsPage<>(collector, getNextPageUrl());
}
@Override
public InfoItemsPage<StreamInfoItem> getPage(final String pageUrl) throws IOException, ExtractionException {
2018-03-01 01:02:43 +01:00
if (pageUrl == null || pageUrl.isEmpty()) {
throw new ExtractionException(new IllegalArgumentException("Page url is empty or null"));
}
2018-03-01 01:02:43 +01:00
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
2020-02-29 16:55:07 +01:00
final JsonArray ajaxJson = getJsonResponse(pageUrl, getExtractorLocalization());
2018-03-01 01:02:43 +01:00
JsonObject sectionListContinuation = ajaxJson.getObject(1).getObject("response")
.getObject("continuationContents").getObject("playlistVideoListContinuation");
2018-03-01 01:02:43 +01:00
collectStreamsFrom(collector, sectionListContinuation.getArray("contents"));
2018-03-01 01:02:43 +01:00
return new InfoItemsPage<>(collector, getNextPageUrlFrom(sectionListContinuation.getArray("continuations")));
2018-02-26 15:55:27 +01:00
}
private String getNextPageUrlFrom(JsonArray continuations) {
if (continuations == null) {
2018-02-26 15:55:27 +01:00
return "";
}
JsonObject nextContinuationData = continuations.getObject(0).getObject("nextContinuationData");
String continuation = nextContinuationData.getString("continuation");
String clickTrackingParams = nextContinuationData.getString("clickTrackingParams");
return "https://www.youtube.com/browse_ajax?ctoken=" + continuation + "&continuation=" + continuation
+ "&itct=" + clickTrackingParams;
}
private void collectStreamsFrom(StreamInfoItemsCollector collector, JsonArray videos) {
collector.reset();
final TimeAgoParser timeAgoParser = getTimeAgoParser();
2020-02-23 13:48:54 +01:00
for (Object video : videos) {
if (((JsonObject) video).getObject("playlistVideoRenderer") != null) {
collector.commit(new YoutubeStreamInfoItemExtractor(((JsonObject) video).getObject("playlistVideoRenderer"), timeAgoParser) {
@Override
public long getViewCount() {
return -1;
}
2020-02-23 13:48:54 +01:00
});
}
}
}
}