Example usage for twitter4j Query setSinceId

List of usage examples for twitter4j Query setSinceId

Introduction

In this page you can find the example usage for twitter4j Query setSinceId.

Prototype

public void setSinceId(long sinceId) 

Source Link

Document

returns tweets with status ids greater than the given id.

Usage

From source file:net.lacolaco.smileessence.view.SearchFragment.java

License:Open Source License

@Override
public void onPullDownToRefresh(final PullToRefreshBase<ListView> refreshView) {
    final MainActivity activity = getMainActivity();
    final Account currentAccount = activity.getCurrentAccount();
    Twitter twitter = TwitterApi.getTwitter(currentAccount);
    final SearchListAdapter adapter = getListAdapter(activity);
    String queryString = adapter.getQuery();
    if (TextUtils.isEmpty(queryString)) {
        new UIHandler() {
            @Override/*from www . j av  a2  s .  com*/
            public void run() {
                notifyTextEmpty(activity);
                refreshView.onRefreshComplete();
            }
        }.post();
        return;
    }
    final Query query = SearchTask.getBaseQuery(activity, queryString);
    if (adapter.getCount() > 0) {
        query.setSinceId(adapter.getTopID());
    }
    new SearchTask(twitter, query, activity) {
        @Override
        protected void onPostExecute(QueryResult queryResult) {
            super.onPostExecute(queryResult);
            if (queryResult != null) {
                java.util.List<twitter4j.Status> tweets = queryResult.getTweets();
                for (int i = tweets.size() - 1; i >= 0; i--) {
                    twitter4j.Status status = tweets.get(i);
                    if (!status.isRetweet()) {
                        StatusViewModel viewModel = new StatusViewModel(status, currentAccount);
                        adapter.addToTop(viewModel);
                        StatusFilter.filter(activity, viewModel);
                    }
                }
                updateListViewWithNotice(refreshView.getRefreshableView(), adapter, true);
                adapter.setTopID(queryResult.getMaxId());
                refreshView.onRefreshComplete();
            }
        }
    }.execute();
}

From source file:nl.b3p.viewer.stripes.TwitterActionBean.java

License:Open Source License

public Resolution create() throws JSONException {
    JSONObject json = new JSONObject();

    json.put("success", Boolean.FALSE);
    String error = null;/*from ww  w  .  j  a  va 2 s  . co m*/

    try {
        // The factory instance is re-useable and thread safe.
        Twitter twitter = new TwitterFactory().getInstance();
        Query query = new Query(term);
        if (latestId != null) {
            Long longVal = Long.valueOf(latestId);
            query.setSinceId(longVal);
        }

        QueryResult result = twitter.search(query);
        JSONArray tweets = new JSONArray();
        for (Status tweet : result.getTweets()) {

            //System.out.println(tweet.getFromUser() + ":" + tweet.getText());
            JSONObject t = new JSONObject();
            t.put("id_str", String.valueOf(tweet.getId()));
            t.put("text", tweet.getText());
            t.put("user_from", tweet.getUser().getScreenName());
            t.put("img_url", tweet.getUser().getProfileImageURL());

            JSONObject geo = new JSONObject();
            if (tweet.getGeoLocation() != null) {
                geo.put("lat", tweet.getGeoLocation().getLatitude());
                geo.put("lon", tweet.getGeoLocation().getLongitude());
            }
            t.put("geo", geo);
            tweets.put(t);
        }
        json.put("tweets", tweets);
        if (tweets.length() > 0) {
            json.put("maxId", String.valueOf(result.getMaxId()));
        } else {
            json.put("maxId", String.valueOf(latestId));
        }
        json.put("success", Boolean.TRUE);
    } catch (Exception e) {

        error = e.toString();
        if (e.getCause() != null) {
            error += "; cause: " + e.getCause().toString();
        }
    }

    if (error != null) {
        json.put("error", error);
    }

    return new StreamingResolution("application/json", new StringReader(json.toString()));
}

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

License:Apache License

public List<Status> pollConsume() throws TwitterException {

    String keywords = te.getProperties().getKeywords();
    Query query = new Query(keywords);
    if (te.getProperties().isFilterOld()) {
        query.setSinceId(lastId);
    }// ww  w  .jav a2 s  . c  o m

    LOG.debug("Searching twitter with keywords: {}", keywords);
    return search(query);
}

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   w ww.jav a  2 s .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.botlibre.sense.twitter.Twitter.java

License:Open Source License

/**
 * Check search keywords./*from  w  w w  .  j a v a  2 s .c  o m*/
 */
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);
    }
}

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

License:Open Source License

/**
 * Check search keywords.//w ww  .j  a va2 s.  co m
 */
