From e4c64668dba93b89ff992ed9dcd1d63a9268838b Mon Sep 17 00:00:00 2001 From: scria1000 <91804886+scria1000@users.noreply.github.com> Date: Wed, 15 Dec 2021 13:43:01 +0000 Subject: Add support for parsing inline static emotes (#589) --- .../infinityforreddit/comment/ParseComment.java | 14 +++++---- .../infinityforreddit/utils/JSONUtils.java | 1 + .../infinityforreddit/utils/Utils.java | 34 +++++++++++++++++++++- 3 files changed, 43 insertions(+), 6 deletions(-) (limited to 'app/src/main/java/ml') 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 62283989..0244cc25 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java @@ -1,5 +1,9 @@ package ml.docilealligator.infinityforreddit.comment; +import static ml.docilealligator.infinityforreddit.comment.Comment.VOTE_TYPE_DOWNVOTE; +import static ml.docilealligator.infinityforreddit.comment.Comment.VOTE_TYPE_NO_VOTE; +import static ml.docilealligator.infinityforreddit.comment.Comment.VOTE_TYPE_UPVOTE; + import android.os.Handler; import android.text.Html; @@ -15,10 +19,6 @@ import java.util.concurrent.Executor; import ml.docilealligator.infinityforreddit.utils.JSONUtils; import ml.docilealligator.infinityforreddit.utils.Utils; -import static ml.docilealligator.infinityforreddit.comment.Comment.VOTE_TYPE_DOWNVOTE; -import static ml.docilealligator.infinityforreddit.comment.Comment.VOTE_TYPE_NO_VOTE; -import static ml.docilealligator.infinityforreddit.comment.Comment.VOTE_TYPE_UPVOTE; - public class ParseComment { public static void parseComment(Executor executor, Handler handler, String response, ArrayList commentData, boolean expandChildren, @@ -189,6 +189,10 @@ public class ParseComment { String commentMarkdown = ""; if (!singleCommentData.isNull(JSONUtils.BODY_KEY)) { commentMarkdown = Utils.parseInlineGifInComments(Utils.modifyMarkdown(singleCommentData.getString(JSONUtils.BODY_KEY).trim())); + if (!singleCommentData.isNull(JSONUtils.MEDIA_METADATA_KEY)) { + JSONObject mediaMetadataObject = singleCommentData.getJSONObject(JSONUtils.MEDIA_METADATA_KEY); + commentMarkdown = Utils.parseInlineEmotes(commentMarkdown, mediaMetadataObject); + } } String commentRawText = Utils.trimTrailingWhitespace( Html.fromHtml(singleCommentData.getString(JSONUtils.BODY_HTML_KEY))).toString(); @@ -229,7 +233,7 @@ public class ParseComment { return new Comment(id, fullName, author, authorFlair, authorFlairHTMLBuilder.toString(), linkAuthor, submitTime, commentMarkdown, commentRawText, linkId, subredditName, parentId, score, voteType, isSubmitter, distinguished, - permalink, awardingsBuilder.toString(),depth, collapsed, hasReply, scoreHidden, saved); + permalink, awardingsBuilder.toString(), depth, collapsed, hasReply, scoreHidden, saved); } @Nullable 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 42e13ce1..888c9a59 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/JSONUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/JSONUtils.java @@ -175,4 +175,5 @@ public class JSONUtils { public static final String CAPTION_URL_KEY = "outbound_url"; public static final String FILES_KEY = "files"; public static final String MP4_MOBILE_KEY = "mp4-mobile"; + public static final String STATUS_KEY = "status"; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java index 68af76f7..14e0fd14 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java @@ -31,7 +31,9 @@ import androidx.core.text.HtmlCompat; import com.bumptech.glide.Glide; import com.google.android.material.snackbar.Snackbar; +import org.json.JSONArray; import org.json.JSONException; +import org.json.JSONObject; import org.xmlpull.v1.XmlPullParserException; import java.io.IOException; @@ -72,7 +74,6 @@ public final class Utils { Pattern.compile("!\\[gif]\\(giphy\\|\\w+\\)"), Pattern.compile("!\\[gif]\\(giphy\\|\\w+\\|downsized\\)"), Pattern.compile("!\\[gif]\\(emote\\|\\w+\\|\\w+\\)"), - Pattern.compile("!\\[img]\\(emote\\|\\w+\\|\\w+\\)") }; public static String modifyMarkdown(String markdown) { @@ -178,6 +179,37 @@ public final class Utils { return markdownStringBuilder.toString(); } + public static String parseInlineEmotes(String markdown, JSONObject mediaMetadataObject) throws JSONException { + JSONArray mediaMetadataNames = mediaMetadataObject.names(); + for (int i = 0; i < mediaMetadataNames.length(); i++) { + if (!mediaMetadataNames.isNull(i)) { + String mediaMetadataKey = mediaMetadataNames.getString(i); + if (mediaMetadataObject.isNull(mediaMetadataKey)) { + continue; + } + JSONObject item = mediaMetadataObject.getJSONObject(mediaMetadataKey); + if (item.isNull(JSONUtils.STATUS_KEY) + || !item.getString(JSONUtils.STATUS_KEY).equals("valid") + || item.isNull(JSONUtils.ID_KEY) + || item.isNull(JSONUtils.T_KEY) + || item.isNull(JSONUtils.S_KEY)) { + continue; + } + String emote_type = item.getString(JSONUtils.T_KEY); + String emote_id = item.getString(JSONUtils.ID_KEY); + + JSONObject s_key = item.getJSONObject(JSONUtils.S_KEY); + if (s_key.isNull(JSONUtils.U_KEY)) { + continue; + } + String emote_url = s_key.getString(JSONUtils.U_KEY); + + markdown = markdown.replace("![img](" + emote_id + ")", "[[" + emote_type + "]](" + emote_url + ") "); + } + } + return markdown; + } + public static CharSequence trimTrailingWhitespace(CharSequence source) { if (source == null) { -- cgit v1.2.3