Example usage for twitter4j QueryResult getTweets

List of usage examples for twitter4j QueryResult getTweets

Introduction

In this page you can find the example usage for twitter4j QueryResult getTweets.

Prototype

List<Status> getTweets();

Source Link

Usage

From source file:com.twit.AppCrawler.java

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

    TwitterFactory tf = new TwitterFactory();
    Twitter twitter = tf.getInstance();//ww w  . ja v a  2 s .c  o  m

    CidadeController cidadeController = new CidadeController(new Cidade());
    List<Cidade> cidades = cidadeController.findEntities();

    String searchStr = "\"#viagem\" ";

    for (Cidade cidade : cidades) {

        //searchStr += "\"" + cidade.getNome() + "\" "; 

    }

    // Create a Query object.
    Query query = new Query(searchStr);

    // Send API request to execute a search with the given query.
    QueryResult result = twitter.search(query);

    // Display search results.
    for (Status status : result.getTweets()) {
        System.out.println("\n@" + status.getUser().getName() + ": " + status.getText());
    }

}

From source file:com.twitter.TwitterCrawler.java

/**
 *
 * @return @throws TwitterException/* w  w w.j  a  v a  2 s  . c o  m*/
 * @throws InterruptedException returns the number of tweets
 */
public TwitterApiResponse stream() throws TwitterException, InterruptedException {
    //************************ Variables *************************
    TwitterApiResponse response = new TwitterApiResponse();
    long timeNow = System.currentTimeMillis();
    long afterInterval = timeNow + (interval * 1000);
    int countTweets = 0;
    //define the tweet id that the application should read relative to the IDs of Tweets it has already processed
    long maxID = 0;
    ArrayList<Status> tweets = new ArrayList<Status>();

    Query query = new Query(keywords);

    //************************ Action *************************
    //max 450 calls in 15 mins -->30/min, 5 calls/10 secs
    while (timeNow < afterInterval) {
        try {
            if (sinceID != 0) {
                query.setSinceId(sinceID);
            }

            if (maxID != 0) {
                query.setMaxId(maxID);
            }

            query.setResultType(Query.ResultType.recent);
            query.setCount(100);
            QueryResult result = twitter.search(query);
            tweets.addAll(result.getTweets());

            if (tweets.size() == 100) {
                countTweets += tweets.size();
                maxID = tweets.get(tweets.size() - 1).getId() - 1;
            } else {
                countTweets += tweets.size();
                break;
            }
            timeNow = System.currentTimeMillis();
        } catch (TwitterException te) {
            System.out.println("Couldn't connect: " + te);
        }
    }

    sinceID = tweets.get(0).getId();
    response.setCount(countTweets);
    response.setSinceID(sinceID);

    return response;
}

From source file:controllers.modules.CorpusModule.java

License:Open Source License

