2018-08-20 00:52:19 +02:00
|
|
|
package org.schabi.newpipe.extractor.comments;
|
|
|
|
|
2018-09-19 00:52:23 +02:00
|
|
|
import org.schabi.newpipe.extractor.ListExtractor.InfoItemsPage;
|
2018-08-20 00:52:19 +02:00
|
|
|
import org.schabi.newpipe.extractor.ListInfo;
|
|
|
|
import org.schabi.newpipe.extractor.NewPipe;
|
2020-04-15 14:09:46 +02:00
|
|
|
import org.schabi.newpipe.extractor.Page;
|
2018-08-20 00:52:19 +02:00
|
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
|
|
|
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
|
2018-09-19 00:52:23 +02:00
|
|
|
import org.schabi.newpipe.extractor.utils.ExtractorHelper;
|
2018-08-20 00:52:19 +02:00
|
|
|
|
2020-02-08 23:58:46 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2022-03-17 14:47:08 +01:00
|
|
|
public final class CommentsInfo extends ListInfo<CommentsInfoItem> {
|
2020-02-08 23:58:46 +01:00
|
|
|
|
2021-06-13 21:11:11 +02:00
|
|
|
private CommentsInfo(
|
|
|
|
final int serviceId,
|
|
|
|
final ListLinkHandler listUrlIdHandler,
|
|
|
|
final String name) {
|
2020-02-08 23:58:46 +01:00
|
|
|
super(serviceId, listUrlIdHandler, name);
|
|
|
|
}
|
2018-08-20 00:52:19 +02:00
|
|
|
|
2021-06-13 21:11:11 +02:00
|
|
|
public static CommentsInfo getInfo(final String url) throws IOException, ExtractionException {
|
2018-08-20 00:52:19 +02:00
|
|
|
return getInfo(NewPipe.getServiceByUrl(url), url);
|
|
|
|
}
|
|
|
|
|
2021-11-08 19:46:19 +01:00
|
|
|
public static CommentsInfo getInfo(final StreamingService service, final String url)
|
2021-06-13 21:11:11 +02:00
|
|
|
throws ExtractionException, IOException {
|
2021-11-08 19:46:19 +01:00
|
|
|
return getInfo(service.getCommentsExtractor(url));
|
2020-02-08 23:58:46 +01:00
|
|
|
}
|
2018-08-20 00:52:19 +02:00
|
|
|
|
2021-06-13 21:11:11 +02:00
|
|
|
public static CommentsInfo getInfo(final CommentsExtractor commentsExtractor)
|
|
|
|
throws IOException, ExtractionException {
|
2018-09-22 20:06:41 +02:00
|
|
|
// for services which do not have a comments extractor
|
2021-06-13 21:11:11 +02:00
|
|
|
if (commentsExtractor == null) {
|
2018-09-19 00:52:23 +02:00
|
|
|
return null;
|
|
|
|
}
|
2018-09-22 20:06:41 +02:00
|
|
|
|
2018-09-19 00:52:23 +02:00
|
|
|
commentsExtractor.fetchPage();
|
2021-06-13 21:11:11 +02:00
|
|
|
|
|
|
|
final String name = commentsExtractor.getName();
|
|
|
|
final int serviceId = commentsExtractor.getServiceId();
|
|
|
|
final ListLinkHandler listUrlIdHandler = commentsExtractor.getLinkHandler();
|
|
|
|
|
|
|
|
final CommentsInfo commentsInfo = new CommentsInfo(serviceId, listUrlIdHandler, name);
|
2018-09-19 00:52:23 +02:00
|
|
|
commentsInfo.setCommentsExtractor(commentsExtractor);
|
2021-06-13 21:11:11 +02:00
|
|
|
final InfoItemsPage<CommentsInfoItem> initialCommentsPage =
|
|
|
|
ExtractorHelper.getItemsPageOrLogError(commentsInfo, commentsExtractor);
|
|
|
|
commentsInfo.setCommentsDisabled(commentsExtractor.isCommentsDisabled());
|
2018-09-22 11:25:59 +02:00
|
|
|
commentsInfo.setRelatedItems(initialCommentsPage.getItems());
|
2022-02-08 11:44:55 +01:00
|
|
|
try {
|
|
|
|
commentsInfo.setCommentsCount(commentsExtractor.getCommentsCount());
|
|
|
|
} catch (Exception e) {
|
|
|
|
commentsInfo.addError(e);
|
|
|
|
}
|
2020-04-15 14:09:46 +02:00
|
|
|
commentsInfo.setNextPage(initialCommentsPage.getNextPage());
|
2018-09-22 20:06:41 +02:00
|
|
|
|
2018-09-19 00:52:23 +02:00
|
|
|
return commentsInfo;
|
|
|
|
}
|
2020-02-08 23:58:46 +01:00
|
|
|
|
2021-06-13 21:11:11 +02:00
|
|
|
public static InfoItemsPage<CommentsInfoItem> getMoreItems(
|
|
|
|
final CommentsInfo commentsInfo,
|
|
|
|
final Page page) throws ExtractionException, IOException {
|
2022-03-17 14:47:08 +01:00
|
|
|
return getMoreItems(NewPipe.getService(commentsInfo.getServiceId()), commentsInfo.getUrl(),
|
|
|
|
page);
|
2018-09-22 20:06:41 +02:00
|
|
|
}
|
2020-02-08 23:58:46 +01:00
|
|
|
|
2021-06-13 21:11:11 +02:00
|
|
|
public static InfoItemsPage<CommentsInfoItem> getMoreItems(
|
|
|
|
final StreamingService service,
|
|
|
|
final CommentsInfo commentsInfo,
|
|
|
|
final Page page) throws IOException, ExtractionException {
|
2022-03-03 12:04:30 +01:00
|
|
|
return getMoreItems(service, commentsInfo.getUrl(), page);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static InfoItemsPage<CommentsInfoItem> getMoreItems(
|
|
|
|
final StreamingService service,
|
|
|
|
final String url,
|
|
|
|
final Page page) throws IOException, ExtractionException {
|
|
|
|
return service.getCommentsExtractor(url).getPage(page);
|
2018-09-19 00:52:23 +02:00
|
|
|
}
|
2020-02-08 23:58:46 +01:00
|
|
|
|
2018-09-19 00:52:23 +02:00
|
|
|
private transient CommentsExtractor commentsExtractor;
|
2021-06-13 21:11:11 +02:00
|
|
|
private boolean commentsDisabled = false;
|
2022-02-08 11:44:55 +01:00
|
|
|
private int commentsCount;
|
2018-09-19 00:52:23 +02:00
|
|
|
|
|
|
|
public CommentsExtractor getCommentsExtractor() {
|
|
|
|
return commentsExtractor;
|
|
|
|
}
|
|
|
|
|
2021-06-13 21:11:11 +02:00
|
|
|
public void setCommentsExtractor(final CommentsExtractor commentsExtractor) {
|
2018-09-19 00:52:23 +02:00
|
|
|
this.commentsExtractor = commentsExtractor;
|
|
|
|
}
|
|
|
|
|
2021-07-07 20:41:59 +02:00
|
|
|
/**
|
2022-02-08 11:44:55 +01:00
|
|
|
* @return <code>true</code> if the comments are disabled otherwise <code>false</code> (default)
|
2021-07-07 20:41:59 +02:00
|
|
|
* @apiNote Warning: This method is experimental and may get removed in a future release.
|
2022-03-17 14:47:08 +01:00
|
|
|
* @return {@code true} if the comments are disabled otherwise {@code false} (default)
|
2021-07-07 20:41:59 +02:00
|
|
|
* @see CommentsExtractor#isCommentsDisabled()
|
|
|
|
*/
|
2021-06-13 21:11:11 +02:00
|
|
|
public boolean isCommentsDisabled() {
|
|
|
|
return commentsDisabled;
|
|
|
|
}
|
|
|
|
|
2021-07-07 20:41:59 +02:00
|
|
|
/**
|
2022-02-08 11:44:55 +01:00
|
|
|
* @param commentsDisabled <code>true</code> if the comments are disabled otherwise <code>false</code>
|
2021-07-07 20:41:59 +02:00
|
|
|
* @apiNote Warning: This method is experimental and may get removed in a future release.
|
2022-03-17 14:47:08 +01:00
|
|
|
* @param commentsDisabled {@code true} if the comments are disabled otherwise {@code false}
|
2021-07-07 20:41:59 +02:00
|
|
|
*/
|
2021-06-13 21:11:11 +02:00
|
|
|
public void setCommentsDisabled(final boolean commentsDisabled) {
|
|
|
|
this.commentsDisabled = commentsDisabled;
|
|
|
|
}
|
2022-02-08 11:44:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the total number of comments.
|
|
|
|
*
|
|
|
|
* @return totalComments
|
|
|
|
*/
|
|
|
|
public int getCommentsCount() {
|
|
|
|
return commentsCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the total number of comments.
|
|
|
|
*
|
|
|
|
* @param commentsCount
|
|
|
|
*/
|
|
|
|
public void setCommentsCount(int commentsCount) {
|
|
|
|
this.commentsCount = commentsCount;
|
|
|
|
}
|
2018-08-20 00:52:19 +02:00
|
|
|
}
|