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:twitterrest.SearchTweet.java

License:Apache License

public static void main(String[] args) throws TwitterException {
    //?/*from   ww w . ja v  a 2s.  c  o m*/
    Configuration configuration = new ConfigurationBuilder().setOAuthConsumerKey(CONSUMER_KEY)
            .setOAuthConsumerSecret(CONSUMER_SECRET).setOAuthAccessToken(ACCESS_TOKEN)
            .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET).build();
    Twitter twitter = new TwitterFactory(configuration).getInstance();
    Query query = new Query();
    try {
        File file = new File("./file/tweets.txt");
        PrintWriter pw;
        //???
        pw = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));

        // ???
        query.setQuery("?");
        query.setLang("ja");
        // 1????Tweet?100?
        query.setCount(100);
        //?????????
        //query.setSince("2013-06-13");
        //query.setUntil("2013-06-17");

        // 150015
        for (int i = 1; i <= 15; i++) {
            // 
            QueryResult result = twitter.search(query);
            System.out.println(": " + result.getTweets().size());
            System.out.println(" : " + new Integer(i).toString());

            // ???
            for (Status tweet : result.getTweets()) {
                // 
                String str = tweet.getText();
                System.out.println(str);
                //
                System.out.println(tweet.getUser());
                //
                System.out.println(tweet.getCreatedAt());
                // ??URL?   
                StringTokenizer sta = new StringTokenizer(str, " ");
                //?
                while (sta.hasMoreTokens()) {
                    String wk = sta.nextToken();
                    if (wk.indexOf("#") == -1 && wk.indexOf("http") == -1 && wk.indexOf("RT") == -1
                            && wk.indexOf("@") == -1) {
                        pw.println(wk);
                    }
                }
                String u = tweet.getUser().getName();
                pw.println(u);
            }
            if (result.hasNext()) {
                query = result.nextQuery();
            } else {
                break;
            }
        }
        pw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:twitterrest.Timeline.java

License:Apache License

public static void main(String[] args) throws TwitterException {

    Configuration configuration = new ConfigurationBuilder().setOAuthConsumerKey(CONSUMER_KEY)
            .setOAuthConsumerSecret(CONSUMER_SECRET).setOAuthAccessToken(ACCESS_TOKEN)
            .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET).build();

    Twitter tw = new TwitterFactory(configuration).getInstance();
    List<Status> list = tw.getUserTimeline("masason");//ScreenName[anondroid5]

    for (Status status : list) {
        System.out.print(status.getCreatedAt() + " ");//CreatedDate
        System.out.print(status.getUser().getScreenName() + " ");//ScreenName
        System.out.println(status.getText());//tweet
    }//  w  w  w  .  ja va2  s  .  c o m
}

From source file:twitterrest.Tweet.java

License:Apache License

public static void main(String[] args) throws IOException, TwitterException {
    //?// w  w w.  j  av  a  2 s  .  c o m
    Configuration configuration = new ConfigurationBuilder().setOAuthConsumerKey(CONSUMER_KEY)
            .setOAuthConsumerSecret(CONSUMER_SECRET).setOAuthAccessToken(ACCESS_TOKEN)
            .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET).build();
    Twitter tw = new TwitterFactory(configuration).getInstance();
    //date?
    Date date = new Date();
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd (EE) HH:mm:ss");

    //tweet Input
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String tweet = "";
    while (tweet.isEmpty()) {
        System.out.print("?? ? : ");
        tweet = br.readLine();
        if (tweet.length() > 140) {
            System.out.println("??????");
            tweet = "";
            continue;
        }
    }
    Status status = tw.updateStatus(tweet + df.format(date));//tweet
    System.out.println(status.getUser().getScreenName() + " ??????? : " + status.getText());
    br.close();
}

From source file:twitterrest.UserSearch.java

License:Apache License

public static void main(String[] args) throws TwitterException {
    //?//from   w ww.  ja v a  2s . co  m
    Configuration configuration = new ConfigurationBuilder().setOAuthConsumerKey(CONSUMER_KEY)
            .setOAuthConsumerSecret(CONSUMER_SECRET).setOAuthAccessToken(ACCESS_TOKEN)
            .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET).build();
    Twitter twitter = new TwitterFactory(configuration).getInstance();
    Query query = new Query();

    // masason?Tweet
    //query.setQuery("from:anondroid3 OR to:anondroid3");
    query.setQuery("from:anondroid3");

    // ???
    QueryResult result = twitter.search(query);
    //
    System.out.println(":" + result.getTweets().size());
    // 1????Tweet?100?
    query.setCount(100);

    for (Status tweet : result.getTweets()) {
        System.out.println("tweet:" + tweet.getText());//
        System.out.println("UserID:" + tweet.getUser().getId());//ID
        System.out.println("Application:" + tweet.getSource());//
        System.out.println("Created Date:" + tweet.getCreatedAt());//?
        System.out.println("GeoLocation:" + tweet.getGeoLocation());//
    }
}

From source file:twittersentimentanalysis.TwitterSentimentAnalysis.java

