Example usage for twitter4j URLEntity getExpandedURL

List of usage examples for twitter4j URLEntity getExpandedURL

Introduction

In this page you can find the example usage for twitter4j URLEntity getExpandedURL.

Prototype

String getExpandedURL();

Source Link

Document

Returns the expanded URL if mentioned URL is shorten.

Usage

From source file:de.vanita5.twittnuker.util.TwitterContentUtils.java

License:Open Source License

public static String formatExpandedUserDescription(final User user) {
    if (user == null)
        return null;
    final String text = user.getDescription();
    if (text == null)
        return null;
    final HtmlBuilder builder = new HtmlBuilder(text, false, true, true);
    final URLEntity[] urls = user.getDescriptionEntities();
    if (urls != null) {
        for (final URLEntity url : urls) {
            final String expanded_url = ParseUtils.parseString(url.getExpandedURL());
            if (expanded_url != null) {
                builder.addLink(expanded_url, expanded_url, url.getStart(), url.getEnd());
            }/*from  www  .  jav  a2s.  c  o m*/
        }
    }
    return toPlainText(builder.build().replace("\n", "<br/>"));
}

From source file:de.vanita5.twittnuker.util.TwitterContentUtils.java

License:Open Source License

public static String formatUserDescription(final User user) {
    if (user == null)
        return null;
    final String text = user.getDescription();
    if (text == null)
        return null;
    final HtmlBuilder builder = new HtmlBuilder(text, false, true, true);
    final URLEntity[] urls = user.getDescriptionEntities();
    if (urls != null) {
        for (final URLEntity url : urls) {
            final URL expanded_url = url.getExpandedURL();
            if (expanded_url != null) {
                builder.addLink(ParseUtils.parseString(expanded_url), url.getDisplayURL(), url.getStart(),
                        url.getEnd());/* www .  jav a  2  s  .  c o m*/
            }
        }
    }
    return builder.build().replace("\n", "<br/>");
}

From source file:de.vanita5.twittnuker.util.TwitterContentUtils.java

License:Open Source License

private static void parseEntities(final HtmlBuilder builder, final EntitySupport entities) {
    // Format media.
    final MediaEntity[] mediaEntities = entities.getMediaEntities();
    if (mediaEntities != null) {
        for (final MediaEntity mediaEntity : mediaEntities) {
            final int start = mediaEntity.getStart(), end = mediaEntity.getEnd();
            final URL mediaUrl = mediaEntity.getMediaURL();
            if (mediaUrl != null && start >= 0 && end >= 0) {
                builder.addLink(ParseUtils.parseString(mediaUrl), mediaEntity.getDisplayURL(), start, end);
            }//from  www .  j a v  a  2s  . c om
        }
    }
    final URLEntity[] urlEntities = entities.getURLEntities();
    if (urlEntities != null) {
        for (final URLEntity urlEntity : urlEntities) {
            final int start = urlEntity.getStart(), end = urlEntity.getEnd();
            final URL expandedUrl = urlEntity.getExpandedURL();
            if (expandedUrl != null && start >= 0 && end >= 0) {
                builder.addLink(ParseUtils.parseString(expandedUrl), urlEntity.getDisplayURL(), start, end);
            }
        }
    }
}

From source file:dk.netarkivet.harvester.tools.TwitterDecidingScope.java

License:Open Source License

/**
 * Adds links to embedded url's and media in a tweet.
 *
 * @param tweet The tweet from which links are to be extracted.
 */// ww  w .j a  v a 2 s .  c  o m
private void extractEmbeddedLinks(Tweet tweet) {
    final URLEntity[] urlEntities = tweet.getURLEntities();
    if (urlEntities != null) {
        for (URLEntity urlEntity : urlEntities) {
            addSeedIfLegal(urlEntity.getURL().toString());
            addSeedIfLegal(urlEntity.getExpandedURL().toString());
            linkCount++;
        }
    }
    final MediaEntity[] mediaEntities = tweet.getMediaEntities();
    if (mediaEntities != null) {
        for (MediaEntity mediaEntity : mediaEntities) {
            final String mediaUrl = mediaEntity.getMediaURL().toString();
            addSeedIfLegal(mediaUrl);
            linkCount++;
        }
    }
}

