mirror of
https://github.com/TeamNewPipe/NewPipeExtractor.git
synced 2024-12-14 14:20:33 +05:30
removed unchecked cast
This commit is contained in:
parent
e85958b180
commit
99a0134b1c
@ -62,7 +62,7 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
|
|||||||
|
|
||||||
JsonArray arr;
|
JsonArray arr;
|
||||||
try {
|
try {
|
||||||
arr = (JsonArray) JsonUtils.getValue(ajaxJson, "response.continuationContents.commentSectionContinuation.continuations");
|
arr = JsonUtils.getArray(ajaxJson, "response.continuationContents.commentSectionContinuation.continuations");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
@ -71,7 +71,7 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
|
|||||||
}
|
}
|
||||||
String continuation;
|
String continuation;
|
||||||
try {
|
try {
|
||||||
continuation = (String) JsonUtils.getValue(arr.getObject(0), "nextContinuationData.continuation");
|
continuation = JsonUtils.getString(arr.getObject(0), "nextContinuationData.continuation");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
@ -111,7 +111,7 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
|
|||||||
|
|
||||||
JsonArray contents;
|
JsonArray contents;
|
||||||
try {
|
try {
|
||||||
contents = (JsonArray) JsonUtils.getValue(ajaxJson, "response.continuationContents.commentSectionContinuation.items");
|
contents = JsonUtils.getArray(ajaxJson, "response.continuationContents.commentSectionContinuation.items");
|
||||||
}catch(Exception e) {
|
}catch(Exception e) {
|
||||||
//no comments
|
//no comments
|
||||||
return;
|
return;
|
||||||
@ -135,7 +135,7 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
|
|||||||
private void fetchTitle(JsonArray contents) {
|
private void fetchTitle(JsonArray contents) {
|
||||||
if(null == title) {
|
if(null == title) {
|
||||||
try {
|
try {
|
||||||
title = getYoutubeText((JsonObject) JsonUtils.getValue(contents.getObject(0), "commentThreadRenderer.commentTargetTitle"));
|
title = getYoutubeText(JsonUtils.getObject(contents.getObject(0), "commentThreadRenderer.commentTargetTitle"));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
title = "Youtube Comments";
|
title = "Youtube Comments";
|
||||||
}
|
}
|
||||||
@ -196,13 +196,13 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
|
|||||||
|
|
||||||
public static String getYoutubeText(@Nonnull JsonObject object) throws ParsingException {
|
public static String getYoutubeText(@Nonnull JsonObject object) throws ParsingException {
|
||||||
try {
|
try {
|
||||||
return (String) JsonUtils.getValue(object, "simpleText");
|
return JsonUtils.getString(object, "simpleText");
|
||||||
} catch (Exception e1) {
|
} catch (Exception e1) {
|
||||||
try {
|
try {
|
||||||
JsonArray arr = (JsonArray) JsonUtils.getValue(object, "runs");
|
JsonArray arr = JsonUtils.getArray(object, "runs");
|
||||||
String result = "";
|
String result = "";
|
||||||
for(int i=0; i<arr.size();i++) {
|
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;
|
return result;
|
||||||
} catch (Exception e2) {
|
} catch (Exception e2) {
|
||||||
|
@ -7,7 +7,6 @@ import org.schabi.newpipe.extractor.utils.JsonUtils;
|
|||||||
import com.grack.nanojson.JsonArray;
|
import com.grack.nanojson.JsonArray;
|
||||||
import com.grack.nanojson.JsonObject;
|
import com.grack.nanojson.JsonObject;
|
||||||
|
|
||||||
|
|
||||||
public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtractor {
|
public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtractor {
|
||||||
|
|
||||||
private final JsonObject json;
|
private final JsonObject json;
|
||||||
@ -26,8 +25,8 @@ public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtract
|
|||||||
@Override
|
@Override
|
||||||
public String getThumbnailUrl() throws ParsingException {
|
public String getThumbnailUrl() throws ParsingException {
|
||||||
try {
|
try {
|
||||||
JsonArray arr = (JsonArray) JsonUtils.getValue(json, "authorThumbnail.thumbnails");
|
JsonArray arr = JsonUtils.getArray(json, "authorThumbnail.thumbnails");
|
||||||
return (String) JsonUtils.getValue(arr.getObject(2), "url");
|
return JsonUtils.getString(arr.getObject(2), "url");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new ParsingException("Could not get thumbnail url", e);
|
throw new ParsingException("Could not get thumbnail url", e);
|
||||||
}
|
}
|
||||||
@ -36,7 +35,7 @@ public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtract
|
|||||||
@Override
|
@Override
|
||||||
public String getName() throws ParsingException {
|
public String getName() throws ParsingException {
|
||||||
try {
|
try {
|
||||||
return YoutubeCommentsExtractor.getYoutubeText((JsonObject) JsonUtils.getValue(json, "authorText"));
|
return YoutubeCommentsExtractor.getYoutubeText(JsonUtils.getObject(json, "authorText"));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new ParsingException("Could not get author name", e);
|
throw new ParsingException("Could not get author name", e);
|
||||||
}
|
}
|
||||||
@ -45,7 +44,7 @@ public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtract
|
|||||||
@Override
|
@Override
|
||||||
public String getPublishedTime() throws ParsingException {
|
public String getPublishedTime() throws ParsingException {
|
||||||
try {
|
try {
|
||||||
return YoutubeCommentsExtractor.getYoutubeText((JsonObject) JsonUtils.getValue(json, "publishedTimeText"));
|
return YoutubeCommentsExtractor.getYoutubeText(JsonUtils.getObject(json, "publishedTimeText"));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new ParsingException("Could not get publishedTimeText", e);
|
throw new ParsingException("Could not get publishedTimeText", e);
|
||||||
}
|
}
|
||||||
@ -54,7 +53,7 @@ public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtract
|
|||||||
@Override
|
@Override
|
||||||
public Integer getLikeCount() throws ParsingException {
|
public Integer getLikeCount() throws ParsingException {
|
||||||
try {
|
try {
|
||||||
return (Integer) JsonUtils.getValue(json, "likeCount");
|
return JsonUtils.getNumber(json, "likeCount").intValue();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new ParsingException("Could not get like count", e);
|
throw new ParsingException("Could not get like count", e);
|
||||||
}
|
}
|
||||||
@ -63,7 +62,7 @@ public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtract
|
|||||||
@Override
|
@Override
|
||||||
public String getCommentText() throws ParsingException {
|
public String getCommentText() throws ParsingException {
|
||||||
try {
|
try {
|
||||||
return YoutubeCommentsExtractor.getYoutubeText((JsonObject) JsonUtils.getValue(json, "contentText"));
|
return YoutubeCommentsExtractor.getYoutubeText(JsonUtils.getObject(json, "contentText"));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new ParsingException("Could not get comment text", e);
|
throw new ParsingException("Could not get comment text", e);
|
||||||
}
|
}
|
||||||
@ -72,7 +71,7 @@ public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtract
|
|||||||
@Override
|
@Override
|
||||||
public String getCommentId() throws ParsingException {
|
public String getCommentId() throws ParsingException {
|
||||||
try {
|
try {
|
||||||
return (String) JsonUtils.getValue(json, "commentId");
|
return JsonUtils.getString(json, "commentId");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new ParsingException("Could not get comment id", e);
|
throw new ParsingException("Could not get comment id", e);
|
||||||
}
|
}
|
||||||
@ -81,8 +80,8 @@ public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtract
|
|||||||
@Override
|
@Override
|
||||||
public String getAuthorThumbnail() throws ParsingException {
|
public String getAuthorThumbnail() throws ParsingException {
|
||||||
try {
|
try {
|
||||||
JsonArray arr = (JsonArray) JsonUtils.getValue(json, "authorThumbnail.thumbnails");
|
JsonArray arr = JsonUtils.getArray(json, "authorThumbnail.thumbnails");
|
||||||
return (String) JsonUtils.getValue(arr.getObject(2), "url");
|
return JsonUtils.getString(arr.getObject(2), "url");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new ParsingException("Could not get author thumbnail", e);
|
throw new ParsingException("Could not get author thumbnail", e);
|
||||||
}
|
}
|
||||||
@ -91,7 +90,7 @@ public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtract
|
|||||||
@Override
|
@Override
|
||||||
public String getAuthorName() throws ParsingException {
|
public String getAuthorName() throws ParsingException {
|
||||||
try {
|
try {
|
||||||
return YoutubeCommentsExtractor.getYoutubeText((JsonObject) JsonUtils.getValue(json, "authorText"));
|
return YoutubeCommentsExtractor.getYoutubeText(JsonUtils.getObject(json, "authorText"));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new ParsingException("Could not get author name", e);
|
throw new ParsingException("Could not get author name", e);
|
||||||
}
|
}
|
||||||
@ -100,8 +99,7 @@ public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtract
|
|||||||
@Override
|
@Override
|
||||||
public String getAuthorEndpoint() throws ParsingException {
|
public String getAuthorEndpoint() throws ParsingException {
|
||||||
try {
|
try {
|
||||||
return "https://youtube.com"
|
return "https://youtube.com" + JsonUtils.getString(json, "authorEndpoint.browseEndpoint.canonicalBaseUrl");
|
||||||
+ (String) JsonUtils.getValue(json, "authorEndpoint.browseEndpoint.canonicalBaseUrl");
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new ParsingException("Could not get author endpoint", e);
|
throw new ParsingException("Could not get author endpoint", e);
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,45 @@ public class JsonUtils {
|
|||||||
return result;
|
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
|
@Nonnull
|
||||||
public static List<Object> getValues(@Nonnull JsonArray array, @Nonnull String path) throws ParsingException {
|
public static List<Object> getValues(@Nonnull JsonArray array, @Nonnull String path) throws ParsingException {
|
||||||
|
Loading…
Reference in New Issue
Block a user