Skip to content

Commit 99a0134

Browse files
committed
removed unchecked cast
1 parent e85958b commit 99a0134

3 files changed

Lines changed: 57 additions & 20 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsExtractor.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private String getNextPageUrl(JsonObject ajaxJson) throws IOException, ParsingEx
6262

6363
JsonArray arr;
6464
try {
65-
arr = (JsonArray) JsonUtils.getValue(ajaxJson, "response.continuationContents.commentSectionContinuation.continuations");
65+
arr = JsonUtils.getArray(ajaxJson, "response.continuationContents.commentSectionContinuation.continuations");
6666
} catch (Exception e) {
6767
return "";
6868
}
@@ -71,7 +71,7 @@ private String getNextPageUrl(JsonObject ajaxJson) throws IOException, ParsingEx
7171
}
7272
String continuation;
7373
try {
74-
continuation = (String) JsonUtils.getValue(arr.getObject(0), "nextContinuationData.continuation");
74+
continuation = JsonUtils.getString(arr.getObject(0), "nextContinuationData.continuation");
7575
} catch (Exception e) {
7676
return "";
7777
}
@@ -111,7 +111,7 @@ private void collectCommentsFrom(CommentsInfoItemsCollector collector, JsonObjec
111111

112112
JsonArray contents;
113113
try {
114-
contents = (JsonArray) JsonUtils.getValue(ajaxJson, "response.continuationContents.commentSectionContinuation.items");
114+
contents = JsonUtils.getArray(ajaxJson, "response.continuationContents.commentSectionContinuation.items");
115115
}catch(Exception e) {
116116
//no comments
117117
return;
@@ -135,7 +135,7 @@ private void collectCommentsFrom(CommentsInfoItemsCollector collector, JsonObjec
135135
private void fetchTitle(JsonArray contents) {
136136
if(null == title) {
137137
try {
138-
title = getYoutubeText((JsonObject) JsonUtils.getValue(contents.getObject(0), "commentThreadRenderer.commentTargetTitle"));
138+
title = getYoutubeText(JsonUtils.getObject(contents.getObject(0), "commentThreadRenderer.commentTargetTitle"));
139139
} catch (Exception e) {
140140
title = "Youtube Comments";
141141
}
@@ -196,13 +196,13 @@ private String findValue(String doc, String start, String end) {
196196

197197
public static String getYoutubeText(@Nonnull JsonObject object) throws ParsingException {
198198
try {
199-
return (String) JsonUtils.getValue(object, "simpleText");
199+
return JsonUtils.getString(object, "simpleText");
200200
} catch (Exception e1) {
201201
try {
202-
JsonArray arr = (JsonArray) JsonUtils.getValue(object, "runs");
202+
JsonArray arr = JsonUtils.getArray(object, "runs");
203203
String result = "";
204204
for(int i=0; i<arr.size();i++) {
205-
result = result + (String) JsonUtils.getValue(arr.getObject(i), "text");
205+
result = result + JsonUtils.getString(arr.getObject(i), "text");
206206
}
207207
return result;
208208
} catch (Exception e2) {

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsInfoItemExtractor.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import com.grack.nanojson.JsonArray;
88
import com.grack.nanojson.JsonObject;
99

10-
1110
public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtractor {
1211

1312
private final JsonObject json;
@@ -26,8 +25,8 @@ public String getUrl() throws ParsingException {
2625
@Override
2726
public String getThumbnailUrl() throws ParsingException {
2827
try {
29-
JsonArray arr = (JsonArray) JsonUtils.getValue(json, "authorThumbnail.thumbnails");
30-
return (String) JsonUtils.getValue(arr.getObject(2), "url");
28+
JsonArray arr = JsonUtils.getArray(json, "authorThumbnail.thumbnails");
29+
return JsonUtils.getString(arr.getObject(2), "url");
3130
} catch (Exception e) {
3231
throw new ParsingException("Could not get thumbnail url", e);
3332
}
@@ -36,7 +35,7 @@ public String getThumbnailUrl() throws ParsingException {
3635
@Override
3736
public String getName() throws ParsingException {
3837
try {
39-
return YoutubeCommentsExtractor.getYoutubeText((JsonObject) JsonUtils.getValue(json, "authorText"));
38+
return YoutubeCommentsExtractor.getYoutubeText(JsonUtils.getObject(json, "authorText"));
4039
} catch (Exception e) {
4140
throw new ParsingException("Could not get author name", e);
4241
}
@@ -45,7 +44,7 @@ public String getName() throws ParsingException {
4544
@Override
4645
public String getPublishedTime() throws ParsingException {
4746
try {
48-
return YoutubeCommentsExtractor.getYoutubeText((JsonObject) JsonUtils.getValue(json, "publishedTimeText"));
47+
return YoutubeCommentsExtractor.getYoutubeText(JsonUtils.getObject(json, "publishedTimeText"));
4948
} catch (Exception e) {
5049
throw new ParsingException("Could not get publishedTimeText", e);
5150
}
@@ -54,7 +53,7 @@ public String getPublishedTime() throws ParsingException {
5453
@Override
5554
public Integer getLikeCount() throws ParsingException {
5655
try {
57-
return (Integer) JsonUtils.getValue(json, "likeCount");
56+
return JsonUtils.getNumber(json, "likeCount").intValue();
5857
} catch (Exception e) {
5958
throw new ParsingException("Could not get like count", e);
6059
}
@@ -63,7 +62,7 @@ public Integer getLikeCount() throws ParsingException {
6362
@Override
6463
public String getCommentText() throws ParsingException {
6564
try {
66-
return YoutubeCommentsExtractor.getYoutubeText((JsonObject) JsonUtils.getValue(json, "contentText"));
65+
return YoutubeCommentsExtractor.getYoutubeText(JsonUtils.getObject(json, "contentText"));
6766
} catch (Exception e) {
6867
throw new ParsingException("Could not get comment text", e);
6968
}
@@ -72,7 +71,7 @@ public String getCommentText() throws ParsingException {
7271
@Override
7372
public String getCommentId() throws ParsingException {
7473
try {
75-
return (String) JsonUtils.getValue(json, "commentId");
74+
return JsonUtils.getString(json, "commentId");
7675
} catch (Exception e) {
7776
throw new ParsingException("Could not get comment id", e);
7877
}
@@ -81,8 +80,8 @@ public String getCommentId() throws ParsingException {
8180
@Override
8281
public String getAuthorThumbnail() throws ParsingException {
8382
try {
84-
JsonArray arr = (JsonArray) JsonUtils.getValue(json, "authorThumbnail.thumbnails");
85-
return (String) JsonUtils.getValue(arr.getObject(2), "url");
83+
JsonArray arr = JsonUtils.getArray(json, "authorThumbnail.thumbnails");
84+
return JsonUtils.getString(arr.getObject(2), "url");
8685
} catch (Exception e) {
8786
throw new ParsingException("Could not get author thumbnail", e);
8887
}
@@ -91,7 +90,7 @@ public String getAuthorThumbnail() throws ParsingException {
9190
@Override
9291
public String getAuthorName() throws ParsingException {
9392
try {
94-
return YoutubeCommentsExtractor.getYoutubeText((JsonObject) JsonUtils.getValue(json, "authorText"));
93+
return YoutubeCommentsExtractor.getYoutubeText(JsonUtils.getObject(json, "authorText"));
9594
} catch (Exception e) {
9695
throw new ParsingException("Could not get author name", e);
9796
}
@@ -100,8 +99,7 @@ public String getAuthorName() throws ParsingException {
10099
@Override
101100
public String getAuthorEndpoint() throws ParsingException {
102101
try {
103-
return "https://youtube.com"
104-
+ (String) JsonUtils.getValue(json, "authorEndpoint.browseEndpoint.canonicalBaseUrl");
102+
return "https://youtube.com" + JsonUtils.getString(json, "authorEndpoint.browseEndpoint.canonicalBaseUrl");
105103
} catch (Exception e) {
106104
throw new ParsingException("Could not get author endpoint", e);
107105
}

extractor/src/main/java/org/schabi/newpipe/extractor/utils/JsonUtils.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,45 @@ public static Object getValue(@Nonnull JsonObject object, @Nonnull String path)
2828
return result;
2929
}
3030

31+
@Nonnull
32+
public static String getString(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException{
33+
Object value = getValue(object, path);
34+
if(value instanceof String) {
35+
return (String) value;
36+
}else {
37+
throw new ParsingException("Unable to get " + path);
38+
}
39+
}
40+
41+
@Nonnull
42+
public static Number getNumber(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException{
43+
Object value = getValue(object, path);
44+
if(value instanceof Number) {
45+
return (Number) value;
46+
}else {
47+
throw new ParsingException("Unable to get " + path);
48+
}
49+
}
50+
51+
@Nonnull
52+
public static JsonObject getObject(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException{
53+
Object value = getValue(object, path);
54+
if(value instanceof JsonObject) {
55+
return (JsonObject) value;
56+
}else {
57+
throw new ParsingException("Unable to get " + path);
58+
}
59+
}
60+
61+
@Nonnull
62+
public static JsonArray getArray(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException{
63+
Object value = getValue(object, path);
64+
if(value instanceof JsonArray) {
65+
return (JsonArray) value;
66+
}else {
67+
throw new ParsingException("Unable to get " + path);
68+
}
69+
}
3170

3271
@Nonnull
3372
public static List<Object> getValues(@Nonnull JsonArray array, @Nonnull String path) throws ParsingException {

0 commit comments

Comments
 (0)