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:nz.co.lolnet.james137137.lolnettwitchaddonbc.TwitterAPI.java

public List<Status> getLatestStatus() {
    List<Status> twitchTweets = new ArrayList<>();
    try {//from ww w  . j a  v  a2  s  .  co m
        Query query = new Query("lolnetNZ twitch.tv");
        QueryResult result;
        result = twitter.search(query);

        List<Status> tweets = result.getTweets();
        for (Status tweet : tweets) {
            if (tweet.getUser().getId() == 495479479 && tweet.getSource().contains("http://www.twitch.tv")) {
                twitchTweets.add(tweet);
            }
        }
    } catch (TwitterException te) {
        System.out.println("Failed to search tweets: " + te.getMessage());
    }
    return twitchTweets;
}

From source file:ontoSentiment.Busca.java

public void buscarPorAssunto(String busca, String lang) throws TwitterException {
    int totalTweets = 0;
    long maxID = -1;
    Query q = new Query(busca + " -filter:retweets -filter:links -filter:replies -filter:images");
    q.setCount(Util.TWEETS_PER_QUERY); // How many tweets, max, to retrieve 
    q.resultType(Query.ResultType.recent); // Get all tweets 
    q.setLang(lang);/*from w w w.j a  va  2 s  .  c  o  m*/
    QueryResult r = Util.getTwitter().search(q);
    do {
        for (Status s : r.getTweets()) {
            totalTweets++;
            if (maxID == -1 || s.getId() < maxID) {
                maxID = s.getId();
            }

            //System.out.printf("O tweet de id %s disse as %s, @%-20s disse: %s\n", new Long(s.getId()).toString(), s.getCreatedAt().toString(), s.getUser().getScreenName(), Util.cleanText(s.getText()));
            System.out.println(Util.cleanText(s.getText()));
        }
        q = r.nextQuery();
        if (q != null) {
            q.setMaxId(maxID);
            r = Util.getTwitter().search(q);
            System.out.println("Total tweets: " + totalTweets);
            System.out.println("Maximo ID: " + maxID);
            Util.imprimirRateLimit(Util.RATE_LIMIT_OPTION_SEARCH_TWEETS);
        }
    } while (q != null);
}

From source file:org.anc.lapps.datasource.twitter.TwitterDatasource.java

/** Contacts the Twitter API and gets any number of tweets corresponding to a certain query. The main
 * purpose of this function is to avoid the limit of 100 tweets that can be extracted at once.
 *
 * @param numberOfTweets the number of tweets to be printed
 * @param query the query to be searched by the twitter client
 * @param twitter the twitter client/* ww w  .  j a v a 2  s  .  c  o m*/
 *
 * @return A JSON string containing a Data object with either a list containing the tweets as a payload
 * (when successful) or a String payload (for errors).
 */
private Data getTweetsByCount(int numberOfTweets, Query query, Twitter twitter) {
    ArrayList<Status> tweets = new ArrayList<>();
    if (!(numberOfTweets > 0)) {
        // Default of 15 tweets
        numberOfTweets = 15;
    }
    // Set the last ID to the maximum possible value as a default
    long lastID = Long.MAX_VALUE;
    int original;
    try {
        while (tweets.size() < numberOfTweets) {

            // Keep number of original to avoid infinite looping when not getting enough tweets
            original = tweets.size();
            // If there are more than 100 tweets left to be extracted, extract
            // 100 during the next query, since 100 is the limit to retrieve at once
            if (numberOfTweets - tweets.size() > 100)
                query.setCount(100);
            else
                query.setCount(numberOfTweets - tweets.size());
            // Extract tweets corresponding to the query then add them to the list
            QueryResult result = twitter.search(query);
            tweets.addAll(result.getTweets());
            // Iterate through the list and get the lastID to know where to start from
            // if there are more tweets to be extracted
            for (Status status : tweets)
                if (status.getId() < lastID)
                    lastID = status.getId();
            query.setMaxId(lastID - 1);
            // Break the loop if the tweet count didn't change. This would prevent an infinite loop when
            // tweets for the specified query are not available
            if (tweets.size() == original)
                break;
        }
    }

    catch (TwitterException te) {
        // Put the list of tweets in Data format then output as JSon String.
        // Since we checked earlier for errors, we assume that an error occuring at this point due
        // to Rate Limits is caused by a too high request. Thus, we output the retrieved tweets and log
        // the error
        String errorDataJson = generateError(te.getMessage());
        logger.error(errorDataJson);
        if (te.exceededRateLimitation() && tweets.size() > 0) {
            Data<ArrayList<Status>> tweetsData = new Data<>();
            tweetsData.setDiscriminator(Discriminators.Uri.LIST);
            tweetsData.setPayload(tweets);
            return tweetsData;
        } else {
            return Serializer.parse(errorDataJson, Data.class);
        }
    }

    // Return a special error message if no tweets are found
    if (tweets.size() == 0) {
        String noTweetsMessage = "No tweets found for the following query. "
                + "Note: Twitter's REST API only retrieves tweets from the past week.";
        String errorDataJson = generateError(noTweetsMessage);
        return Serializer.parse(errorDataJson, Data.class);
    }

    else {
        // Put the list of tweets in Data format then output as JSon String.
        Data<ArrayList<Status>> tweetsData = new Data<>();
        tweetsData.setDiscriminator(Discriminators.Uri.LIST);
        tweetsData.setPayload(tweets);
        return tweetsData;
    }
}