public void checkAutoFollowSearch(int friendCount) {
    if (getAutoFollowSearch().isEmpty()) {
        return;
    }
    log("Processing autofollow search", Level.FINE, getAutoFollowSearch());
    try {
        Network memory = getBot().memory().newMemory();
        Vertex twitter = memory.createVertex(getPrimitive());
        Vertex vertex = twitter.getRelationship(Primitive.LASTAUTOFOLLOWSEARCH);
        long last = 0;
        long max = 0;
        int count = 0;
        if (vertex != null) {
            last = ((Number) vertex.getData()).longValue();
        }
        for (String followSearch : getAutoFollowSearch()) {
            Query query = new Query(followSearch);
            if (vertex != null) {
                query.setSinceId(last);
            }
            SearchResource search = getConnection().search();
            QueryResult result = search.search(query);
            List<Status> tweets = result.getTweets();
            if (tweets != null) {
                for (Status tweet : tweets) {
                    if (count > this.maxSearch) {
                        break;
                    }
                    if (tweet.getId() > last) {
                        log("Autofollow search", Level.FINE, tweet.getText(), tweet.getUser().getScreenName(),
                                followSearch);
                        if (checkFriendship(tweet.getUser().getId(), false)) {
                            friendCount++;
                            if (friendCount >= getMaxFriends()) {
                                log("Max friend limit", Level.FINE, getMaxFriends());
                                return;
                            }
                        }
                        count++;
                        if (tweet.getId() > max) {
                            max = tweet.getId();
                        }
                    }
                }
            }
            if (count > this.maxSearch) {
                break;
            }
        }
        if (max != 0) {
            twitter.setRelationship(Primitive.LASTAUTOFOLLOWSEARCH, memory.createVertex(max));
            memory.save();
        }
    } catch (Exception exception) {
        log(exception);
    }
}

From source file:org.mule.twitter.TwitterConnector.java

License:Open Source License

/**
 * Returns tweets that match a specified query.
 * <p/>/*from  ww  w. j  a v  a  2  s  .c  o  m*/
 * This method calls http://search.twitter.com/search.json
 * <p/>
 * {@sample.xml ../../../doc/twitter-connector.xml.sample twitter:search}
 *
 * @param query The search query.
 * @param lang Restricts tweets to the given language, given by an <a href="http://en.wikipedia.org/wiki/ISO_639-1">ISO 639-1 code</a>
 * @param locale Specify the language of the query you are sending (only ja is currently effective). This is intended for language-specific clients and the default should work in the majority of cases.
 * @param maxId If specified, returns tweets with status ids less than the given id
 * @param since If specified, returns tweets since the given date. Date should be formatted as YYYY-MM-DD
 * @param sinceId Returns tweets with status ids greater than the given id.
 * @param geocode A {@link String} containing the latitude and longitude separated by ','. Used to get the tweets by users located within a given radius of the given latitude/longitude, where the user's location is taken from their Twitter profile
 * @param radius The radius to be used in the geocode -ONLY VALID IF A GEOCODE IS GIVEN-
 * @param unit The unit of measurement of the given radius. Can be 'mi' or 'km'. Miles by default.
 * @param until If specified, returns tweets with generated before the given date. Date should be formatted as YYYY-MM-DD
 * @param resultType If specified, returns tweets included popular or real time or both in the responce. Both by default. Can be 'mixed', 'popular' or 'recent'.
 * @return the {@link QueryResult}
 * @throws TwitterException when Twitter service or network is unavailable
 */
@Processor
public QueryResult search(String query, @Optional String lang, @Optional String locale, @Optional Long maxId,
        @Optional String since, @Optional Long sinceId, @Optional String geocode, @Optional String radius,
        @Default(value = Query.MILES) @Optional String unit, @Optional String until,
        @Optional String resultType) throws TwitterException {
    final Query q = new Query(query);

    if (lang != null) {
        q.setLang(lang);
    }
    if (locale != null) {
        q.setLocale(locale);
    }
    if (maxId != null && maxId.longValue() != 0) {
        q.setMaxId(maxId.longValue());
    }

    if (since != null) {
        q.setSince(since);
    }
    if (sinceId != null && sinceId.longValue() != 0) {
        q.setSinceId(sinceId.longValue());
    }
    if (geocode != null) {
        final String[] geocodeSplit = StringUtils.split(geocode, ',');
        final double latitude = Double.parseDouble(StringUtils.replace(geocodeSplit[0], " ", ""));
        final double longitude = Double.parseDouble(StringUtils.replace(geocodeSplit[1], " ", ""));
        q.setGeoCode(new GeoLocation(latitude, longitude), Double.parseDouble(radius), unit);
    }
    if (until != null) {
        q.setUntil(until);
    }
    if (resultType != null) {
        q.setResultType(resultType);
    }
    return twitter.search(q);
}

From source file:org.rhq.plugins.twitter.FeedComponent.java

License:Open Source License

/**
 * Gather measurement data/*from  w  w w.  j a  va 2 s  .  co  m*/
 *  @see org.rhq.core.pluginapi.measurement.MeasurementFacet#getValues(org.rhq.core.domain.measurement.MeasurementReport, java.util.Set)
 */
