2626import javax .annotation .Nonnull ;
2727
2828public 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 ));
0 commit comments