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.
@@ -67,22 +72,26 @@ 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 (
77+ item , null , getUrl (), getBaseUrl (), isReply ()));
7278 }
7379 }
7480 }
7581 }
7682
7783 private void collectRepliesFrom (@ Nonnull final CommentsInfoItemsCollector collector ,
7884 @ Nonnull final JsonObject json ) throws ParsingException {
79- final JsonArray contents = json .getArray ("children" );
85+ final JsonArray contents = json .getArray (CHILDREN );
8086
8187 for (final Object c : contents ) {
8288 if (c instanceof JsonObject ) {
83- final JsonObject item = ((JsonObject ) c ).getObject ("comment" );
84- if (!item .getBoolean ("isDeleted" )) {
85- collector .commit (new PeertubeCommentsInfoItemExtractor (item , this ));
89+ final JsonObject content = (JsonObject ) c ;
90+ final JsonObject item = content .getObject ("comment" );
91+ final JsonArray children = content .getArray (CHILDREN );
92+ if (!item .getBoolean (IS_DELETED )) {
93+ collector .commit (new PeertubeCommentsInfoItemExtractor (
94+ item , children , getUrl (), getBaseUrl (), isReply ()));
8695 }
8796 }
8897 }
@@ -95,36 +104,46 @@ public InfoItemsPage<CommentsInfoItem> getPage(final Page page)
95104 throw new IllegalArgumentException ("Page doesn't contain an URL" );
96105 }
97106
98- final Response response = getDownloader ().get (page .getUrl ());
99-
100107 JsonObject json = null ;
101- if (response != null && !Utils .isBlank (response .responseBody ())) {
108+ final CommentsInfoItemsCollector collector = new CommentsInfoItemsCollector (getServiceId ());
109+ final long total ;
110+ if (page .getBody () == null ) {
111+ final Response response = getDownloader ().get (page .getUrl ());
112+ if (response != null && !Utils .isBlank (response .responseBody ())) {
113+ try {
114+ json = JsonParser .object ().from (response .responseBody ());
115+ } catch (final Exception e ) {
116+ throw new ParsingException ("Could not parse json data for comments info" , e );
117+ }
118+ }
119+ if (json != null ) {
120+ PeertubeParsingHelper .validate (json );
121+ if (isReply () || json .has (CHILDREN )) {
122+ total = json .getArray (CHILDREN ).size ();
123+ collectRepliesFrom (collector , json );
124+ } else {
125+ total = json .getLong (TOTAL );
126+ collectCommentsFrom (collector , json );
127+ }
128+ } else {
129+ throw new ExtractionException ("Unable to get PeerTube kiosk info" );
130+ }
131+ } else {
102132 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 );
133+ json = JsonParser .object ().from (new String (page .getBody (), StandardCharsets .UTF_8 ));
134+ isReply = true ;
135+ total = json .getArray (CHILDREN ).size ();
136+ collectRepliesFrom (collector , json );
137+ } catch (final JsonParserException e ) {
138+ throw new ParsingException (
139+ "Could not parse json data for nested comments info" , e );
106140 }
107141 }
108142
109- if (json != null ) {
110- PeertubeParsingHelper .validate (json );
111- final long total ;
112- final CommentsInfoItemsCollector collector
113- = new CommentsInfoItemsCollector (getServiceId ());
143+ return new InfoItemsPage <>(collector ,
144+ PeertubeParsingHelper .getNextPage (page .getUrl (), total ));
114145
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- }
122146
123- return new InfoItemsPage <>(collector ,
124- PeertubeParsingHelper .getNextPage (page .getUrl (), total ));
125- } else {
126- throw new ExtractionException ("Unable to get PeerTube kiosk info" );
127- }
128147 }
129148
130149 @ Override
0 commit comments