33import com .grack .nanojson .JsonArray ;
44import com .grack .nanojson .JsonObject ;
55
6+ import com .grack .nanojson .JsonWriter ;
67import org .schabi .newpipe .extractor .Page ;
78import org .schabi .newpipe .extractor .comments .CommentsInfoItemExtractor ;
89import org .schabi .newpipe .extractor .exceptions .ParsingException ;
1920public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtractor {
2021
2122 private final JsonObject json ;
23+ private JsonObject commentRenderer ;
2224 private final String url ;
2325 private final TimeAgoParser timeAgoParser ;
2426
@@ -30,6 +32,10 @@ public YoutubeCommentsInfoItemExtractor(final JsonObject json,
3032 this .timeAgoParser = timeAgoParser ;
3133 }
3234
35+ private JsonObject getCommentRenderer () throws ParsingException {
36+ return commentRenderer != null ? commentRenderer : (commentRenderer = JsonUtils .getObject (json , "comment.commentRenderer" ));
37+ }
38+
3339 @ Override
3440 public String getUrl () throws ParsingException {
3541 return url ;
@@ -38,7 +44,7 @@ public String getUrl() throws ParsingException {
3844 @ Override
3945 public String getThumbnailUrl () throws ParsingException {
4046 try {
41- final JsonArray arr = JsonUtils .getArray (json , "authorThumbnail.thumbnails" );
47+ final JsonArray arr = JsonUtils .getArray (getCommentRenderer () , "authorThumbnail.thumbnails" );
4248 return JsonUtils .getString (arr .getObject (2 ), "url" );
4349 } catch (final Exception e ) {
4450 throw new ParsingException ("Could not get thumbnail url" , e );
@@ -48,7 +54,7 @@ public String getThumbnailUrl() throws ParsingException {
4854 @ Override
4955 public String getName () throws ParsingException {
5056 try {
51- return getTextFromObject (JsonUtils .getObject (json , "authorText" ));
57+ return getTextFromObject (JsonUtils .getObject (getCommentRenderer () , "authorText" ));
5258 } catch (final Exception e ) {
5359 return EMPTY_STRING ;
5460 }
@@ -57,7 +63,7 @@ public String getName() throws ParsingException {
5763 @ Override
5864 public String getTextualUploadDate () throws ParsingException {
5965 try {
60- return getTextFromObject (JsonUtils .getObject (json , "publishedTimeText" ));
66+ return getTextFromObject (JsonUtils .getObject (getCommentRenderer () , "publishedTimeText" ));
6167 } catch (final Exception e ) {
6268 throw new ParsingException ("Could not get publishedTimeText" , e );
6369 }
@@ -95,7 +101,7 @@ public int getLikeCount() throws ParsingException {
95101 // Try first to get the exact like count by using the accessibility data
96102 final String likeCount ;
97103 try {
98- likeCount = Utils .removeNonDigitCharacters (JsonUtils .getString (json ,
104+ likeCount = Utils .removeNonDigitCharacters (JsonUtils .getString (getCommentRenderer () ,
99105 "actionButtons.commentActionButtonsRenderer.likeButton.toggleButtonRenderer.accessibilityData.accessibilityData.label" ));
100106 } catch (final Exception e ) {
101107 // Use the approximate like count returned into the voteCount object
@@ -146,11 +152,11 @@ public String getTextualLikeCount() throws ParsingException {
146152 */
147153 try {
148154 // If a comment has no likes voteCount is not set
149- if (!json .has ("voteCount" )) {
155+ if (!getCommentRenderer () .has ("voteCount" )) {
150156 return EMPTY_STRING ;
151157 }
152158
153- final JsonObject voteCountObj = JsonUtils .getObject (json , "voteCount" );
159+ final JsonObject voteCountObj = JsonUtils .getObject (getCommentRenderer () , "voteCount" );
154160 if (voteCountObj .isEmpty ()) {
155161 return EMPTY_STRING ;
156162 }
@@ -163,7 +169,7 @@ public String getTextualLikeCount() throws ParsingException {
163169 @ Override
164170 public String getCommentText () throws ParsingException {
165171 try {
166- final JsonObject contentText = JsonUtils .getObject (json , "contentText" );
172+ final JsonObject contentText = JsonUtils .getObject (getCommentRenderer () , "contentText" );
167173 if (contentText .isEmpty ()) {
168174 // completely empty comments as described in
169175 // https://github.com/TeamNewPipe/NewPipeExtractor/issues/380#issuecomment-668808584
@@ -181,7 +187,7 @@ public String getCommentText() throws ParsingException {
181187 @ Override
182188 public String getCommentId () throws ParsingException {
183189 try {
184- return JsonUtils .getString (json , "commentId" );
190+ return JsonUtils .getString (getCommentRenderer () , "commentId" );
185191 } catch (final Exception e ) {
186192 throw new ParsingException ("Could not get comment id" , e );
187193 }
@@ -190,7 +196,7 @@ public String getCommentId() throws ParsingException {
190196 @ Override
191197 public String getUploaderAvatarUrl () throws ParsingException {
192198 try {
193- JsonArray arr = JsonUtils .getArray (json , "authorThumbnail.thumbnails" );
199+ JsonArray arr = JsonUtils .getArray (getCommentRenderer () , "authorThumbnail.thumbnails" );
194200 return JsonUtils .getString (arr .getObject (2 ), "url" );
195201 } catch (final Exception e ) {
196202 throw new ParsingException ("Could not get author thumbnail" , e );
@@ -199,24 +205,24 @@ public String getUploaderAvatarUrl() throws ParsingException {
199205
200206 @ Override
201207 public boolean isHeartedByUploader () throws ParsingException {
202- final JsonObject commentActionButtonsRenderer = json .getObject ("actionButtons" )
208+ final JsonObject commentActionButtonsRenderer = getCommentRenderer () .getObject ("actionButtons" )
203209 .getObject ("commentActionButtonsRenderer" );
204210 return commentActionButtonsRenderer .has ("creatorHeart" );
205211 }
206212
207213 @ Override
208- public boolean isPinned () {
209- return json .has ("pinnedCommentBadge" );
214+ public boolean isPinned () throws ParsingException {
215+ return getCommentRenderer () .has ("pinnedCommentBadge" );
210216 }
211217
212- public boolean isUploaderVerified () {
213- return json .has ("authorCommentBadge" );
218+ public boolean isUploaderVerified () throws ParsingException {
219+ return getCommentRenderer () .has ("authorCommentBadge" );
214220 }
215221
216222 @ Override
217223 public String getUploaderName () throws ParsingException {
218224 try {
219- return getTextFromObject (JsonUtils .getObject (json , "authorText" ));
225+ return getTextFromObject (JsonUtils .getObject (getCommentRenderer () , "authorText" ));
220226 } catch (final Exception e ) {
221227 return EMPTY_STRING ;
222228 }
@@ -225,7 +231,7 @@ public String getUploaderName() throws ParsingException {
225231 @ Override
226232 public String getUploaderUrl () throws ParsingException {
227233 try {
228- return "https://www.youtube.com/channel/" + JsonUtils .getString (json ,
234+ return "https://www.youtube.com/channel/" + JsonUtils .getString (getCommentRenderer () ,
229235 "authorEndpoint.browseEndpoint.browseId" );
230236 } catch (final Exception e ) {
231237 return EMPTY_STRING ;
0 commit comments