Example usage for twitter4j Status getMediaEntities

List of usage examples for twitter4j Status getMediaEntities

Introduction

In this page you can find the example usage for twitter4j Status getMediaEntities.

Prototype

MediaEntity[] getMediaEntities();

Source Link

Document

Returns an array of MediaEntities if medias are available in the tweet.

Usage

From source file:co.thehotnews.lambda.TheHotNewsSpeechlet.java

License:Open Source License

private SpeechletResponse getCurrentStatus(String name, String screenName) {
    List<Status> statuses = null;
    try {//w w w .  j  ava 2 s  .  c o  m
        Twitter twitter = twitterFactory.getInstance();
        statuses = twitter.getUserTimeline(screenName);

    } catch (Exception e) {
        log.error("Problem getting current status for " + screenName, e);
        return getErrorResponse();
    }

    Status status = statuses.get(0);

    String statusText = ResponseUtils.removeUrl(status.getText());
    if (statusText == null || statusText.trim().length() == 0) {
        // assume there was only a link in the tweet.
        statusText = " a link. ";
    }

    String verb = status.isRetweet() ? " retweeted: " : " tweeted: ";
    String speechText = name + verb + statusText;
    String cardText = name + verb + status.getText();
    String mediaURl = null;
    if (status.getMediaEntities() != null && status.getMediaEntities().length > 0) {
        mediaURl = status.getMediaEntities()[0].getMediaURLHttps();
    }

    StandardCard card = new StandardCard();
    card.setTitle(APP_NAME);
    card.setText(cardText);
    if (mediaURl != null) {
        Image image = new Image();
        image.setLargeImageUrl(mediaURl);
        card.setImage(image);
    }

    // Create the plain text output.
    PlainTextOutputSpeech speech = new PlainTextOutputSpeech();
    speech.setText(speechText);

    return SpeechletResponse.newTellResponse(speech, card);
}

From source file:com.alainesp.fan.sanderson.SummaryFragment.java

License:Open Source License