public static Result update(UUID corpus) {
    OpinionCorpus corpusObj = null;/*  w ww  .  j  a va2 s.co  m*/
    if (corpus != null) {
        corpusObj = fetchResource(corpus, OpinionCorpus.class);
    }
    OpinionCorpusFactory corpusFactory = null;

    MultipartFormData formData = request().body().asMultipartFormData();
    if (formData != null) {
        // if we have a multi-part form with a file.
        if (formData.getFiles() != null) {
            // get either the file named "file" or the first one.
            FilePart filePart = ObjectUtils.defaultIfNull(formData.getFile("file"),
                    Iterables.getFirst(formData.getFiles(), null));
            if (filePart != null) {
                corpusFactory = (OpinionCorpusFactory) new OpinionCorpusFactory().setFile(filePart.getFile())
                        .setFormat(FilenameUtils.getExtension(filePart.getFilename()));
            }
        }
    } else {
        // otherwise try as a json body.
        JsonNode json = request().body().asJson();
        if (json != null) {
            OpinionCorpusFactoryModel optionsVM = Json.fromJson(json, OpinionCorpusFactoryModel.class);
            if (optionsVM != null) {
                corpusFactory = optionsVM.toFactory();
            } else {
                throw new IllegalArgumentException();
            }

            if (optionsVM.grabbers != null) {
                if (optionsVM.grabbers.twitter != null) {
                    if (StringUtils.isNotBlank(optionsVM.grabbers.twitter.query)) {
                        TwitterFactory tFactory = new TwitterFactory();
                        Twitter twitter = tFactory.getInstance();
                        twitter.setOAuthConsumer(
                                Play.application().configuration().getString("twitter4j.oauth.consumerKey"),
                                Play.application().configuration().getString("twitter4j.oauth.consumerSecret"));
                        twitter.setOAuthAccessToken(new AccessToken(
                                Play.application().configuration().getString("twitter4j.oauth.accessToken"),
                                Play.application().configuration()
                                        .getString("twitter4j.oauth.accessTokenSecret")));

                        Query query = new Query(optionsVM.grabbers.twitter.query);
                        query.count(ObjectUtils.defaultIfNull(optionsVM.grabbers.twitter.limit, 10));
                        query.resultType(Query.RECENT);
                        if (StringUtils.isNotEmpty(corpusFactory.getLanguage())) {
                            query.lang(corpusFactory.getLanguage());
                        } else if (corpusObj != null) {
                            query.lang(corpusObj.getLanguage());
                        }

                        QueryResult qr;
                        try {
                            qr = twitter.search(query);
                        } catch (TwitterException e) {
                            throw new IllegalArgumentException();
                        }

                        StringBuilder tweets = new StringBuilder();
                        for (twitter4j.Status status : qr.getTweets()) {
                            // quote for csv, normalize space, and remove higher unicode characters. 
                            String text = StringEscapeUtils.escapeCsv(StringUtils
                                    .normalizeSpace(status.getText().replaceAll("[^\\u0000-\uFFFF]", "")));
                            tweets.append(text + System.lineSeparator());
                        }

                        corpusFactory.setContent(tweets.toString());
                        corpusFactory.setFormat("txt");
                    }
                }
            }
        } else {
            // if not json, then just create empty.
            corpusFactory = new OpinionCorpusFactory();
        }
    }

    if (corpusFactory == null) {
        throw new IllegalArgumentException();
    }

    if (corpus == null && StringUtils.isEmpty(corpusFactory.getTitle())) {
        corpusFactory.setTitle("Untitled corpus");
    }

    corpusFactory.setOwnerId(SessionedAction.getUsername(ctx())).setExistingId(corpus).setEm(em());

    DocumentCorpusModel corpusVM = null;
    corpusObj = corpusFactory.create();
    if (!em().contains(corpusObj)) {
        em().persist(corpusObj);

        corpusVM = (DocumentCorpusModel) createViewModel(corpusObj);
        corpusVM.populateSize(em(), corpusObj);
        return created(corpusVM.asJson());
    }

    for (PersistentObject obj : corpusObj.getDocuments()) {
        if (em().contains(obj)) {
            em().merge(obj);
        } else {
            em().persist(obj);
        }
    }
    em().merge(corpusObj);

    corpusVM = (DocumentCorpusModel) createViewModel(corpusObj);
    corpusVM.populateSize(em(), corpusObj);
    return ok(corpusVM.asJson());
}

From source file:cp.Demo.java

