Skip to content

Commit c0ceb5c

Browse files
authored
Merge pull request #340 from wb9688/peertube-deleted-comments
Handle isDeleted for PeerTube comments
2 parents f4ed7ce + 3239aa8 commit c0ceb5c

2 files changed

Lines changed: 93 additions & 75 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeCommentsExtractor.java

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,19 @@
1515
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
1616
import org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper;
1717
import org.schabi.newpipe.extractor.utils.JsonUtils;
18-
import org.schabi.newpipe.extractor.utils.Parser;
19-
import org.schabi.newpipe.extractor.utils.Parser.RegexException;
2018
import org.schabi.newpipe.extractor.utils.Utils;
2119

2220
import java.io.IOException;
2321

24-
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.*;
22+
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.COUNT_KEY;
23+
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.ITEMS_PER_PAGE;
24+
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.START_KEY;
2525

2626
public class PeertubeCommentsExtractor extends CommentsExtractor {
27-
2827
private InfoItemsPage<CommentsInfoItem> initPage;
2928
private long total;
3029

31-
public PeertubeCommentsExtractor(StreamingService service, ListLinkHandler uiHandler) {
30+
public PeertubeCommentsExtractor(final StreamingService service, final ListLinkHandler uiHandler) {
3231
super(service, uiHandler);
3332
}
3433

@@ -38,22 +37,18 @@ public InfoItemsPage<CommentsInfoItem> getInitialPage() throws IOException, Extr
3837
return initPage;
3938
}
4039

41-
private void collectStreamsFrom(CommentsInfoItemsCollector collector, JsonObject json, String pageUrl) throws ParsingException {
42-
JsonArray contents;
43-
try {
44-
contents = (JsonArray) JsonUtils.getValue(json, "data");
45-
} catch (Exception e) {
46-
throw new ParsingException("unable to extract comments info", e);
47-
}
40+
private void collectCommentsFrom(final CommentsInfoItemsCollector collector, final JsonObject json) throws ParsingException {
41+
final JsonArray contents = json.getArray("data");
4842

49-
for (Object c : contents) {
43+
for (final Object c : contents) {
5044
if (c instanceof JsonObject) {
5145
final JsonObject item = (JsonObject) c;
52-
PeertubeCommentsInfoItemExtractor extractor = new PeertubeCommentsInfoItemExtractor(item, this);
53-
collector.commit(extractor);
46+
if (!item.getBoolean("isDeleted")) {
47+
final PeertubeCommentsInfoItemExtractor extractor = new PeertubeCommentsInfoItemExtractor(item, this);
48+
collector.commit(extractor);
49+
}
5450
}
5551
}
56-
5752
}
5853

5954
@Override
@@ -63,8 +58,8 @@ public String getNextPageUrl() throws IOException, ExtractionException {
6358
}
6459

6560
@Override
66-
public InfoItemsPage<CommentsInfoItem> getPage(String pageUrl) throws IOException, ExtractionException {
67-
Response response = getDownloader().get(pageUrl);
61+
public InfoItemsPage<CommentsInfoItem> getPage(final String pageUrl) throws IOException, ExtractionException {
62+
final Response response = getDownloader().get(pageUrl);
6863
JsonObject json = null;
6964
if (response != null && !Utils.isBlank(response.responseBody())) {
7065
try {
@@ -74,11 +69,11 @@ public InfoItemsPage<CommentsInfoItem> getPage(String pageUrl) throws IOExceptio
7469
}
7570
}
7671

77-
CommentsInfoItemsCollector collector = new CommentsInfoItemsCollector(getServiceId());
72+
final CommentsInfoItemsCollector collector = new CommentsInfoItemsCollector(getServiceId());
7873
if (json != null) {
79-
Number number = JsonUtils.getNumber(json, "total");
74+
final Number number = JsonUtils.getNumber(json, "total");
8075
if (number != null) this.total = number.longValue();
81-
collectStreamsFrom(collector, json, pageUrl);
76+
collectCommentsFrom(collector, json);
8277
} else {
8378
throw new ExtractionException("Unable to get peertube comments info");
8479
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeCommentsExtractorTest.java

Lines changed: 77 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -18,74 +18,97 @@
1818
import static org.schabi.newpipe.extractor.ServiceList.PeerTube;
1919

2020
public class PeertubeCommentsExtractorTest {
21+
public static class Default {
22+
private static PeertubeCommentsExtractor extractor;
2123

22-
private static PeertubeCommentsExtractor extractor;
24+
@BeforeClass
25+
public static void setUp() throws Exception {
26+
NewPipe.init(DownloaderTestImpl.getInstance());
27+
extractor = (PeertubeCommentsExtractor) PeerTube
28+
.getCommentsExtractor("https://framatube.org/videos/watch/04af977f-4201-4697-be67-a8d8cae6fa7a");
29+
}
2330

24-
@BeforeClass
25-
public static void setUp() throws Exception {
26-
NewPipe.init(DownloaderTestImpl.getInstance());
27-
extractor = (PeertubeCommentsExtractor) PeerTube
28-
.getCommentsExtractor("https://framatube.org/videos/watch/04af977f-4201-4697-be67-a8d8cae6fa7a");
29-
}
31+
@Test
32+
public void testGetComments() throws IOException, ExtractionException {
33+
InfoItemsPage<CommentsInfoItem> comments = extractor.getInitialPage();
34+
assertTrue(comments.getErrors().isEmpty());
3035

31-
@Test
32-
public void testGetComments() throws IOException, ExtractionException {
33-
boolean result = false;
34-
InfoItemsPage<CommentsInfoItem> comments = extractor.getInitialPage();
35-
result = findInComments(comments, "@root A great documentary on a great guy.");
36+
boolean result = findInComments(comments, "@root A great documentary on a great guy.");
37+
while (comments.hasNextPage() && !result) {
38+
comments = extractor.getPage(comments.getNextPageUrl());
39+
result = findInComments(comments, "@root A great documentary on a great guy.");
40+
}
3641

37-
while (comments.hasNextPage() && !result) {
38-
comments = extractor.getPage(comments.getNextPageUrl());
39-
result = findInComments(comments, "@root A great documentary on a great guy.");
42+
assertTrue(result);
4043
}
4144

42-
assertTrue(result);
43-
}
45+
@Test
46+
public void testGetCommentsFromCommentsInfo() throws IOException, ExtractionException {
47+
final CommentsInfo commentsInfo = CommentsInfo.getInfo("https://framatube.org/videos/watch/a8ea95b8-0396-49a6-8f30-e25e25fb2828");
48+
assertTrue(commentsInfo.getErrors().isEmpty());
49+
assertEquals("Comments", commentsInfo.getName());
4450

45-
@Test
46-
public void testGetCommentsFromCommentsInfo() throws IOException, ExtractionException {
47-
boolean result = false;
48-
CommentsInfo commentsInfo = CommentsInfo.getInfo("https://framatube.org/videos/watch/a8ea95b8-0396-49a6-8f30-e25e25fb2828");
49-
assertEquals("Comments", commentsInfo.getName());
50-
result = findInComments(commentsInfo.getRelatedItems(), "Loved it!!!");
51-
52-
String nextPage = commentsInfo.getNextPageUrl();
53-
while (!Utils.isBlank(nextPage) && !result) {
54-
InfoItemsPage<CommentsInfoItem> moreItems = CommentsInfo.getMoreItems(PeerTube, commentsInfo, nextPage);
55-
result = findInComments(moreItems.getItems(), "Loved it!!!");
56-
nextPage = moreItems.getNextPageUrl();
51+
boolean result = findInComments(commentsInfo.getRelatedItems(), "Loved it!!!");
52+
String nextPage = commentsInfo.getNextPageUrl();
53+
while (!Utils.isBlank(nextPage) && !result) {
54+
final InfoItemsPage<CommentsInfoItem> moreItems = CommentsInfo.getMoreItems(PeerTube, commentsInfo, nextPage);
55+
result = findInComments(moreItems.getItems(), "Loved it!!!");
56+
nextPage = moreItems.getNextPageUrl();
57+
}
58+
assertTrue(result);
5759
}
5860

59-
assertTrue(result);
60-
}
61+
@Test
62+
public void testGetCommentsAllData() throws IOException, ExtractionException {
63+
final InfoItemsPage<CommentsInfoItem> comments = extractor.getInitialPage();
64+
for (final CommentsInfoItem c : comments.getItems()) {
65+
assertFalse(Utils.isBlank(c.getUploaderUrl()));
66+
assertFalse(Utils.isBlank(c.getUploaderName()));
67+
assertFalse(Utils.isBlank(c.getUploaderAvatarUrl()));
68+
assertFalse(Utils.isBlank(c.getCommentId()));
69+
assertFalse(Utils.isBlank(c.getCommentText()));
70+
assertFalse(Utils.isBlank(c.getName()));
71+
assertFalse(Utils.isBlank(c.getTextualUploadDate()));
72+
assertFalse(Utils.isBlank(c.getThumbnailUrl()));
73+
assertFalse(Utils.isBlank(c.getUrl()));
74+
assertEquals(-1, c.getLikeCount());
75+
}
76+
}
6177

62-
@Test
63-
public void testGetCommentsAllData() throws IOException, ExtractionException {
64-
InfoItemsPage<CommentsInfoItem> comments = extractor.getInitialPage();
65-
for (CommentsInfoItem c : comments.getItems()) {
66-
assertFalse(Utils.isBlank(c.getUploaderUrl()));
67-
assertFalse(Utils.isBlank(c.getUploaderName()));
68-
assertFalse(Utils.isBlank(c.getUploaderAvatarUrl()));
69-
assertFalse(Utils.isBlank(c.getCommentId()));
70-
assertFalse(Utils.isBlank(c.getCommentText()));
71-
assertFalse(Utils.isBlank(c.getName()));
72-
assertFalse(Utils.isBlank(c.getTextualUploadDate()));
73-
assertFalse(Utils.isBlank(c.getThumbnailUrl()));
74-
assertFalse(Utils.isBlank(c.getUrl()));
75-
assertFalse(c.getLikeCount() != -1);
78+
private boolean findInComments(final InfoItemsPage<CommentsInfoItem> comments, final String comment) {
79+
return findInComments(comments.getItems(), comment);
7680
}
77-
}
7881

79-
private boolean findInComments(InfoItemsPage<CommentsInfoItem> comments, String comment) {
80-
return findInComments(comments.getItems(), comment);
82+
private boolean findInComments(final List<CommentsInfoItem> comments, final String comment) {
83+
for (final CommentsInfoItem c : comments) {
84+
if (c.getCommentText().contains(comment)) {
85+
return true;
86+
}
87+
}
88+
return false;
89+
}
8190
}
8291

83-
private boolean findInComments(List<CommentsInfoItem> comments, String comment) {
84-
for (CommentsInfoItem c : comments) {
85-
if (c.getCommentText().contains(comment)) {
86-
return true;
87-
}
92+
public static class DeletedComments {
93+
private static PeertubeCommentsExtractor extractor;
94+
95+
@BeforeClass
96+
public static void setUp() throws Exception {
97+
NewPipe.init(DownloaderTestImpl.getInstance());
98+
extractor = (PeertubeCommentsExtractor) PeerTube
99+
.getCommentsExtractor("https://framatube.org/videos/watch/217eefeb-883d-45be-b7fc-a788ad8507d3");
100+
}
101+
102+
@Test
103+
public void testGetComments() throws IOException, ExtractionException {
104+
final InfoItemsPage<CommentsInfoItem> comments = extractor.getInitialPage();
105+
assertTrue(comments.getErrors().isEmpty());
106+
}
107+
108+
@Test
109+
public void testGetCommentsFromCommentsInfo() throws IOException, ExtractionException {
110+
final CommentsInfo commentsInfo = CommentsInfo.getInfo("https://framatube.org/videos/watch/217eefeb-883d-45be-b7fc-a788ad8507d3");
111+
assertTrue(commentsInfo.getErrors().isEmpty());
88112
}
89-
return false;
90113
}
91114
}

0 commit comments

Comments
 (0)