Search in sources :

Example 1 with Timeline

use of weibo4j.Timeline in project twitter-2-weibo by rjyo.

the class Twitter2Weibo method syncTwitter.

public void syncTwitter(String userId) {
    T2WUser user = helper.findOneByUser(userId);
    weibo.setToken(user.getToken());
    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()));
    }
    StatusFilters filters = new StatusFilters();
    // should be used first
    filters.use(new NoSyncFilter());
    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) {
                // Record latestId, and sync next time
                user.setLatestId(statuses.get(0).getId());
            }
            log.info(String.format("First time use for @%s. Set latest ID to %d.", userId, latestId));
        } else {
            Timeline tl = new Timeline();
            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);
                // safe keeper
                if (status.getId() < user.getLatestId())
                    continue;
                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 {
                                // with image urls removed
                                statusText = buf.toString();
                                tl.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) {
                        tl.UpdateStatus(statusText, (float) location.getLatitude(), (float) location.getLongitude(), "");
                        log.info(String.format("@%s - %s sent with geo locations.", name, statusText));
                    } else {
                        tl.UpdateStatus(statusText);
                        log.info(String.format("@%s - %s sent.", name, statusText));
                    }
                } catch (WeiboException e) {
                    if (e.getStatusCode() != 400) {
                        // resending same tweet
                        log.warn("Failed to update Weibo");
                        break;
                    }
                }
                user.setLatestId(status.getId());
            }
        }
        helper.saveUser(user);
    } catch (Exception e) {
        if (!(e instanceof TwitterException)) {
            log.error("Failed to update.", e);
        }
    }
}
Also used : AccessToken(twitter4j.auth.AccessToken) StatusImageExtractor(h2weibo.utils.StatusImageExtractor) WeiboException(weibo4j.model.WeiboException) Timeline(weibo4j.Timeline) WeiboException(weibo4j.model.WeiboException) T2WUser(h2weibo.model.T2WUser) ImageItem(weibo4j.http.ImageItem)

Aggregations

T2WUser (h2weibo.model.T2WUser)1 StatusImageExtractor (h2weibo.utils.StatusImageExtractor)1 AccessToken (twitter4j.auth.AccessToken)1 Timeline (weibo4j.Timeline)1 ImageItem (weibo4j.http.ImageItem)1 WeiboException (weibo4j.model.WeiboException)1