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,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 ));
0 commit comments