removed commentsInfo from streamInfo

This commit is contained in:
Ritvik Saraf 2018-09-22 23:36:41 +05:30
parent 7ed0da0493
commit 0e86475891
3 changed files with 26 additions and 95 deletions

View File

@ -10,6 +10,7 @@ import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler; import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import org.schabi.newpipe.extractor.utils.ExtractorHelper; import org.schabi.newpipe.extractor.utils.ExtractorHelper;
public class CommentsInfo extends ListInfo<CommentsInfoItem>{ public class CommentsInfo extends ListInfo<CommentsInfoItem>{
@ -23,13 +24,13 @@ public class CommentsInfo extends ListInfo<CommentsInfoItem>{
return getInfo(NewPipe.getServiceByUrl(url), url); 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)); return getInfo(serviceByUrl.getCommentsExtractor(url));
} }
private static CommentsInfo getInfo(CommentsExtractor commentsExtractor) throws IOException, ExtractionException { private static CommentsInfo getInfo(CommentsExtractor commentsExtractor) throws IOException, ExtractionException {
//for services which do not have a comments extractor // for services which do not have a comments extractor
if(null == commentsExtractor) { if (null == commentsExtractor) {
return null; return null;
} }
@ -41,60 +42,27 @@ public class CommentsInfo extends ListInfo<CommentsInfoItem>{
commentsInfo.setCommentsExtractor(commentsExtractor); commentsInfo.setCommentsExtractor(commentsExtractor);
InfoItemsPage<CommentsInfoItem> initialCommentsPage = ExtractorHelper.getItemsPageOrLogError(commentsInfo, InfoItemsPage<CommentsInfoItem> initialCommentsPage = ExtractorHelper.getItemsPageOrLogError(commentsInfo,
commentsExtractor); commentsExtractor);
commentsInfo.setComments(new ArrayList<>());
commentsInfo.getComments().addAll(initialCommentsPage.getItems());
//tmp
commentsInfo.setRelatedItems(initialCommentsPage.getItems()); commentsInfo.setRelatedItems(initialCommentsPage.getItems());
commentsInfo.setNextPageUrl(initialCommentsPage.getNextPageUrl()); commentsInfo.setNextPageUrl(initialCommentsPage.getNextPageUrl());
commentsInfo.setHasMoreComments(initialCommentsPage.hasNextPage());
commentsInfo.setNextCommentsPageUrl(initialCommentsPage.getNextPageUrl());
return commentsInfo; return commentsInfo;
} }
public static void loadMoreComments(CommentsInfo commentsInfo) { public static InfoItemsPage<CommentsInfoItem> getMoreItems(CommentsInfo commentsInfo, String pageUrl)
if (commentsInfo.hasMoreComments()) { throws ExtractionException, IOException {
if(null == commentsInfo.getCommentsExtractor()) { return getMoreItems(NewPipe.getService(commentsInfo.getServiceId()), commentsInfo, pageUrl);
try { }
commentsInfo.setCommentsExtractor(NewPipe.getService(commentsInfo.getServiceId()).getCommentsExtractor(commentsInfo.getUrl()));
public static InfoItemsPage<CommentsInfoItem> getMoreItems(StreamingService service, CommentsInfo commentsInfo,
String pageUrl) throws IOException, ExtractionException {
if (null == commentsInfo.getCommentsExtractor()) {
commentsInfo.setCommentsExtractor(service.getCommentsExtractor(commentsInfo.getUrl()));
commentsInfo.getCommentsExtractor().fetchPage(); commentsInfo.getCommentsExtractor().fetchPage();
} catch (ExtractionException | IOException e) {
commentsInfo.addError(e);
return;
}
}
try {
InfoItemsPage<CommentsInfoItem> 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);
}
} }
return commentsInfo.getCommentsExtractor().getPage(pageUrl);
} }
private transient CommentsExtractor commentsExtractor; private transient CommentsExtractor commentsExtractor;
private List<CommentsInfoItem> comments;
private boolean hasMoreComments;
private String nextCommentsPageUrl;
public List<CommentsInfoItem> getComments() {
return comments;
}
public void setComments(List<CommentsInfoItem> comments) {
this.comments = comments;
}
public boolean hasMoreComments() {
return hasMoreComments;
}
public void setHasMoreComments(boolean hasMoreComments) {
this.hasMoreComments = hasMoreComments;
}
public CommentsExtractor getCommentsExtractor() { public CommentsExtractor getCommentsExtractor() {
return commentsExtractor; return commentsExtractor;
@ -104,12 +72,4 @@ public class CommentsInfo extends ListInfo<CommentsInfoItem>{
this.commentsExtractor = commentsExtractor; this.commentsExtractor = commentsExtractor;
} }
public String getNextCommentsPageUrl() {
return nextCommentsPageUrl;
}
public void setNextCommentsPageUrl(String nextCommentsPageUrl) {
this.nextCommentsPageUrl = nextCommentsPageUrl;
}
} }

