Example usage for twitter4j Status getUser

List of usage examples for twitter4j Status getUser

Introduction

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

Prototype

User getUser();

Source Link

Document

Return the user associated with the status.
This can be null if the instance is from User.getStatus().

Usage

From source file:io.rakam.datasource.twitter.TweetProcessor.java

License:Apache License

@Override
public void onStatus(Status status) {
    Map<String, Object> map = new HashMap<>();

    GeoLocation geoLocation = status.getGeoLocation();
    if (geoLocation != null) {
        map.put("latitude", geoLocation.getLatitude());
        map.put("longitude", geoLocation.getLongitude());
    }//from ww  w  .j  a v a2s. co  m

    map.put("_time", status.getCreatedAt().getTime());
    Place place = status.getPlace();
    if (place != null) {
        map.put("country_code", place.getCountryCode());
        map.put("place", place.getName());
        map.put("place_type", place.getPlaceType());
        map.put("place_id", place.getId());
    }

    User user = status.getUser();
    map.put("_user", user.getId());
    map.put("user_lang", user.getLang());
    map.put("user_created", user.getCreatedAt());
    map.put("user_followers", user.getFollowersCount());
    map.put("user_status_count", user.getStatusesCount());
    map.put("user_verified", user.isVerified());

    map.put("id", status.getId());
    map.put("is_reply", status.getInReplyToUserId() > -1);
    map.put("is_retweet", status.isRetweet());
    map.put("has_media", status.getMediaEntities().length > 0);
    map.put("urls",
            Arrays.stream(status.getURLEntities()).map(URLEntity::getText).collect(Collectors.toList()));
    map.put("hashtags", Arrays.stream(status.getHashtagEntities()).map(HashtagEntity::getText)
            .collect(Collectors.toList()));
    map.put("user_mentions", Arrays.stream(status.getUserMentionEntities()).map(UserMentionEntity::getText)
            .collect(Collectors.toList()));
    map.put("language", "und".equals(status.getLang()) ? null : status.getLang());
    map.put("is_positive", classifier.isPositive(status.getText()));

    Event event = new Event().properties(map).collection(collection);
    buffer.add(event);

    commitIfNecessary();
}

From source file:it.greenvulcano.gvesb.social.twitter.directcall.TwitterOperationBase.java

License:Open Source License

/**
 * @param parser//from ww  w. j  a  v  a  2s . co m
 * @param root
 * @param status
 * @throws XMLUtilsException
 */
protected void dumpTweet(XMLUtils parser, Element root, Status status) throws XMLUtilsException {
    Element tweet = parser.insertElement(root, "Tweet");
    parser.setAttribute(tweet, "id", String.valueOf(status.getId()));
    parser.setAttribute(tweet, "createdAt",
            DateUtils.dateToString(status.getCreatedAt(), DateUtils.FORMAT_ISO_DATETIME_UTC));
    parser.setAttribute(tweet, "fromUser", status.getUser().getScreenName());
    parser.setAttribute(tweet, "fromUserId", String.valueOf(status.getUser().getId()));
    parser.insertText(tweet, status.getText());
}

From source file:it.unipr.aotlab.TwitterMiner.twitter.client.TwitterStreamListener.java

License:Open Source License

/**
 * This method calls the update function of the database to store
 * informations about received tweets//from  w w w. jav a  2  s .  c  o  m
 */
@Override
public void onStatus(Status status) {

    System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());

    hashtag = status.getHashtagEntities();
    mentions = status.getUserMentionEntities();

    redisDB.update(status.getUser().getScreenName(), hashtag, mentions);
}

From source file:JavaFiles.TwitterAPI.java

public Report getTweet(Report report, String keyword) {

    try {/*w w w. j a  v  a2 s  . c  om*/
        Query query = new Query(keyword);
        QueryResult result = twitter.search(query);

        for (Status status : result.getTweets()) {
            report.setTweet(status.getText());
            report.setTweetID(status.getId());
            report.setUserID(status.getUser().getName());
        }
    } catch (TwitterException te) {
        te.printStackTrace();
    }
    return report;
}

From source file:jp.gihyo.wicket.page.ajax.AjaxTimeline.java

License:Apache License

