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
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ public class CommentsInfoItem extends InfoItem {
private boolean heartedByUploader;
private boolean pinned;
private int streamPosition;
private int replyCount;
@Nullable
private Page replies;

public static final int NO_LIKE_COUNT = -1;
public static final int NO_STREAM_POSITION = -1;

public static final int UNKNOWN_REPLY_COUNT = -1;

public CommentsInfoItem(final int serviceId, final String url, final String name) {
super(InfoType.COMMENT, serviceId, url, name);
}
Expand Down Expand Up @@ -91,7 +94,7 @@ public void setUploadDate(@Nullable final DateWrapper uploadDate) {

/**
* @return the comment's like count
* or {@link CommentsInfoItem#NO_LIKE_COUNT} if it is unavailable
* or {@link CommentsInfoItem#NO_LIKE_COUNT} if it is unavailable
*/
public int getLikeCount() {
return likeCount;
Expand Down Expand Up @@ -140,12 +143,21 @@ public void setStreamPosition(final int streamPosition) {
/**
* Get the playback position of the stream to which this comment belongs.
* This is not supported by all services.
*
* @return the playback position in seconds or {@link #NO_STREAM_POSITION} if not available
*/
public int getStreamPosition() {
return streamPosition;
}

public void setReplyCount(final int replyCount) {
this.replyCount = replyCount;
}

public int getReplyCount() {
return replyCount;
}

public void setReplies(@Nullable final Page replies) {
this.replies = replies;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public interface CommentsInfoItemExtractor extends InfoItemExtractor {
* or {@link CommentsInfoItem#NO_LIKE_COUNT} if it is unavailable.
*
* <br>
*
* <p>
* NOTE: Currently only implemented for YT {@link
* YoutubeCommentsInfoItemExtractor#getLikeCount()}
* with limitations (only approximate like count is returned)
*
* @see StreamExtractor#getLikeCount()
* @return the comment's like count
* or {@link CommentsInfoItem#NO_LIKE_COUNT} if it is unavailable
* @see StreamExtractor#getLikeCount()
*/
default int getLikeCount() throws ParsingException {
return CommentsInfoItem.NO_LIKE_COUNT;
Expand Down Expand Up @@ -103,14 +103,26 @@ default boolean isUploaderVerified() throws ParsingException {

/**
* The playback position of the stream to which this comment belongs.
*
* @see CommentsInfoItem#getStreamPosition()
*/
default int getStreamPosition() throws ParsingException {
return CommentsInfoItem.NO_STREAM_POSITION;
}

/**
* The count of comment replies.
*
* @return the count of the replies
* or {@link CommentsInfoItem#UNKNOWN_REPLY_COUNT} if replies are not supported
*/
default int getReplyCount() throws ParsingException {
return CommentsInfoItem.UNKNOWN_REPLY_COUNT;
}

/**
* The continuation page which is used to get comment replies from.
*
* @return the continuation Page for the replies, or null if replies are not supported
*/
@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ public CommentsInfoItem extract(final CommentsInfoItemExtractor extractor)
addError(e);
}

try {
resultItem.setReplyCount(extractor.getReplyCount());
} catch (final Exception e) {
addError(e);
}

try {
resultItem.setReplies(extractor.getReplies());
} catch (final Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.schabi.newpipe.extractor.services.youtube.extractors;

import static org.schabi.newpipe.extractor.comments.CommentsInfoItem.UNKNOWN_REPLY_COUNT;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject;

import com.grack.nanojson.JsonArray;
Expand Down Expand Up @@ -248,6 +249,15 @@ public String getUploaderUrl() throws ParsingException {
}
}

@Override
public int getReplyCount() throws ParsingException {
final JsonObject commentRenderer = getCommentRenderer();
if (commentRenderer.has("replyCount")) {
return commentRenderer.getInt("replyCount");
}
return UNKNOWN_REPLY_COUNT;
}

@Override
public Page getReplies() throws ParsingException {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertGreater;
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
import static org.schabi.newpipe.extractor.comments.CommentsInfoItem.UNKNOWN_REPLY_COUNT;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -328,5 +331,17 @@ public void testGetCommentsFirstReplies() throws IOException, ExtractionExceptio
assertEquals("First", replies.getItems().get(0).getCommentText(),
"First reply comment did not match");
}

@Test
public void testGetCommentsReplyCount() throws IOException, ExtractionException {
final InfoItemsPage<CommentsInfoItem> comments = extractor.getInitialPage();

DefaultTests.defaultTestListOfItems(YouTube, comments.getItems(), comments.getErrors());

final CommentsInfoItem firstComment = comments.getItems().get(0);

assertNotEquals(UNKNOWN_REPLY_COUNT, firstComment.getReplyCount(), "Could not get the reply count of the first comment");
assertGreater(300, firstComment.getReplyCount());
}
}
}