View File

@ -265,12 +265,6 @@ public class StreamInfo extends Info {
streamInfo.setRelatedStreams(ExtractorHelper.getRelatedVideosOrLogError(streamInfo, extractor)); streamInfo.setRelatedStreams(ExtractorHelper.getRelatedVideosOrLogError(streamInfo, extractor));
try {
streamInfo.setCommentsInfo(CommentsInfo.getInfo(streamInfo.getUrl()));
} catch (Exception e) {
streamInfo.addError(e);
}
return streamInfo; return streamInfo;
} }
@ -298,8 +292,6 @@ public class StreamInfo extends Info {
private StreamInfoItem nextVideo; private StreamInfoItem nextVideo;
private List<InfoItem> relatedStreams; private List<InfoItem> relatedStreams;
private CommentsInfo commentsInfo;
private long startPosition = 0; private long startPosition = 0;
private List<Subtitles> subtitles; private List<Subtitles> subtitles;
@ -496,12 +488,4 @@ public class StreamInfo extends Info {
this.subtitles = subtitles; this.subtitles = subtitles;
} }
public CommentsInfo getCommentsInfo() {
return commentsInfo;
}
public void setCommentsInfo(CommentsInfo commentsInfo) {
this.commentsInfo = commentsInfo;
}
} }

View File

@ -6,6 +6,7 @@ import static org.schabi.newpipe.extractor.ServiceList.YouTube;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import org.jsoup.helper.StringUtil;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.schabi.newpipe.Downloader; 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.comments.CommentsInfoItem;
import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeCommentsExtractor; import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeCommentsExtractor;
import org.schabi.newpipe.extractor.stream.StreamInfo;
public class YoutubeCommentsExtractorTest { public class YoutubeCommentsExtractorTest {
@ -42,31 +42,18 @@ public class YoutubeCommentsExtractorTest {
assertTrue(result); 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 @Test
public void testGetCommentsFromCommentsInfo() throws IOException, ExtractionException { public void testGetCommentsFromCommentsInfo() throws IOException, ExtractionException {
boolean result = false; boolean result = false;
CommentsInfo commentsInfo = CommentsInfo.getInfo("https://www.youtube.com/watch?v=rrgFN3AxGfs"); 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())); 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) { String nextPage = commentsInfo.getNextPageUrl();
CommentsInfo.loadMoreComments(commentsInfo); while (!StringUtil.isBlank(nextPage) && !result) {
result = findInComments(commentsInfo.getComments(), "i should really be in the top comment.lol"); InfoItemsPage<CommentsInfoItem> moreItems = CommentsInfo.getMoreItems(YouTube, commentsInfo, nextPage);
result = findInComments(moreItems.getItems(), "i should really be in the top comment.lol");
nextPage = moreItems.getNextPageUrl();
} }
assertTrue(result); assertTrue(result);