[YouTube] Fix extraction of series playlists and don't return the view count as the stream count for learning playlists

ITEM_COUNT_UNKNOWN is returned when the JSON array which contains usally the number of videos is less than 3 items.
Also apply the same type of optimizations done in other PlaylistExtractors in YoutubePlaylistExtractor.
This commit is contained in:
TiA4f8R 2022-03-12 17:28:36 +01:00
parent 58a247907e
commit 8b3f90eb7e
No known key found for this signature in database
GPG Key ID: E6D3E7F5949450DD
1 changed files with 122 additions and 75 deletions

View File

@ -21,22 +21,31 @@ import org.schabi.newpipe.extractor.utils.JsonUtils;
import org.schabi.newpipe.extractor.utils.Utils; import org.schabi.newpipe.extractor.utils.Utils;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.*; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.*;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8; import static org.schabi.newpipe.extractor.utils.Utils.*;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
@SuppressWarnings("WeakerAccess")
public class YoutubePlaylistExtractor extends PlaylistExtractor { public class YoutubePlaylistExtractor extends PlaylistExtractor {
private JsonObject initialData; // Minimum size of the stats array in the browse response which includes the streams count
private static final int STATS_ARRAY_WITH_STREAMS_COUNT_MIN_SIZE = 2;
// Names of some objects in JSON response frequently used in this class
private static final String PLAYLIST_VIDEO_RENDERER = "playlistVideoRenderer";
private static final String PLAYLIST_VIDEO_LIST_RENDERER = "playlistVideoListRenderer";
private static final String VIDEO_OWNER_RENDERER = "videoOwnerRenderer";
private JsonObject browseResponse;
private JsonObject playlistInfo; private JsonObject playlistInfo;
public YoutubePlaylistExtractor(StreamingService service, ListLinkHandler linkHandler) { public YoutubePlaylistExtractor(final StreamingService service,
final ListLinkHandler linkHandler) {
super(service, linkHandler); super(service, linkHandler);
} }
@ -49,37 +58,42 @@ public class YoutubePlaylistExtractor extends PlaylistExtractor {
.value("browseId", "VL" + getId()) .value("browseId", "VL" + getId())
.value("params", "wgYCCAA%3D") // Show unavailable videos .value("params", "wgYCCAA%3D") // Show unavailable videos
.done()) .done())
.getBytes(UTF_8); .getBytes(StandardCharsets.UTF_8);
initialData = getJsonPostResponse("browse", body, localization); browseResponse = getJsonPostResponse("browse", body, localization);
YoutubeParsingHelper.defaultAlertsCheck(initialData); YoutubeParsingHelper.defaultAlertsCheck(browseResponse);
playlistInfo = getPlaylistInfo(); playlistInfo = getPlaylistInfo();
} }
private JsonObject getUploaderInfo() throws ParsingException { private JsonObject getUploaderInfo() throws ParsingException {
final JsonArray items = initialData.getObject("sidebar") final JsonArray items = browseResponse.getObject("sidebar")
.getObject("playlistSidebarRenderer").getArray("items"); .getObject("playlistSidebarRenderer")
.getArray("items");
JsonObject videoOwner = items.getObject(1) JsonObject videoOwner = items.getObject(1)
.getObject("playlistSidebarSecondaryInfoRenderer").getObject("videoOwner"); .getObject("playlistSidebarSecondaryInfoRenderer")
if (videoOwner.has("videoOwnerRenderer")) { .getObject("videoOwner");
return videoOwner.getObject("videoOwnerRenderer"); if (videoOwner.has(VIDEO_OWNER_RENDERER)) {
return videoOwner.getObject(VIDEO_OWNER_RENDERER);
} }
// we might want to create a loop here instead of using duplicated code // we might want to create a loop here instead of using duplicated code
videoOwner = items.getObject(items.size()) videoOwner = items.getObject(items.size())
.getObject("playlistSidebarSecondaryInfoRenderer").getObject("videoOwner"); .getObject("playlistSidebarSecondaryInfoRenderer")
if (videoOwner.has("videoOwnerRenderer")) { .getObject("videoOwner");
return videoOwner.getObject("videoOwnerRenderer"); if (videoOwner.has(VIDEO_OWNER_RENDERER)) {
return videoOwner.getObject(VIDEO_OWNER_RENDERER);
} }
throw new ParsingException("Could not get uploader info"); throw new ParsingException("Could not get uploader info");
} }
private JsonObject getPlaylistInfo() throws ParsingException { private JsonObject getPlaylistInfo() throws ParsingException {
try { try {
return initialData.getObject("sidebar").getObject("playlistSidebarRenderer") return browseResponse.getObject("sidebar")
.getArray("items").getObject(0) .getObject("playlistSidebarRenderer")
.getArray("items")
.getObject(0)
.getObject("playlistSidebarPrimaryInfoRenderer"); .getObject("playlistSidebarPrimaryInfoRenderer");
} catch (final Exception e) { } catch (final Exception e) {
throw new ParsingException("Could not get PlaylistInfo", e); throw new ParsingException("Could not get PlaylistInfo", e);
@ -90,33 +104,41 @@ public class YoutubePlaylistExtractor extends PlaylistExtractor {
@Override @Override
public String getName() throws ParsingException { public String getName() throws ParsingException {
final String name = getTextFromObject(playlistInfo.getObject("title")); final String name = getTextFromObject(playlistInfo.getObject("title"));
if (!isNullOrEmpty(name)) return name; if (!isNullOrEmpty(name)) {
return name;
return initialData.getObject("microformat").getObject("microformatDataRenderer").getString("title");
} }
return browseResponse.getObject("microformat")
.getObject("microformatDataRenderer")
.getString("title");
}
@Nonnull
@Override @Override
public String getThumbnailUrl() throws ParsingException { public String getThumbnailUrl() throws ParsingException {
String url = playlistInfo.getObject("thumbnailRenderer").getObject("playlistVideoThumbnailRenderer") String url = playlistInfo.getObject("thumbnailRenderer")
.getObject("thumbnail").getArray("thumbnails").getObject(0).getString("url"); .getObject("playlistVideoThumbnailRenderer")
.getObject("thumbnail")
.getArray("thumbnails")
.getObject(0)
.getString("url");
if (isNullOrEmpty(url)) { if (isNullOrEmpty(url)) {
url = initialData.getObject("microformat").getObject("microformatDataRenderer").getObject("thumbnail") url = browseResponse.getObject("microformat")
.getArray("thumbnails").getObject(0).getString("url"); .getObject("microformatDataRenderer")
.getObject("thumbnail")
.getArray("thumbnails")
.getObject(0)
.getString("url");
if (isNullOrEmpty(url)) throw new ParsingException("Could not get playlist thumbnail"); if (isNullOrEmpty(url)) {
throw new ParsingException("Could not get playlist thumbnail");
}
} }
return fixThumbnailUrl(url); return fixThumbnailUrl(url);
} }
@Override
public String getBannerUrl() {
// Banner can't be handled by frontend right now.
// Whoever is willing to implement this should also implement it in the frontend.
return "";
}
@Override @Override
public String getUploaderUrl() throws ParsingException { public String getUploaderUrl() throws ParsingException {
try { try {
@ -138,7 +160,11 @@ public class YoutubePlaylistExtractor extends PlaylistExtractor {
@Override @Override
public String getUploaderAvatarUrl() throws ParsingException { public String getUploaderAvatarUrl() throws ParsingException {
try { try {
final String url = getUploaderInfo().getObject("thumbnail").getArray("thumbnails").getObject(0).getString("url"); final String url = getUploaderInfo()
.getObject("thumbnail")
.getArray("thumbnails")
.getObject(0)
.getString("url");
return fixThumbnailUrl(url); return fixThumbnailUrl(url);
} catch (final Exception e) { } catch (final Exception e) {
@ -148,14 +174,29 @@ public class YoutubePlaylistExtractor extends PlaylistExtractor {
@Override @Override
public boolean isUploaderVerified() throws ParsingException { public boolean isUploaderVerified() throws ParsingException {
// YouTube doesn't provide this information
return false; return false;
} }
@Override @Override
public long getStreamCount() throws ParsingException { public long getStreamCount() throws ParsingException {
try { try {
final String viewsText = getTextFromObject(getPlaylistInfo().getArray("stats").getObject(0)); final JsonArray stats = playlistInfo.getArray("stats");
return Long.parseLong(Utils.removeNonDigitCharacters(viewsText)); // For unknown reasons, YouTube don't provide the stream count for learning playlists
// on the desktop client but only the number of views and the playlist modified date
// On normal playlists, at least 3 items are returned: the number of videos, the number
// of views and the playlist modification date
// We can get it by using another client, however it seems we can't get the avatar
// uploader URL with another client than the WEB client
if (stats.size() > STATS_ARRAY_WITH_STREAMS_COUNT_MIN_SIZE) {
final String videosText = getTextFromObject(playlistInfo.getArray("stats")
.getObject(0));
if (videosText != null) {
return Long.parseLong(Utils.removeNonDigitCharacters(videosText));
}
}
return ITEM_COUNT_UNKNOWN;
} catch (final Exception e) { } catch (final Exception e) {
throw new ParsingException("Could not get video count from playlist", e); throw new ParsingException("Could not get video count from playlist", e);
} }
@ -164,19 +205,19 @@ public class YoutubePlaylistExtractor extends PlaylistExtractor {
@Nonnull @Nonnull
@Override @Override
public String getSubChannelName() { public String getSubChannelName() {
return ""; return EMPTY_STRING;
} }
@Nonnull @Nonnull
@Override @Override
public String getSubChannelUrl() { public String getSubChannelUrl() {
return ""; return EMPTY_STRING;
} }
@Nonnull @Nonnull
@Override @Override
public String getSubChannelAvatarUrl() { public String getSubChannelAvatarUrl() {
return ""; return EMPTY_STRING;
} }
@Nonnull @Nonnull
@ -185,26 +226,31 @@ public class YoutubePlaylistExtractor extends PlaylistExtractor {
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId()); final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
Page nextPage = null; Page nextPage = null;
final JsonArray contents = initialData.getObject("contents") final JsonArray contents = browseResponse.getObject("contents")
.getObject("twoColumnBrowseResultsRenderer").getArray("tabs").getObject(0) .getObject("twoColumnBrowseResultsRenderer")
.getObject("tabRenderer").getObject("content").getObject("sectionListRenderer") .getArray("tabs")
.getArray("contents").getObject(0).getObject("itemSectionRenderer") .getObject(0)
.getObject("tabRenderer")
.getObject("content")
.getObject("sectionListRenderer")
.getArray("contents"); .getArray("contents");
if (contents.getObject(0).has("playlistSegmentRenderer")) { final JsonObject videoPlaylistObject = contents.stream()
for (final Object segment : contents) { .map(JsonObject.class::cast)
if (((JsonObject) segment).getObject("playlistSegmentRenderer") .map(content -> content.getObject("itemSectionRenderer")
.has("videoList")) { .getArray("contents")
collectStreamsFrom(collector, ((JsonObject) segment) .getObject(0))
.getObject("playlistSegmentRenderer").getObject("videoList") .filter(contentItemSectionRendererContents ->
.getObject("playlistVideoListRenderer").getArray("contents")); contentItemSectionRendererContents.has(PLAYLIST_VIDEO_LIST_RENDERER)
} || contentItemSectionRendererContents.has(
} "playlistSegmentRenderer"))
.findFirst()
.orElse(null);
return new InfoItemsPage<>(collector, null); if (videoPlaylistObject != null && videoPlaylistObject.has(PLAYLIST_VIDEO_LIST_RENDERER)) {
} else if (contents.getObject(0).has("playlistVideoListRenderer")) { final JsonArray videosArray = videoPlaylistObject
final JsonObject videos = contents.getObject(0).getObject("playlistVideoListRenderer"); .getObject(PLAYLIST_VIDEO_LIST_RENDERER)
final JsonArray videosArray = videos.getArray("contents"); .getArray("contents");
collectStreamsFrom(collector, videosArray); collectStreamsFrom(collector, videosArray);
nextPage = getNextPageFrom(videosArray); nextPage = getNextPageFrom(videosArray);
@ -229,7 +275,8 @@ public class YoutubePlaylistExtractor extends PlaylistExtractor {
final JsonObject ajaxJson = JsonUtils.toJsonObject(getValidJsonResponseBody(response)); final JsonObject ajaxJson = JsonUtils.toJsonObject(getValidJsonResponseBody(response));
final JsonArray continuation = ajaxJson.getArray("onResponseReceivedActions") final JsonArray continuation = ajaxJson.getArray("onResponseReceivedActions")
.getObject(0).getObject("appendContinuationItemsAction") .getObject(0)
.getObject("appendContinuationItemsAction")
.getArray("continuationItems"); .getArray("continuationItems");
collectStreamsFrom(collector, continuation); collectStreamsFrom(collector, continuation);
@ -237,8 +284,9 @@ public class YoutubePlaylistExtractor extends PlaylistExtractor {
return new InfoItemsPage<>(collector, getNextPageFrom(continuation)); return new InfoItemsPage<>(collector, getNextPageFrom(continuation));
} }
private Page getNextPageFrom(final JsonArray contents) throws IOException, @Nullable
ExtractionException { private Page getNextPageFrom(final JsonArray contents)
throws IOException, ExtractionException {
if (isNullOrEmpty(contents)) { if (isNullOrEmpty(contents)) {
return null; return null;
} }
@ -255,7 +303,7 @@ public class YoutubePlaylistExtractor extends PlaylistExtractor {
getExtractorLocalization(), getExtractorContentCountry()) getExtractorLocalization(), getExtractorContentCountry())
.value("continuation", continuation) .value("continuation", continuation)
.done()) .done())
.getBytes(UTF_8); .getBytes(StandardCharsets.UTF_8);
return new Page(YOUTUBEI_V1_URL + "browse?key=" + getKey(), body); return new Page(YOUTUBEI_V1_URL + "browse?key=" + getKey(), body);
} else { } else {
@ -263,20 +311,19 @@ public class YoutubePlaylistExtractor extends PlaylistExtractor {
} }
} }
private void collectStreamsFrom(final StreamInfoItemsCollector collector, private void collectStreamsFrom(@Nonnull final StreamInfoItemsCollector collector,
final JsonArray videos) { @Nonnull final JsonArray videos) {
final TimeAgoParser timeAgoParser = getTimeAgoParser(); final TimeAgoParser timeAgoParser = getTimeAgoParser();
for (final Object video : videos) { videos.stream()
if (((JsonObject) video).has("playlistVideoRenderer")) { .filter(video -> ((JsonObject) video).has(PLAYLIST_VIDEO_RENDERER))
collector.commit(new YoutubeStreamInfoItemExtractor(((JsonObject) video) .map(video -> new YoutubeStreamInfoItemExtractor(((JsonObject) video)
.getObject("playlistVideoRenderer"), timeAgoParser) { .getObject(PLAYLIST_VIDEO_RENDERER), timeAgoParser) {
@Override @Override
public long getViewCount() { public long getViewCount() {
return -1; return -1;
} }
}); })
} .forEachOrdered(collector::commit);
}
} }
} }