Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugins {
allprojects {
apply(plugin = "java-library")

version = "v0.25.2"
version = "v0.26.0"

tasks.withType<JavaCompile> {
options.encoding = Charsets.UTF_8.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.schabi.newpipe.extractor.stream.Description;
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
import org.schabi.newpipe.extractor.utils.JsonUtils;
import org.schabi.newpipe.extractor.utils.Utils;

import java.io.IOException;
Expand Down Expand Up @@ -61,6 +62,7 @@ public class YoutubePlaylistExtractor extends PlaylistExtractor {
private JsonObject playlistHeader;

private boolean isNewPlaylistInterface;
private Boolean isCoursePlaylist = null;

public YoutubePlaylistExtractor(final StreamingService service,
final ListLinkHandler linkHandler) {
Expand Down Expand Up @@ -175,6 +177,31 @@ private JsonObject getPlaylistHeader() {
return playlistHeader;
}

private Boolean isCoursePlaylist() {
if (isCoursePlaylist == null) {
try {
isCoursePlaylist = JsonUtils.getArray(getPlaylistHeader(),
"onDescriptionTap.commandExecutorCommand.commands")
.stream()
.filter(JsonObject.class::isInstance)
.map(JsonObject.class::cast)
.anyMatch(object -> {
try {
final String tag = JsonUtils.getString(object,
"showEngagementPanelEndpoint.identifier.tag");
return tag.equals("engagement-panel-course-metadata");
} catch (final ParsingException e) {
return false;
}
});
System.out.println(isCoursePlaylist);
} catch (final Exception e) {
isCoursePlaylist = false;
}
}
return isCoursePlaylist;
}

@Nonnull
@Override
public String getName() throws ParsingException {
Expand Down Expand Up @@ -420,13 +447,30 @@ private Page getNextPageFrom(final JsonArray contents)
private void collectStreamsFrom(@Nonnull final StreamInfoItemsCollector collector,
@Nonnull final JsonArray videos) {
final TimeAgoParser timeAgoParser = getTimeAgoParser();
final PlaylistExtractor playlistExtractor = this;
videos.stream()
.filter(JsonObject.class::isInstance)
.map(JsonObject.class::cast)
.forEach(video -> {
if (video.has(PLAYLIST_VIDEO_RENDERER)) {
collector.commit(new YoutubeStreamInfoItemExtractor(
video.getObject(PLAYLIST_VIDEO_RENDERER), timeAgoParser));
video.getObject(PLAYLIST_VIDEO_RENDERER), timeAgoParser) {
@Override
public String getUploaderName() throws ParsingException {
if (isCoursePlaylist()) {
return playlistExtractor.getUploaderName();
}
return super.getUploaderName();
}

@Override
public String getUploaderUrl() throws ParsingException {
if (isCoursePlaylist()) {
return playlistExtractor.getUploaderUrl();
}
return super.getUploaderUrl();
}
});
} else if (video.has(RICH_ITEM_RENDERER)) {
final JsonObject richItemRenderer = video.getObject(RICH_ITEM_RENDERER);
if (richItemRenderer.has("content")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,77 +414,6 @@ public long getLikeCount() throws ParsingException {
.getObject("menuRenderer")
.getArray("topLevelButtons");

try {
return parseLikeCountFromLikeButtonViewModel(topLevelButtons);
} catch (final ParsingException ignored) {
// A segmentedLikeDislikeButtonRenderer could be returned instead of a
// segmentedLikeDislikeButtonViewModel, so ignore extraction errors relative to
// segmentedLikeDislikeButtonViewModel object
}

try {
return parseLikeCountFromLikeButtonRenderer(topLevelButtons);
} catch (final ParsingException e) {
throw new ParsingException("Could not get like count", e);
}
}

private static long parseLikeCountFromLikeButtonRenderer(
@Nonnull final JsonArray topLevelButtons) throws ParsingException {
String likesString = null;
final JsonObject likeToggleButtonRenderer = topLevelButtons.stream()
.filter(JsonObject.class::isInstance)
.map(JsonObject.class::cast)
.map(button -> button.getObject("segmentedLikeDislikeButtonRenderer")
.getObject("likeButton")
.getObject("toggleButtonRenderer"))
.filter(toggleButtonRenderer -> !isNullOrEmpty(toggleButtonRenderer))
.findFirst()
.orElse(null);

if (likeToggleButtonRenderer != null) {
// Use one of the accessibility strings available (this one has the same path as the
// one used for comments' like count extraction)
likesString = likeToggleButtonRenderer.getObject("accessibilityData")
.getObject("accessibilityData")
.getString("label");

// Use the other accessibility string available which contains the exact like count
if (likesString == null) {
likesString = likeToggleButtonRenderer.getObject("accessibility")
.getString("label");
}

// Last method: use the defaultText's accessibility data, which contains the exact like
// count too, except when it is equal to 0, where a localized string is returned instead
if (likesString == null) {
likesString = likeToggleButtonRenderer.getObject("defaultText")
.getObject("accessibility")
.getObject("accessibilityData")
.getString("label");
}

// This check only works with English localizations!
if (likesString != null && likesString.toLowerCase().contains("no likes")) {
return 0;
}
}

// If ratings are allowed and the likes string is null, it means that we couldn't extract
// the full like count from accessibility data
if (likesString == null) {
throw new ParsingException("Could not get like count from accessibility data");
}

try {
return Long.parseLong(Utils.removeNonDigitCharacters(likesString));
} catch (final NumberFormatException e) {
throw new ParsingException("Could not parse \"" + likesString + "\" as a long", e);
}
}

private static long parseLikeCountFromLikeButtonViewModel(
@Nonnull final JsonArray topLevelButtons) throws ParsingException {
// Try first with the current video actions buttons data structure
final JsonObject likeToggleButtonViewModel = topLevelButtons.stream()
.filter(JsonObject.class::isInstance)
Expand All @@ -509,14 +438,14 @@ private static long parseLikeCountFromLikeButtonViewModel(
throw new ParsingException("Could not find buttonViewModel's accessibilityText string");
}

// The like count is always returned as a number in this element, even for videos with no
// likes
// The like count is always returned as a number in this element for videos with likes
try {
return Long.parseLong(Utils.removeNonDigitCharacters(accessibilityText));
} catch (final NumberFormatException e) {
throw new ParsingException(
"Could not parse \"" + accessibilityText + "\" as a long", e);
// If an exception was thrown, the video has zero likes
}

return 0;
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,15 @@ public String getUrl() throws ParsingException {

@Override
public String getName() throws ParsingException {
final String name = getTextFromObject(videoInfo.getObject("title"));
final JsonObject title = videoInfo.getObject("title");
final String name = getTextFromObject(title);
if (!isNullOrEmpty(name)) {
return name;
}
// Videos can have no title, e.g. https://www.youtube.com/watch?v=nc1kN8ZSfGQ
if (!isNullOrEmpty(title) && !title.has("runs")) {
return "";
}
throw new ParsingException("Could not get name");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,30 @@ public long getDuration() throws ParsingException {
return -1;
}

final List<String> potentialDurations = JsonUtils.getArray(lockupViewModel,
"contentImage.thumbnailViewModel.overlays")
.streamAsJsonObjects()
.flatMap(jsonObject -> jsonObject
.getObject("thumbnailOverlayBadgeViewModel")
.getArray("thumbnailBadges")
.streamAsJsonObjects())
.map(jsonObject -> jsonObject
.getObject("thumbnailBadgeViewModel")
.getString("text"))
.collect(Collectors.toList());
final JsonArray overlays = JsonUtils.getArray(lockupViewModel,
"contentImage.thumbnailViewModel.overlays");

List<JsonObject> thumbnailBadges = overlays.streamAsJsonObjects()
.flatMap(jsonObject -> jsonObject
.getObject("thumbnailOverlayBadgeViewModel")
.getArray("thumbnailBadges")
.streamAsJsonObjects())
.collect(Collectors.toList());

if (thumbnailBadges.isEmpty()) {
thumbnailBadges = overlays.streamAsJsonObjects()
.flatMap(jsonObject -> jsonObject
.getObject("thumbnailBottomOverlayViewModel")
.getArray("badges")
.streamAsJsonObjects())
.collect(Collectors.toList());
}

final List<String> potentialDurations = thumbnailBadges.stream()
.map(jsonObject -> jsonObject
.getObject("thumbnailBadgeViewModel")
.getString("text"))
.collect(Collectors.toList());

if (potentialDurations.isEmpty()) {
throw new ParsingException("Could not get duration: No parsable durations detected");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ public static class MembersOnlyTests implements InitYoutubeTest {
void testOnlyMembersOnlyVideos() throws Exception {
final YoutubePlaylistExtractor extractor = (YoutubePlaylistExtractor) YouTube
.getPlaylistExtractor(
// auto-generated playlist with only membersOnly videos
// autogenerated playlist with only membersOnly videos
"https://www.youtube.com/playlist?list=UUMOQuLXlFNAeDJMSmuzHU5axw");
extractor.fetchPage();

Expand All @@ -530,4 +530,23 @@ void testOnlyMembersOnlyVideos() throws Exception {
assertTrue(membershipVideos.isEmpty());
}
}

public static class CoursePlaylistTest implements InitYoutubeTest {

@Test
void uploaderName() throws Exception {
final YoutubePlaylistExtractor extractor = (YoutubePlaylistExtractor) YouTube
.getPlaylistExtractor(
"https://www.youtube.com/playlist?list=PLWxziGKTUvQFIsbbFcTZz7jOT4TMGnZBh");
extractor.fetchPage();

final List<StreamInfoItem> allItems = extractor.getInitialPage().getItems()
.stream()
.filter(StreamInfoItem.class::isInstance)
.map(StreamInfoItem.class::cast)
.collect(Collectors.toUnmodifiableList());
assertEquals(14, allItems.size());
assertEquals(extractor.getUploaderName(), allItems.get(0).getUploaderName());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,29 @@ void lockupViewModelPremiere()
() -> assertFalse(extractor.isShortFormContent())
);
}

@Test
void emptyTitle() throws FileNotFoundException, JsonParserException {
final var json = JsonParser.object().from(new FileInputStream(getMockPath(
YoutubeStreamInfoItemTest.class, "emptyTitle") + ".json"));
final var timeAgoParser = TimeAgoPatternsManager.getTimeAgoParserFor(Localization.DEFAULT);
final var extractor = new YoutubeStreamInfoItemExtractor(json, timeAgoParser);
assertAll(
() -> assertEquals(StreamType.VIDEO_STREAM, extractor.getStreamType()),
() -> assertFalse(extractor.isAd()),
() -> assertEquals("https://www.youtube.com/watch?v=nc1kN8ZSfGQ", extractor.getUrl()),
() -> assertEquals("", extractor.getName()),
() -> assertEquals(39, extractor.getDuration()),
() -> assertEquals("hyper", extractor.getUploaderName()),
() -> assertEquals("https://www.youtube.com/channel/UCSezUnbvCLYBXuUlPcXU_QQ", extractor.getUploaderUrl()),
() -> assertFalse(extractor.getUploaderAvatars().isEmpty()),
() -> assertTrue(extractor.isUploaderVerified()),
() -> assertEquals("8 years ago", extractor.getTextualUploadDate()),
() -> assertNotNull(extractor.getUploadDate()),
() -> assertTrue(extractor.getViewCount() >= 1318193),
() -> assertFalse(extractor.getThumbnails().isEmpty()),
() -> assertNull(extractor.getShortDescription()),
() -> assertFalse(extractor.isShortFormContent())
);
}
}
Loading