private void MoviesListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_MoviesListValueChanged

    // Obtain the movie object
    Movie mov;//from   w  w  w .  jav  a2s  .  c  o  m
    try {
        mov = (Movie) MoviesList.getSelectedValue();
        System.out.println(mov.getApiQuery());
    } catch (Exception ex) {
        PosterLabel.setIcon(null);
        PosterLabel.setText("Movie not found");
        return;
    }

    // Display the movie poster
    try {
        // Load the movie poster
        URL url = new URL(mov.getPosterLink());
        ImageIcon img = new ImageIcon(url);

        // Rescale the movie poster
        int w = img.getIconWidth();
        int h = img.getIconHeight();
        int labelH = MovieTabs.getHeight() - 36;
        img.setImage(img.getImage().getScaledInstance(labelH * w / h, labelH, Image.SCALE_SMOOTH));

        // Put the movie poster into the label, and change the text
        PosterLabel.setIcon(img);
        PosterLabel.setText("");
    } catch (Exception ex) {
        // If for some reason the poster isn't available, indicate this
        PosterLabel.setIcon(null);
        PosterLabel.setText("Poster could not be displayed");
    }

    // Display tweets about the movie
    try {
        // clear out the tweets
        TweetsArea.setText("");

        // The factory instance is re-useable and thread safe.
        Twitter twitter = TwitterFactory.getSingleton();
        Query query = new Query(mov.getTitle());
        QueryResult result = twitter.search(query);
        String tweet;
        for (Status status : result.getTweets()) {
            tweet = String.format("[%s] @%s tweeted %s\n", status.getCreatedAt(),
                    status.getUser().getScreenName(), status.getText());
            TweetsArea.append(tweet);
        }
    } catch (TwitterException ex) {
        TweetsArea.setText(ex.toString());
    }

    // Display reviews of the movie
    try {
        ReviewsArea.setText("");
        for (Review rev : (new ReviewRequest(mov.getMovie())).getReviews()) {
            String review = String.format("[%s] (by %s, %s)\n%s\n%s\n\n", rev.getDateOfReview(),
                    rev.getCritic(), rev.getPublication(), rev.getQuote(), rev.getReviewLinkUrl());
            ReviewsArea.append(review);
        }
    } catch (IOException ex) {
        Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ReviewRequest.ReviewRequestException ex) {
        Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ParseException ex) {
        Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:crawler.DataSearch.java

License:Apache License

public void collectNewTweets() {

    while (keywords.size() > 0) {
        String hash = keywords.get(0);
        Query query = new Query(hash);
        query.setCount(Settings.searchTweetNo);
        QueryResult result;

        try {//  w w  w. jav  a  2s .c o m
            result = twitter.search(query);
            List<Status> tweets = result.getTweets();
            if (tweets.size() == 0)
                TwitterCrawler.TFFreq.remove(hash);
            for (Status status : tweets) {
                //System.out.println("{"+hash+"}["+status.getCreatedAt().toString()+"]- [USER: " + status.getUser().getScreenName() + "] - " + status.getText());
                String temp;
                if (trash(status.getText())) {
                    if (Settings.limitTime) {
                        long currentTime = System.currentTimeMillis();
                        long tweetTime = status.getCreatedAt().getTime();

                        if (currentTime - tweetTime > Settings.timeLimit)
                            continue;
                    }
                    if (hashtext.containsKey(hash))
                        temp = hashtext.get(hash) + status.getText();
                    else
                        temp = status.getText();
                    hashtext.put(hash, temp);
                }
            }
            //System.out.println("{"+hash+"} with "+tweets.size()+"tweets");
            keywords.remove(0);
        } catch (TwitterException e) {
            if (e.getErrorCode() == 88) {
                System.out.println("Using other keys???");

                String ConsumerKey_temp = Settings.ConsumerKey.get(0);
                String ConsumerSecret_temp = Settings.ConsumerSecret.get(0);
                String AccessToken_temp = Settings.AccessToken.get(0);
                String AccessSecret_temp = Settings.AccessSecret.get(0);

                Settings.ConsumerKey.remove(ConsumerKey_temp);
                Settings.ConsumerSecret.remove(ConsumerSecret_temp);
                Settings.AccessToken.remove(AccessToken_temp);
                Settings.AccessSecret.remove(AccessSecret_temp);

                twitter = new TwitterFactory(
                        OA.build(Settings.ConsumerKey.get(0), Settings.ConsumerSecret.get(0),
                                Settings.AccessToken.get(0), Settings.AccessSecret.get(0))).getInstance();

                Settings.ConsumerKey.add(Settings.ConsumerKey.size(), ConsumerKey_temp);
                Settings.ConsumerSecret.add(Settings.ConsumerSecret.size(), ConsumerSecret_temp);
                Settings.AccessToken.add(Settings.AccessToken.size(), AccessToken_temp);
                Settings.AccessSecret.add(Settings.AccessSecret.size(), AccessSecret_temp);

                System.out.println("**************************************************************");
                System.out.println("[" + ConsumerKey_temp + "] to [" + Settings.ConsumerKey.get(0) + "]");
                System.out.println("[" + ConsumerSecret_temp + "] to [" + Settings.ConsumerSecret.get(0) + "]");
                System.out.println("[" + AccessToken_temp + "] to [" + Settings.AccessToken.get(0) + "]");
                System.out.println("[" + AccessSecret_temp + "] to [" + Settings.AccessSecret.get(0) + "]");
                System.out.println("**************************************************************");

                continue;
            }
            e.printStackTrace();
        }
    }

}

From source file:crawler.TwitterFeed.java

/**
 *
 *///w ww  .j av  a 2s  .  com
public void get_tweets() throws SQLException {
    CSVReader cr = new CSVReader();
    ArrayList<String> names = new ArrayList<>();
    Map<String, String> name_ticker;
    names = cr.get_company_names(tickers);
    System.out.println(names);
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setOAuthConsumerKey("WNSycI2GS33ZAwHJ9Fb4A");
    cb.setOAuthConsumerSecret("BBWeSThT3ZV2g9c5BDuUduDyNAWyzouMI0XjQy7KUc");
    cb.setOAuthAccessToken("1852271029-SLfE061bImfcRxWQZpy1pAgpEkfFhg3757Q9JRf");
    cb.setOAuthAccessTokenSecret("1uPSfYPbaENtXFYWsryBIAHnUsmG3pT6lGH0NzxTCGW7O");
    Twitter twitter = new TwitterFactory(cb.build()).getInstance();
    for (String s : names) {
        String[] temp;
        if (s.contains(",")) {
            temp = s.split(",");
        } else {
            temp = s.split(" ");
        }

        System.out.println(
                "--------------------------------------------------------------------------------------"
                        + temp[0]);
        Query query = new Query("#" + temp[0]);
        query.lang("en");
        int numberOfTweets = 500;
        long lastID = Long.MAX_VALUE;
        ArrayList<Status> tweets = new ArrayList<>();
        while (tweets.size() < numberOfTweets) {
            if (numberOfTweets - tweets.size() > 100) {
                query.setCount(100);
            } else {
                query.setCount(numberOfTweets - tweets.size());
            }
            try {
                QueryResult result = twitter.search(query);
                tweets.addAll(result.getTweets());
                System.out.println("Gathered " + tweets.size() + " tweets");
                for (Status t : tweets) {
                    if (t.getId() < lastID) {
                        lastID = t.getId();
                    }
                }

            } catch (TwitterException te) {
                System.out.println("Couldn't connect: " + te);
            }
            query.setMaxId(lastID - 1);
        }
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/CrawledData", "root", "");
        for (int i = 0; i < tweets.size(); i++) {
            Status t = (Status) tweets.get(i);

            //GeoLocation loc = t.getGeoLocation();
            String user = t.getUser().getScreenName();
            String msg = t.getText();
            String time = t.getCreatedAt().toString();
            //if (loc!=null) {
            //    Double lat = t.getGeoLocation().getLatitude();
            //    Double lon = t.getGeoLocation().getLongitude();
            //    System.out.println(i + " USER: " + user + " wrote: " + msg + " located at " + lat + ", " + lon);
            //} 
            //else{ 
            System.out.println(i + " USER: " + user + " wrote: " + msg + " at: " + time);
            //}
            try {
                Class.forName("com.mysql.jdbc.Driver");

                PreparedStatement ps = con
                        .prepareStatement("INSERT IGNORE INTO TwitterFeed VALUES (?,?,?,?,?)");
                ps.setString(1, s);
                ps.setString(2, cr.get_ticker_from_company(s));
                ps.setString(3, user);
                ps.setString(4, msg);
                ps.setString(5, time);

                int k = ps.executeUpdate();
                if (k <= 0) {
                    System.out.println("Entry Unsuccessful");
                } else {
                    System.out.println("Entry Successful");
                }

            } catch (ClassNotFoundException | SQLException e) {
                System.out.println(e);
            }

        }
        con.close();
    }
}

From source file:crawling.FoundUsersBySearchGeo.java

License:Apache License

private static void doASearch(Twitter twitter, Query query) throws TwitterException {
    //try {/*from  w w  w  .  jav  a  2s. c o  m*/
    QueryResult result = twitter.search(query);
    List<Status> tweets = result.getTweets();
    int thisCount = 0;
    for (Status tweet : tweets) {
        //System.out.println("@" + tweet.getFromUser() + tweet.getId() + " - " + tweet.getText());

        // Check the tweet
        Long id = tweet.getUser().getId();
        String screenName = tweet.getUser().getScreenName();

        /*String location = "";
        if (tweet.getUser().getLocation() != null){
          location += tweet.getUser().getLocation();
        if (tweet.getGeoLocation() != null){
          location += "::" + tweet.getGeoLocation().toString();
        }*/
        String location = tweet.getUser().getLocation() + "::" + tweet.getGeoLocation();
        // If the tweet is a retweet, the source of the tweet is from the target area
        if (tweet.isRetweet()) {
            id = tweet.getRetweetedStatus().getUser().getId();
            screenName = tweet.getRetweetedStatus().getUser().getScreenName();
            //location = tweet.getRetweetedStatus().getUser().getLocation();
            location = tweet.getRetweetedStatus().getUser().getLocation() + "::"
                    + tweet.getRetweetedStatus().getGeoLocation();
        }

        if (discoveredUsers.containsKey(id)) {
            //System.out.println("Already found this user: " + id);
            long num = discoveredUsers.get(id);
            discoveredUsers.put(id, num + 1);
        } else {
            discoveredUsers.put(id, (long) 1);
            storeUserID(id, screenName, location);
            thisCount++;
        }
    }
    if (currentIndex < histCount) {
        avgUsers[currentIndex] = thisCount;
        currentIndex++;
    } else {
        currentIndex = 0;
    }

    /* Calculate the average #users in last 10 times */
    double sum = 0;
    for (int num : avgUsers)
        sum += num;
    System.out.print(
            "\r" + count + ", the average number of users in last " + histCount + " is: " + sum / histCount);

    //} catch (TwitterException te) {
    //    te.printStackTrace();
    //    System.out.println("Failed to search tweets: " + te.getMessage());
    //    System.exit(-1);
    //}

}

From source file:crawling.FoundUsersBySearchHashtag.java

License:Apache License

private static void doASearch(Twitter twitter, Query query) throws TwitterException {
    //try {//from w ww . jav  a2  s  . c  o m
    QueryResult result = twitter.search(query);
    List<Status> tweets = result.getTweets();
    int thisCount = 0;
    for (Status tweet : tweets) {
        //System.out.println("@" + tweet.getFromUser() + tweet.getId() + " - " + tweet.getText());

        // Check the tweet
        Long id = tweet.getUser().getId();
        String screenName = tweet.getUser().getScreenName();

        /*String location = "";
        if (tweet.getUser().getLocation() != null){
          location += tweet.getUser().getLocation();
        if (tweet.getGeoLocation() != null){
          location += "::" + tweet.getGeoLocation().toString();
        }*/
        String location = tweet.getUser().getLocation() + "::" + tweet.getGeoLocation();
        // If the tweet is a retweet, the source of the tweet is from the target area
        /*if (tweet.isRetweet()){
          id = tweet.getRetweetedStatus().getUser().getId();
          screenName = tweet.getRetweetedStatus().getUser().getScreenName();
          //location = tweet.getRetweetedStatus().getUser().getLocation();
          location = tweet.getRetweetedStatus().getUser().getLocation() + "::" + tweet.getRetweetedStatus().getGeoLocation();
        }*/

        if (discoveredUsers.containsKey(id)) {
            //System.out.println("Already found this user: " + id);
            long num = discoveredUsers.get(id);
            discoveredUsers.put(id, num + 1);
        } else {
            discoveredUsers.put(id, (long) 1);
            storeUserID(id, screenName, location);
            thisCount++;
        }
    }
    if (currentIndex < histCount) {
        avgUsers[currentIndex] = thisCount;
        currentIndex++;
    } else {
        currentIndex = 0;
    }

    /* Calculate the average #users in last 10 times */
    double sum = 0;
    for (int num : avgUsers)
        sum += num;
    System.out.print(
            "\r" + count + ", the average number of users in last " + histCount + " is: " + sum / histCount);

    //} catch (TwitterException te) {
    //    te.printStackTrace();
    //    System.out.println("Failed to search tweets: " + te.getMessage());
    //    System.exit(-1);
    //}

}

From source file:crawling.SearchTweetsHashtag.java

License:Apache License

private static void doASearch(Twitter twitter, Query query) throws TwitterException {
    //try {/* www .j a v a2 s .  c o  m*/
    QueryResult result = twitter.search(query);
    List<Status> tweets = result.getTweets();
    int thisCount = 0;
    for (Status tweet : tweets) {

        // Check the tweet
        Long id = tweet.getId();

        if (discoveredTweets.containsKey(id)) {
            //System.out.println("Already found this user: " + id);
            long num = discoveredTweets.get(id);
            discoveredTweets.put(id, num + 1);
        } else {
            discoveredTweets.put(id, (long) 1);
            storeATweet(tweet);
            thisCount++;
        }
    }
    if (currentIndex < histCount) {
        avgUsers[currentIndex] = thisCount;
        currentIndex++;
    } else {
        currentIndex = 0;
    }

    /* Calculate the average #users in last 10 times */
    double sum = 0;
    for (int num : avgUsers)
        sum += num;
    System.out.print(
            "\r" + count + ", the average number of tweets in last " + histCount + " is: " + sum / histCount);

    //} catch (TwitterException te) {
    //    te.printStackTrace();
    //    System.out.println("Failed to search tweets: " + te.getMessage());
    //    System.exit(-1);
    //}

}

From source file:crawltweets2mongo.MonoThread.java

void getNewTweets(String keyword) {
    try {//from  w  w w.j  a  v a 2s. com
        Query query = new Query(keyword);
        query.setCount(20000);
        QueryResult result;
        result = twitter.search(query);

        //System.out.println("Getting Tweets..by Key.");
        List<Status> tweets = result.getTweets();

        for (Status tweet : tweets) {

            BasicDBObject basicObj = new BasicDBObject();
            basicObj.put("user_Rname", tweet.getUser().getName());
            basicObj.put("user_name", tweet.getUser().getScreenName());
            basicObj.put("retweet_count", tweet.getRetweetCount());
            basicObj.put("tweet_followers_count", tweet.getUser().getFollowersCount());

            UserMentionEntity[] mentioned = tweet.getUserMentionEntities();
            basicObj.put("tweet_mentioned_count", mentioned.length);
            basicObj.put("tweet_ID", tweet.getId());
            basicObj.put("tweet_text", tweet.getText());
            Status temp1 = tweet.getRetweetedStatus();
            if (temp1 != null)
                basicObj.put("Re_tweet_ID", temp1.getUser().getId());
            GeoLocation loc = tweet.getGeoLocation();
            if (loc != null) {
                basicObj.put("Latitude", loc.getLatitude());
                basicObj.put("Longitude", loc.getLongitude());
            }
            basicObj.put("CreateTime", tweet.getCreatedAt());
            basicObj.put("FavoriteCount", tweet.getFavoriteCount());
            basicObj.put("user_Id", tweet.getUser().getId());

            if (tweet.getUser().getTimeZone() != null)
                basicObj.put("UsertimeZone", tweet.getUser().getTimeZone());
            if (tweet.getUser().getStatus() != null)
                basicObj.put("UserStatus", tweet.getUser().getStatus());
            //basicObj.put("tweetLocation", tweet.getPlace().getGeometryCoordinates());
            String U_Loc = tweet.getUser().getLocation();
            if (U_Loc != null)
                basicObj.put("userLocation", U_Loc);
            basicObj.put("number_of_rt", tweet.getRetweetCount());

            if (mentioned.length > 0) {
                basicObj.append("mentions", pickMentions(mentioned));
            }
            try {

                collection.insert(basicObj);
            } catch (Exception e) {
                //System.out.println("MongoDB Connection Error : " + e.getMessage());
                //                            loadMenu();
            }
        }
        collection.ensureIndex(new BasicDBObject("tweet_ID", 1), new BasicDBObject("unique", true));
    } catch (TwitterException ex) {
        java.util.logging.Logger.getLogger(MonoThread.class.getName()).log(Level.SEVERE, null, ex);
    }

}