removed unchecked cast

This commit is contained in:
Ritvik Saraf 2018-10-19 19:33:36 +05:30
parent e85958b180
commit 99a0134b1c
3 changed files with 57 additions and 20 deletions

View file

@ -62,7 +62,7 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
JsonArray arr;
try {
arr = (JsonArray) JsonUtils.getValue(ajaxJson, "response.continuationContents.commentSectionContinuation.continuations");
arr = JsonUtils.getArray(ajaxJson, "response.continuationContents.commentSectionContinuation.continuations");
} catch (Exception e) {
return "";
}
@ -71,7 +71,7 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
}
String continuation;
try {
continuation = (String) JsonUtils.getValue(arr.getObject(0), "nextContinuationData.continuation");
continuation = JsonUtils.getString(arr.getObject(0), "nextContinuationData.continuation");
} catch (Exception e) {
return "";
}
@ -111,7 +111,7 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
JsonArray contents;
try {
contents = (JsonArray) JsonUtils.getValue(ajaxJson, "response.continuationContents.commentSectionContinuation.items");
contents = JsonUtils.getArray(ajaxJson, "response.continuationContents.commentSectionContinuation.items");
}catch(Exception e) {
//no comments
return;
@ -135,7 +135,7 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
private void fetchTitle(JsonArray contents) {
if(null == title) {
try {
title = getYoutubeText((JsonObject) JsonUtils.getValue(contents.getObject(0), "commentThreadRenderer.commentTargetTitle"));
title = getYoutubeText(JsonUtils.getObject(contents.getObject(0), "commentThreadRenderer.commentTargetTitle"));
} catch (Exception e) {
title = "Youtube Comments";
}
@ -196,13 +196,13 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
public static String getYoutubeText(@Nonnull JsonObject object) throws ParsingException {
try {
return (String) JsonUtils.getValue(object, "simpleText");
return JsonUtils.getString(object, "simpleText");
} catch (Exception e1) {
try {
JsonArray arr = (JsonArray) JsonUtils.getValue(object, "runs");
JsonArray arr = JsonUtils.getArray(object, "runs");
String result = "";
for(int i=0; i<arr.size();i++) {
result = result + (String) JsonUtils.getValue(arr.getObject(i), "text");
result = result + JsonUtils.getString(arr.getObject(i), "text");
}
return result;
} catch (Exception e2) {

View file

@ -7,7 +7,6 @@ import org.schabi.newpipe.extractor.utils.JsonUtils;
import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;
public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtractor {
private final JsonObject json;
@ -26,8 +25,8 @@ public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtract
@Override
public String getThumbnailUrl() throws ParsingException {
try {
JsonArray arr = (JsonArray) JsonUtils.getValue(json, "authorThumbnail.thumbnails");
return (String) JsonUtils.getValue(arr.getObject(2), "url");
JsonArray arr = JsonUtils.getArray(json, "authorThumbnail.thumbnails");
return JsonUtils.getString(arr.getObject(2), "url");
} catch (Exception e) {
throw new ParsingException("Could not get thumbnail url", e);
}
@ -36,7 +35,7 @@ public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtract
@Override
public String getName() throws ParsingException {
try {
return YoutubeCommentsExtractor.getYoutubeText((JsonObject) JsonUtils.getValue(json, "authorText"));
return YoutubeCommentsExtractor.getYoutubeText(JsonUtils.getObject(json, "authorText"));
} catch (Exception e) {
throw new ParsingException("Could not get author name", e);
}
@ -45,7 +44,7 @@ public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtract
@Override
public String getPublishedTime() throws ParsingException {
try {
return YoutubeCommentsExtractor.getYoutubeText((JsonObject) JsonUtils.getValue(json, "publishedTimeText"));
return YoutubeCommentsExtractor.getYoutubeText(JsonUtils.getObject(json, "publishedTimeText"));
} catch (Exception e) {
throw new ParsingException("Could not get publishedTimeText", e);
}
@ -54,7 +53,7 @@ public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtract
@Override
public Integer getLikeCount() throws ParsingException {
try {
return (Integer) JsonUtils.getValue(json, "likeCount");
return JsonUtils.getNumber(json, "likeCount").intValue();
} catch (Exception e) {
throw new ParsingException("Could not get like count", e);
}
@ -63,7 +62,7 @@ public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtract
@Override
public String getCommentText() throws ParsingException {
try {
return YoutubeCommentsExtractor.getYoutubeText((JsonObject) JsonUtils.getValue(json, "contentText"));
return YoutubeCommentsExtractor.getYoutubeText(JsonUtils.getObject(json, "contentText"));
} catch (Exception e) {
throw new ParsingException("Could not get comment text", e);
}
@ -72,7 +71,7 @@ public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtract
@Override
public String getCommentId() throws ParsingException {
try {
return (String) JsonUtils.getValue(json, "commentId");
return JsonUtils.getString(json, "commentId");
} catch (Exception e) {
throw new ParsingException("Could not get comment id", e);
}
@ -81,8 +80,8 @@ public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtract
@Override
public String getAuthorThumbnail() throws ParsingException {
try {
JsonArray arr = (JsonArray) JsonUtils.getValue(json, "authorThumbnail.thumbnails");
return (String) JsonUtils.getValue(arr.getObject(2), "url");
JsonArray arr = JsonUtils.getArray(json, "authorThumbnail.thumbnails");
return JsonUtils.getString(arr.getObject(2), "url");
} catch (Exception e) {
throw new ParsingException("Could not get author thumbnail", e);
}
@ -91,7 +90,7 @@ public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtract
@Override
public String getAuthorName() throws ParsingException {
try {
return YoutubeCommentsExtractor.getYoutubeText((JsonObject) JsonUtils.getValue(json, "authorText"));
return YoutubeCommentsExtractor.getYoutubeText(JsonUtils.getObject(json, "authorText"));
} catch (Exception e) {
throw new ParsingException("Could not get author name", e);
}
@ -100,8 +99,7 @@ public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtract
@Override
public String getAuthorEndpoint() throws ParsingException {
try {
return "https://youtube.com"
+ (String) JsonUtils.getValue(json, "authorEndpoint.browseEndpoint.canonicalBaseUrl");
return "https://youtube.com" + JsonUtils.getString(json, "authorEndpoint.browseEndpoint.canonicalBaseUrl");
} catch (Exception e) {
throw new ParsingException("Could not get author endpoint", e);
}

View file

@ -28,6 +28,45 @@ public class JsonUtils {
return result;
}
@Nonnull
public static String getString(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException{
Object value = getValue(object, path);
if(value instanceof String) {
return (String) value;
}else {
throw new ParsingException("Unable to get " + path);
}
}
@Nonnull
public static Number getNumber(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException{
Object value = getValue(object, path);
if(value instanceof Number) {
return (Number) value;
}else {
throw new ParsingException("Unable to get " + path);
}
}
@Nonnull
public static JsonObject getObject(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException{
Object value = getValue(object, path);
if(value instanceof JsonObject) {
return (JsonObject) value;
}else {
throw new ParsingException("Unable to get " + path);
}
}
@Nonnull
public static JsonArray getArray(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException{
Object value = getValue(object, path);
if(value instanceof JsonArray) {
return (JsonArray) value;
}else {
throw new ParsingException("Unable to get " + path);
}
}
@Nonnull
public static List<Object> getValues(@Nonnull JsonArray array, @Nonnull String path) throws ParsingException {