private void constructPage() {
    final TweetForm form = new TweetForm("tweetForm");
    add(form);/*from   w w w  .  ja va2s .  c  om*/

    feedback = new FeedbackPanel("feedback");
    feedback.setOutputMarkupId(true);
    add(feedback);

    IModel<List<Status>> statusModel = new LoadableDetachableModel<List<Status>>() {
        @Override
        protected List<Status> load() {
            try {
                Twitter twitter = AppSession.get().getTwitterSession();
                return twitter.getFriendsTimeline(new Paging(currentPageNumber, ITEMS_PER_PAGE));
            } catch (TwitterException ex) {
                AjaxTimeline.this.error(getString("canNotRetrieveFriendTimeline"));
                return Collections.emptyList();
            }
        }
    };

    ListView<Status> timeline = new ListView<Status>("statusView", statusModel) {
        @Override
        protected void populateItem(final ListItem<Status> item) {
            final Status status = item.getModelObject();
            String userUrl = "http://twitter.com/" + status.getUser().getScreenName();
            ExternalLink imageLink = new ExternalLink("imageLink", userUrl);

            //ImageR|?[lg?A<img>^Osrc??X`?X
            WebMarkupContainer userImage = new WebMarkupContainer("userImage");
            userImage.add(new SimpleAttributeModifier("src", status.getUser().getProfileImageURL().toString()));

            imageLink.add(userImage);
            item.add(imageLink);

            ExternalLink screenNameLink = new ExternalLink("screenName", userUrl,
                    status.getUser().getScreenName());
            item.add(screenNameLink);

            Label content = new Label("tweetContent", status.getText());
            item.add(content);

            ExternalLink tweetLink = new ExternalLink("tweetLink", userUrl + "/status/" + status.getId(), null);
            item.add(tweetLink);

            Label time = new Label("tweetTime",
                    new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(status.getCreatedAt()));
            tweetLink.add(time);

            Label clientName = new Label("clientName", status.getSource());
            item.add(clientName.setEscapeModelStrings(false));

            /*
             * YXe?[^XCo^\xNX?B
             * ?\bh?A??[JNX`?B
             * ??[JNX?ANXOstatus?ANZX?B
             */
            class FavorateLabel extends Label {
                private static final long serialVersionUID = -2194580825236126312L;
                private Status targetStatus;
                private boolean needRefresh;

                public FavorateLabel(String id) {
                    super(id);
                    this.targetStatus = status;

                    setDefaultModel(new AbstractReadOnlyModel<String>() {
                        @Override
                        public String getObject() {
                            try {
                                if (needRefresh) {
                                    targetStatus = getCurrentStatus(status.getId());
                                    needRefresh = false;
                                }
                                return targetStatus == null ? "" : targetStatus.isFavorited() ? "unfav" : "fav";
                            } catch (TwitterException ex) {
                                LOGGER.error("Can not fetch current status for status id = " + status.getId(),
                                        ex);
                                return "error";
                            }
                        }
                    });
                }

                public void setNeedRefresh(boolean needRefresh) {
                    this.needRefresh = needRefresh;
                }
            }

            //CNx
            final FavorateLabel favName = new FavorateLabel("favName");
            favName.setOutputMarkupId(true);

            /*
             * AjaxCN?B
             * Xe?[^XCo^o^?Ao^???s?B
             * o^???AAjaxg?Ay?[WS?ACo^Xe?[^X
             * Nx??B
             */
            AjaxLink<Void> favLink = new AjaxLink<Void>("favLink") {
                @Override
                public void onClick(AjaxRequestTarget target) {
                    try {
                        Status currentStatus = getCurrentStatus(status.getId());
                        Twitter twitterSession = AppSession.get().getTwitterSession();
                        if (currentStatus.isFavorited()) {
                            twitterSession.destroyFavorite(currentStatus.getId());
                            info(getString("favorateRemoved"));
                        } else {
                            twitterSession.createFavorite(currentStatus.getId());
                            info(getString("favorateRegistered"));
                        }
                        favName.setNeedRefresh(true);
                        target.addComponent(feedback); //o^?bZ?[W\?AtB?[hobNpl?X?V?B
                        target.addComponent(favName);
                    } catch (TwitterException ex) {
                        String message = getString("catNotCreateFavorite") + ": " + ex.getStatusCode();
                        error(message);
                        LOGGER.error(message, ex);
                    }
                }
            };
            item.add(favLink);
            favLink.add(favName);

            //AJAX LINK
            item.add(new AjaxLink<Void>("replyLink") {
                @Override
                public void onClick(AjaxRequestTarget target) {
                    String targetScreenName = status.getUser().getScreenName();
                    form.insertText(target, "@" + targetScreenName + " ");
                }
            });

            //Q?l?AreplyLinkJavaScriptp
            //                item.add(new Link<Void>("replyLink") {
            //                    @Override
            //                    public void onClick() {
            //                    }
            //
            //                    @Override
            //                    protected CharSequence getOnClickScript(CharSequence url) {
            //                        return "getElementById('" + form.getTextAreaId() + "').value = '@" + status.getUser().getScreenName() + " ';" +
            //                               "getElementById('" + form.getTextAreaId() + "').focus(); return false;";
            //                    }
            //                });
        }
    };

    //ListView\e?s????BreuseItemsv?peB??A
    //y?[W\?Ay?[WTu~bg?AXgeIuWFNg\??B
    //twitterey?[We?X??AXg????AXge?
    //Xe?[^Xu?A??dv?B
    timeline.setReuseItems(true);
    add(timeline);

    /*
     * y?[WO?EirQ?[^
     */
    add(new PagingLink("paging", AjaxTimeline.class, new AbstractReadOnlyModel<Integer>() {
        @Override
        public Integer getObject() {
            return getCurrentPage();
        }
    }));
}

