diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfo.java b/extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfo.java index 62a376f4e..d247c34b3 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfo.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfo.java @@ -10,6 +10,7 @@ import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler; +import org.schabi.newpipe.extractor.stream.StreamInfoItem; import org.schabi.newpipe.extractor.utils.ExtractorHelper; public class CommentsInfo extends ListInfo{ @@ -23,78 +24,45 @@ public class CommentsInfo extends ListInfo{ return getInfo(NewPipe.getServiceByUrl(url), url); } - private static CommentsInfo getInfo(StreamingService serviceByUrl, String url) throws ExtractionException, IOException { + public static CommentsInfo getInfo(StreamingService serviceByUrl, String url) throws ExtractionException, IOException { return getInfo(serviceByUrl.getCommentsExtractor(url)); } private static CommentsInfo getInfo(CommentsExtractor commentsExtractor) throws IOException, ExtractionException { - //for services which do not have a comments extractor - if(null == commentsExtractor) { + // for services which do not have a comments extractor + if (null == commentsExtractor) { return null; } - + commentsExtractor.fetchPage(); String name = commentsExtractor.getName(); int serviceId = commentsExtractor.getServiceId(); ListLinkHandler listUrlIdHandler = commentsExtractor.getUIHandler(); CommentsInfo commentsInfo = new CommentsInfo(serviceId, listUrlIdHandler, name); commentsInfo.setCommentsExtractor(commentsExtractor); - InfoItemsPage initialCommentsPage = ExtractorHelper.getItemsPageOrLogError(commentsInfo, - commentsExtractor); - commentsInfo.setComments(new ArrayList<>()); - commentsInfo.getComments().addAll(initialCommentsPage.getItems()); - //tmp + InfoItemsPage initialCommentsPage = ExtractorHelper.getItemsPageOrLogError(commentsInfo, + commentsExtractor); commentsInfo.setRelatedItems(initialCommentsPage.getItems()); commentsInfo.setNextPageUrl(initialCommentsPage.getNextPageUrl()); - - commentsInfo.setHasMoreComments(initialCommentsPage.hasNextPage()); - commentsInfo.setNextCommentsPageUrl(initialCommentsPage.getNextPageUrl()); + return commentsInfo; } - public static void loadMoreComments(CommentsInfo commentsInfo) { - if (commentsInfo.hasMoreComments()) { - if(null == commentsInfo.getCommentsExtractor()) { - try { - commentsInfo.setCommentsExtractor(NewPipe.getService(commentsInfo.getServiceId()).getCommentsExtractor(commentsInfo.getUrl())); - commentsInfo.getCommentsExtractor().fetchPage(); - } catch (ExtractionException | IOException e) { - commentsInfo.addError(e); - return; - } - } - try { - InfoItemsPage commentsPage = commentsInfo.getCommentsExtractor() - .getPage(commentsInfo.getNextCommentsPageUrl()); - commentsInfo.getComments().addAll(commentsPage.getItems()); - commentsInfo.setHasMoreComments(commentsPage.hasNextPage()); - commentsInfo.setNextCommentsPageUrl(commentsPage.getNextPageUrl()); - } catch (IOException | ExtractionException e) { - commentsInfo.addError(e); - } + public static InfoItemsPage getMoreItems(CommentsInfo commentsInfo, String pageUrl) + throws ExtractionException, IOException { + return getMoreItems(NewPipe.getService(commentsInfo.getServiceId()), commentsInfo, pageUrl); + } + + public static InfoItemsPage getMoreItems(StreamingService service, CommentsInfo commentsInfo, + String pageUrl) throws IOException, ExtractionException { + if (null == commentsInfo.getCommentsExtractor()) { + commentsInfo.setCommentsExtractor(service.getCommentsExtractor(commentsInfo.getUrl())); + commentsInfo.getCommentsExtractor().fetchPage(); } + return commentsInfo.getCommentsExtractor().getPage(pageUrl); } private transient CommentsExtractor commentsExtractor; - private List comments; - private boolean hasMoreComments; - private String nextCommentsPageUrl; - - public List getComments() { - return comments; - } - - public void setComments(List comments) { - this.comments = comments; - } - - public boolean hasMoreComments() { - return hasMoreComments; - } - - public void setHasMoreComments(boolean hasMoreComments) { - this.hasMoreComments = hasMoreComments; - } public CommentsExtractor getCommentsExtractor() { return commentsExtractor; @@ -104,12 +72,4 @@ public class CommentsInfo extends ListInfo{ this.commentsExtractor = commentsExtractor; } - public String getNextCommentsPageUrl() { - return nextCommentsPageUrl; - } - - public void setNextCommentsPageUrl(String nextCommentsPageUrl) { - this.nextCommentsPageUrl = nextCommentsPageUrl; - } - } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfo.java b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfo.java index 0d452f6fe..a71482a50 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfo.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfo.java @@ -265,12 +265,6 @@ public class StreamInfo extends Info { streamInfo.setRelatedStreams(ExtractorHelper.getRelatedVideosOrLogError(streamInfo, extractor)); - try { - streamInfo.setCommentsInfo(CommentsInfo.getInfo(streamInfo.getUrl())); - } catch (Exception e) { - streamInfo.addError(e); - } - return streamInfo; } @@ -298,8 +292,6 @@ public class StreamInfo extends Info { private StreamInfoItem nextVideo; private List relatedStreams; - private CommentsInfo commentsInfo; - private long startPosition = 0; private List subtitles; @@ -496,12 +488,4 @@ public class StreamInfo extends Info { this.subtitles = subtitles; } - public CommentsInfo getCommentsInfo() { - return commentsInfo; - } - - public void setCommentsInfo(CommentsInfo commentsInfo) { - this.commentsInfo = commentsInfo; - } - } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeCommentsExtractorTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeCommentsExtractorTest.java index ee13a15cc..f811e1124 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeCommentsExtractorTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeCommentsExtractorTest.java @@ -6,6 +6,7 @@ import static org.schabi.newpipe.extractor.ServiceList.YouTube; import java.io.IOException; import java.util.List; +import org.jsoup.helper.StringUtil; import org.junit.BeforeClass; import org.junit.Test; import org.schabi.newpipe.Downloader; @@ -15,7 +16,6 @@ import org.schabi.newpipe.extractor.comments.CommentsInfo; import org.schabi.newpipe.extractor.comments.CommentsInfoItem; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeCommentsExtractor; -import org.schabi.newpipe.extractor.stream.StreamInfo; public class YoutubeCommentsExtractorTest { @@ -41,32 +41,19 @@ public class YoutubeCommentsExtractorTest { assertTrue(result); } - - @Test - public void testGetCommentsFromStreamInfo() throws IOException, ExtractionException { - boolean result = false; - StreamInfo streamInfo = StreamInfo.getInfo("https://www.youtube.com/watch?v=rrgFN3AxGfs"); - - result = findInComments(streamInfo.getCommentsInfo().getComments(), "i should really be in the top comment.lol"); - - while (streamInfo.getCommentsInfo().hasMoreComments() && !result) { - CommentsInfo.loadMoreComments(streamInfo.getCommentsInfo()); - result = findInComments(streamInfo.getCommentsInfo().getComments(), "i should really be in the top comment.lol"); - } - - assertTrue(result); - } @Test public void testGetCommentsFromCommentsInfo() throws IOException, ExtractionException { boolean result = false; CommentsInfo commentsInfo = CommentsInfo.getInfo("https://www.youtube.com/watch?v=rrgFN3AxGfs"); assertTrue("what the fuck am i doing with my life.wmv".equals(commentsInfo.getName())); - result = findInComments(commentsInfo.getComments(), "i should really be in the top comment.lol"); + result = findInComments(commentsInfo.getRelatedItems(), "i should really be in the top comment.lol"); - while (commentsInfo.hasMoreComments() && !result) { - CommentsInfo.loadMoreComments(commentsInfo); - result = findInComments(commentsInfo.getComments(), "i should really be in the top comment.lol"); + String nextPage = commentsInfo.getNextPageUrl(); + while (!StringUtil.isBlank(nextPage) && !result) { + InfoItemsPage moreItems = CommentsInfo.getMoreItems(YouTube, commentsInfo, nextPage); + result = findInComments(moreItems.getItems(), "i should really be in the top comment.lol"); + nextPage = moreItems.getNextPageUrl(); } assertTrue(result);