Example usage for twitter4j QueryResult getMaxId

List of usage examples for twitter4j QueryResult getMaxId

Introduction

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

Prototype

long getMaxId();

Source Link

Usage

From source file:ch.schrimpf.core.TwitterCrawler.java

License:Open Source License

/**
 * Performs a single crawl step according to the previous initialized query
 * until the specified limit is reached. Received tweets are stored in the
 * *.csv specified in the easyTwitterCrawler.properties file.
 * <p/>//  ww w.  j  av a 2 s.c o  m
 * TODO make selecting values flexible
 *
 * @param limit to stop on
 */
public void crwal(int limit) {
    LOG.info("receiving tweets...");
    int i = 0;
    while (i < limit && running) {
        try {
            QueryResult res = twitter.search(query);
            if (res.getMaxId() > last) {
                for (Status status : res.getTweets()) {
                    String[] line = { String.valueOf(status.getId()), String.valueOf(status.getCreatedAt()),
                            status.getText(), String.valueOf(status.getUser()),
                            String.valueOf(status.getPlace()), status.getLang() };
                    csv.writeResult(Arrays.asList(line));
                    i++;
                }
                last = res.getMaxId();
            } else {
                break;
            }
        } catch (TwitterException e) {
            LOG.warning("could not process tweets");
        }
    }
    tweets += i;
    LOG.info(i + " tweets received in this crawl");
    LOG.info("totally " + tweets + " received");
}

From source file:net.lacolaco.smileessence.activity.MainActivity.java

License:Open Source License

public void startNewSearch(final Twitter twitter, final String query) {
    setLastSearch(query);/*from   w  w w. j  av a2  s .c  om*/
    if (!TextUtils.isEmpty(query)) {
        final SearchListAdapter adapter = (SearchListAdapter) getListAdapter(ADAPTER_SEARCH);
        adapter.initSearch(query);
        adapter.clear();
        adapter.updateForce();
        new SearchTask(twitter, query, this) {
            @Override
            protected void onPostExecute(QueryResult queryResult) {
                super.onPostExecute(queryResult);
                if (queryResult != null) {
                    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, getCurrentAccount());
                            adapter.addToTop(viewModel);
                            StatusFilter.filter(MainActivity.this, viewModel);
                        }
                    }
                    adapter.setTopID(queryResult.getMaxId());
                    adapter.updateForce();
                }
            }
        }.execute();
    }
}

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//  ww w.j av  a2 s  .  c  o  m
            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;//  w w  w  .  jav a  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.shredzone.flufftron.service.TwitterService.java

License:Open Source License

/**
 * Polls new fluff tweets for a {@link Person}.
 *
 * @param person// w  ww  .jav  a  2s  .c om
 *            {@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

private String parseSinceId(QueryResult searchResult) {
    if (searchResult == null) {
        return null;
    }// w  w  w  .  j a va2  s.  co m

    return "" + searchResult.getMaxId();
}