From source file:jp.gihyo.wicket.page.paging.PagingTimeline.java

License:Apache License

private void constructPage() {
    final TweetForm form = new TweetForm("tweetForm");
    add(form);/*from w  w w  .j  a v  a2s.  com*/

    feedback = new FeedbackPanel("feedback");
    feedback.setOutputMarkupId(true);
    add(feedback);

    /*
     * ^CCpIModel?B
     * ??[hf?[^NGXg?EX|X?ETCNLbV?A
     * LoadableDetachableModelNXgp?B
     */
    IModel<List<Status>> statusModel = new LoadableDetachableModel<List<Status>>() {
        @Override
        protected List<Status> load() {
            try {
                Twitter twitter = AppSession.get().getTwitterSession();
                return twitter.getFriendsTimeline(new Paging(currentPageNumber, ITEMS_PER_PAGE));
            } catch (TwitterException ex) {
                PagingTimeline.this.error(getString("canNotRetrieveFriendTimeline"));
                return Collections.emptyList();
            }
        }
    };

    /*
     * ^CCXg
     */
    ListView<Status> timeline = new ListView<Status>("statusView", statusModel) {
        @Override
        protected void populateItem(final ListItem<Status> item) {
            final Status status = item.getModelObject();
            String userUrl = "http://twitter.com/" + status.getUser().getScreenName();
            ExternalLink imageLink = new ExternalLink("imageLink", userUrl);

            //ImageR|?[lg?A<img>^Osrc??X`?X
            WebMarkupContainer userImage = new WebMarkupContainer("userImage");
            userImage.add(new SimpleAttributeModifier("src", status.getUser().getProfileImageURL().toString()));

            imageLink.add(userImage);
            item.add(imageLink);

            ExternalLink screenNameLink = new ExternalLink("screenName", userUrl,
                    status.getUser().getScreenName());
            item.add(screenNameLink);

            Label content = new Label("tweetContent", status.getText());
            item.add(content);

            ExternalLink tweetLink = new ExternalLink("tweetLink", userUrl + "/status/" + status.getId(), null);
            item.add(tweetLink);

            Label time = new Label("tweetTime",
                    new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(status.getCreatedAt()));
            tweetLink.add(time);

            Label clientName = new Label("clientName", status.getSource());
            item.add(clientName.setEscapeModelStrings(false));

            /*
             * CNx?BIModel?f?A?fav/unfav
             * \?B
             */
            final Label favName = new Label("favName", new AbstractReadOnlyModel<String>() {
                @Override
                public String getObject() {
                    return status.isFavorited() ? "unfav" : "fav";
                }
            });

            /*
             * CN?BNbN?C?E??s?B
             */
            Link<Void> favLink = new Link<Void>("favLink") {
                @Override
                public void onClick() {
                    try {
                        Twitter twitterSession = AppSession.get().getTwitterSession();
                        if (status.isFavorited()) {
                            twitterSession.destroyFavorite(status.getId());
                            info(getString("favorateRemoved"));
                        } else {
                            twitterSession.createFavorite(status.getId());
                            info(getString("favorateRegistered"));
                        }
                    } catch (TwitterException ex) {
                        String message = getString("catNotCreateFavorite") + ": " + ex.getStatusCode();
                        error(message);
                        LOGGER.error(message, ex);
                    }
                }
            };
            item.add(favLink);
            favLink.add(favName);

            /*
             * vCpN?B
             * ?u@screenName?v?B
             */
            item.add(new Link<Void>("replyLink") {
                @Override
                public void onClick() {
                    String targetScreenName = status.getUser().getScreenName();
                    form.insertText("@" + targetScreenName + " ");
                }
            });
        }
    };

    //ListView\e?s????BreuseItemsv?peB??A
    //y?[W\?Ay?[WTu~bg?AXgeIuWFNg\??B
    //twitterey?[We?X??AXg????AXge?
    //Xe?[^Xu?A??dv?B
    timeline.setReuseItems(true);

    add(timeline);

    /*
     * y?[WO?EirQ?[^
     */
    add(new PagingLink("paging", PagingTimeline.class, new AbstractReadOnlyModel<Integer>() {
        @Override
        public Integer getObject() {
            return getCurrentPage();
        }
    }));
}