private static Tweet getTweetObject(Status status) {
    Tweet tweet = new Tweet();
    int sentimentScore = StanfordCoreNLPTool.findSentiment(status.getText());
    if (sentimentScore != -1) {
        tweet.setDateTime(status.getCreatedAt());
        tweet.setTweetText(status.getText());
        tweet.setUsername(status.getUser().getScreenName());
        tweet.setSentimentScore(sentimentScore);
        GeoLocation geoLocation = status.getGeoLocation();
        if (geoLocation != null) {
            tweet.setLongitude(geoLocation.getLongitude() + "");
            tweet.setLatitude(geoLocation.getLatitude() + "");
        } else {//  www.  j a va  2  s. c o m
            tweet.setLongitude(null);//
            tweet.setLatitude(null);//
        }
        Place place = status.getPlace();
        if (place != null) {
            tweet.setCountry(place.getCountry());
            tweet.setPlace(place.getFullName());
        } else {
            tweet.setCountry(null);//
            tweet.setPlace(null);//
        }
    } else
        tweet = null;
    return tweet;
}

From source file:TwitterStats.Beans.TweetRelevantesBean.java

public String doBuscarNumEstudio() {
    try {/* w w  w  . j ava2s  . co m*/
        this.listaTweets = twitter.getTuitsCuenta(this.busqueda, this.numEstudio, this.numTweetsMostrar);

        if (listaTweets.size() > 0) {
            Status st = listaTweets.get(0);
            this.cuentaTwitter = new CuentaTwitter();
            this.cuentaTwitter.setImgUsuario(st.getUser().getProfileImageURL());
            this.cuentaTwitter.setNombreUsuario(st.getUser().getName());
            this.cuentaTwitter.setNombreTwitterUsuario(st.getUser().getScreenName());
            this.cuentaTwitter.setDescripcionUsuario(st.getUser().getDescription());
            this.cuentaTwitter.setNumSeguidores(st.getUser().getFollowersCount());
            this.cuentaTwitter.setNumSiguiendo(st.getUser().getFriendsCount());
            this.cuentaTwitter.setNumMegusta(st.getUser().getFavouritesCount());
            this.cuentaTwitter.setNumTweets(st.getUser().getStatusesCount());
        }

    } catch (TwitterException ex) {
        Logger.getLogger(TweetRelevantesBean.class.getName()).log(Level.SEVERE, null, ex);
        this.listaTweets = new ArrayList<>();
    }
    return "resultadosTweetsRelevantes.xhtml";
}

From source file:TwitterStats.Beans.TweetRelevantesBean.java

public String doBuscarFechas() {
    SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd");
    try {/*from  www .  j  a  va2s . co  m*/
        if (!fechaInicio.equals("") && !fechaFin.equals("")) {
            Date fInicio = dt.parse(fechaInicio);
            Date fFin = dt.parse(fechaFin);
            this.listaTweets = twitter.getTuitsCuenta(busqueda, fInicio, fFin, numTweetsMostrar);

            if (listaTweets.size() > 0) {
                Status st = listaTweets.get(0);
                this.cuentaTwitter = new CuentaTwitter();
                this.cuentaTwitter.setImgUsuario(st.getUser().getProfileImageURL());
                this.cuentaTwitter.setNombreUsuario(st.getUser().getName());
                this.cuentaTwitter.setNombreTwitterUsuario(st.getUser().getScreenName());
                this.cuentaTwitter.setDescripcionUsuario(st.getUser().getDescription());
                this.cuentaTwitter.setNumSeguidores(st.getUser().getFollowersCount());
                this.cuentaTwitter.setNumSiguiendo(st.getUser().getFriendsCount());
                this.cuentaTwitter.setNumMegusta(st.getUser().getFavouritesCount());
                this.cuentaTwitter.setNumTweets(st.getUser().getStatusesCount());
            }

        } else {
            this.listaTweets = new ArrayList<>();
        }
    } catch (ParseException | TwitterException ex) {
        Logger.getLogger(TweetRelevantesBean.class.getName()).log(Level.SEVERE, null, ex);
        this.listaTweets = new ArrayList<>();
    }
    return "resultadosTweetsRelevantes.xhtml";
}

From source file:TwitterStats.Facade.Twitter.java

public Map<String, Double> getRepercusionTuits(List<String> tuits) throws TwitterException {
    Map<String, Double> map = new HashMap<>();

    for (int j = 0; j < tuits.size(); j++) {
        ResponseList<Status> statuses = twitter.getRetweets(Long.parseLong(tuits.get(j).split("/status/")[1]));
        map.put(tuits.get(j), (double) statuses.size());

        //List<Status> res = statuses.subList(0, 20);

        for (Status status : statuses) {
            map.put(tuits.get(j), map.get(tuits.get(j)) + status.getUser().getFollowersCount());
        }//  w  w  w.  j  a va 2 s .c  o  m
    }

    double total = 0;

    for (Map.Entry<String, Double> entry : map.entrySet()) {
        total = total + (double) entry.getValue();
    }

    for (String key : map.keySet()) {
        map.put(key, (map.get(key) * 100) / total);
    }

    return map;
}

From source file:twitterstreamsample.NewJFrame.java

private void ClickActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ClickActionPerformed
    // TODO add your handling code here:

    String consKey = "Your key";
    String consSecret = "Your Secret";
    String accToken = "Your token";
    String accSecret = "Your secret";

    StatusListener listener = new StatusListener() {

        public void onStatus(Status status) {
            textArea.append(status.getUser().getName() + ":" + status.getText());
        }//from   ww w  . j a v a  2 s.  com

        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
        }

        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
        }

        public void onException(Exception ex) {
            ex.printStackTrace();
        }

        @Override
        public void onScrubGeo(long l, long l1) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void onStallWarning(StallWarning sw) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

    };

    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();

    twitterStream.setOAuthConsumer(consKey, consSecret);

    twitterStream.setOAuthAccessToken(new AccessToken(accToken, accSecret));

    twitterStream.addListener(listener);

    twitterStream.sample();

}