From edcab6e9ecd13e7ea5690e53bf0b4b06e84e9af2 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sat, 4 Jul 2020 18:06:31 +0200 Subject: [PATCH] CLean up --- .../fedilabtube/client/PeertubeAPI.java | 168 +++++++++--------- .../fedilab/fedilabtube/helper/Helper.java | 1 + 2 files changed, 85 insertions(+), 84 deletions(-) diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java b/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java index 5a91c46..1d3ff17 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java @@ -120,10 +120,10 @@ public class PeertubeAPI { private static PeertubeNotification parsePeertubeNotifications(JSONObject resobj) { PeertubeNotification peertubeNotification = new PeertubeNotification(); try { - peertubeNotification.setId(resobj.get("id").toString()); + peertubeNotification.setId(resobj.getString("id")); peertubeNotification.setType(resobj.getInt("type")); - peertubeNotification.setUpdatedAt(Helper.mstStringToDate(resobj.get("updatedAt").toString())); - peertubeNotification.setCreatedAt(Helper.mstStringToDate(resobj.get("createdAt").toString())); + peertubeNotification.setUpdatedAt(Helper.mstStringToDate(resobj.getString("updatedAt"))); + peertubeNotification.setCreatedAt(Helper.mstStringToDate(resobj.getString("createdAt"))); peertubeNotification.setRead(resobj.getBoolean("read")); if (resobj.has("comment")) { @@ -132,42 +132,42 @@ public class PeertubeAPI { if (comment.has("account")) { JSONObject account = comment.getJSONObject("account"); PeertubeAccountNotification peertubeAccountNotification = new PeertubeAccountNotification(); - peertubeAccountNotification.setDisplayName(account.get("displayName").toString()); - peertubeAccountNotification.setName(account.get("name").toString()); - peertubeAccountNotification.setId(account.get("id").toString()); + peertubeAccountNotification.setDisplayName(account.getString("displayName")); + peertubeAccountNotification.setName(account.getString("name")); + peertubeAccountNotification.setId(account.getString("id")); if (account.has("host")) { - peertubeAccountNotification.setHost(account.get("host").toString()); + peertubeAccountNotification.setHost(account.getString("host")); } - peertubeAccountNotification.setAvatar(account.getJSONObject("avatar").get("path").toString()); + peertubeAccountNotification.setAvatar(account.getJSONObject("avatar").getString("path")); peertubeComment.setPeertubeAccountNotification(peertubeAccountNotification); } if (comment.has("video")) { JSONObject video = comment.getJSONObject("video"); PeertubeVideoNotification peertubeVideoNotification = new PeertubeVideoNotification(); - peertubeVideoNotification.setUuid(video.get("uuid").toString()); - peertubeVideoNotification.setName(video.get("name").toString()); - peertubeVideoNotification.setId(video.get("id").toString()); + peertubeVideoNotification.setUuid(video.getString("uuid")); + peertubeVideoNotification.setName(video.getString("name")); + peertubeVideoNotification.setId(video.getString("id")); peertubeComment.setPeertubeVideoNotification(peertubeVideoNotification); } - peertubeComment.setId(comment.get("id").toString()); - peertubeComment.setThreadId(comment.get("threadId").toString()); + peertubeComment.setId(comment.getString("id")); + peertubeComment.setThreadId(comment.getString("threadId")); peertubeNotification.setPeertubeComment(peertubeComment); } if (resobj.has("video")) { PeertubeVideoNotification peertubeVideoNotification = new PeertubeVideoNotification(); JSONObject video = resobj.getJSONObject("video"); - peertubeVideoNotification.setUuid(video.get("uuid").toString()); - peertubeVideoNotification.setName(video.get("name").toString()); - peertubeVideoNotification.setId(video.get("id").toString()); + peertubeVideoNotification.setUuid(video.getString("uuid")); + peertubeVideoNotification.setName(video.getString("name")); + peertubeVideoNotification.setId(video.getString("id")); if (video.has("channel")) { PeertubeAccountNotification peertubeAccountNotification = new PeertubeAccountNotification(); JSONObject channel = video.getJSONObject("channel"); - peertubeAccountNotification.setDisplayName(channel.get("displayName").toString()); - peertubeAccountNotification.setName(channel.get("name").toString()); - peertubeAccountNotification.setId(channel.get("id").toString()); + peertubeAccountNotification.setDisplayName(channel.getString("displayName")); + peertubeAccountNotification.setName(channel.getString("name")); + peertubeAccountNotification.setId(channel.getString("id")); if (channel.has("avatar")) { - peertubeAccountNotification.setAvatar(channel.getJSONObject("avatar").get("path").toString()); + peertubeAccountNotification.setAvatar(channel.getJSONObject("avatar").getString("path")); } peertubeVideoNotification.setPeertubeAccountNotification(peertubeAccountNotification); } @@ -182,29 +182,29 @@ public class PeertubeAPI { JSONObject following = actorFollow.getJSONObject("following"); PeertubeAccountNotification peertubeAccountNotification = new PeertubeAccountNotification(); - peertubeAccountNotification.setDisplayName(follower.get("displayName").toString()); - peertubeAccountNotification.setName(follower.get("name").toString()); - peertubeAccountNotification.setId(follower.get("id").toString()); + peertubeAccountNotification.setDisplayName(follower.getString("displayName")); + peertubeAccountNotification.setName(follower.getString("name")); + peertubeAccountNotification.setId(follower.getString("id")); if (follower.has("host")) { - peertubeAccountNotification.setHost(follower.get("host").toString()); + peertubeAccountNotification.setHost(follower.getString("host")); } if (follower.has("avatar")) { - peertubeAccountNotification.setAvatar(follower.getJSONObject("avatar").get("path").toString()); + peertubeAccountNotification.setAvatar(follower.getJSONObject("avatar").getString("path")); } peertubeActorFollow.setFollower(peertubeAccountNotification); PeertubeAccountNotification peertubeAccounFollowingNotification = new PeertubeAccountNotification(); - peertubeAccounFollowingNotification.setDisplayName(following.get("displayName").toString()); - peertubeAccounFollowingNotification.setName(following.get("name").toString()); + peertubeAccounFollowingNotification.setDisplayName(following.getString("displayName")); + peertubeAccounFollowingNotification.setName(following.getString("name")); try { - peertubeAccounFollowingNotification.setId(following.get("id").toString()); + peertubeAccounFollowingNotification.setId(following.getString("id")); } catch (Exception ignored) { } if (following.has("avatar")) { - peertubeAccounFollowingNotification.setAvatar(following.getJSONObject("avatar").get("path").toString()); + peertubeAccounFollowingNotification.setAvatar(following.getJSONObject("avatar").getString("path")); } peertubeActorFollow.setFollowing(peertubeAccounFollowingNotification); - peertubeActorFollow.setId(actorFollow.get("id").toString()); + peertubeActorFollow.setId(actorFollow.getString("id")); peertubeNotification.setPeertubeActorFollow(peertubeActorFollow); } @@ -232,31 +232,31 @@ public class PeertubeAPI { } } try { - peertube.setId(resobj.get("id").toString()); + peertube.setId(resobj.getString("id")); peertube.setCache(resobj); - peertube.setUuid(resobj.get("uuid").toString()); - peertube.setName(resobj.get("name").toString()); - peertube.setDescription(resobj.get("description").toString()); - peertube.setEmbedPath(resobj.get("embedPath").toString()); - peertube.setPreviewPath(resobj.get("previewPath").toString()); - peertube.setThumbnailPath(resobj.get("thumbnailPath").toString()); + peertube.setUuid(resobj.getString("uuid")); + peertube.setName(resobj.getString("name")); + peertube.setDescription(resobj.getString("description")); + peertube.setEmbedPath(resobj.getString("embedPath")); + peertube.setPreviewPath(resobj.getString("previewPath")); + peertube.setThumbnailPath(resobj.getString("thumbnailPath")); peertube.setAccount(parseAccountResponsePeertube(resobj.getJSONObject("account"))); try { peertube.setChannel(parseAccountResponsePeertube(resobj.getJSONObject("channel"))); } catch (Exception ignored) { } - peertube.setView(Integer.parseInt(resobj.get("views").toString())); - peertube.setLike(Integer.parseInt(resobj.get("likes").toString())); - peertube.setDislike(Integer.parseInt(resobj.get("dislikes").toString())); - peertube.setDuration(Integer.parseInt(resobj.get("duration").toString())); - peertube.setSensitive(Boolean.parseBoolean(resobj.get("nsfw").toString())); + peertube.setView(resobj.getInt("views")); + peertube.setLike(resobj.getInt("likes")); + peertube.setDislike(resobj.getInt("dislikes")); + peertube.setDuration(resobj.getInt("duration")); + peertube.setSensitive(resobj.getBoolean("nsfw")); try { - peertube.setCommentsEnabled(Boolean.parseBoolean(resobj.get("commentsEnabled").toString())); + peertube.setCommentsEnabled(resobj.getBoolean("commentsEnabled")); } catch (Exception ignored) { } try { - peertube.setCreated_at(Helper.mstStringToDate(resobj.get("createdAt").toString())); + peertube.setCreated_at(Helper.mstStringToDate(resobj.getString("createdAt"))); } catch (ParseException e) { e.printStackTrace(); } @@ -266,10 +266,10 @@ public class PeertubeAPI { LinkedHashMap category = new LinkedHashMap<>(); LinkedHashMap license = new LinkedHashMap<>(); LinkedHashMap privacy = new LinkedHashMap<>(); - category.put(resobj.getJSONObject("category").getInt("id"), resobj.getJSONObject("category").get("label").toString()); - license.put(resobj.getJSONObject("licence").getInt("id"), resobj.getJSONObject("licence").get("label").toString()); - privacy.put(resobj.getJSONObject("privacy").getInt("id"), resobj.getJSONObject("privacy").get("label").toString()); - langue.put(resobj.getJSONObject("language").get("id").toString(), resobj.getJSONObject("language").get("label").toString()); + category.put(resobj.getJSONObject("category").getInt("id"), resobj.getJSONObject("category").getString("label")); + license.put(resobj.getJSONObject("licence").getInt("id"), resobj.getJSONObject("licence").getString("label")); + privacy.put(resobj.getJSONObject("privacy").getInt("id"), resobj.getJSONObject("privacy").getString("label")); + langue.put(resobj.getJSONObject("language").getString("id"), resobj.getJSONObject("language").getString("label")); peertube.setCategory(category); peertube.setLicense(license); @@ -293,21 +293,21 @@ public class PeertubeAPI { private static Peertube parseSinglePeertube(String instance, JSONObject resobj) { Peertube peertube = new Peertube(); try { - peertube.setId(resobj.get("id").toString()); - peertube.setUuid(resobj.get("uuid").toString()); - peertube.setName(resobj.get("name").toString()); + peertube.setId(resobj.getString("id")); + peertube.setUuid(resobj.getString("uuid")); + peertube.setName(resobj.getString("name")); peertube.setCache(resobj); peertube.setInstance(instance); - peertube.setHost(resobj.getJSONObject("account").get("host").toString()); - peertube.setDescription(resobj.get("description").toString()); - peertube.setEmbedPath(resobj.get("embedPath").toString()); - peertube.setPreviewPath(resobj.get("previewPath").toString()); - peertube.setThumbnailPath(resobj.get("thumbnailPath").toString()); - peertube.setView(Integer.parseInt(resobj.get("views").toString())); - peertube.setLike(Integer.parseInt(resobj.get("likes").toString())); - peertube.setCommentsEnabled(Boolean.parseBoolean(resobj.get("commentsEnabled").toString())); - peertube.setDislike(Integer.parseInt(resobj.get("dislikes").toString())); - peertube.setDuration(Integer.parseInt(resobj.get("duration").toString())); + peertube.setHost(resobj.getJSONObject("account").getString("host")); + peertube.setDescription(resobj.getString("description")); + peertube.setEmbedPath(resobj.getString("embedPath")); + peertube.setPreviewPath(resobj.getString("previewPath")); + peertube.setThumbnailPath(resobj.getString("thumbnailPath")); + peertube.setView(resobj.getInt("views")); + peertube.setLike(resobj.getInt("likes")); + peertube.setCommentsEnabled(resobj.getBoolean("commentsEnabled")); + peertube.setDislike(resobj.getInt("dislikes")); + peertube.setDuration(resobj.getInt("duration")); peertube.setAccount(parseAccountResponsePeertube(resobj.getJSONObject("account"))); List tags = new ArrayList<>(); try { @@ -323,15 +323,15 @@ public class PeertubeAPI { peertube.setChannel(parseAccountResponsePeertube(resobj.getJSONObject("channel"))); } catch (Exception ignored) { } - peertube.setSensitive(Boolean.parseBoolean(resobj.get("nsfw").toString())); + peertube.setSensitive(resobj.getBoolean("nsfw")); try { - peertube.setCreated_at(Helper.mstStringToDate(resobj.get("createdAt").toString())); + peertube.setCreated_at(Helper.mstStringToDate(resobj.getString("createdAt"))); } catch (ParseException e) { e.printStackTrace(); } try { - peertube.setCreated_at(Helper.mstStringToDate(resobj.get("createdAt").toString())); + peertube.setCreated_at(Helper.mstStringToDate(resobj.getString("createdAt"))); } catch (ParseException e) { e.printStackTrace(); } @@ -341,7 +341,7 @@ public class PeertubeAPI { for (int j = 0; j < files.length(); j++) { JSONObject attObj = files.getJSONObject(j); - resolutions.add(attObj.getJSONObject("resolution").get("id").toString()); + resolutions.add(attObj.getJSONObject("resolution").getString("id")); } peertube.setResolution(resolutions); peertube.setStreamService(true); @@ -349,7 +349,7 @@ public class PeertubeAPI { JSONArray files = resobj.getJSONArray("files"); for (int j = 0; j < files.length(); j++) { JSONObject attObj = files.getJSONObject(j); - resolutions.add(attObj.getJSONObject("resolution").get("id").toString()); + resolutions.add(attObj.getJSONObject("resolution").getString("id")); } peertube.setResolution(resolutions); peertube.setStreamService(false); @@ -359,10 +359,10 @@ public class PeertubeAPI { LinkedHashMap category = new LinkedHashMap<>(); LinkedHashMap license = new LinkedHashMap<>(); LinkedHashMap privacy = new LinkedHashMap<>(); - category.put(resobj.getJSONObject("category").getInt("id"), resobj.getJSONObject("category").get("label").toString()); - license.put(resobj.getJSONObject("licence").getInt("id"), resobj.getJSONObject("licence").get("label").toString()); - privacy.put(resobj.getJSONObject("privacy").getInt("id"), resobj.getJSONObject("privacy").get("label").toString()); - langue.put(resobj.getJSONObject("language").get("id").toString(), resobj.getJSONObject("language").get("label").toString()); + category.put(resobj.getJSONObject("category").getInt("id"), resobj.getJSONObject("category").getString("label")); + license.put(resobj.getJSONObject("licence").getInt("id"), resobj.getJSONObject("licence").getString("label")); + privacy.put(resobj.getJSONObject("privacy").getInt("id"), resobj.getJSONObject("privacy").getString("label")); + langue.put(resobj.getJSONObject("language").getString("id"), resobj.getJSONObject("language").get("label").toString()); peertube.setCategory(category); peertube.setLicense(license); @@ -520,7 +520,7 @@ public class PeertubeAPI { account.setDisplay_name(resobj.get("displayName").toString()); account.setHost(resobj.get("host").toString()); if (resobj.has("createdAt")) - account.setCreated_at(Helper.mstStringToDate(resobj.get("createdAt").toString())); + account.setCreated_at(Helper.mstStringToDate(resobj.getString("createdAt"))); else account.setCreated_at(new Date()); @@ -534,17 +534,17 @@ public class PeertubeAPI { account.setFollowing_count(0); account.setStatuses_count(0); if (resobj.has("description")) - account.setNote(resobj.get("description").toString()); + account.setNote(resobj.getString("description")); else account.setNote(""); - account.setUrl(resobj.get("url").toString()); + account.setUrl(resobj.getString("url")); account.setSocial("PEERTUBE"); - if (resobj.has("avatar") && !resobj.get("avatar").toString().equals("null")) { + if (resobj.has("avatar") && !resobj.getString("avatar").equals("null")) { account.setAvatar("https://" + instance + resobj.getJSONObject("avatar").get("path")); account.setAvatar_static("https://" + instance + resobj.getJSONObject("avatar").get("path")); } else account.setAvatar(null); - account.setAvatar_static(resobj.get("avatar").toString()); + account.setAvatar_static(resobj.getString("avatar")); } catch (JSONException ignored) { } catch (ParseException e) { e.printStackTrace(); @@ -806,9 +806,9 @@ public class PeertubeAPI { try { String response = new HttpsConnection(context).post(getAbsoluteUrl("/users/token"), 60, params, null); JSONObject resobj = new JSONObject(response); - String token = resobj.get("access_token").toString(); + String token = resobj.getString("access_token"); if (resobj.has("refresh_token")) - refresh_token = resobj.get("refresh_token").toString(); + refresh_token = resobj.getString("refresh_token"); newValues.put("access_token", token); newValues.put("refresh_token", refresh_token); } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException | HttpsConnection.HttpsConnectionException e) { @@ -1360,7 +1360,7 @@ public class PeertubeAPI { try { HttpsConnection httpsConnection = new HttpsConnection(context); String response = httpsConnection.get(getAbsoluteUrl(String.format("/users/me/videos/%s/rating", id)), 60, null, prefKeyOauthTokenT); - return new JSONObject(response).get("rating").toString(); + return new JSONObject(response).getString("rating"); } catch (HttpsConnection.HttpsConnectionException e) { setError(e.getStatusCode(), e); } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { @@ -1741,11 +1741,11 @@ public class PeertubeAPI { Instance instance = new Instance(); try { - instance.setUri(resobj.get("uri").toString()); - instance.setTitle(resobj.get("title").toString()); - instance.setDescription(resobj.get("description").toString()); - instance.setEmail(resobj.get("email").toString()); - instance.setVersion(resobj.get("version").toString()); + instance.setUri(resobj.getString("uri")); + instance.setTitle(resobj.getString("title")); + instance.setDescription(resobj.getString("description")); + instance.setEmail(resobj.getString("email")); + instance.setVersion(resobj.getString("version")); } catch (JSONException e) { setDefaultError(e); } @@ -1802,7 +1802,7 @@ public class PeertubeAPI { String message = statusCode + " - " + error.getMessage(); try { JSONObject jsonObject = new JSONObject(Objects.requireNonNull(error.getMessage())); - String errorM = jsonObject.get("error").toString(); + String errorM = jsonObject.getString("error"); message = "Error " + statusCode + " : " + errorM; } catch (JSONException e) { if (error.getMessage().split(".").length > 0) { diff --git a/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java b/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java index 277e525..a6bd055 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java +++ b/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java @@ -123,6 +123,7 @@ public class Helper { public static String[] openid = { "ac-normandie.fr", "education.fr", + "education.gouv.fr" //TODO: remove this one used for tests // "ac-orleans-tours.fr" };