From source file:jp.gihyo.wicket.page.simple.MyTimeline.java

License:Apache License

private void constructPage() {
    final TweetForm form = new TweetForm("tweetForm");
    add(form);/*  www  .j  av  a 2 s .  c o  m*/

    feedback = new FeedbackPanel("feedback");
    feedback.setOutputMarkupId(true);
    add(feedback);

    /*
     * ^CCpIModel?B
     * ??[hf?[^NGXg?EX|X?ETCNLbV?A
     * LoadableDetachableModelNXgp?B
     */
    IModel<List<Status>> statusModel = new LoadableDetachableModel<List<Status>>() {
        @Override
        protected List<Status> load() {
            try {
                Twitter twitter = AppSession.get().getTwitterSession();
                return twitter.getFriendsTimeline(new Paging(1, ITEMS_PER_PAGE));
            } catch (TwitterException ex) {
                MyTimeline.this.error(getString("canNotRetrieveFriendTimeline"));
                return Collections.emptyList();
            }
        }
    };

    /*
     * ^CCXg
     */
    ListView<Status> timeline = new ListView<Status>("statusView", statusModel) {
        @Override
        protected void populateItem(final ListItem<Status> item) {
            final Status status = item.getModelObject();
            String userUrl = "http://twitter.com/" + status.getUser().getScreenName();
            ExternalLink imageLink = new ExternalLink("imageLink", userUrl);
            byte[] byteArray = {};/* f?[^?? */
            UrlResourceStream stream = new UrlResourceStream(status.getUser().getProfileImageURL());
            try {
                stream.getInputStream().read(byteArray);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ResourceStreamNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Image userImage = new Image("userImage", new ByteArrayResource("image/jpeg", byteArray));
            imageLink.add(userImage);
            item.add(imageLink);

            ExternalLink screenNameLink = new ExternalLink("screenName", userUrl,
                    status.getUser().getScreenName());
            item.add(screenNameLink);

            Label content = new Label("tweetContent", status.getText());
            item.add(content);

            ExternalLink tweetLink = new ExternalLink("tweetLink", userUrl + "/status/" + status.getId(), null);
            item.add(tweetLink);

            Label time = new Label("tweetTime",
                    new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(status.getCreatedAt()));
            tweetLink.add(time);

            Label clientName = new Label("clientName", status.getSource());
            item.add(clientName.setEscapeModelStrings(false));

            /*
             * CNx?BIModel?f?A?fav/unfav
             * \?B
             */
            final Label favName = new Label("favName", new AbstractReadOnlyModel<String>() {
                @Override
                public String getObject() {
                    return status.isFavorited() ? "unfav" : "fav";
                }
            });

            /*
             * CN?BNbN?C?E??s?B
             */
            Link<Void> favLink = new Link<Void>("favLink") {
                @Override
                public void onClick() {
                    try {
                        Twitter twitterSession = AppSession.get().getTwitterSession();
                        if (status.isFavorited()) {
                            twitterSession.destroyFavorite(status.getId());
                            info(getString("favorateRemoved"));
                        } else {
                            twitterSession.createFavorite(status.getId());
                            info(getString("favorateRegistered"));
                        }
                    } catch (TwitterException ex) {
                        String message = getString("catNotCreateFavorite") + ": " + ex.getStatusCode();
                        error(message);
                        LOGGER.error(message, ex);
                    }
                }
            };
            item.add(favLink);
            favLink.add(favName);

            /*
             * vCpN?B
             * ?u@screenName?v?B
             */
            item.add(new Link<Void>("replyLink") {
                @Override
                public void onClick() {
                    String targetScreenName = status.getUser().getScreenName();
                    form.insertText("@" + targetScreenName + " ");
                }
            });
        }
    };
    //ListView\e?s????BreuseItemsv?peB??A
    //y?[W\?Ay?[WTu~bg?AXgeIuWFNg\??B
    //twitterey?[We?X??AXg????AXge?
    //Xe?[^Xu?A??dv?B
    timeline.setReuseItems(true);

    add(timeline);
}

From source file:jp.maju.wifiserver.client.ClientActivity.java

License:Apache License

@Override
public void onPosted(Status status, boolean isLogin) {
    // This is called from another thread!
    sendMessage("tweet://" + status.getId() + ":" + status.getUser().getScreenName() + ":"
            + PreferenceUtil.getUUID(getApplication()) + ":" + isLogin);
}

From source file:jp.wda.azeda.core.daemons.impl.RetweetsCrawlerImpl.java

License:Apache License

/**
 *
 *//*w  ww. j a  v  a 2  s .c  o  m*/
@Aspect("aop.requiredTx")
public void crawl() throws TwitterException {
    log.debug("crawl retweets!!" + tweets.size());
    if (tweets.size() == 0) {
        loadTweets();
    }
    if (tweets.size() == 0) {
        return;
    }

    RetweetedTweet tweet = tweets.remove(0);
    log.debug("check tweet " + tweet.getTweetID());

    Twitter twitter = new TwitterFactory().getInstance();
    AccessToken accessToken = new AccessToken(tweet.getAccessToken().getAccessToken(),
            tweet.getAccessToken().getAccessTokenSecret());
    twitter.setOAuthAccessToken(accessToken);

    ResponseList<Status> rt = getRetweets(twitter, tweet.getTweetID());
    if (!alive) {
        return;
    }
    if (rt == null || rt.size() == 0) {
        return;
    }

    HashMap<Long, Status> retweets = new HashMap<Long, Status>();
    for (Status status : rt) {
        retweets.put(status.getUser().getId(), status);
    }

    List<Retweeter> registered = retweetersDao.getRetweeterIDs(tweet.getTweetID());
    for (Retweeter retweeter : registered) {
        long registeredUserID = retweeter.getRetweeterID();
        if (retweets.containsKey(registeredUserID)) {
            retweets.remove(registeredUserID);
            continue;
        }
    }
    if (retweets.size() == 0) {
        return;
    }

    for (Status status : retweets.values()) {
        long retweeter = status.getUser().getId();
        long oldest = System.currentTimeMillis() - retweetExpired * 60 * 1000L;
        if (status.getCreatedAt().getTime() < oldest) {
            log.info(tweet.getTweetID() + " retweet by " + status.getUser().getScreenName() + " ? "
                    + retweetExpired + "?????????");
            insertRetweeter(twitter.getId(), tweet.getTweetID(), retweeter);
            continue;
        }

        List<Status> userTimeline = loadRetweetersTweets(twitter, tweet.getTweetID(), retweeter, 1);
        if (!alive) {
            return;
        }
        if (userTimeline == null) {
            continue;
        }

        int skips = 0;
        for (int i = userTimeline.size() - 1; i >= 0; i--) {
            Status s = userTimeline.get(i);
            UserMentionEntity[] mentions = s.getUserMentionEntities();
            if (s.isRetweet()) {
                skips++;
                continue;
            }
            if (mentions == null || mentions.length > 0) {
                skips++;
                continue;
            }

            log.debug(s.getId() + ":" + s.getCreatedAt() + "(" + s.isRetweet() + ") " + s.getText());

            Retweet retweet = new Retweet();
            retweet.setTweetID(s.getId());
            retweet.setRetweetTo(tweet.getTweetID());
            retweet.setRetweeterID(retweeter);
            retweet.setTweettext(s.getText());
            retweet.setCreatedat(new Timestamp(s.getCreatedAt().getTime()));
            retweet.setSkiptweets(skips);
            retweet.setScreenName(status.getUser().getScreenName());
            dtoRetweets.add(retweet);

            insertRetweeter(twitter.getId(), tweet.getTweetID(), retweeter);
            break;
        }
    }

    register();
}

From source file:Jums.SearchTweets.java

License:Apache License

/**
 * Usage: java twitter4j.examples.search.SearchTweets [query]
 *
 * @param args search query/*from w ww  .  j  a va 2  s.  c  o m*/
 */
public void main(String args) {
    System.out.println("java twitter4j.examples.search.SearchTweets [query]");
    System.exit(-1);

    Twitter twitter = new TwitterFactory().getInstance();
    try {
        Query query = new Query(args);
        QueryResult result;
        do {
            result = twitter.search(query);
            List<Status> tweets = result.getTweets();
            for (Status tweet : tweets) {
                System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText());
            }
        } while ((query = result.nextQuery()) != null);
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to search tweets: " + te.getMessage());
        System.exit(-1);
    }
}