From source file:org.apache.aries.sample.twitter.TwitterQuery.java

License:Apache License

public void start(BundleContext context) throws Exception {
    Twitter twitter = new Twitter();
    Query query = new Query("from:theasf");

    try {/*from w  ww.j  av a 2  s .com*/
        QueryResult result = twitter.search(query);
        List<Tweet> tweets = result.getTweets();
        System.out.println("hits:" + tweets.size());
        for (Tweet tweet : tweets) {
            System.out.println(tweet.getFromUser() + ":" + StringEscapeUtils.unescapeXml(tweet.getText()));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.apache.blur.demo.twitter.Whiteboard.java

License:Apache License

/**
 * @param args/*from   www  .  j a v  a 2  s . c  om*/
 * @throws TwitterException
 */
public static void main(String[] args) throws TwitterException {
    Twitter twitter = new TwitterFactory(new ConfigurationBuilder().build()).getInstance();
    OAuth2Token token = twitter.getOAuth2Token();
    System.out.println(token.getTokenType());

    try {
        Query query = new Query("Apache");
        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);
    }

}

From source file:org.apache.camel.component.twitter.consumer.search.SearchConsumer.java

License:Apache License

private List<Status> search(Query query) throws TwitterException {
    Integer numberOfPages = 1;//from   w  w  w.j a v  a2  s .  c om

    if (ObjectHelper.isNotEmpty(te.getProperties().getLang())) {
        query.setLang(te.getProperties().getLang());
    }

    if (ObjectHelper.isNotEmpty(te.getProperties().getCount())) {
        query.setCount(te.getProperties().getCount());
    }

    if (ObjectHelper.isNotEmpty(te.getProperties().getNumberOfPages())) {
        numberOfPages = te.getProperties().getNumberOfPages();
    }

    LOG.debug("Searching with " + numberOfPages + " pages.");

    Twitter twitter = te.getProperties().getTwitter();
    QueryResult qr = twitter.search(query);
    List<Status> tweets = qr.getTweets();

    for (int i = 1; i < numberOfPages; i++) {
        if (!qr.hasNext()) {
            break;
        }

        qr = twitter.search(qr.nextQuery());
        tweets.addAll(qr.getTweets());
    }

    if (te.getProperties().isFilterOld()) {
        for (Status t : tweets) {
            checkLastId(t.getId());
        }
    }

    return tweets;
}

From source file:org.apache.camel.component.twitter.producer.SearchProducer.java

License:Apache License

@Override
public void process(Exchange exchange) throws Exception {
    long myLastId = lastId;
    // KEYWORDS//from  ww w.j  av a  2s. c o m
    // keywords from header take precedence
    String keywords = exchange.getIn().getHeader(TwitterConstants.TWITTER_KEYWORDS, String.class);
    if (keywords == null) {
        keywords = te.getProperties().getKeywords();
    }

    if (keywords == null) {
        throw new CamelExchangeException("No keywords to use for query", exchange);
    }

    Query query = new Query(keywords);

    // filter of older tweets
    if (te.getProperties().isFilterOld() && myLastId != 0) {
        query.setSinceId(myLastId);
    }

    // language
    String lang = exchange.getIn().getHeader(TwitterConstants.TWITTER_SEARCH_LANGUAGE, String.class);
    if (lang == null) {
        lang = te.getProperties().getLang();
    }

    if (ObjectHelper.isNotEmpty(lang)) {
        query.setLang(lang);
    }

    // number of elemnt per page
    Integer count = exchange.getIn().getHeader(TwitterConstants.TWITTER_COUNT, Integer.class);
    if (count == null) {
        count = te.getProperties().getCount();
    }
    if (ObjectHelper.isNotEmpty(count)) {
        query.setCount(count);
    }

    // number of pages
    Integer numberOfPages = exchange.getIn().getHeader(TwitterConstants.TWITTER_NUMBER_OF_PAGES, Integer.class);
    if (numberOfPages == null) {
        numberOfPages = te.getProperties().getNumberOfPages();
    }

    Twitter twitter = te.getProperties().getTwitter();
    log.debug("Searching twitter with keywords: {}", keywords);
    QueryResult results = twitter.search(query);
    List<Status> list = results.getTweets();

    for (int i = 1; i < numberOfPages; i++) {
        if (!results.hasNext()) {
            break;
        }
        log.debug("Fetching page");
        results = twitter.search(results.nextQuery());
        list.addAll(results.getTweets());
    }

    if (te.getProperties().isFilterOld()) {
        for (Status t : list) {
            long newId = t.getId();
            if (newId > myLastId) {
                myLastId = newId;
            }
        }
    }

    exchange.getIn().setBody(list);
    // update the lastId after finished the processing
    if (myLastId > lastId) {
        lastId = myLastId;
    }
}

From source file:org.bireme.interop.toJson.TweetLoader.java

License:Open Source License

private void loadQueryTweets() throws TwitterException {
    if (query == null) {
        tweetIterator = null;//from  ww w  . j  ava2s  .  com
    } else {
        query.setMaxId(max_id);
        final QueryResult result = twitter.search(query);
        if (result == null) {
            tweetIterator = null;
        } else {
            final List<Status> respLst = result.getTweets();
            if ((lowerDate == null) || respLst.get(respLst.size() - 1).getCreatedAt().after(lowerDate)) {
                tweetIterator = respLst.iterator();
            } else {
                tweetIterator = null;
            }
        }
    }
    retweetIterator = null;
}

From source file:org.botlibre.sense.twitter.Twitter.java

License:Open Source License

/**
 * Learn responses from the tweet search.
 *//*  w  ww. j  av a 2  s. com*/
public void learnSearch(String tweetSearch, int maxSearch, boolean processTweets, boolean processReplies) {
    log("Learning from tweet search", Level.INFO, tweetSearch);
    try {
        Network memory = getBot().memory().newMemory();
        int count = 0;
        this.errors = 0;
        Set<Long> processed = new HashSet<Long>();
        Query query = new Query(tweetSearch);
        query.count(100);
        SearchResource search = getConnection().search();
        QueryResult result = search.search(query);
        List<Status> tweets = result.getTweets();
        if (tweets != null) {
            log("Processing search results", Level.INFO, tweets.size(), tweetSearch);
            for (Status tweet : tweets) {
                if (count > maxSearch) {
                    log("Max search results processed", Level.INFO, maxSearch);
                    break;
                }
                if (!processed.contains(tweet.getId())) {
                    log("Processing search result", Level.INFO, tweet.getUser().getScreenName(), tweetSearch,
                            tweet.getText());
                    processed.add(tweet.getId());
                    learnTweet(tweet, processTweets, processReplies, memory);
                    count++;
                }
            }
            memory.save();
        }
        // Search only returns 7 days, search for users as well.
        TextStream stream = new TextStream(tweetSearch);
        while (!stream.atEnd()) {
            stream.skipToAll("from:", true);
            if (stream.atEnd()) {
                break;
            }
            String user = stream.nextWord();
            String arg[] = new String[1];
            arg[0] = user;
            ResponseList<User> users = getConnection().lookupUsers(arg);
            if (!users.isEmpty()) {
                long id = users.get(0).getId();
                boolean more = true;
                int page = 1;
                while (more) {
                    Paging pageing = new Paging(page);
                    ResponseList<Status> timeline = getConnection().getUserTimeline(id, pageing);
                    if ((timeline == null) || (timeline.size() < 20)) {
                        more = false;
                    }
                    page++;
                    if ((timeline == null) || timeline.isEmpty()) {
                        more = false;
                        break;
                    }
                    log("Processing user timeline", Level.INFO, user, timeline.size());
                    for (int index = timeline.size() - 1; index >= 0; index--) {
                        if (count >= maxSearch) {
                            more = false;
                            break;
                        }
                        Status tweet = timeline.get(index);
                        if (!processed.contains(tweet.getId())) {
                            log("Processing user timeline result", Level.INFO, tweet.getUser().getScreenName(),
                                    tweet.getText());
                            processed.add(tweet.getId());
                            learnTweet(tweet, processTweets, processReplies, memory);
                            count++;
                        }
                    }
                    memory.save();
                }
                if (count >= maxSearch) {
                    log("Max search results processed", Level.INFO, maxSearch);
                    break;
                }
            }
        }
    } catch (Exception exception) {
        log(exception);
    }
}

From source file:org.botlibre.sense.twitter.Twitter.java

License:Open Source License

/**
 * Check search keywords./*  w  w  w.j a v a2  s .c om*/
 */
public void checkSearch() {
    if (getTweetSearch().isEmpty()) {
        return;
    }
    log("Processing search", Level.FINE, getTweetSearch());
    try {
        Network memory = getBot().memory().newMemory();
        Vertex twitter = memory.createVertex(getPrimitive());
        Vertex vertex = twitter.getRelationship(Primitive.LASTSEARCH);
        long last = 0;
        long max = 0;
        int count = 0;
        this.errors = 0;
        if (vertex != null) {
            last = ((Number) vertex.getData()).longValue();
        }
        Set<Long> processed = new HashSet<Long>();
        for (String tweetSearch : getTweetSearch()) {
            Query query = new Query(tweetSearch);
            if (vertex != null) {
                query.setSinceId(last);
            }
            SearchResource search = getConnection().search();
            QueryResult result = search.search(query);
            List<Status> tweets = result.getTweets();
            if (tweets != null) {
                log("Processing search results", Level.FINE, tweets.size(), tweetSearch);
                for (Status tweet : tweets) {
                    if (count > this.maxSearch) {
                        log("Max search results processed", Level.FINE, this.maxSearch);
                        break;
                    }
                    if (tweet.getId() > last && !processed.contains(tweet.getId())) {
                        if (tweet.getId() > max) {
                            max = tweet.getId();
                        }
                        boolean match = false;
                        // Exclude replies/mentions
                        if (getIgnoreReplies() && tweet.getText().indexOf('@') != -1) {
                            log("Ignoring: Tweet is reply", Level.FINER, tweet.getText());
                            continue;
                        }
                        // Exclude retweets
                        if (tweet.isRetweet()) {
                            log("Ignoring: Tweet is retweet", Level.FINER, tweet.getText());
                            continue;
                        }
                        // Exclude protected
                        if (tweet.getUser().isProtected()) {
                            log("Ignoring: Tweet is protected", Level.FINER, tweet.getText());
                            continue;
                        }
                        // Exclude self
                        if (tweet.getUser().getScreenName().equals(getUserName())) {
                            log("Ignoring: Tweet is from myself", Level.FINER, tweet.getText());
                            continue;
                        }
                        // Ignore profanity
                        if (Utils.checkProfanity(tweet.getText())) {
                            log("Ignoring: Tweet contains profanity", Level.FINER, tweet.getText());
                            continue;
                        }
                        List<String> statusWords = new TextStream(tweet.getText().toLowerCase()).allWords();
                        for (String text : getStatusKeywords()) {
                            List<String> keywords = new TextStream(text.toLowerCase()).allWords();
                            if (statusWords.containsAll(keywords)) {
                                match = true;
                                break;
                            }
                        }
                        if (getLearn()) {
                            learnTweet(tweet, true, true, memory);
                        }
                        if (match) {
                            processed.add(tweet.getId());
                            log("Processing search", Level.INFO, tweet.getUser().getScreenName(), tweetSearch,
                                    tweet.getText());
                            input(tweet);
                            Utils.sleep(500);
                            count++;
                        } else {
                            if (!tweet.isRetweetedByMe()) {
                                boolean found = false;
                                // Check retweet.
                                for (String keywords : getRetweet()) {
                                    List<String> keyWords = new TextStream(keywords).allWords();
                                    if (!keyWords.isEmpty()) {
                                        if (statusWords.containsAll(keyWords)) {
                                            found = true;
                                            processed.add(tweet.getId());
                                            count++;
                                            retweet(tweet);
                                            Utils.sleep(500);
                                            break;
                                        }
                                    }
                                }
                                if (!found) {
                                    log("Missing keywords", Level.FINER, tweet.getText());
                                }
                            } else {
                                log("Already retweeted", Level.FINER, tweet.getText());
                            }
                        }
                    }
                }
            }
            if (count > this.maxSearch) {
                break;
            }
            if (this.errors > this.maxErrors) {
                break;
            }
        }
        if (max != 0) {
            twitter.setRelationship(Primitive.LASTSEARCH, memory.createVertex(max));
            memory.save();
        }
    } catch (Exception exception) {
        log(exception);
    }
    // Wait for language processing.
    int count = 0;
    while (count < 60 && !getBot().memory().getActiveMemory().isEmpty()) {
        Utils.sleep(1000);
    }
}