diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2015-09-18 22:26:08 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2015-09-18 22:26:08 +0900 |
commit | 0d2cf7dc057e21df9d6d680939df4f0b475862e9 (patch) | |
tree | 18364070478f7c73d63be6ca8b0b9b584b0ddb0e | |
parent | 7d57f6ac0aa8c32ad119cd74cb9cf279809dcbf4 (diff) | |
download | SmileEssence-0d2cf7dc057e21df9d6d680939df4f0b475862e9.tar.gz |
ツイート・DM 本文中の URL エンティティを display_url に展開(りぐれっしょん)
4 files changed, 36 insertions, 27 deletions
diff --git a/app/src/main/java/net/lacolaco/smileessence/entity/DirectMessage.java b/app/src/main/java/net/lacolaco/smileessence/entity/DirectMessage.java index c7b3dfba..df9d50e8 100644 --- a/app/src/main/java/net/lacolaco/smileessence/entity/DirectMessage.java +++ b/app/src/main/java/net/lacolaco/smileessence/entity/DirectMessage.java @@ -51,7 +51,7 @@ public class DirectMessage extends EntitySupport { id = message.getId(); sender = User.fromTwitter(message.getSender()); recipient = User.fromTwitter(message.getRecipient()); - text = message.getText(); + text = extractText(message, false); createdAt = message.getCreatedAt(); updateEntities(message); diff --git a/app/src/main/java/net/lacolaco/smileessence/entity/EntitySupport.java b/app/src/main/java/net/lacolaco/smileessence/entity/EntitySupport.java index 1df7cbd1..b82bb182 100644 --- a/app/src/main/java/net/lacolaco/smileessence/entity/EntitySupport.java +++ b/app/src/main/java/net/lacolaco/smileessence/entity/EntitySupport.java @@ -1,9 +1,9 @@ package net.lacolaco.smileessence.entity; +import com.google.common.collect.Lists; import twitter4j.*; -import java.util.ArrayList; -import java.util.List; +import java.util.*; public abstract class EntitySupport { private List<String> mentions; @@ -90,4 +90,36 @@ public abstract class EntitySupport { return names; } + public String extractText(twitter4j.Status status, boolean expand) { + return extractText(status, status.getText(), expand); + } + + public String extractText(twitter4j.DirectMessage status, boolean expand) { + return extractText(status, status.getText(), expand); + } + + private String extractText(twitter4j.EntitySupport status, String text, boolean expand) { + SortedSet<twitter4j.URLEntity> set = new TreeSet<>((a, b) -> a.getStart() - b.getStart()); + if (status.getURLEntities() != null) { + for (URLEntity entity : status.getURLEntities()) { + set.add(entity); + } + } + if (status.getExtendedMediaEntities() != null) { + for (URLEntity entity : status.getExtendedMediaEntities()) { + set.add(entity); + } + } else if (status.getMediaEntities() != null) { + for (URLEntity entity : status.getMediaEntities()) { + set.add(entity); + } + } + + for (URLEntity entity : set) { + String newString = expand ? entity.getExpandedURL() : entity.getDisplayURL(); + text = text.replaceFirst(entity.getText(), newString); + } + + return text; + } } diff --git a/app/src/main/java/net/lacolaco/smileessence/entity/Tweet.java b/app/src/main/java/net/lacolaco/smileessence/entity/Tweet.java index 3da59c38..5a2d1e55 100644 --- a/app/src/main/java/net/lacolaco/smileessence/entity/Tweet.java +++ b/app/src/main/java/net/lacolaco/smileessence/entity/Tweet.java @@ -56,7 +56,7 @@ public class Tweet extends EntitySupport { private void update(twitter4j.Status status) { id = status.getId(); user = User.fromTwitter(status.getUser()); - text = TwitterUtils.replaceURLEntities(status.getText(), status.getURLEntities(), false); + text = extractText(status, false); createdAt = status.getCreatedAt(); source = status.getSource(); favoriteCount = status.getFavoriteCount(); diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/util/TwitterUtils.java b/app/src/main/java/net/lacolaco/smileessence/twitter/util/TwitterUtils.java index 4d957f0b..7d1a0afc 100644 --- a/app/src/main/java/net/lacolaco/smileessence/twitter/util/TwitterUtils.java +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/util/TwitterUtils.java @@ -47,29 +47,6 @@ import java.util.Collection; public class TwitterUtils { // -------------------------- STATIC METHODS -------------------------- - /** - * Replace urls by entities - * - * @param text raw text - * @param entities url entities - * @param expand if true, use expanded url - * @return replaced text - */ - public static String replaceURLEntities(String text, URLEntity[] entities, boolean expand) { - if (TextUtils.isEmpty(text)) { - return ""; - } else if (entities == null) { - return text; - } - if (entities.length == 0) { - return text; - } - for (URLEntity entity : entities) { - text = text.replace(entity.getURL(), expand ? entity.getExpandedURL() : entity.getDisplayURL()); - } - return text; - } - public static Paging getPaging(int count) { return new Paging(1).count(count); } |