feat(youtube/comments): support creator replies

This commit is contained in:
FineFindus 2023-09-25 10:40:45 +02:00
parent 917554acc4
commit dd7b2d9798
No known key found for this signature in database
GPG Key ID: 64873EE210FF8E6B
5 changed files with 80 additions and 0 deletions

View File

@ -30,6 +30,7 @@ public class CommentsInfoItem extends InfoItem {
private int replyCount; private int replyCount;
@Nullable @Nullable
private Page replies; private Page replies;
private boolean creatorReply;
public static final int NO_LIKE_COUNT = -1; public static final int NO_LIKE_COUNT = -1;
public static final int NO_STREAM_POSITION = -1; public static final int NO_STREAM_POSITION = -1;
@ -172,4 +173,13 @@ public class CommentsInfoItem extends InfoItem {
public Page getReplies() { public Page getReplies() {
return this.replies; return this.replies;
} }
public void setCreatorReply(final boolean creatorReply) {
this.creatorReply = creatorReply;
}
public boolean hasCreatorReply() {
return creatorReply;
}
} }

View File

@ -134,4 +134,12 @@ public interface CommentsInfoItemExtractor extends InfoItemExtractor {
default Page getReplies() throws ParsingException { default Page getReplies() throws ParsingException {
return null; return null;
} }
/**
* Whether the comment was replied to by the creator.
*/
@Nullable
default boolean hasCreatorReply() throws ParsingException {
return false;
}
} }

View File

@ -101,6 +101,13 @@ public final class CommentsInfoItemsCollector
addError(e); addError(e);
} }
try {
resultItem.setCreatorReply(extractor.hasCreatorReply());
} catch (final Exception e) {
addError(e);
}
return resultItem; return resultItem;
} }

View File

@ -277,4 +277,16 @@ public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtract
return null; return null;
} }
} }
@Override
public boolean hasCreatorReply() throws ParsingException {
try {
final JsonObject commentRepliesRenderer = JsonUtils.getObject(json,
"replies.commentRepliesRenderer");
return commentRepliesRenderer.has("viewRepliesCreatorThumbnail");
} catch (final Exception e) {
return false;
}
}
} }

View File

@ -352,6 +352,49 @@ public class YoutubeCommentsExtractorTest {
} }
} }
public static class CreatorReply {
private final static String url = "https://www.youtube.com/watch?v=bem4adjGKjE";
private static YoutubeCommentsExtractor extractor;
@BeforeAll
public static void setUp() throws Exception {
YoutubeTestsUtils.ensureStateless();
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "creatorReply"));
extractor = (YoutubeCommentsExtractor) YouTube
.getCommentsExtractor(url);
extractor.fetchPage();
}
@Test
void testGetCommentsAllData() throws IOException, ExtractionException {
final InfoItemsPage<CommentsInfoItem> comments = extractor.getInitialPage();
DefaultTests.defaultTestListOfItems(YouTube, comments.getItems(), comments.getErrors());
boolean creatorReply = false;
for (final CommentsInfoItem c : comments.getItems()) {
assertFalse(Utils.isBlank(c.getUploaderUrl()));
assertFalse(Utils.isBlank(c.getUploaderName()));
YoutubeTestsUtils.testImages(c.getUploaderAvatars());
assertFalse(Utils.isBlank(c.getCommentId()));
assertFalse(Utils.isBlank(c.getName()));
assertFalse(Utils.isBlank(c.getTextualUploadDate()));
assertNotNull(c.getUploadDate());
YoutubeTestsUtils.testImages(c.getThumbnails());
assertFalse(Utils.isBlank(c.getUrl()));
assertTrue(c.getLikeCount() >= 0);
assertFalse(Utils.isBlank(c.getCommentText().getContent()));
if (c.hasCreatorReply()) {
creatorReply = true;
}
}
assertTrue(creatorReply, "No comments was replied to by creator");
}
}
public static class FormattingTest { public static class FormattingTest {
private final static String url = "https://www.youtube.com/watch?v=zYpyS2HaZHM"; private final static String url = "https://www.youtube.com/watch?v=zYpyS2HaZHM";