diff options
3 files changed, 76 insertions, 113 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java index 754ae6e7..6b162fe6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java @@ -15,8 +15,6 @@ import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.concurrent.Executor; @@ -269,7 +267,7 @@ public class ParseComment { String parentId = singleCommentData.getString(JSONUtils.PARENT_ID_KEY); boolean isSubmitter = singleCommentData.getBoolean(JSONUtils.IS_SUBMITTER_KEY); String distinguished = singleCommentData.getString(JSONUtils.DISTINGUISHED_KEY); - Map<String, MediaMetadata> mediaMetadataMap = parseMediaMetadata(singleCommentData); + Map<String, MediaMetadata> mediaMetadataMap = JSONUtils.parseMediaMetadata(singleCommentData); String commentMarkdown = ""; if (!singleCommentData.isNull(JSONUtils.BODY_KEY)) { commentMarkdown = Utils.parseInlineRedditImages( @@ -309,64 +307,6 @@ public class ParseComment { } @Nullable - private static Map<String, MediaMetadata> parseMediaMetadata(JSONObject data) { - try { - if (data.has(JSONUtils.MEDIA_METADATA_KEY)) { - Map<String, MediaMetadata> mediaMetadataMap = new HashMap<>(); - JSONObject mediaMetadataJSON = data.getJSONObject(JSONUtils.MEDIA_METADATA_KEY); - for (Iterator<String> it = mediaMetadataJSON.keys(); it.hasNext();) { - try { - String k = it.next(); - JSONObject media = mediaMetadataJSON.getJSONObject(k); - String e = media.getString(JSONUtils.E_KEY); - - JSONObject originalItemJSON = media.getJSONObject(JSONUtils.S_KEY); - MediaMetadata.MediaItem originalItem; - if (e.equalsIgnoreCase("Image")) { - originalItem = new MediaMetadata.MediaItem(originalItemJSON.getInt(JSONUtils.X_KEY), - originalItemJSON.getInt(JSONUtils.Y_KEY), originalItemJSON.getString(JSONUtils.U_KEY)); - } else { - if (originalItemJSON.has(JSONUtils.MP4_KEY)) { - originalItem = new MediaMetadata.MediaItem(originalItemJSON.getInt(JSONUtils.X_KEY), - originalItemJSON.getInt(JSONUtils.Y_KEY), originalItemJSON.getString(JSONUtils.GIF_KEY), - originalItemJSON.getString(JSONUtils.MP4_KEY)); - } else { - originalItem = new MediaMetadata.MediaItem(originalItemJSON.getInt(JSONUtils.X_KEY), - originalItemJSON.getInt(JSONUtils.Y_KEY), originalItemJSON.getString(JSONUtils.GIF_KEY)); - } - } - String id = media.getString(JSONUtils.ID_KEY); - - MediaMetadata.MediaItem downscaledItem; - if (media.has(JSONUtils.P_KEY)) { - JSONArray downscales = media.getJSONArray(JSONUtils.P_KEY); - JSONObject downscaledItemJSON; - if (downscales.length() <= 3) { - downscaledItemJSON = downscales.getJSONObject(downscales.length() - 1); - } else { - downscaledItemJSON = downscales.getJSONObject(3); - } - downscaledItem = new MediaMetadata.MediaItem(downscaledItemJSON.getInt(JSONUtils.X_KEY), - downscaledItemJSON.getInt(JSONUtils.Y_KEY), downscaledItemJSON.getString(JSONUtils.U_KEY)); - } else { - downscaledItem = originalItem; - } - - mediaMetadataMap.put(id, new MediaMetadata(id, e, originalItem, downscaledItem)); - } catch (JSONException e) { - e.printStackTrace(); - } - } - return mediaMetadataMap; - } - } catch (JSONException e) { - e.printStackTrace(); - } - - return null; - } - - @Nullable private static String parseSentCommentErrorMessage(String response) { try { JSONObject responseObject = new JSONObject(response).getJSONObject(JSONUtils.JSON_KEY); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java index dfade7c1..ddbde65c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java @@ -5,16 +5,12 @@ import android.os.Handler; import android.text.Html; import android.text.TextUtils; -import androidx.annotation.Nullable; - import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; -import java.util.HashMap; import java.util.HashSet; -import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -216,7 +212,7 @@ public class ParsePost { } } - Map<String, MediaMetadata> mediaMetadataMap = parseMediaMetadata(data); + Map<String, MediaMetadata> mediaMetadataMap = JSONUtils.parseMediaMetadata(data); if (data.has(JSONUtils.CROSSPOST_PARENT_LIST)) { //Cross post //data.getJSONArray(JSONUtils.CROSSPOST_PARENT_LIST).getJSONObject(0) out of bounds???????????? @@ -240,53 +236,6 @@ public class ParsePost { } } - @Nullable - private static Map<String, MediaMetadata> parseMediaMetadata(JSONObject data) { - try { - if (data.has(JSONUtils.MEDIA_METADATA_KEY)) { - Map<String, MediaMetadata> mediaMetadataMap = new HashMap<>(); - JSONObject mediaMetadataJSON = data.getJSONObject(JSONUtils.MEDIA_METADATA_KEY); - for (Iterator<String> it = mediaMetadataJSON.keys(); it.hasNext();) { - try { - String k = it.next(); - JSONObject media = mediaMetadataJSON.getJSONObject(k); - - JSONObject originalItemJSON = media.getJSONObject(JSONUtils.S_KEY); - MediaMetadata.MediaItem originalItem = new MediaMetadata.MediaItem(originalItemJSON.getInt(JSONUtils.X_KEY), - originalItemJSON.getInt(JSONUtils.Y_KEY), originalItemJSON.getString(JSONUtils.U_KEY)); - - JSONArray downscales = media.getJSONArray(JSONUtils.P_KEY); - JSONObject downscaledItemJSON; - MediaMetadata.MediaItem downscaledItem; - if (downscales.length() <= 0) { - downscaledItem = originalItem; - } else { - if (downscales.length() <= 3) { - downscaledItemJSON = downscales.getJSONObject(downscales.length() - 1); - - } else { - downscaledItemJSON = downscales.getJSONObject(3); - } - downscaledItem = new MediaMetadata.MediaItem(downscaledItemJSON.getInt(JSONUtils.X_KEY), - downscaledItemJSON.getInt(JSONUtils.Y_KEY), downscaledItemJSON.getString(JSONUtils.U_KEY)); - } - - String id = media.getString(JSONUtils.ID_KEY); - mediaMetadataMap.put(id, new MediaMetadata(id, media.getString(JSONUtils.E_KEY), - originalItem, downscaledItem)); - } catch (JSONException e) { - e.printStackTrace(); - } - } - return mediaMetadataMap; - } - } catch (JSONException e) { - e.printStackTrace(); - } - - return null; - } - private static Post parseData(JSONObject data, String permalink, String id, String fullName, String subredditName, String subredditNamePrefixed, String author, String authorFlair, String authorFlairHTML, long postTimeMillis, String title, diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/JSONUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/JSONUtils.java index 79182378..6ddf5493 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/JSONUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/JSONUtils.java @@ -1,5 +1,17 @@ package ml.docilealligator.infinityforreddit.utils; +import androidx.annotation.Nullable; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import ml.docilealligator.infinityforreddit.MediaMetadata; + /** * Created by alex on 2/25/18. */ @@ -182,4 +194,66 @@ public class JSONUtils { public static final String HD_KEY = "hd"; public static final String SUGGESTED_SORT_KEY = "suggested_sort"; public static final String P_KEY = "p"; + + @Nullable + public static Map<String, MediaMetadata> parseMediaMetadata(JSONObject data) { + try { + if (data.has(JSONUtils.MEDIA_METADATA_KEY)) { + Map<String, MediaMetadata> mediaMetadataMap = new HashMap<>(); + JSONObject mediaMetadataJSON = data.getJSONObject(JSONUtils.MEDIA_METADATA_KEY); + for (Iterator<String> it = mediaMetadataJSON.keys(); it.hasNext();) { + try { + String k = it.next(); + JSONObject media = mediaMetadataJSON.getJSONObject(k); + String e = media.getString(JSONUtils.E_KEY); + + JSONObject originalItemJSON = media.getJSONObject(JSONUtils.S_KEY); + MediaMetadata.MediaItem originalItem; + if (e.equalsIgnoreCase("Image")) { + originalItem = new MediaMetadata.MediaItem(originalItemJSON.getInt(JSONUtils.X_KEY), + originalItemJSON.getInt(JSONUtils.Y_KEY), originalItemJSON.getString(JSONUtils.U_KEY)); + } else { + if (originalItemJSON.has(JSONUtils.MP4_KEY)) { + originalItem = new MediaMetadata.MediaItem(originalItemJSON.getInt(JSONUtils.X_KEY), + originalItemJSON.getInt(JSONUtils.Y_KEY), originalItemJSON.getString(JSONUtils.GIF_KEY), + originalItemJSON.getString(JSONUtils.MP4_KEY)); + } else { + originalItem = new MediaMetadata.MediaItem(originalItemJSON.getInt(JSONUtils.X_KEY), + originalItemJSON.getInt(JSONUtils.Y_KEY), originalItemJSON.getString(JSONUtils.GIF_KEY)); + } + } + + MediaMetadata.MediaItem downscaledItem; + if (media.has(JSONUtils.P_KEY)) { + JSONArray downscales = media.getJSONArray(JSONUtils.P_KEY); + JSONObject downscaledItemJSON; + if (downscales.length() <= 0) { + downscaledItem = originalItem; + } else { + if (downscales.length() <= 3) { + downscaledItemJSON = downscales.getJSONObject(downscales.length() - 1); + } else { + downscaledItemJSON = downscales.getJSONObject(3); + } + downscaledItem = new MediaMetadata.MediaItem(downscaledItemJSON.getInt(JSONUtils.X_KEY), + downscaledItemJSON.getInt(JSONUtils.Y_KEY), downscaledItemJSON.getString(JSONUtils.U_KEY)); + } + } else { + downscaledItem = originalItem; + } + + String id = media.getString(JSONUtils.ID_KEY); + mediaMetadataMap.put(id, new MediaMetadata(id, e, originalItem, downscaledItem)); + } catch (JSONException e) { + e.printStackTrace(); + } + } + return mediaMetadataMap; + } + } catch (JSONException e) { + e.printStackTrace(); + } + + return null; + } } |