Skip to content

Commit dd7b2d9

Browse files
committed
feat(youtube/comments): support creator replies
1 parent 917554a commit dd7b2d9

5 files changed

Lines changed: 80 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
@@ -30,6 +30,7 @@ public class CommentsInfoItem extends InfoItem {
3030
private int replyCount;
3131
@Nullable
3232
private Page replies;
33+
private boolean creatorReply;
3334

3435
public static final int NO_LIKE_COUNT = -1;
3536
public static final int NO_STREAM_POSITION = -1;
@@ -172,4 +173,13 @@ public void setReplies(@Nullable final Page replies) {
172173
public Page getReplies() {
173174
return this.replies;
174175
}
176+
177+
public void setCreatorReply(final boolean creatorReply) {
178+
this.creatorReply = creatorReply;
179+
}
180+
181+
public boolean hasCreatorReply() {
182+
return creatorReply;
183+
}
184+
175185
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,12 @@ default int getReplyCount() throws ParsingException {
134134
default Page getReplies() throws ParsingException {
135135
return null;
136136
}
137+
138+
/**
139+
* Whether the comment was replied to by the creator.
140+
*/
141+
@Nullable
142+
default boolean hasCreatorReply() throws ParsingException {
143+
return false;
144+
}
137145
}

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
@@ -101,6 +101,13 @@ public CommentsInfoItem extract(final CommentsInfoItemExtractor extractor)
101101
addError(e);
102102
}
103103

104+
try {
105+
resultItem.setCreatorReply(extractor.hasCreatorReply());
106+
} catch (final Exception e) {
107+
addError(e);
108+
}
109+
110+
104111
return resultItem;
105112
}
106113

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
@@ -277,4 +277,16 @@ public Page getReplies() {
277277
return null;
278278
}
279279
}
280+
281+
@Override
282+
public boolean hasCreatorReply() throws ParsingException {
283+
try {
284+
final JsonObject commentRepliesRenderer = JsonUtils.getObject(json,
285+
"replies.commentRepliesRenderer");
286+
return commentRepliesRenderer.has("viewRepliesCreatorThumbnail");
287+
} catch (final Exception e) {
288+
return false;
289+
}
290+
}
291+
280292
}

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
@@ -352,6 +352,49 @@ public void testCommentsCount() throws IOException, ExtractionException {
352352
}
353353
}
354354

355+
public static class CreatorReply {
356+
private final static String url = "https://www.youtube.com/watch?v=bem4adjGKjE";
357+
private static YoutubeCommentsExtractor extractor;
358+
359+
@BeforeAll
360+
public static void setUp() throws Exception {
361+
YoutubeTestsUtils.ensureStateless();
362+
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "creatorReply"));
363+
extractor = (YoutubeCommentsExtractor) YouTube
364+
.getCommentsExtractor(url);
365+
extractor.fetchPage();
366+
}
367+
368+
@Test
369+
void testGetCommentsAllData() throws IOException, ExtractionException {
370+
final InfoItemsPage<CommentsInfoItem> comments = extractor.getInitialPage();
371+
372+
DefaultTests.defaultTestListOfItems(YouTube, comments.getItems(), comments.getErrors());
373+
374+
boolean creatorReply = false;
375+
376+
for (final CommentsInfoItem c : comments.getItems()) {
377+
assertFalse(Utils.isBlank(c.getUploaderUrl()));
378+
assertFalse(Utils.isBlank(c.getUploaderName()));
379+
YoutubeTestsUtils.testImages(c.getUploaderAvatars());
380+
assertFalse(Utils.isBlank(c.getCommentId()));
381+
assertFalse(Utils.isBlank(c.getName()));
382+
assertFalse(Utils.isBlank(c.getTextualUploadDate()));
383+
assertNotNull(c.getUploadDate());
384+
YoutubeTestsUtils.testImages(c.getThumbnails());
385+
assertFalse(Utils.isBlank(c.getUrl()));
386+
assertTrue(c.getLikeCount() >= 0);
387+
assertFalse(Utils.isBlank(c.getCommentText().getContent()));
388+
if (c.hasCreatorReply()) {
389+
creatorReply = true;
390+
}
391+
}
392+
assertTrue(creatorReply, "No comments was replied to by creator");
393+
394+
}
395+
}
396+
397+
355398
public static class FormattingTest {
356399

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

0 commit comments

Comments
 (0)