From source file:info.usbo.skypetwitter.Run.java

License:Apache License

public static void main(String[] args) throws SkypeException, IOException {
    System.out.println("Working Directory = " + System.getProperty("user.dir"));
    Properties props = new Properties();
    loadProperties(props, "twitter4j.properties");
    loadProperties(props, "skype.properties");
    loadProperties(props, "app.properties");
    String chat_group_id = props.getProperty("skype.chat_group_id");
    String twitter_user_id = props.getProperty("twitter.user");
    data_dir = props.getProperty("data.dir");
    Integer twitter_timeout = Integer.parseInt(props.getProperty("twitter.timeout"));
    System.out.println("Twitter user: " + twitter_user_id);
    System.out.println("Twitter timeout: " + twitter_timeout);
    if ("".equals(twitter_user_id)) {
        return;//from  w ww. j  a v  a2 s .co  m
    }
    if (load_file() == 0) {
        System.out.println("File not found");
        return;
    }
    while (true) {
        bChanged = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm");
        System.out.println("Looking at " + sdf.format(Calendar.getInstance().getTime()));
        Chat ch = Skype.chat(chat_group_id);
        Twitter twitter = new TwitterFactory().getInstance();
        try {
            List<Status> statuses;
            statuses = twitter.getUserTimeline(twitter_user_id);
            String sText;
            for (Status status : statuses) {
                Date d = status.getCreatedAt();
                //  ?
                Calendar cal = Calendar.getInstance();
                cal.setTime(d);
                cal.add(Calendar.HOUR_OF_DAY, 7);
                d = cal.getTime();
                sText = "@" + status.getUser().getScreenName() + "  " + sdf.format(d)
                        + " ( https://twitter.com/" + twitter_user_id + "/status/" + status.getId() + " ): \r\n"
                        + status.getText() + "\r\n***";
                for (URLEntity e : status.getURLEntities()) {
                    sText = sText.replaceAll(e.getURL(), e.getExpandedURL());
                }
                for (MediaEntity e : status.getMediaEntities()) {
                    sText = sText.replaceAll(e.getURL(), e.getMediaURL());
                }
                if (twitter_ids.indexOf(status.getId()) == -1) {
                    System.out.println(sText);
                    ch.send(sText);
                    twitter_ids.add(status.getId());
                    bChanged = 1;
                }
            }
        } catch (TwitterException te) {
            te.printStackTrace();
            System.out.println("Failed to get timeline: " + te.getMessage());
            // System.exit(-1);
        } catch (SkypeException ex) {
            ex.printStackTrace();
            System.out.println("Failed to send message: " + ex.getMessage());
        }

        // VK
        try {
            vk();
            for (VK v : vk) {
                if (vk_ids.indexOf(v.getId()) == -1) {
                    Date d = v.getDate();
                    //  ?
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(d);
                    cal.add(Calendar.HOUR_OF_DAY, 7);
                    d = cal.getTime();
                    String sText = "@Depersonilized (VK)  " + sdf.format(d)
                            + " ( http://vk.com/Depersonilized?w=wall-0_" + v.getId() + " ): \r\n"
                            + v.getText();
                    if (!"".equals(v.getAttachment())) {
                        sText += "\r\n" + v.getAttachment();
                    }
                    sText += "\r\n***";
                    System.out.println(sText);
                    ch.send(sText);
                    vk_ids.add(v.getId());
                    bChanged = 1;
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
            System.out.println("Failed to get vk: " + e.getMessage());
            // System.exit(-1);
        } catch (SkypeException ex) {
            ex.printStackTrace();
            System.out.println("Failed to send message: " + ex.getMessage());
        }
        if (bChanged == 1) {
            save_file();
        }
        try {
            Thread.sleep(1000 * 60 * twitter_timeout);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
            System.out.println("Failed to sleep: " + ex.getMessage());
        }
    }
}

From source file:net.lacolaco.smileessence.twitter.util.TwitterUtils.java

License:Open Source License

/**
 * Replace urls by entities/*from   w ww  . j  a v a 2s  . c o m*/
 *
 * @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;
}

From source file:net.lacolaco.smileessence.view.dialog.MessageDetailDialogFragment.java

License:Open Source License

private ArrayList<Command> getCommands(Activity activity, DirectMessage message, Account account) {
    ArrayList<Command> commands = new ArrayList<>();
    // URL//from   w w w .j  a  v a 2  s  . co m
    if (message.getURLEntities() != null) {
        for (URLEntity urlEntity : message.getURLEntities()) {
            commands.add(new CommandOpenURL(activity, urlEntity.getExpandedURL()));
        }
    }
    for (MediaEntity mediaEntity : getMediaEntities(message)) {
        commands.add(new CommandOpenURL(activity, mediaEntity.getMediaURL()));
    }
    return commands;
}

From source file:net.lacolaco.smileessence.view.dialog.MessageMenuDialogFragment.java

License:Open Source License

public void addBottomCommands(Activity activity, DirectMessage message, Account account,
        ArrayList<Command> commands) {
    commands.add(new CommandSaveAsTemplate(activity, message.getText()));
    //User//from   w  ww .  j  a  v a 2s. co  m
    for (String screenName : TwitterUtils.getScreenNames(message, null)) {
        commands.add(new CommandOpenUserDetail(activity, screenName, account));
    }
    for (Command command : getHashtagCommands(activity, message)) {
        commands.add(command);
    }
    // Media
    if (message.getURLEntities() != null) {
        for (URLEntity urlEntity : message.getURLEntities()) {
            commands.add(new CommandOpenURL(activity, urlEntity.getExpandedURL()));
        }
    }
    for (MediaEntity mediaEntity : message.getExtendedMediaEntities().length == 0 ? message.getMediaEntities()
            : message.getExtendedMediaEntities()) {
        commands.add(new CommandOpenURL(activity, mediaEntity.getMediaURL()));
    }
}

From source file:net.lacolaco.smileessence.view.dialog.StatusDetailDialogFragment.java

License:Open Source License

private ArrayList<Command> getCommands(Activity activity, Status status, Account account) {
    ArrayList<Command> commands = new ArrayList<>();
    // URL/*w w w . j a  va2s  .  co  m*/
    if (status.getURLEntities() != null) {
        for (URLEntity urlEntity : status.getURLEntities()) {
            commands.add(new CommandOpenURL(activity, urlEntity.getExpandedURL()));
        }
    }
    for (MediaEntity mediaEntity : getMediaEntities(status)) {
        commands.add(new CommandOpenURL(activity, mediaEntity.getMediaURL()));
    }
    // ReplyToAll
    commands.add(new StatusCommandReplyToAll(activity, status, account));
    return commands;
}

From source file:net.lacolaco.smileessence.view.dialog.StatusMenuDialogFragment.java

License:Open Source License

public void addBottomCommands(Activity activity, Status status, Account account, ArrayList<Command> commands) {
    commands.add(new CommandSaveAsTemplate(activity, TwitterUtils.getOriginalStatusText(status)));
    //User// w  w w  .  ja  v  a 2 s.  co  m
    for (String screenName : TwitterUtils.getScreenNames(status, null)) {
        commands.add(new CommandOpenUserDetail(activity, screenName, account));
    }
    for (Command command : getHashtagCommands(activity, status)) {
        commands.add(command);
    }
    // Media
    if (status.getURLEntities() != null) {
        for (URLEntity urlEntity : status.getURLEntities()) {
            commands.add(new CommandOpenURL(activity, urlEntity.getExpandedURL()));
        }
    }
    for (MediaEntity mediaEntity : status.getExtendedMediaEntities().length == 0 ? status.getMediaEntities()
            : status.getExtendedMediaEntities()) {
        commands.add(new CommandOpenURL(activity, mediaEntity.getMediaURL()));
    }
}