Skip to main content

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

service
StreamingService
required
The streaming service this extractor belongs to
uiHandler
ListLinkHandler
required
The list link handler containing URL information for the commented content

Base Methods (Inherited from Extractor)

getId()

public String getId() throws ParsingException
Returns the unique identifier for the content being commented on.
return
String
The content’s unique ID

getName()

public String getName() throws ParsingException
Returns the name for this comments section.
return
String
Always returns “Comments”

getUrl()

public String getUrl() throws ParsingException
Returns the URL of the content being commented on.
return
String
The content URL

getOriginalUrl()

public String getOriginalUrl() throws ParsingException
Returns the original URL as provided.
return
String
The original URL

getBaseUrl()

public String getBaseUrl() throws ParsingException
Returns the base URL of the service.
return
String
The base URL

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.
return
StreamingService
The associated streaming service

getServiceId()

public int getServiceId()
Returns the numeric ID of the streaming service.
return
int
The service ID

List Extraction Methods (Inherited from ListExtractor)

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.
page
Page
required
The page reference obtained from a previous page’s getNextPage() method
return
InfoItemsPage<CommentsInfoItem>
Page object containing comments and next page reference

Comments-Specific Methods

isCommentsDisabled()

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.
return
boolean
true if comments are disabled, false otherwise (default)

getCommentsCount()

public int getCommentsCount() throws ExtractionException
Returns the total number of comments.
return
int
Total comment count, or -1 if unavailable

Localization Methods

forceLocalization()

public void forceLocalization(Localization localization)
Forces a specific localization for this extractor.
localization
Localization
required
The localization to use

forceContentCountry()

public void forceContentCountry(ContentCountry contentCountry)
Forces a specific content country for this extractor.
contentCountry
ContentCountry
required
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();
}

Loading Comment Replies

// 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());
    }
}

CommentsInfoItem Properties

Each CommentsInfoItem in the results contains:
commentId
String
Unique identifier for the comment
commentText
String
The comment text content
uploaderName
String
Name of the comment author
uploaderUrl
String
URL to the comment author’s channel
uploaderAvatars
List<Image>
Author’s profile pictures
likeCount
int
Number of likes on the comment
textualUploadDate
String
Original date string as provided by the service
uploadDate
DateWrapper
Parsed upload date
replies
Page
Page reference to load comment replies, or null if no replies
repliesCount
int
Number of replies to this comment
isPinned
boolean
Whether the comment is pinned
isUploaderVerified
boolean
Whether the comment author is verified
isHeartedByUploader
boolean
Whether the comment was hearted by the content creator

InfoItemsPage Object

The InfoItemsPage returned by getInitialPage() and getPage() contains:
items
List<CommentsInfoItem>
The list of comments on this page
nextPage
Page
Reference to the next page, or null if this is the last page
errors
List<Throwable>
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.)

Build docs developers (and LLMs) love