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(long sinceId) 

Source Link

Usage

From source file:h2weibo.Twitter2Weibo.java

License:Open Source License

public void syncTwitter(String userId) {
    T2WUser user = helper.findOneByUser(userId);

    weibo.setToken(user.getToken(), user.getTokenSecret());

    Twitter twitter = new TwitterFactory().getInstance();
    if (user.getTwitterToken() != null) {
        twitter.setOAuthAccessToken(new AccessToken(user.getTwitterToken(), user.getTwitterTokenSecret()));
        log.debug(String.format("Using OAuth for %s", user.getUserId()));
    }// w  ww . ja v  a2s.co m

    StatusFilters filters = new StatusFilters();
    filters.use(new NoSyncFilter()); // should be used first
    filters.use(new TcoStatusFilter()).use(new URLStatusFilter()).use(new TagStatusFilter())
            .use(new FlickrImageFilter());

    NoMentionFilter mentionFilter = new NoMentionFilter();
    UserMappingFilter mappingFilter = new UserMappingFilter(helper);

    if (!user.ready()) {
        log.debug(String.format("Skipping @%s ...", user.getUserId()));
        return;
    }

    // gets Twitter instance with default credentials
    String screenName = user.getUserId();
    long latestId = user.getLatestId();

    log.debug(String.format("Checking @%s's timeline, latest ID = %d.", userId, latestId));

    try {
        if (latestId == 0) {
            List<Status> statuses = twitter.getUserTimeline(screenName);
            if (statuses.size() > 0) {
                user.setLatestId(statuses.get(0).getId()); // Record latestId, and sync next time
            }
            log.info(String.format("First time use for @%s. Set latest ID to %d.", userId, latestId));
        } else {
            Paging paging = new Paging(latestId);
            List<Status> statuses = twitter.getUserTimeline(screenName, paging);

            // sync from the oldest one
            for (int i = statuses.size() - 1; i >= 0; i--) {
                Status status = statuses.get(i);

                if (status.getId() < user.getLatestId())
                    continue; // safe keeper

                String name = status.getUser().getScreenName();
                String statusText = status.getText();
                log.info(String.format("%s - %s", name, statusText));
                try {
                    if (status.isRetweet()) {
                        if (user.isDropRetweets()) {
                            user.setLatestId(status.getId());
                            log.debug("Skipped " + statusText + " because status is a retweet.");
                            continue;
                        } else {
                            filters.remove(mentionFilter);
                            filters.use(mappingFilter);
                        }
                    } else {
                        if (user.isDropMentions()) {
                            filters.remove(mappingFilter);
                            filters.use(mentionFilter);
                        } else {
                            filters.remove(mentionFilter);
                            filters.use(mappingFilter);
                        }
                    }

                    statusText = filters.filter(statusText);
                    if (statusText == null) {
                        user.setLatestId(status.getId());
                        log.info(String.format("Skipped %s because of the filter.", statusText));
                        continue;
                    }

                    if (!user.isNoImage()) {
                        // add twitter images to status text
                        MediaEntity[] mediaEntities = status.getMediaEntities();
                        if (mediaEntities != null) {
                            for (MediaEntity entity : mediaEntities) {
                                statusText += " " + entity.getMediaURL();
                            }
                            log.info("with media url: " + statusText);
                        }

                        StatusImageExtractor ex = new StatusImageExtractor();
                        StringBuffer buf = new StringBuffer(statusText);
                        byte[] image = ex.extract(buf);
                        if (image != null) {
                            user.setLatestId(status.getId());
                            try {
                                statusText = buf.toString(); // with image urls removed
                                weibo.uploadStatus(statusText, new ImageItem(image));
                                log.info(String.format("@%s - %s sent with image.", name, statusText));
                            } catch (WeiboException e) {
                                log.error("Faile to update image.", e);
                            }
                            continue;
                        }
                    }

                    GeoLocation location = status.getGeoLocation();
                    if (user.isWithGeo() && location != null) {
                        weibo.updateStatus(statusText, location.getLatitude(), location.getLongitude());
                        log.info(String.format("@%s - %s sent with geo locations.", name, statusText));
                    } else {
                        weibo.updateStatus(statusText);
                        log.info(String.format("@%s - %s sent.", name, statusText));
                    }
                } catch (WeiboException e) {
                    if (e.getStatusCode() != 400) { // resending same tweet
                        log.error("Failed to update Weibo", e);
                        break;
                    } else {
                        log.error("Sending same message", e);
                    }
                }
                log.info(String.format("Sent: by %s - %s", name, statusText));
                user.setLatestId(status.getId());
            }
        }
        helper.saveUser(user);
    } catch (Exception e) {
        if (!(e instanceof TwitterException)) {
            log.error("Failed to update.", e);
        }
    }
}

