Skip to content

Commit 34b05a0

Browse files
committed
feat(youtube/comments): support creator replies
1 parent 0821f09 commit 34b05a0

5 files changed

Lines changed: 79 additions & 0 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfoItem.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class CommentsInfoItem extends InfoItem {
3131
@Nullable
3232
private Page replies;
3333
private boolean isChannelOwner;
34+
private boolean creatorReply;
3435

3536
public static final int NO_LIKE_COUNT = -1;
3637
public static final int NO_STREAM_POSITION = -1;
@@ -182,4 +183,13 @@ public boolean isChannelOwner() {
182183
return isChannelOwner;
183184
}
184185

186+
187+
public void setCreatorReply(final boolean creatorReply) {
188+
this.creatorReply = creatorReply;
189+
}
190+
191+
public boolean hasCreatorReply() {
192+
return creatorReply;
193+
}
194+
185195
}

extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfoItemExtractor.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,11 @@ default Page getReplies() throws ParsingException {
141141
default boolean isChannelOwner() throws ParsingException {
142142
return false;
143143
}
144+
145+
/**
146+
* Whether the comment was replied to by the creator.
147+
*/
148+
default boolean hasCreatorReply() throws ParsingException {
149+
return false;
150+
}
144151
}

extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfoItemsCollector.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ public CommentsInfoItem extract(final CommentsInfoItemExtractor extractor)
108108
}
109109

110110

111+
try {
112+
resultItem.setCreatorReply(extractor.hasCreatorReply());
113+
} catch (final Exception e) {
114+
addError(e);
115+
}
116+
117+
111118
return resultItem;
112119
}
113120

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,4 +283,16 @@ public boolean isChannelOwner() throws ParsingException {
283283
return getCommentRenderer().getBoolean("authorIsChannelOwner");
284284
}
285285

286+
287+
@Override
288+
public boolean hasCreatorReply() throws ParsingException {
289+
try {
290+
final JsonObject commentRepliesRenderer = JsonUtils.getObject(json,
291+
"replies.commentRepliesRenderer");
292+
return commentRepliesRenderer.has("viewRepliesCreatorThumbnail");
293+
} catch (final Exception e) {
294+
return false;
295+
}
296+
}
297+
286298
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeCommentsExtractorTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,49 @@ void testGetCommentsAllData() throws IOException, ExtractionException {
395395
}
396396

397397

398+
public static class CreatorReply {
399+
private final static String url = "https://www.youtube.com/watch?v=bem4adjGKjE";
400+
private static YoutubeCommentsExtractor extractor;
401+
402+
@BeforeAll
403+
public static void setUp() throws Exception {
404+
YoutubeTestsUtils.ensureStateless();
405+
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "creatorReply"));
406+
extractor = (YoutubeCommentsExtractor) YouTube
407+
.getCommentsExtractor(url);
408+
extractor.fetchPage();
409+
}
410+
411+
@Test
412+
void testGetCommentsAllData() throws IOException, ExtractionException {
413+
final InfoItemsPage<CommentsInfoItem> comments = extractor.getInitialPage();
414+
415+
DefaultTests.defaultTestListOfItems(YouTube, comments.getItems(), comments.getErrors());
416+
417+
boolean creatorReply = false;
418+
419+
for (final CommentsInfoItem c : comments.getItems()) {
420+
assertFalse(Utils.isBlank(c.getUploaderUrl()));
421+
assertFalse(Utils.isBlank(c.getUploaderName()));
422+
YoutubeTestsUtils.testImages(c.getUploaderAvatars());
423+
assertFalse(Utils.isBlank(c.getCommentId()));
424+
assertFalse(Utils.isBlank(c.getName()));
425+
assertFalse(Utils.isBlank(c.getTextualUploadDate()));
426+
assertNotNull(c.getUploadDate());
427+
YoutubeTestsUtils.testImages(c.getThumbnails());
428+
assertFalse(Utils.isBlank(c.getUrl()));
429+
assertTrue(c.getLikeCount() >= 0);
430+
assertFalse(Utils.isBlank(c.getCommentText().getContent()));
431+
if (c.hasCreatorReply()) {
432+
creatorReply = true;
433+
}
434+
}
435+
assertTrue(creatorReply, "No comments was replied to by creator");
436+
437+
}
438+
}
439+
440+
398441
public static class FormattingTest {
399442

400443
private final static String url = "https://www.youtube.com/watch?v=zYpyS2HaZHM";

0 commit comments

Comments
 (0)