public void getValues(MeasurementReport report, Set<MeasurementScheduleRequest> metrics) throws Exception {

    for (MeasurementScheduleRequest req : metrics) {
        if (req.getName().equals("tweetCount")) {
            Twitter twitter = tFactory.getInstance();
            Paging paging = new Paging();

            MeasurementDataNumeric res;
            if (isSearch) {
                Query q = new Query(keyword);
                q.setSinceId(lastId);
                if (lastId == NOT_YET_SET)
                    q.setRpp(1);
                else
                    q.setRpp(20);
                QueryResult qr = twitter.search(q);
                List<Tweet> tweets = qr.getTweets();
                res = new MeasurementDataNumeric(req, (double) tweets.size());

                eventPoller.addTweets(tweets);
                if (tweets.size() > 0)
                    lastId = tweets.get(0).getId();
            } else {
                List<Status> statuses;
                if (lastId == NOT_YET_SET) {
                    paging.setCount(1);
                } else {
                    paging.setCount(100);
                }
                paging.setSinceId(lastId);
                statuses = twitter.getUserTimeline(keyword, paging);
                res = new MeasurementDataNumeric(req, (double) statuses.size());

                eventPoller.addStatuses(statuses);
                if (statuses.size() > 0)
                    lastId = statuses.get(0).getId();

            }
            report.addData(res);
        }
    }
}

From source file:org.shredzone.flufftron.service.TwitterService.java

License:Open Source License

/**
 * Polls new fluff tweets for a {@link Person}.
 *
 * @param person/* w w w. ja v  a2s.  c o  m*/
 *            {@link Person} to find fluff tweets for
 * @throws TwitterException
 *             if the fluff tweets could not be retrieved
 */
public void pollNewFluffs(Person person) throws TwitterException {
    Timeline timeline = person.getTimeline();

    String user = timeline.getTwitter();
    if (user == null || user.isEmpty()) {
        return;
    }

    Query q = new Query().resultType("recent").rpp(50);
    q.query(HASHTAG + " @" + user);

    if (timeline.getLastId() != 0) {
        q.setSinceId(timeline.getLastId());
    }

    Date lastFluff = timeline.getLastFluff();

    QueryResult r = twitter.search(q);

    // The iterator is a workaround because twitter4j seems to be built in a funny
    // way that allows Generics and JDK1.4, but breaks compatibility with Java 7.
    Iterator<Tweet> it = r.getTweets().iterator();
    while (it.hasNext()) {
        Tweet tweet = it.next();

        if (user.equalsIgnoreCase(tweet.getFromUser())) {
            // ignore eigenflausch
            continue;
        }

        Fluff fluff = new Fluff();
        fluff.setPersonId(person.getId());
        fluff.setTwitId(tweet.getId());
        fluff.setCreated(tweet.getCreatedAt());
        fluff.setFrom(tweet.getFromUser());
        fluff.setText(tweet.getText());

        fluffDao.save(fluff);

        if (lastFluff == null || (fluff.getCreated() != null && fluff.getCreated().after(lastFluff))) {
            lastFluff = fluff.getCreated();
        }
    }

    timeline.setLastId(r.getMaxId());
    timeline.setLastFluff(lastFluff);
    personDao.save(person);
}

From source file:org.sociotech.communitymashup.source.twitter.TwitterSourceService.java

License:Open Source License

/**
 * Searches for the query defined in the configuration and adds the results.
 *///w ww. j av  a  2s  .  c o  m
private void search() {
    // get query from configuration
    String query = source.getPropertyValue(TwitterProperties.SEARCH_PROPERTY);

    if (query == null || query.isEmpty()) {
        return;
    }

    QueryResult searchResult = null;

    try {
        Query twitterQuery = new Query(query);
        // set requested number of tweets
        twitterQuery.setCount(getNumberOfSearchTweets());

        // add possible geo location parameter
        addSearchGeoLocation(twitterQuery);

        // if defined set since id
        String sinceId = source.getPropertyValue(TwitterProperties.SEARCH_SINCE_ID_PROPERTY);
        if (sinceId != null && !sinceId.isEmpty()) {
            long sinceIdVal = new Long(sinceId);
            twitterQuery.setSinceId(sinceIdVal);
        }
        searchResult = twitterAPI.search(twitterQuery);
    } catch (TwitterException e) {
        log("Could not search for " + query + "(" + e.getMessage() + ")", LogService.LOG_WARNING);
        return;
    }

    if (searchResult == null) {
        return;
    }

    String sinceId = parseSinceId(searchResult);

    if (sinceId != null) {
        // set it in configuration
        source.addProperty(TwitterProperties.SEARCH_SINCE_ID_PROPERTY, sinceId);
    }

    List<Status> tweets = searchResult.getTweets();

    log("Got " + tweets.size() + " tweets for search " + query, LogService.LOG_DEBUG);

    // add all tweets
    addTweetList(tweets);
}