From source file:it.baywaylabs.jumpersumo.twitter.TwitterListener.java

License:Open Source License

/**
 * This method is called when invoke <b>execute()</b>.<br />
 * Do not invoke manually. Use: <i>new TwitterListener().execute("");</i>
 *
 * @param params/*  www  .  j a va  2s.c  om*/
 */
@Override
protected List<String> doInBackground(String... params) {

    Intelligence ai = new Intelligence();

    try {

        User user = twitter.verifyCredentials();
        Paging paging;
        if (idLastTwit != 0)
            paging = new Paging(idLastTwit + 1);
        else
            paging = new Paging();
        paging.count(100);
        statuses = twitter.getMentionsTimeline(paging);

        if (statuses.size() != 0 && statuses != null) {

            Log.d(TAG, "Showing @" + user.getScreenName() + "'s mentions:");
            Log.d(TAG, statuses.get(0).getText() + "-- ID: " + statuses.get(0).getId());

            if (!"".equals(statuses.get(0).getText()) && licenses.size() == 0) {

                extractCommand(ai, statuses);
                Log.d(TAG, "Non ho licenze attive!");

            } else if (!"".equals(statuses.get(0).getText()) && licenses.size() > 0
                    && f.boolContainsIgnoreCase(licenses, String.valueOf(statuses.get(0).getUser().getId()))) {

                extractCommand(ai, statuses);
                Log.i(TAG, "Seguo la lista delle licenze: " + licenses.toString());

            } else {
                publishProgress(false);
            }

            if (deviceController != null) {

                Random r = new Random();
                Interpreter interp = new Interpreter(deviceController);

                for (int i = 0; i < execute.size(); i++) {
                    Log.d(TAG, "Seguenza istruzioni rilevate: " + execute.toString());

                    if (execute.size() >= 1 && "EXECUTE".equals(execute.get(i))
                            && f.getUrls(statuses.get(0).getText()).size() != 0) {

                        String url = statuses.get(0).getURLEntities()[0].getExpandedURL();
                        // String url = f.getUrls(statuses.get(0).getText()).get(0);
                        File folder = new File(Constants.DIR_ROBOT);
                        Log.d(TAG, "URL Expanded: " + url);

                        if (downloadFileUrl(url, folder)) {
                            FileFilter ff = new FileFilter();
                            File[] list = folder.listFiles(ff);
                            Log.e(TAG, "Lista file: " + list.length);
                            if (list != null && list.length >= 1) {

                                if (list[0].getName().endsWith(".csv") || list[0].getName().endsWith(".txt")) {
                                    String commandsList = "";
                                    try {
                                        commandsList = f.getStringFromFile(list[0].getPath());
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                    Log.d(TAG, "Lista comandi: " + commandsList);
                                    interp.doListCommands(commandsList);
                                    list[0].delete();

                                    if (i == execute.size() - 1) {
                                        try {
                                            StatusUpdate reply = new StatusUpdate(
                                                    ai.actionAnswer(execute.get(i)).get(
                                                            r.nextInt(ai.actionAnswer(execute.get(i)).size()))
                                                            + "@" + statuses.get(0).getUser().getScreenName());
                                            reply.setInReplyToStatusId(statuses.get(0).getId());
                                            twitter.updateStatus(reply);
                                        } catch (TwitterException te) {
                                            Log.e(TAG, "Twitter Post Error: " + te.getMessage());
                                        }
                                    }
                                }

                            }

                        }

                    } else if (execute.size() >= 1 && "PHOTO".equals(execute.get(i))) {
                        // Download immagine e post nel tweet
                        if (interp.doCommand(execute.get(i)) == 0) {

                            String result = FTPDownloadFile(ip_host, 21, "anonymous", "", context);

                            if (i == execute.size() - 1 && !"".equals(result)) {
                                try {
                                    StatusUpdate reply = new StatusUpdate(ai.actionAnswer(execute.get(i))
                                            .get(r.nextInt(ai.actionAnswer(execute.get(i)).size())) + "@"
                                            + statuses.get(0).getUser().getScreenName());
                                    reply.setInReplyToStatusId(statuses.get(0).getId());
                                    File image = new File(Constants.DIR_ROBOT_IMG + "/" + result);
                                    Log.d(TAG, "Nome File immagine: " + image.getPath());
                                    reply.media(image);
                                    twitter.updateStatus(reply);
                                    image.delete();
                                } catch (TwitterException te) {
                                    Log.e(TAG, "Twitter Post Error: " + te.getMessage());
                                }
                            }
                        }
                    } else if (execute.size() >= 1 && !"PHOTO".equals(execute.get(i))
                            && !"EXECUTE".equals(execute.get(i))) {

                        if (interp.doCommand(execute.get(i)) == 0) {

                            if (i == execute.size() - 1) {
                                try {
                                    StatusUpdate reply = new StatusUpdate(ai.actionAnswer(execute.get(i))
                                            .get(r.nextInt(ai.actionAnswer(execute.get(i)).size())) + "@"
                                            + statuses.get(0).getUser().getScreenName());
                                    reply.setInReplyToStatusId(statuses.get(0).getId());
                                    twitter.updateStatus(reply);
                                } catch (TwitterException te) {
                                    Log.e(TAG, "Twitter Post Error: " + te.getMessage());
                                }
                            }
                        }
                    }
                }

            }
        }

        Log.d(TAG, "ready exit");

    } catch (TwitterException te) {
        te.printStackTrace();
        Log.e(TAG, "Failed to get timeline: " + te.getMessage());
    }

    return execute;
}

From source file:jp.wda.azeda.core.daemons.impl.RetweetsCrawlerImpl.java

License:Apache License

/**
 *
 * @param twitter/*  w  w w.j  ava 2 s .  c  o  m*/
 * @param retweetTo
 * @param retweeter
 * @param page
 * @return
 * @throws TwitterException
 */
private List<Status> loadRetweetersTweets(Twitter twitter, long retweetTo, long retweeter, int page)
        throws TwitterException {
    if (!alive) {
        return null;
    }
    if (page > timelinePages) {
        log.info(retweetTo + " retweet by " + retweeter + " ? " + (timelinePages * 100)
                + "????????????");
        insertRetweeter(twitter.getId(), retweetTo, retweeter);
        return null;
    }

    Paging paging = new Paging(retweetTo);
    paging.setPage(page);
    paging.setCount(100);
    ResponseList<Status> userTimeline = gtUserTimeline(twitter, retweeter, paging);

    if (userTimeline.size() == 0) {
        return null;
    }

    List<Status> result = new ArrayList<Status>();
    for (Status s : userTimeline) {
        result.add(s);
        if (s.isRetweet()) {
            if (s.getRetweetedStatus().getId() != retweetTo) {
                continue;
            }
            log.debug("found!!");
            return result;
        }
    }

    return loadRetweetersTweets(twitter, retweetTo, retweeter, page + 1);
}

From source file:net.lacolaco.smileessence.twitter.util.TwitterUtils.java

License:Open Source License

public static Paging getPaging(int count) {
    return new Paging(1).count(count);
}

From source file:org.apache.camel.component.twitter.consumer.directmessage.DirectMessageConsumer.java

License:Apache License

public List<DirectMessage> pollConsume() throws TwitterException {
    List<DirectMessage> list = te.getProperties().getTwitter().getDirectMessages(new Paging(lastId));
    for (DirectMessage dm : list) {
        checkLastId(dm.getId());// w w  w. j  av  a  2 s. com
    }
    return list;
}

From source file:org.apache.camel.component.twitter.consumer.timeline.HomeConsumer.java

License:Apache License

public List<Status> pollConsume() throws TwitterException {
    List<Status> list = te.getProperties().getTwitter().getHomeTimeline(new Paging(lastId));
    for (Status s : list) {
        checkLastId(s.getId());//w ww .  j  a  va 2  s  .  co m
    }
    return list;
}

From source file:org.apache.camel.component.twitter.consumer.timeline.MentionsConsumer.java

License:Apache License

public List<Status> pollConsume() throws TwitterException {
    List<Status> list = te.getProperties().getTwitter().getMentionsTimeline(new Paging(lastId));
    for (Status s : list) {
        checkLastId(s.getId());//from www .  j  a v  a  2 s.  c om
    }
    return list;
}

From source file:org.apache.camel.component.twitter.consumer.timeline.RetweetsConsumer.java

License:Apache License

public List<Status> pollConsume() throws TwitterException {
    List<Status> list = te.getProperties().getTwitter().getRetweetsOfMe(new Paging(lastId));
    for (Status s : list) {
        checkLastId(s.getId());/*from  w w w. j a  va  2s.c o  m*/
    }
    return list;
}

From source file:org.apache.camel.component.twitter.consumer.timeline.UserConsumer.java

License:Apache License

public List<Status> pollConsume() throws TwitterException {
    List<Status> list = te.getProperties().getTwitter().getUserTimeline(te.getProperties().getUser(),
            new Paging(lastId));
    for (Status s : list) {
        checkLastId(s.getId());//from   www.  ja v a  2 s .  c  o  m
    }
    return list;
}

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

License:Open Source License

/**
 * Learn responses from the tweet search.
 *//*from  www  . j a v  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);
    }
}