Skip to content

Commit 0e86475

Browse files
committed
removed commentsInfo from streamInfo
1 parent 7ed0da0 commit 0e86475

3 files changed

Lines changed: 26 additions & 95 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfo.java

Lines changed: 19 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.schabi.newpipe.extractor.StreamingService;
1111
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
1212
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
13+
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
1314
import org.schabi.newpipe.extractor.utils.ExtractorHelper;
1415

1516
public class CommentsInfo extends ListInfo<CommentsInfoItem>{
@@ -23,78 +24,45 @@ public static CommentsInfo getInfo(String url) throws IOException, ExtractionExc
2324
return getInfo(NewPipe.getServiceByUrl(url), url);
2425
}
2526

26-
private static CommentsInfo getInfo(StreamingService serviceByUrl, String url) throws ExtractionException, IOException {
27+
public static CommentsInfo getInfo(StreamingService serviceByUrl, String url) throws ExtractionException, IOException {
2728
return getInfo(serviceByUrl.getCommentsExtractor(url));
2829
}
2930

3031
private static CommentsInfo getInfo(CommentsExtractor commentsExtractor) throws IOException, ExtractionException {
31-
//for services which do not have a comments extractor
32-
if(null == commentsExtractor) {
32+
// for services which do not have a comments extractor
33+
if (null == commentsExtractor) {
3334
return null;
3435
}
35-
36+
3637
commentsExtractor.fetchPage();
3738
String name = commentsExtractor.getName();
3839
int serviceId = commentsExtractor.getServiceId();
3940
ListLinkHandler listUrlIdHandler = commentsExtractor.getUIHandler();
4041
CommentsInfo commentsInfo = new CommentsInfo(serviceId, listUrlIdHandler, name);
4142
commentsInfo.setCommentsExtractor(commentsExtractor);
42-
InfoItemsPage<CommentsInfoItem> initialCommentsPage = ExtractorHelper.getItemsPageOrLogError(commentsInfo,
43-
commentsExtractor);
44-
commentsInfo.setComments(new ArrayList<>());
45-
commentsInfo.getComments().addAll(initialCommentsPage.getItems());
46-
//tmp
43+
InfoItemsPage<CommentsInfoItem> initialCommentsPage = ExtractorHelper.getItemsPageOrLogError(commentsInfo,
44+
commentsExtractor);
4745
commentsInfo.setRelatedItems(initialCommentsPage.getItems());
4846
commentsInfo.setNextPageUrl(initialCommentsPage.getNextPageUrl());
49-
50-
commentsInfo.setHasMoreComments(initialCommentsPage.hasNextPage());
51-
commentsInfo.setNextCommentsPageUrl(initialCommentsPage.getNextPageUrl());
47+
5248
return commentsInfo;
5349
}
5450

55-
public static void loadMoreComments(CommentsInfo commentsInfo) {
56-
if (commentsInfo.hasMoreComments()) {
57-
if(null == commentsInfo.getCommentsExtractor()) {
58-
try {
59-
commentsInfo.setCommentsExtractor(NewPipe.getService(commentsInfo.getServiceId()).getCommentsExtractor(commentsInfo.getUrl()));
60-
commentsInfo.getCommentsExtractor().fetchPage();
61-
} catch (ExtractionException | IOException e) {
62-
commentsInfo.addError(e);
63-
return;
64-
}
65-
}
66-
try {
67-
InfoItemsPage<CommentsInfoItem> commentsPage = commentsInfo.getCommentsExtractor()
68-
.getPage(commentsInfo.getNextCommentsPageUrl());
69-
commentsInfo.getComments().addAll(commentsPage.getItems());
70-
commentsInfo.setHasMoreComments(commentsPage.hasNextPage());
71-
commentsInfo.setNextCommentsPageUrl(commentsPage.getNextPageUrl());
72-
} catch (IOException | ExtractionException e) {
73-
commentsInfo.addError(e);
74-
}
51+
public static InfoItemsPage<CommentsInfoItem> getMoreItems(CommentsInfo commentsInfo, String pageUrl)
52+
throws ExtractionException, IOException {
53+
return getMoreItems(NewPipe.getService(commentsInfo.getServiceId()), commentsInfo, pageUrl);
54+
}
55+
56+
public static InfoItemsPage<CommentsInfoItem> getMoreItems(StreamingService service, CommentsInfo commentsInfo,
57+
String pageUrl) throws IOException, ExtractionException {
58+
if (null == commentsInfo.getCommentsExtractor()) {
59+
commentsInfo.setCommentsExtractor(service.getCommentsExtractor(commentsInfo.getUrl()));
60+
commentsInfo.getCommentsExtractor().fetchPage();
7561
}
62+
return commentsInfo.getCommentsExtractor().getPage(pageUrl);
7663
}
7764

7865
private transient CommentsExtractor commentsExtractor;
79-
private List<CommentsInfoItem> comments;
80-
private boolean hasMoreComments;
81-
private String nextCommentsPageUrl;
82-
83-
public List<CommentsInfoItem> getComments() {
84-
return comments;
85-
}
86-
87-
public void setComments(List<CommentsInfoItem> comments) {
88-
this.comments = comments;
89-
}
90-
91-
public boolean hasMoreComments() {
92-
return hasMoreComments;
93-
}
94-
95-
public void setHasMoreComments(boolean hasMoreComments) {
96-
this.hasMoreComments = hasMoreComments;
97-
}
9866

9967
public CommentsExtractor getCommentsExtractor() {
10068
return commentsExtractor;
@@ -104,12 +72,4 @@ public void setCommentsExtractor(CommentsExtractor commentsExtractor) {
10472
this.commentsExtractor = commentsExtractor;
10573
}
10674

107-
public String getNextCommentsPageUrl() {
108-
return nextCommentsPageUrl;
109-
}
110-
111-
public void setNextCommentsPageUrl(String nextCommentsPageUrl) {
112-
this.nextCommentsPageUrl = nextCommentsPageUrl;
113-
}
114-
11575
}

extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfo.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,6 @@ private static StreamInfo extractOptionalData(StreamInfo streamInfo, StreamExtra
265265

266266
streamInfo.setRelatedStreams(ExtractorHelper.getRelatedVideosOrLogError(streamInfo, extractor));
267267

268-
try {
269-
streamInfo.setCommentsInfo(CommentsInfo.getInfo(streamInfo.getUrl()));
270-
} catch (Exception e) {
271-
streamInfo.addError(e);
272-
}
273-
274268
return streamInfo;
275269
}
276270

@@ -298,8 +292,6 @@ private static StreamInfo extractOptionalData(StreamInfo streamInfo, StreamExtra
298292
private StreamInfoItem nextVideo;
299293
private List<InfoItem> relatedStreams;
300294

301-
private CommentsInfo commentsInfo;
302-
303295
private long startPosition = 0;
304296
private List<Subtitles> subtitles;
305297

@@ -496,12 +488,4 @@ public void setSubtitles(List<Subtitles> subtitles) {
496488
this.subtitles = subtitles;
497489
}
498490

499-
public CommentsInfo getCommentsInfo() {
500-
return commentsInfo;
501-
}
502-
503-
public void setCommentsInfo(CommentsInfo commentsInfo) {
504-
this.commentsInfo = commentsInfo;
505-
}
506-
507491
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeCommentsExtractorTest.java

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.io.IOException;
77
import java.util.List;
88

9+
import org.jsoup.helper.StringUtil;
910
import org.junit.BeforeClass;
1011
import org.junit.Test;
1112
import org.schabi.newpipe.Downloader;
@@ -15,7 +16,6 @@
1516
import org.schabi.newpipe.extractor.comments.CommentsInfoItem;
1617
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
1718
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeCommentsExtractor;
18-
import org.schabi.newpipe.extractor.stream.StreamInfo;
1919

2020
public class YoutubeCommentsExtractorTest {
2121

@@ -41,32 +41,19 @@ public void testGetComments() throws IOException, ExtractionException {
4141

4242
assertTrue(result);
4343
}
44-
45-
@Test
46-
public void testGetCommentsFromStreamInfo() throws IOException, ExtractionException {
47-
boolean result = false;
48-
StreamInfo streamInfo = StreamInfo.getInfo("https://www.youtube.com/watch?v=rrgFN3AxGfs");
49-
50-
result = findInComments(streamInfo.getCommentsInfo().getComments(), "i should really be in the top comment.lol");
51-
52-
while (streamInfo.getCommentsInfo().hasMoreComments() && !result) {
53-
CommentsInfo.loadMoreComments(streamInfo.getCommentsInfo());
54-
result = findInComments(streamInfo.getCommentsInfo().getComments(), "i should really be in the top comment.lol");
55-
}
56-
57-
assertTrue(result);
58-
}
5944

6045
@Test
6146
public void testGetCommentsFromCommentsInfo() throws IOException, ExtractionException {
6247
boolean result = false;
6348
CommentsInfo commentsInfo = CommentsInfo.getInfo("https://www.youtube.com/watch?v=rrgFN3AxGfs");
6449
assertTrue("what the fuck am i doing with my life.wmv".equals(commentsInfo.getName()));
65-
result = findInComments(commentsInfo.getComments(), "i should really be in the top comment.lol");
50+
result = findInComments(commentsInfo.getRelatedItems(), "i should really be in the top comment.lol");
6651

67-
while (commentsInfo.hasMoreComments() && !result) {
68-
CommentsInfo.loadMoreComments(commentsInfo);
69-
result = findInComments(commentsInfo.getComments(), "i should really be in the top comment.lol");
52+
String nextPage = commentsInfo.getNextPageUrl();
53+
while (!StringUtil.isBlank(nextPage) && !result) {
54+
InfoItemsPage<CommentsInfoItem> moreItems = CommentsInfo.getMoreItems(YouTube, commentsInfo, nextPage);
55+
result = findInComments(moreItems.getItems(), "i should really be in the top comment.lol");
56+
nextPage = moreItems.getNextPageUrl();
7057
}
7158

7259
assertTrue(result);

0 commit comments

Comments
 (0)