Example usage for twitter4j Paging Paging

List of usage examples for twitter4j Paging Paging

Introduction

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

Prototype

public Paging(int page, int count, long sinceId) 

Source Link

Usage

From source file:de.jetwick.tw.TwitterSearch.java

License:Apache License

public List<JTweet> getTweets(JUser user, int start, int tweets) throws TwitterException {
    List<JTweet> res = new ArrayList<JTweet>();
    int currentPage = start / tweets;

    if (tweets > 100)
        throw new IllegalStateException("Twitter does not allow more than 100 tweets per page!");

    if (tweets == 0)
        throw new IllegalStateException("tweets should be positive!");

    for (int trial = 0; trial < 2; trial++) {
        try {//from   w  w w.j  a  va 2 s.  c om
            ResponseList rList = twitter.getUserTimeline(user.getScreenName(),
                    new Paging(currentPage + 1, tweets, 1));
            rateLimit--;
            for (Object st : rList) {
                Tweet tw = toTweet((Status) st);
                res.add(new JTweet(tw, user.init(tw)));
            }
            break;
        } catch (TwitterException ex) {
            logger.warn("Exception while getTweets. trial:" + trial + " page:" + currentPage + " - "
                    + Helper.getMsg(ex));
            if (ex.exceededRateLimitation())
                return res;

            continue;
        }
    }

    return res;
}

From source file:de.jetwick.tw.TwitterSearch.java

License:Apache License

/**
 * This method only returns up to 800 statuses, including retweets.
 *///from w ww.j  av  a 2  s.c  o m
public long getHomeTimeline(Collection<JTweet> result, int tweets, long lastId) throws TwitterException {
    if (lastId <= 0)
        lastId = 1;

    Map<String, JUser> userMap = new LinkedHashMap<String, JUser>();
    int hitsPerPage = 100;
    long maxId = lastId;
    long sinceId = lastId;
    int maxPages = tweets / hitsPerPage + 1;

    END_PAGINATION: for (int page = 0; page < maxPages; page++) {
        Paging paging = new Paging(page + 1, tweets, sinceId);
        // avoid that more recent results disturb our paging!
        if (page > 0)
            paging.setMaxId(maxId);

        Collection<Status> tmp = twitter.getHomeTimeline(paging);
        rateLimit--;
        for (Status st : tmp) {
            // determine maxId in the first page
            if (page == 0 && maxId < st.getId())
                maxId = st.getId();

            if (st.getId() < sinceId)
                break END_PAGINATION;

            Tweet tw = toTweet(st);
            String userName = tw.getFromUser().toLowerCase();
            JUser user = userMap.get(userName);
            if (user == null) {
                user = new JUser(st.getUser()).init(tw);
                userMap.put(userName, user);
            }

            result.add(new JTweet(tw, user));
        }

        // sinceId could force us to leave earlier than defined by maxPages
        if (tmp.size() < hitsPerPage)
            break;
    }

    return maxId;
}