Overview
The CommentsExtractor class extracts comments from videos and streams on streaming services. It extends ListExtractor<CommentsInfoItem> and provides methods to retrieve paginated comments and metadata about comment availability.
Package: org.schabi.newpipe.extractor.comments
Extends: ListExtractor<CommentsInfoItem>
Constructor
The streaming service this extractor belongs to
The list link handler containing URL information for the commented content
getId()
public String getId() throws ParsingException
Returns the unique identifier for the content being commented on.
getName()
public String getName() throws ParsingException
Returns the name for this comments section.
Always returns “Comments”
getUrl()
public String getUrl() throws ParsingException
Returns the URL of the content being commented on.
getOriginalUrl()
public String getOriginalUrl() throws ParsingException
Returns the original URL as provided.
getBaseUrl()
public String getBaseUrl() throws ParsingException
Returns the base URL of the service.
fetchPage()
public void fetchPage() throws IOException, ExtractionException
Fetches the comments page. Must be called before accessing other data methods.
getService()
public StreamingService getService()
Returns the streaming service this extractor belongs to.
The associated streaming service
getServiceId()
public int getServiceId()
Returns the numeric ID of the streaming service.
getInitialPage()
public abstract InfoItemsPage<CommentsInfoItem> getInitialPage() throws IOException, ExtractionException
Returns the first page of comments.
return
InfoItemsPage<CommentsInfoItem>
Page object containing initial comments and next page reference
getPage()
public abstract InfoItemsPage<CommentsInfoItem> getPage(Page page) throws IOException, ExtractionException
Returns a specific page of comments.
The page reference obtained from a previous page’s getNextPage() method
return
InfoItemsPage<CommentsInfoItem>
Page object containing comments and next page reference
public boolean isCommentsDisabled() throws ExtractionException
Returns whether comments are disabled for this content.
Experimental Method: This method is experimental and may be removed or changed in future releases.
true if comments are disabled, false otherwise (default)
public int getCommentsCount() throws ExtractionException
Returns the total number of comments.
Total comment count, or -1 if unavailable
Localization Methods
forceLocalization()
public void forceLocalization(Localization localization)
Forces a specific localization for this extractor.
forceContentCountry()
public void forceContentCountry(ContentCountry contentCountry)
Forces a specific content country for this extractor.
The content country to use
Usage Example
StreamingService service = NewPipe.getService("YouTube");
CommentsExtractor extractor = service.getCommentsExtractor(
"https://youtube.com/watch?v=dQw4w9WgXcQ"
);
// Fetch the page first
extractor.fetchPage();
// Check if comments are available
if (extractor.isCommentsDisabled()) {
System.out.println("Comments are disabled");
return;
}
// Get comment count
int totalComments = extractor.getCommentsCount();
System.out.println("Total comments: " + totalComments);
// Get first page of comments
InfoItemsPage<CommentsInfoItem> initialPage = extractor.getInitialPage();
List<CommentsInfoItem> comments = initialPage.getItems();
for (CommentsInfoItem comment : comments) {
System.out.println("Author: " + comment.getUploaderName());
System.out.println("Text: " + comment.getCommentText());
System.out.println("Likes: " + comment.getLikeCount());
System.out.println("---");
}
// Paginate through comments
Page nextPage = initialPage.getNextPage();
while (nextPage != null) {
InfoItemsPage<CommentsInfoItem> page = extractor.getPage(nextPage);
// Process comments on this page...
nextPage = page.getNextPage();
}
// Get a comment that has replies
CommentsInfoItem comment = comments.get(0);
if (comment.getReplies() != null) {
// Get the replies page
Page repliesPage = comment.getReplies();
InfoItemsPage<CommentsInfoItem> replies = extractor.getPage(repliesPage);
for (CommentsInfoItem reply : replies.getItems()) {
System.out.println("Reply from: " + reply.getUploaderName());
System.out.println("Reply text: " + reply.getCommentText());
}
}
Each CommentsInfoItem in the results contains:
Unique identifier for the comment
Name of the comment author
URL to the comment author’s channel
Author’s profile pictures
Number of likes on the comment
Original date string as provided by the service
Page reference to load comment replies, or null if no replies
Number of replies to this comment
Whether the comment is pinned
Whether the comment author is verified
Whether the comment was hearted by the content creator
InfoItemsPage Object
The InfoItemsPage returned by getInitialPage() and getPage() contains:
The list of comments on this page
Reference to the next page, or null if this is the last page
Any errors that occurred during extraction of this page
Notes
- Always call
fetchPage() before accessing comments
- Use
isCommentsDisabled() to check if comments are available before trying to extract them
- The
getCommentsCount() method may return -1 if the service doesn’t provide the total count
- Comments can be nested (replies to comments), accessed via the
getReplies() method on individual comments
- The
isCommentsDisabled() method is marked as experimental and may change in future versions
- Some services may not support all comment features (likes, replies, pinning, etc.)