private String getTweetText(twitter4j.Status tweet) {
    SpannableStringBuilder text = new SpannableStringBuilder(tweet.getText());

    URLEntity[] urls = tweet.getURLEntities();
    MediaEntity[] medias = tweet.getMediaEntities();

    for (MediaEntity media : medias)
        if ("photo".equals(media.getType())) {
            InternetHelper.getRemoteFile(media.getMediaURLHttps());
            text.setSpan(new ImageSpan(null, media.getMediaURLHttps()), media.getStart(), media.getEnd(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }/*from   www  .j  a v  a  2  s  .c  o  m*/

    for (URLEntity url : urls) {
        text.setSpan(new URLSpan(url.getURL()), url.getStart(), url.getEnd(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        text.replace(url.getStart(), url.getEnd(), url.getExpandedURL());
    }

    return Html.toHtml(text);
}

From source file:com.daiv.android.twitter.utils.TweetLinkUtils.java

License:Apache License

public static String[] getLinksInStatus(Status status) {
    UserMentionEntity[] users = status.getUserMentionEntities();
    String mUsers = "";

    for (UserMentionEntity name : users) {
        String n = name.getScreenName();
        if (n.length() > 1) {
            mUsers += n + "  ";
        }//from w w  w .j a  v a2  s .com
    }

    HashtagEntity[] hashtags = status.getHashtagEntities();
    String mHashtags = "";

    for (HashtagEntity hashtagEntity : hashtags) {
        String text = hashtagEntity.getText();
        if (text.length() > 1) {
            mHashtags += text + "  ";
        }
    }

    URLEntity[] urls = status.getURLEntities();
    String expandedUrls = "";
    String compressedUrls = "";

    for (URLEntity entity : urls) {
        String url = entity.getExpandedURL();
        if (url.length() > 1) {
            expandedUrls += url + "  ";
            compressedUrls += entity.getURL() + "  ";
        }
    }

    MediaEntity[] medias = status.getMediaEntities();
    String mediaExp = "";
    String mediaComp = "";
    String mediaDisplay = "";

    for (MediaEntity e : medias) {
        String url = e.getURL();
        if (url.length() > 1) {
            mediaComp += url + "  ";
            mediaExp += e.getExpandedURL() + "  ";
            mediaDisplay += e.getDisplayURL() + "  ";
        }
    }

    String[] sExpandedUrls;
    String[] sCompressedUrls;
    String[] sMediaExp;
    String[] sMediaComp;
    String[] sMediaDisplay;

    try {
        sCompressedUrls = compressedUrls.split("  ");
    } catch (Exception e) {
        sCompressedUrls = new String[0];
    }

    try {
        sExpandedUrls = expandedUrls.split("  ");
    } catch (Exception e) {
        sExpandedUrls = new String[0];
    }

    try {
        sMediaComp = mediaComp.split("  ");
    } catch (Exception e) {
        sMediaComp = new String[0];
    }

    try {
        sMediaExp = mediaExp.split("  ");
    } catch (Exception e) {
        sMediaExp = new String[0];
    }

    try {
        sMediaDisplay = mediaDisplay.split("  ");
    } catch (Exception e) {
        sMediaDisplay = new String[0];
    }

    String tweetTexts = status.getText();

    String imageUrl = "";
    String otherUrl = "";

    for (int i = 0; i < sCompressedUrls.length; i++) {
        String comp = sCompressedUrls[i];
        String exp = sExpandedUrls[i];

        if (comp.length() > 1 && exp.length() > 1) {
            String str = exp.toLowerCase();

            try {
                String replacement = exp.replace("http://", "").replace("https://", "").replace("www.", "");

                boolean hasCom = replacement.contains(".com");
                replacement = replacement.substring(0, 30) + "...";

                if (hasCom && !replacement.contains(".com")) { // the link was too long...
                    replacement = exp.replace("http://", "").replace("https://", "").replace("www.", "");
                    replacement = replacement.substring(0, replacement.indexOf(".com") + 6) + "...";
                }

                tweetTexts = tweetTexts.replace(comp, replacement);
            } catch (Exception e) {
                tweetTexts = tweetTexts.replace(comp,
                        exp.replace("http://", "").replace("https://", "").replace("www.", ""));
            }
            if (str.contains("instag") && !str.contains("blog.insta")) {
                imageUrl = exp + "media/?size=l";
                otherUrl += exp + "  ";
            } else if (exp.toLowerCase().contains("youtub")
                    && !(str.contains("channel") || str.contains("user"))) {
                // first get the youtube video code
                int start = exp.indexOf("v=") + 2;
                int end = exp.length();
                if (exp.substring(start).contains("&")) {
                    end = exp.indexOf("&");
                } else if (exp.substring(start).contains("?")) {
                    end = exp.indexOf("?");
                }
                try {
                    imageUrl = "http://img.youtube.com/vi/" + exp.substring(start, end) + "/hqdefault.jpg";
                } catch (Exception e) {
                    imageUrl = "http://img.youtube.com/vi/" + exp.substring(start, exp.length() - 1)
                            + "/hqdefault.jpg";
                }
                otherUrl += exp + "  ";
            } else if (str.contains("youtu.be")) {
                // first get the youtube video code
                int start = exp.indexOf(".be/") + 4;
                int end = exp.length();
                if (exp.substring(start).contains("&")) {
                    end = exp.indexOf("&");
                } else if (exp.substring(start).contains("?")) {
                    end = exp.indexOf("?");
                }
                try {
                    imageUrl = "http://img.youtube.com/vi/" + exp.substring(start, end) + "/hqdefault.jpg";
                } catch (Exception e) {
                    imageUrl = "http://img.youtube.com/vi/" + exp.substring(start, exp.length() - 1)
                            + "/hqdefault.jpg";
                }
                otherUrl += exp + "  ";
            } else if (str.contains("twitpic")) {
                int start = exp.indexOf(".com/") + 5;
                imageUrl = "http://twitpic.com/show/full/" + exp.substring(start).replace("/", "");
                otherUrl += exp + "  ";
            } else if (str.contains("i.imgur") && !str.contains("/a/")) {
                int start = exp.indexOf(".com/") + 5;
                imageUrl = "http://i.imgur.com/" + exp.replace("http://i.imgur.com/", "").replace(".jpg", "")
                        + "l.jpg";
                imageUrl = imageUrl.replace("gallery/", "");
                otherUrl += exp + "  ";
            } else if (str.contains("imgur") && !str.contains("/a/")) {
                int start = exp.indexOf(".com/") + 6;
                imageUrl = "http://i.imgur.com/" + exp.replace("http://imgur.com/", "").replace(".jpg", "")
                        + "l.jpg";
                imageUrl = imageUrl.replace("gallery/", "").replace("a/", "");
                otherUrl += exp + "  ";
            } else if (str.contains("pbs.twimg.com")) {
                imageUrl = exp;
                otherUrl += exp + "  ";
            } else if (str.contains("ow.ly/i/")) {
                imageUrl = "http://static.ow.ly/photos/original/"
                        + exp.substring(exp.lastIndexOf("/")).replaceAll("/", "") + ".jpg";
                otherUrl += exp + "  ";
            } else if (str.contains("p.twipple.jp")) {
                imageUrl = "http://p.twipple.jp/show/large/" + exp.replace("p.twipple.jp/", "")
                        .replace("http://", "").replace("https://", "").replace("www.", "");
                otherUrl += exp + "  ";
            } else if (str.contains(".jpg") || str.contains(".png")) {
                imageUrl = exp;
                otherUrl += exp + "  ";
            } else if (str.contains("img.ly")) {
                imageUrl = exp.replace("https", "http").replace("http://img.ly/", "http://img.ly/show/large/");
                otherUrl += exp + "  ";
            } else {
                otherUrl += exp + "  ";
            }
        }
    }

    for (int i = 0; i < sMediaComp.length; i++) {
        String comp = sMediaComp[i];
        String exp = sMediaExp[i];

        if (comp.length() > 1 && exp.length() > 1) {
            try {
                String replacement = sMediaDisplay[i].replace("http://", "").replace("https://", "")
                        .replace("www.", "");

                boolean hasCom = replacement.contains(".com");
                replacement = replacement.substring(0, 22) + "...";

                if (hasCom && !replacement.contains(".com")) { // the link was too long...
                    replacement = sMediaDisplay[i].replace("http://", "").replace("https://", "")
                            .replace("www.", "");
                    replacement = replacement.substring(0, replacement.indexOf(".com") + 6) + "...";
                }

                tweetTexts = tweetTexts.replace(comp, replacement);
            } catch (Exception e) {
                tweetTexts = tweetTexts.replace(comp,
                        sMediaDisplay[i].replace("http://", "").replace("https://", "").replace("www.", ""));
            }
            imageUrl = status.getMediaEntities()[0].getMediaURL();

            for (MediaEntity m : status.getExtendedMediaEntities()) {
                if (m.getType().equals("photo")) {
                    if (!imageUrl.contains(m.getMediaURL())) {
                        imageUrl += " " + m.getMediaURL();
                    }
                }
            }

            otherUrl += sMediaDisplay[i];
        }
    }

    return new String[] { tweetTexts, imageUrl, otherUrl, mHashtags, mUsers };
}

From source file:com.daiv.android.twitter.utils.TweetLinkUtils.java

License:Apache License

public static ArrayList<String> getAllExternalPictures(Status status) {
    URLEntity[] urls = status.getURLEntities();
    String expandedUrls = "";
    String compressedUrls = "";

    for (URLEntity entity : urls) {
        String url = entity.getExpandedURL();
        if (url.length() > 1) {
            expandedUrls += url + "  ";
            compressedUrls += entity.getURL() + "  ";
        }/*ww  w  .  java  2  s.  c  o m*/
    }

    MediaEntity[] medias = status.getMediaEntities();
    String mediaExp = "";
    String mediaComp = "";

    for (MediaEntity e : medias) {
        String url = e.getURL();
        if (url.length() > 1) {
            mediaComp += url + "  ";
            mediaExp += e.getExpandedURL() + "  ";
        }
    }

    String[] sExpandedUrls;
    String[] sCompressedUrls;
    String[] sMediaExp;
    String[] sMediaComp;

    try {
        sCompressedUrls = compressedUrls.split("  ");
    } catch (Exception e) {
        sCompressedUrls = new String[0];
    }

    try {
        sExpandedUrls = expandedUrls.split("  ");
    } catch (Exception e) {
        sExpandedUrls = new String[0];
    }

    try {
        sMediaComp = mediaComp.split("  ");
    } catch (Exception e) {
        sMediaComp = new String[0];
    }

    try {
        sMediaExp = mediaExp.split("  ");
    } catch (Exception e) {
        sMediaExp = new String[0];
    }

    ArrayList<String> images = new ArrayList<String>();

    for (int i = 0; i < sCompressedUrls.length; i++) {
        String comp = sCompressedUrls[i];
        String exp = sExpandedUrls[i];

        if (comp.length() > 1 && exp.length() > 1) {
            String str = exp.toLowerCase();

            if (str.contains("instag") && !str.contains("blog.insta")) {
                images.add(exp + "media/?size=m");
            } else if (exp.toLowerCase().contains("youtub")
                    && !(str.contains("channel") || str.contains("user"))) {
                // first get the youtube video code
                int start = exp.indexOf("v=") + 2;
                int end = exp.length();
                if (exp.substring(start).contains("&")) {
                    end = exp.indexOf("&");
                } else if (exp.substring(start).contains("?")) {
                    end = exp.indexOf("?");
                }
                try {
                    images.add("http://img.youtube.com/vi/" + exp.substring(start, end) + "/hqdefault.jpg");
                } catch (Exception e) {
                    images.add("http://img.youtube.com/vi/" + exp.substring(start, exp.length() - 1)
                            + "/hqdefault.jpg");
                }
            } else if (str.contains("youtu.be")) {
                // first get the youtube video code
                int start = exp.indexOf(".be/") + 4;
                int end = exp.length();
                if (exp.substring(start).contains("&")) {
                    end = exp.indexOf("&");
                } else if (exp.substring(start).contains("?")) {
                    end = exp.indexOf("?");
                }
                try {
                    images.add("http://img.youtube.com/vi/" + exp.substring(start, end) + "/hqdefault.jpg");
                } catch (Exception e) {
                    images.add("http://img.youtube.com/vi/" + exp.substring(start, exp.length() - 1)
                            + "/mqefault.jpg");
                }
            } else if (str.contains("twitpic")) {
                int start = exp.indexOf(".com/") + 5;
                images.add("http://twitpic.com/show/full/" + exp.substring(start).replace("/", ""));
            } else if (str.contains("i.imgur") && !str.contains("/a/")) {
                images.add(("http://i.imgur.com/" + exp.replace("http://i.imgur.com/", "").replace(".jpg", "")
                        + "m.jpg").replace("gallery/", ""));
            } else if (str.contains("imgur") && !str.contains("/a/")) {
                images.add(("http://i.imgur.com/" + exp.replace("http://imgur.com/", "").replace(".jpg", "")
                        + "m.jpg").replace("gallery/", "").replace("a/", ""));
            } else if (str.contains("pbs.twimg.com")) {
                images.add(exp);
            } else if (str.contains("ow.ly/i/")) {
                images.add("http://static.ow.ly/photos/original/"
                        + exp.substring(exp.lastIndexOf("/")).replaceAll("/", "") + ".jpg");
            } else if (str.contains("p.twipple.jp")) {
                images.add("http://p.twipple.jp/show/large/" + exp.replace("p.twipple.jp/", "")
                        .replace("http://", "").replace("https://", "").replace("www.", ""));
            } else if (str.contains(".jpg") || str.contains(".png")) {
                images.add(exp);
            } else if (str.contains("img.ly")) {
                images.add(exp.replace("https", "http").replace("http://img.ly/", "http://img.ly/show/large/"));
            }
        }
    }

    for (int i = 0; i < sMediaComp.length; i++) {
        String comp = sMediaComp[i];
        String exp = sMediaExp[i];

        if (comp.length() > 1 && exp.length() > 1) {
            images.add(status.getMediaEntities()[0].getMediaURL());
        }
    }

    return images;
}

From source file:com.dwdesign.tweetings.model.ParcelableStatus.java

License:Open Source License

public ParcelableStatus(Status status, final long account_id, final boolean is_gap,
        final boolean large_inline_image_preview) {

    this.is_gap = is_gap;
    this.account_id = account_id;
    status_id = status.getId();//from  www  . j  av a 2  s  . c o m
    is_retweet = status.isRetweet();
    final Status retweeted_status = is_retweet ? status.getRetweetedStatus() : null;
    final User retweet_user = retweeted_status != null ? status.getUser() : null;
    retweet_id = retweeted_status != null ? retweeted_status.getId() : -1;
    retweeted_by_id = retweet_user != null ? retweet_user.getId() : -1;
    retweeted_by_name = retweet_user != null ? retweet_user.getName() : null;
    retweeted_by_screen_name = retweet_user != null ? retweet_user.getScreenName() : null;
    if (retweeted_status != null) {
        status = retweeted_status;
    }
    final User user = status.getUser();
    user_id = user != null ? user.getId() : -1;
    name = user != null ? user.getName() : null;
    screen_name = user != null ? user.getScreenName() : null;
    profile_image_url = user != null ? user.getProfileImageURL() : null;
    profile_image_url_string = profile_image_url != null ? profile_image_url.toString() : null;
    is_protected = user != null ? user.isProtected() : false;
    is_verified = user != null ? user.isVerified() : false;
    final MediaEntity[] medias = status.getMediaEntities();

    status_timestamp = getTime(status.getCreatedAt());
    text_html = formatStatusText(status);
    final PreviewImage preview = getPreviewImage(text_html,
            large_inline_image_preview ? INLINE_IMAGE_PREVIEW_DISPLAY_OPTION_CODE_LARGE_HIGH
                    : INLINE_IMAGE_PREVIEW_DISPLAY_OPTION_CODE_SMALL);
    text_plain = status.getText();
    retweet_count = status.getRetweetCount();
    in_reply_to_screen_name = status.getInReplyToScreenName();
    in_reply_to_status_id = status.getInReplyToStatusId();
    source = status.getSource();
    location = new ParcelableLocation(status.getGeoLocation());
    location_string = location.toString();
    is_favorite = status.isFavorited();
    has_media = medias != null && medias.length > 0 || preview.has_image;
    text = text_html != null ? Html.fromHtml(text_html) : null;
    image_preview_url_string = preview.matched_url;
    image_orig_url_string = preview.orig_url;
    image_preview_url = parseURL(image_preview_url_string);
    text_unescaped = unescape(text_html);
    String play = null;
    URLEntity[] urls = status.getURLEntities();
    if (urls != null) {
        for (final URLEntity url : urls) {
            final URL tco_url = url.getURL();
            final URL expanded_url = url.getExpandedURL();
            if (tco_url != null && expanded_url != null
                    && expanded_url.toString().contains("play.google.com/store/apps")) {
                play = expanded_url.toString();
                break;
            }

        }
    }
    play_package = play;
    is_possibly_sensitive = status.isPossiblySensitive();
}

From source file:com.fsatir.twitter.TwitterManagedBean.java

public List<Media> trendImageList(Twitter twitter, String trendName) {

    Query query = new Query(trendName + " AND filter:images");
    query.setCount(1);//from  w w  w.ja va2  s .  c om
    QueryResult result;
    try {
        do {
            result = twitter.search(query);

            List<twitter4j.Status> tweets = result.getTweets();
            int counter = 0;
            for (twitter4j.Status status : tweets) {

                Media myMedia = new Media();
                int control = 0;
                for (MediaEntity me : status.getMediaEntities()) {
                    //Tek grsel almay kesinletirmek iin kontrol.
                    //leride modeldeki trendImgURL diziye dntrlerek kontrol kaldrlabilir.
                    if (control < 1)
                        myMedia.setTrendImgURL(me.getMediaURLHttps());
                    control++;
                }
                // URL null deilse, grsel ve bigileri list'e eklenir.
                if (myMedia.getTrendImgURL() != null) {
                    myMedia.setTweetID(status.getId());
                    myMedia.setFavorite_count(status.getFavoriteCount());
                    myMedia.setRetweet_count(status.getRetweetCount());
                    //myMedia.setTrendImgURL(status.getMediaEntities()[0].getMediaURLHttps());
                    myMedia.setTrendName(trendName);
                    myMedia.setName(Long.toString(status.getId()));
                    myMedia.setType(status.getMediaEntities()[0].getType());

                    myMediaList.add(counter++, myMedia);
                }
            }
        } while ((query = result.nextQuery()) != null && result.getRateLimitStatus().getRemaining() > 0);

    } catch (TwitterException e) {
        e.printStackTrace();
    }

    myMediaListFiltered = filterList(myMediaList);

    return myMediaListFiltered;

}

From source file:com.github.jcustenborder.kafka.connect.twitter.StatusConverter.java

License:Apache License

public static void convert(Status status, Struct struct) {
    struct.put("CreatedAt", status.getCreatedAt()).put("Id", status.getId()).put("Text", status.getText())
            .put("Source", status.getSource()).put("Truncated", status.isTruncated())
            .put("InReplyToStatusId", status.getInReplyToStatusId())
            .put("InReplyToUserId", status.getInReplyToUserId())
            .put("InReplyToScreenName", status.getInReplyToScreenName()).put("Favorited", status.isFavorited())
            .put("Retweeted", status.isRetweeted()).put("FavoriteCount", status.getFavoriteCount())
            .put("Retweet", status.isRetweet()).put("RetweetCount", status.getRetweetCount())
            .put("RetweetedByMe", status.isRetweetedByMe())
            .put("CurrentUserRetweetId", status.getCurrentUserRetweetId())
            .put("PossiblySensitive", status.isPossiblySensitive()).put("Lang", status.getLang());

    Struct userStruct;// w w w  .j  a  v a 2 s .com
    if (null != status.getUser()) {
        userStruct = new Struct(USER_SCHEMA);
        convert(status.getUser(), userStruct);
    } else {
        userStruct = null;
    }
    struct.put("User", userStruct);

    Struct placeStruct;
    if (null != status.getPlace()) {
        placeStruct = new Struct(PLACE_SCHEMA);
        convert(status.getPlace(), placeStruct);
    } else {
        placeStruct = null;
    }
    struct.put("Place", placeStruct);

    Struct geoLocationStruct;
    if (null != status.getGeoLocation()) {
        geoLocationStruct = new Struct(GEO_LOCATION_SCHEMA);
        convert(status.getGeoLocation(), geoLocationStruct);
    } else {
        geoLocationStruct = null;
    }
    struct.put("GeoLocation", geoLocationStruct);
    List<Long> contributers = new ArrayList<>();

    if (null != status.getContributors()) {
        for (Long l : status.getContributors()) {
            contributers.add(l);
        }
    }
    struct.put("Contributors", contributers);

    List<String> withheldInCountries = new ArrayList<>();
    if (null != status.getWithheldInCountries()) {
        for (String s : status.getWithheldInCountries()) {
            withheldInCountries.add(s);
        }
    }
    struct.put("WithheldInCountries", withheldInCountries);

    struct.put("HashtagEntities", convert(status.getHashtagEntities()));
    struct.put("UserMentionEntities", convert(status.getUserMentionEntities()));
    struct.put("MediaEntities", convert(status.getMediaEntities()));
    struct.put("SymbolEntities", convert(status.getSymbolEntities()));
    struct.put("URLEntities", convert(status.getURLEntities()));
}

From source file:com.javielinux.utils.Utils.java

License:Apache License

static public String getTextURLs(twitter4j.Status st) {
    String out = "";
    URLEntity[] urls = st.getURLEntities();

    if (urls != null && urls.length > 0) {
        for (URLEntity url : urls) {
            if (url.getDisplayURL() != null && !url.getDisplayURL().equals("")) {
                out += url.getURL().toString() + SEP_VALUES + url.getDisplayURL() + SEP_VALUES
                        + url.getExpandedURL().toString() + SEP_BLOCK;
            }/*from w  w  w. j av a2 s  .  co  m*/
        }
    }

    MediaEntity[] medias = st.getMediaEntities();

    if (medias != null && medias.length > 0) {
        for (MediaEntity media : medias) {
            if (media.getDisplayURL() != null && !media.getDisplayURL().equals("")) {
                out += media.getURL().toString() + SEP_VALUES + media.getDisplayURL() + SEP_VALUES
                        + media.getExpandedURL().toString() + SEP_VALUES + media.getMediaURL().toString()
                        + ":thumb" + SEP_VALUES + media.getMediaURL().toString() + ":medium" + SEP_BLOCK;
            }
        }
    }

    return out;
}

From source file:com.klinker.android.twitter.utils.TweetLinkUtils.java

License:Apache License

public static String[] getLinksInStatus(Status status) {
    UserMentionEntity[] users = status.getUserMentionEntities();
    String mUsers = "";

    for (UserMentionEntity name : users) {
        String n = name.getScreenName();
        if (n.length() > 1) {
            mUsers += n + "  ";
        }//from www. j a  va  2s  .  c  o  m
    }

    HashtagEntity[] hashtags = status.getHashtagEntities();
    String mHashtags = "";

    for (HashtagEntity hashtagEntity : hashtags) {
        String text = hashtagEntity.getText();
        if (text.length() > 1) {
            mHashtags += text + "  ";
        }
    }

    URLEntity[] urls = status.getURLEntities();
    String expandedUrls = "";
    String compressedUrls = "";

    for (URLEntity entity : urls) {
        String url = entity.getExpandedURL();
        if (url.length() > 1) {
            expandedUrls += url + "  ";
            compressedUrls += entity.getURL() + "  ";
        }
    }

    MediaEntity[] medias = status.getMediaEntities();
    String mediaExp = "";
    String mediaComp = "";
    String mediaDisplay = "";

    for (MediaEntity e : medias) {
        String url = e.getURL();
        if (url.length() > 1) {
            mediaComp += url + "  ";
            mediaExp += e.getExpandedURL() + "  ";
            mediaDisplay += e.getDisplayURL() + "  ";
        }
    }

    String[] sExpandedUrls;
    String[] sCompressedUrls;
    String[] sMediaExp;
    String[] sMediaComp;
    String[] sMediaDisplay;

    try {
        sCompressedUrls = compressedUrls.split("  ");
    } catch (Exception e) {
        sCompressedUrls = new String[0];
    }

    try {
        sExpandedUrls = expandedUrls.split("  ");
    } catch (Exception e) {
        sExpandedUrls = new String[0];
    }

    try {
        sMediaComp = mediaComp.split("  ");
    } catch (Exception e) {
        sMediaComp = new String[0];
    }

    try {
        sMediaExp = mediaExp.split("  ");
    } catch (Exception e) {
        sMediaExp = new String[0];
    }

    try {
        sMediaDisplay = mediaDisplay.split("  ");
    } catch (Exception e) {
        sMediaDisplay = new String[0];
    }

    String tweetTexts = status.getText();

    String imageUrl = "";
    String otherUrl = "";

    for (int i = 0; i < sCompressedUrls.length; i++) {
        String comp = sCompressedUrls[i];
        String exp = sExpandedUrls[i];

        if (comp.length() > 1 && exp.length() > 1) {
            String str = exp.toLowerCase();

            try {
                tweetTexts = tweetTexts.replace(comp,
                        exp.replace("http://", "").replace("https://", "").replace("www.", "").substring(0, 30)
                                + "...");
            } catch (Exception e) {
                tweetTexts = tweetTexts.replace(comp,
                        exp.replace("http://", "").replace("https://", "").replace("www.", ""));
            }
            if (str.contains("instag") && !str.contains("blog.insta")) {
                imageUrl = exp + "media/?size=m";
                otherUrl += exp + "  ";
            } else if (exp.toLowerCase().contains("youtub")
                    && !(str.contains("channel") || str.contains("user"))) {
                // first get the youtube video code
                int start = exp.indexOf("v=") + 2;
                int end = exp.length();
                if (exp.substring(start).contains("&")) {
                    end = exp.indexOf("&");
                } else if (exp.substring(start).contains("?")) {
                    end = exp.indexOf("?");
                }
                try {
                    imageUrl = "http://img.youtube.com/vi/" + exp.substring(start, end) + "/hqdefault.jpg";
                } catch (Exception e) {
                    imageUrl = "http://img.youtube.com/vi/" + exp.substring(start, exp.length() - 1)
                            + "/hqdefault.jpg";
                }
                otherUrl += exp + "  ";
            } else if (str.contains("youtu.be")) {
                // first get the youtube video code
                int start = exp.indexOf(".be/") + 4;
                int end = exp.length();
                if (exp.substring(start).contains("&")) {
                    end = exp.indexOf("&");
                } else if (exp.substring(start).contains("?")) {
                    end = exp.indexOf("?");
                }
                try {
                    imageUrl = "http://img.youtube.com/vi/" + exp.substring(start, end) + "/hqdefault.jpg";
                } catch (Exception e) {
                    imageUrl = "http://img.youtube.com/vi/" + exp.substring(start, exp.length() - 1)
                            + "/mqefault.jpg";
                }
                otherUrl += exp + "  ";
            } else if (str.contains("twitpic")) {
                int start = exp.indexOf(".com/") + 5;
                imageUrl = "http://twitpic.com/show/full/" + exp.substring(start).replace("/", "");
                otherUrl += exp + "  ";
            } else if (str.contains("i.imgur") && !str.contains("/a/")) {
                int start = exp.indexOf(".com/") + 5;
                imageUrl = "http://i.imgur.com/" + exp.replace("http://i.imgur.com/", "").replace(".jpg", "")
                        + "m.jpg";
                imageUrl = imageUrl.replace("gallery/", "");
                otherUrl += exp + "  ";
            } else if (str.contains("imgur") && !str.contains("/a/")) {
                int start = exp.indexOf(".com/") + 6;
                imageUrl = "http://i.imgur.com/" + exp.replace("http://imgur.com/", "").replace(".jpg", "")
                        + "m.jpg";
                imageUrl = imageUrl.replace("gallery/", "").replace("a/", "");
                otherUrl += exp + "  ";
            } else if (str.contains("pbs.twimg.com")) {
                imageUrl = exp;
                otherUrl += exp + "  ";
            } else if (str.contains("ow.ly/i")) {
                imageUrl = "http://static.ow.ly/photos/original/"
                        + exp.substring(exp.lastIndexOf("/")).replaceAll("/", "") + ".jpg";
                otherUrl += exp + "  ";
            } else if (str.contains("p.twipple.jp")) {
                imageUrl = "http://p.twipple.jp/show/large/" + exp.replace("p.twipple.jp/", "")
                        .replace("http://", "").replace("https://", "").replace("www.", "");
                otherUrl += exp + "  ";
            } else if (str.contains(".jpg") || str.contains(".png")) {
                imageUrl = exp;
                otherUrl += exp + "  ";
            } else {
                otherUrl += exp + "  ";
            }
        }
    }

    for (int i = 0; i < sMediaComp.length; i++) {
        String comp = sMediaComp[i];
        String exp = sMediaExp[i];

        if (comp.length() > 1 && exp.length() > 1) {
            try {
                tweetTexts = tweetTexts.replace(comp, sMediaDisplay[i].replace("http://", "")
                        .replace("https://", "").replace("www.", "").substring(0, 22) + "...");
            } catch (Exception e) {
                tweetTexts = tweetTexts.replace(comp,
                        sMediaDisplay[i].replace("http://", "").replace("https://", "").replace("www.", ""));
            }
            imageUrl = status.getMediaEntities()[0].getMediaURL();

            for (MediaEntity m : status.getExtendedMediaEntities()) {
                if (m.getType().equals("photo")) {
                    if (!imageUrl.contains(m.getMediaURL())) {
                        imageUrl += " " + m.getMediaURL();
                    }
                }
            }

            otherUrl += sMediaDisplay[i];
        }
    }

    return new String[] { tweetTexts, imageUrl, otherUrl, mHashtags, mUsers };
}

From source file:com.klinker.android.twitter.utils.TweetLinkUtils.java

License:Apache License

public static ArrayList<String> getAllExternalPictures(Status status) {
    URLEntity[] urls = status.getURLEntities();
    String expandedUrls = "";
    String compressedUrls = "";

    for (URLEntity entity : urls) {
        String url = entity.getExpandedURL();
        if (url.length() > 1) {
            expandedUrls += url + "  ";
            compressedUrls += entity.getURL() + "  ";
        }//from  ww w  .ja va2  s .  c o m
    }

    MediaEntity[] medias = status.getMediaEntities();
    String mediaExp = "";
    String mediaComp = "";

    for (MediaEntity e : medias) {
        String url = e.getURL();
        if (url.length() > 1) {
            mediaComp += url + "  ";
            mediaExp += e.getExpandedURL() + "  ";
        }
    }

    String[] sExpandedUrls;
    String[] sCompressedUrls;
    String[] sMediaExp;
    String[] sMediaComp;

    try {
        sCompressedUrls = compressedUrls.split("  ");
    } catch (Exception e) {
        sCompressedUrls = new String[0];
    }

    try {
        sExpandedUrls = expandedUrls.split("  ");
    } catch (Exception e) {
        sExpandedUrls = new String[0];
    }

    try {
        sMediaComp = mediaComp.split("  ");
    } catch (Exception e) {
        sMediaComp = new String[0];
    }

    try {
        sMediaExp = mediaExp.split("  ");
    } catch (Exception e) {
        sMediaExp = new String[0];
    }

    ArrayList<String> images = new ArrayList<String>();

    for (int i = 0; i < sCompressedUrls.length; i++) {
        String comp = sCompressedUrls[i];
        String exp = sExpandedUrls[i];

        if (comp.length() > 1 && exp.length() > 1) {
            String str = exp.toLowerCase();

            if (str.contains("instag") && !str.contains("blog.insta")) {
                images.add(exp + "media/?size=m");
            } else if (exp.toLowerCase().contains("youtub")
                    && !(str.contains("channel") || str.contains("user"))) {
                // first get the youtube video code
                int start = exp.indexOf("v=") + 2;
                int end = exp.length();
                if (exp.substring(start).contains("&")) {
                    end = exp.indexOf("&");
                } else if (exp.substring(start).contains("?")) {
                    end = exp.indexOf("?");
                }
                try {
                    images.add("http://img.youtube.com/vi/" + exp.substring(start, end) + "/hqdefault.jpg");
                } catch (Exception e) {
                    images.add("http://img.youtube.com/vi/" + exp.substring(start, exp.length() - 1)
                            + "/hqdefault.jpg");
                }
            } else if (str.contains("youtu.be")) {
                // first get the youtube video code
                int start = exp.indexOf(".be/") + 4;
                int end = exp.length();
                if (exp.substring(start).contains("&")) {
                    end = exp.indexOf("&");
                } else if (exp.substring(start).contains("?")) {
                    end = exp.indexOf("?");
                }
                try {
                    images.add("http://img.youtube.com/vi/" + exp.substring(start, end) + "/hqdefault.jpg");
                } catch (Exception e) {
                    images.add("http://img.youtube.com/vi/" + exp.substring(start, exp.length() - 1)
                            + "/mqefault.jpg");
                }
            } else if (str.contains("twitpic")) {
                int start = exp.indexOf(".com/") + 5;
                images.add("http://twitpic.com/show/full/" + exp.substring(start).replace("/", ""));
            } else if (str.contains("i.imgur") && !str.contains("/a/")) {
                images.add(("http://i.imgur.com/" + exp.replace("http://i.imgur.com/", "").replace(".jpg", "")
                        + "m.jpg").replace("gallery/", ""));
            } else if (str.contains("imgur") && !str.contains("/a/")) {
                images.add(("http://i.imgur.com/" + exp.replace("http://imgur.com/", "").replace(".jpg", "")
                        + "m.jpg").replace("gallery/", "").replace("a/", ""));
            } else if (str.contains("pbs.twimg.com")) {
                images.add(exp);
            } else if (str.contains("ow.ly/i")) {
                images.add("http://static.ow.ly/photos/original/"
                        + exp.substring(exp.lastIndexOf("/")).replaceAll("/", "") + ".jpg");
            } else if (str.contains("p.twipple.jp")) {
                images.add("http://p.twipple.jp/show/large/" + exp.replace("p.twipple.jp/", "")
                        .replace("http://", "").replace("https://", "").replace("www.", ""));
            } else if (str.contains(".jpg") || str.contains(".png")) {
                images.add(exp);
            }
        }
    }

    for (int i = 0; i < sMediaComp.length; i++) {
        String comp = sMediaComp[i];
        String exp = sMediaExp[i];

        if (comp.length() > 1 && exp.length() > 1) {
            images.add(status.getMediaEntities()[0].getMediaURL());
        }
    }

    return images;
}