NewPipeExtractor/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeCommentsExtractorTes...

67 lines
2.5 KiB
Java
Raw Normal View History

2018-08-20 00:52:19 +02:00
package org.schabi.newpipe.extractor.services.youtube;
import static org.junit.Assert.assertTrue;
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
import java.io.IOException;
2018-09-02 22:54:03 +02:00
import java.util.List;
2018-08-20 00:52:19 +02:00
import org.junit.BeforeClass;
import org.junit.Test;
import org.schabi.newpipe.Downloader;
import org.schabi.newpipe.extractor.ListExtractor.InfoItemsPage;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.comments.CommentsInfoItem;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeCommentsExtractor;
2018-09-02 22:54:03 +02:00
import org.schabi.newpipe.extractor.stream.StreamInfo;
2018-08-20 00:52:19 +02:00
public class YoutubeCommentsExtractorTest {
private static YoutubeCommentsExtractor extractor;
@BeforeClass
public static void setUp() throws Exception {
NewPipe.init(Downloader.getInstance());
extractor = (YoutubeCommentsExtractor) YouTube
.getCommentsExtractor("https://www.youtube.com/watch?v=rrgFN3AxGfs");
}
@Test
public void testGetComments() throws IOException, ExtractionException {
boolean result = false;
InfoItemsPage<CommentsInfoItem> comments = extractor.getInitialPage();
result = findInComments(comments, "i should really be in the top comment.lol");
2018-09-02 22:54:03 +02:00
while (comments.hasNextPage() && !result) {
comments = extractor.getPage(comments.getNextPageUrl());
result = findInComments(comments, "i should really be in the top comment.lol");
}
assertTrue(result);
}
2018-09-02 22:54:03 +02:00
@Test
public void testGetCommentsFromStreamInfo() throws IOException, ExtractionException {
boolean result = false;
StreamInfo streamInfo = StreamInfo.getInfo("https://www.youtube.com/watch?v=rrgFN3AxGfs");
result = findInComments(streamInfo.getComments(), "i should really be in the top comment.lol");
while (streamInfo.hasMoreComments() && !result) {
StreamInfo.loadMoreComments(streamInfo);
result = findInComments(streamInfo.getComments(), "i should really be in the top comment.lol");
}
assertTrue(result);
}
private boolean findInComments(InfoItemsPage<CommentsInfoItem> comments, String comment) {
2018-09-02 22:54:03 +02:00
return findInComments(comments.getItems(), comment);
}
private boolean findInComments(List<CommentsInfoItem> comments, String comment) {
return comments.stream().filter(c -> c.getCommentText().contains(comment)).findAny().isPresent();
}
2018-08-20 00:52:19 +02:00
}