33import com .grack .nanojson .JsonArray ;
44import com .grack .nanojson .JsonObject ;
55import com .grack .nanojson .JsonParser ;
6+ import com .grack .nanojson .JsonParserException ;
67import org .schabi .newpipe .extractor .Page ;
78import org .schabi .newpipe .extractor .StreamingService ;
89import org .schabi .newpipe .extractor .comments .CommentsExtractor ;
1718import org .schabi .newpipe .extractor .utils .Utils ;
1819
1920import java .io .IOException ;
21+ import java .nio .charset .StandardCharsets ;
2022
2123import static org .schabi .newpipe .extractor .services .peertube .PeertubeParsingHelper .COUNT_KEY ;
2224import static org .schabi .newpipe .extractor .services .peertube .PeertubeParsingHelper .ITEMS_PER_PAGE ;
2628import javax .annotation .Nonnull ;
2729
2830public class PeertubeCommentsExtractor extends CommentsExtractor {
31+ static final String CHILDREN = "children" ;
32+ private static final String IS_DELETED = "isDeleted" ;
33+ private static final String TOTAL = "total" ;
2934
3035 /**
3136 * Use {@link #isReply()} to access this variable.
@@ -49,7 +54,7 @@ public InfoItemsPage<CommentsInfoItem> getInitialPage()
4954 }
5055 }
5156
52- private boolean isReply () throws ParsingException {
57+ boolean isReply () throws ParsingException {
5358 if (isReply == null ) {
5459 if (getOriginalUrl ().contains ("/videos/watch/" )) {
5560 isReply = false ;
@@ -67,22 +72,24 @@ private void collectCommentsFrom(@Nonnull final CommentsInfoItemsCollector colle
6772 for (final Object c : contents ) {
6873 if (c instanceof JsonObject ) {
6974 final JsonObject item = (JsonObject ) c ;
70- if (!item .getBoolean ("isDeleted" )) {
71- collector .commit (new PeertubeCommentsInfoItemExtractor (item , this ));
75+ if (!item .getBoolean (IS_DELETED )) {
76+ collector .commit (new PeertubeCommentsInfoItemExtractor (item , null , this ));
7277 }
7378 }
7479 }
7580 }
7681
7782 private void collectRepliesFrom (@ Nonnull final CommentsInfoItemsCollector collector ,
7883 @ Nonnull final JsonObject json ) throws ParsingException {
79- final JsonArray contents = json .getArray ("children" );
84+ final JsonArray contents = json .getArray (CHILDREN );
8085
8186 for (final Object c : contents ) {
8287 if (c instanceof JsonObject ) {
83- final JsonObject item = ((JsonObject ) c ).getObject ("comment" );
84- if (!item .getBoolean ("isDeleted" )) {
85- collector .commit (new PeertubeCommentsInfoItemExtractor (item , this ));
88+ final JsonObject content = (JsonObject ) c ;
89+ final JsonObject item = content .getObject ("comment" );
90+ final JsonArray children = content .getArray (CHILDREN );
91+ if (!item .getBoolean (IS_DELETED )) {
92+ collector .commit (new PeertubeCommentsInfoItemExtractor (item , children , this ));
8693 }
8794 }
8895 }
@@ -95,36 +102,46 @@ public InfoItemsPage<CommentsInfoItem> getPage(final Page page)
95102 throw new IllegalArgumentException ("Page doesn't contain an URL" );
96103 }
97104
98- final Response response = getDownloader ().get (page .getUrl ());
99-
100105 JsonObject json = null ;
101- if (response != null && !Utils .isBlank (response .responseBody ())) {
106+ final CommentsInfoItemsCollector collector = new CommentsInfoItemsCollector (getServiceId ());
107+ final long total ;
108+ if (page .getBody () == null ) {
109+ final Response response = getDownloader ().get (page .getUrl ());
110+ if (response != null && !Utils .isBlank (response .responseBody ())) {
111+ try {
112+ json = JsonParser .object ().from (response .responseBody ());
113+ } catch (final Exception e ) {
114+ throw new ParsingException ("Could not parse json data for comments info" , e );
115+ }
116+ }
117+ if (json != null ) {
118+ PeertubeParsingHelper .validate (json );
119+ if (isReply () || json .has (CHILDREN )) {
120+ total = json .getArray (CHILDREN ).size ();
121+ collectRepliesFrom (collector , json );
122+ } else {
123+ total = json .getLong (TOTAL );
124+ collectCommentsFrom (collector , json );
125+ }
126+ } else {
127+ throw new ExtractionException ("Unable to get PeerTube kiosk info" );
128+ }
129+ } else {
102130 try {
103- json = JsonParser .object ().from (response .responseBody ());
104- } catch (final Exception e ) {
105- throw new ParsingException ("Could not parse json data for comments info" , e );
131+ json = JsonParser .object ().from (new String (page .getBody (), StandardCharsets .UTF_8 ));
132+ isReply = true ;
133+ total = json .getArray (CHILDREN ).size ();
134+ collectRepliesFrom (collector , json );
135+ } catch (final JsonParserException e ) {
136+ throw new ParsingException (
137+ "Could not parse json data for nested comments info" , e );
106138 }
107139 }
108140
109- if (json != null ) {
110- PeertubeParsingHelper .validate (json );
111- final long total ;
112- final CommentsInfoItemsCollector collector
113- = new CommentsInfoItemsCollector (getServiceId ());
141+ return new InfoItemsPage <>(collector ,
142+ PeertubeParsingHelper .getNextPage (page .getUrl (), total ));
114143
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- }
122144
123- return new InfoItemsPage <>(collector ,
124- PeertubeParsingHelper .getNextPage (page .getUrl (), total ));
125- } else {
126- throw new ExtractionException ("Unable to get PeerTube kiosk info" );
127- }
128145 }
129146
130147 @ Override
0 commit comments