Skip to content

Commit 7b54457

Browse files
authored
Merge pull request #941 from TeamNewPipe/feat/peertube-comment-replies
[PeerTube] Support comment replies
2 parents f45966d + 4e66b22 commit 7b54457

2 files changed

Lines changed: 66 additions & 8 deletions

File tree

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

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
import javax.annotation.Nonnull;
2727

2828
public class PeertubeCommentsExtractor extends CommentsExtractor {
29+
30+
/**
31+
* Use {@link #isReply()} to access this variable.
32+
*/
33+
private Boolean isReply = null;
34+
2935
public PeertubeCommentsExtractor(final StreamingService service,
3036
final ListLinkHandler uiHandler) {
3137
super(service, uiHandler);
@@ -35,12 +41,27 @@ public PeertubeCommentsExtractor(final StreamingService service,
3541
@Override
3642
public InfoItemsPage<CommentsInfoItem> getInitialPage()
3743
throws IOException, ExtractionException {
38-
return getPage(new Page(getUrl() + "?" + START_KEY + "=0&"
39-
+ COUNT_KEY + "=" + ITEMS_PER_PAGE));
44+
if (isReply()) {
45+
return getPage(new Page(getOriginalUrl()));
46+
} else {
47+
return getPage(new Page(getUrl() + "?" + START_KEY + "=0&"
48+
+ COUNT_KEY + "=" + ITEMS_PER_PAGE));
49+
}
4050
}
4151

42-
private void collectCommentsFrom(final CommentsInfoItemsCollector collector,
43-
final JsonObject json) throws ParsingException {
52+
private boolean isReply() throws ParsingException {
53+
if (isReply == null) {
54+
if (getOriginalUrl().contains("/videos/watch/")) {
55+
isReply = false;
56+
} else {
57+
isReply = getOriginalUrl().contains("/comment-threads/");
58+
}
59+
}
60+
return isReply;
61+
}
62+
63+
private void collectCommentsFrom(@Nonnull final CommentsInfoItemsCollector collector,
64+
@Nonnull final JsonObject json) throws ParsingException {
4465
final JsonArray contents = json.getArray("data");
4566

4667
for (final Object c : contents) {
@@ -53,6 +74,20 @@ private void collectCommentsFrom(final CommentsInfoItemsCollector collector,
5374
}
5475
}
5576

77+
private void collectRepliesFrom(@Nonnull final CommentsInfoItemsCollector collector,
78+
@Nonnull final JsonObject json) throws ParsingException {
79+
final JsonArray contents = json.getArray("children");
80+
81+
for (final Object c : contents) {
82+
if (c instanceof JsonObject) {
83+
final JsonObject item = ((JsonObject) c).getObject("comment");
84+
if (!item.getBoolean("isDeleted")) {
85+
collector.commit(new PeertubeCommentsInfoItemExtractor(item, this));
86+
}
87+
}
88+
}
89+
}
90+
5691
@Override
5792
public InfoItemsPage<CommentsInfoItem> getPage(final Page page)
5893
throws IOException, ExtractionException {
@@ -73,11 +108,17 @@ public InfoItemsPage<CommentsInfoItem> getPage(final Page page)
73108

74109
if (json != null) {
75110
PeertubeParsingHelper.validate(json);
76-
final long total = json.getLong("total");
77-
111+
final long total;
78112
final CommentsInfoItemsCollector collector
79113
= new CommentsInfoItemsCollector(getServiceId());
80-
collectCommentsFrom(collector, json);
114+
115+
if (isReply() || json.has("children")) {
116+
total = json.getArray("children").size();
117+
collectRepliesFrom(collector, json);
118+
} else {
119+
total = json.getLong("total");
120+
collectCommentsFrom(collector, json);
121+
}
81122

82123
return new InfoItemsPage<>(collector,
83124
PeertubeParsingHelper.getNextPage(page.getUrl(), total));

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import org.jsoup.Jsoup;
66
import org.jsoup.nodes.Document;
7+
import org.schabi.newpipe.extractor.Page;
78
import org.schabi.newpipe.extractor.ServiceList;
89
import org.schabi.newpipe.extractor.comments.CommentsInfoItemExtractor;
910
import org.schabi.newpipe.extractor.exceptions.ParsingException;
@@ -12,6 +13,7 @@
1213
import org.schabi.newpipe.extractor.stream.Description;
1314
import org.schabi.newpipe.extractor.utils.JsonUtils;
1415

16+
import javax.annotation.Nullable;
1517
import java.util.Objects;
1618

1719
public class PeertubeCommentsInfoItemExtractor implements CommentsInfoItemExtractor {
@@ -29,7 +31,7 @@ public PeertubeCommentsInfoItemExtractor(final JsonObject item,
2931

3032
@Override
3133
public String getUrl() throws ParsingException {
32-
return url;
34+
return url + "/" + getCommentId();
3335
}
3436

3537
@Override
@@ -101,4 +103,19 @@ public String getUploaderUrl() throws ParsingException {
101103
return ServiceList.PeerTube.getChannelLHFactory()
102104
.fromId("accounts/" + name + "@" + host, baseUrl).getUrl();
103105
}
106+
107+
@Override
108+
@Nullable
109+
public Page getReplies() throws ParsingException {
110+
if (JsonUtils.getNumber(item, "totalReplies").intValue() == 0) {
111+
return null;
112+
}
113+
final String threadId = JsonUtils.getNumber(item, "threadId").toString();
114+
return new Page(url + "/" + threadId, threadId);
115+
}
116+
117+
@Override
118+
public int getReplyCount() throws ParsingException {
119+
return JsonUtils.getNumber(item, "totalReplies").intValue();
120+
}
104121
}

0 commit comments

Comments
 (0)