Skip to content

Commit 70748e7

Browse files
committed
[PeerTube] Add support for comment replies
1 parent 197d887 commit 70748e7

File tree

2 files changed

+67
-8
lines changed

2 files changed

+67
-8
lines changed

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

Lines changed: 50 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,29 @@ 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+
final String id = getId();
45+
final String idd = getOriginalUrl();
46+
if (isReply()) {
47+
return getPage(new Page(getOriginalUrl()));
48+
} else {
49+
return getPage(new Page(getUrl() + "?" + START_KEY + "=0&"
50+
+ COUNT_KEY + "=" + ITEMS_PER_PAGE));
51+
}
4052
}
4153

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

4669
for (final Object c : contents) {
@@ -53,6 +76,20 @@ private void collectCommentsFrom(final CommentsInfoItemsCollector collector,
5376
}
5477
}
5578

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

74111
if (json != null) {
75112
PeertubeParsingHelper.validate(json);
76-
final long total = json.getLong("total");
77-
113+
final long total;
78114
final CommentsInfoItemsCollector collector
79115
= new CommentsInfoItemsCollector(getServiceId());
80-
collectCommentsFrom(collector, json);
116+
117+
if (isReply() || json.has("children")) {
118+
total = json.getArray("children").size();
119+
collectRepliesFrom(collector, json);
120+
} else {
121+
total = json.getLong("total");
122+
collectCommentsFrom(collector, json);
123+
}
81124

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

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

Lines changed: 17 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;
@@ -28,7 +29,7 @@ public PeertubeCommentsInfoItemExtractor(final JsonObject item,
2829

2930
@Override
3031
public String getUrl() throws ParsingException {
31-
return url;
32+
return url + "/" + getCommentId();
3233
}
3334

3435
@Override
@@ -98,4 +99,19 @@ public String getUploaderUrl() throws ParsingException {
9899
return ServiceList.PeerTube.getChannelLHFactory()
99100
.fromId("accounts/" + name + "@" + host, baseUrl).getUrl();
100101
}
102+
103+
@Override
104+
public Page getReplies() throws ParsingException {
105+
if (JsonUtils.getNumber(item, "totalReplies").intValue() == 0) {
106+
return null;
107+
}
108+
final String threadId = JsonUtils.getNumber(item, "threadId").toString();
109+
//final String threadUrl = JsonUtils.getString(item, "url");
110+
return new Page(url + "/" + threadId, threadId);
111+
}
112+
@Override
113+
public int getReplyCount() throws ParsingException {
114+
final int s = JsonUtils.getNumber(item, "totalReplies").intValue();
115+
return s;
116+
}
101117
}

0 commit comments

Comments
 (0)