Example usage for twitter4j Status getId

List of usage examples for twitter4j Status getId

Introduction

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

Prototype

long getId();

Source Link

Document

Returns the id of the status

Usage

From source file:com.appspot.bitlyminous.handler.twitter.SearchHandler.java

License:Apache License

@Override
public StatusUpdate execute(Status tweet) {
    GoogleSearchQueryFactory factory = GoogleSearchQueryFactory
            .newInstance(ApplicationConstants.GOOGLE_API_KEY);
    WebSearchQuery query = factory.newWebSearchQuery();
    query.setReferrer(ApplicationConstants.GOOGLE_API_REFERER);
    PagedList<WebResult> response = query.withQuery(getMentionText(tweet)).list();
    if (response.isEmpty()) {
        return null;
    } else {//from www  .java2  s . com
        StatusUpdate update = new StatusUpdate(
                "@" + tweet.getUser().getScreenName() + " " + buildStatus(response.get(0)));
        update.setInReplyToStatusId(tweet.getId());
        return update;
    }
}

From source file:com.appspot.bitlyminous.handler.twitter.SendRelatedUrlsHandler.java

License:Apache License

@Override
public StatusUpdate execute(Status tweet) {
    try {//from  w  ww  .j av  a  2  s .  c om
        DeliciousGateway deliciousGateway = getDeliciousGateway(context.getVersion());
        DiggGateway diggGateway = getDiggGateway();
        List<Url> urls = context.getUrls();
        for (Url entity : urls) {
            WebSearchQuery googleQuery = getGoogleWebSearchQuery();
            googleQuery.withRelatedSite(entity.getUrl());
            List<Url> relatedUrls = new ArrayList<Url>();
            List<String> tags = entity.getTags();
            if (!tags.isEmpty()) {
                for (String tag : tags) {
                    try {
                        relatedUrls.addAll(deliciousGateway.getPopularUrls(tag));
                        relatedUrls.addAll(convertToUrls(diggGateway.getPopularStories(tag, 5)));
                    } catch (Exception e) {
                        logger.log(Level.WARNING, "Error while getting related urls.", e);
                    }
                }
                relatedUrls.addAll(convertToUrls(googleQuery.withQuery(createQuery(tags)).list()));
                relatedUrls = getBestMatches(entity, relatedUrls, 2);
            }

            if (!relatedUrls.isEmpty()) {
                StatusUpdate reply = new StatusUpdate(
                        "@" + tweet.getUser().getScreenName() + " " + buildStatus(relatedUrls));
                reply.setInReplyToStatusId(tweet.getId());
                return reply;
            }
        }
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Error while sending related urls.", e);
    }

    return null;
}

From source file:com.appspot.bitlyminous.handler.twitter.TweetForMeHandler.java

License:Apache License

@Override
public StatusUpdate execute(Status tweet) {
    try {/*w  w w. j  a  v a2  s  . c om*/
        User user = context.getUser();
        if (user == null) {
            user = getUserByScreenName(tweet.getUser().getScreenName());
            context.setUser(user);
        }
        if (user == null || user.getTwitterToken() == null || user.getTwitterSecret() == null) {
            StatusUpdate reply = new StatusUpdate(
                    ApplicationResources.getLocalizedString("com.appspot.bitlyminous.message.notAuthenticated",
                            new String[] { "@" + tweet.getUser().getScreenName(), "Twitter",
                                    shortenUrl(ApplicationConstants.TWITTER_CALLBACK_URL) }));
            reply.setInReplyToStatusId(tweet.getId());
            return reply;
        } else {
            Twitter twitter = getTwitterClient(user.getTwitterToken().getValue(),
                    user.getTwitterSecret().getValue());
            String status = buildStatus(user.getUrls(), user.getTags());
            if (status != null) {
                StatusUpdate reply = new StatusUpdate(status);
                reply.setInReplyToStatusId(tweet.getId());
                twitter.updateStatus(reply);
            }
            return null;
        }
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Error while tweeting on behalf of user.", e);
    }
    return null;
}

From source file:com.appspot.bitlyminous.service.impl.TwitterServiceImpl.java

License:Apache License

public void handleMentions() {
    try {/*from  ww  w .j av a 2 s.co m*/
        Twitter twitter = getTwitterClient();
        TwitterHandlerFactory factory = TwitterHandlerFactory.newInstance(new TwitterContext());
        Version version = getCurrentVersion();
        ResponseList<Status> homeTimeline = twitter
                .getMentions(new Paging(getCurrentVersion().getLastMentionId()));
        long lastMentionId = 0L;
        for (Status tweet : homeTimeline) {
            if (tweet.getId() > version.getLastMentionId()) {
                if (tweet.getId() > lastMentionId) {
                    lastMentionId = tweet.getId();
                }
                TwitterHandler command = factory.createCommand(tweet);
                StatusUpdate reply = command.execute(tweet);
                if (reply != null) {
                    twitter.updateStatus(reply);
                }
            }
        }
        if (lastMentionId > version.getLastMentionId()) {
            version.setLastMentionId(lastMentionId);
            EntityManager entityManager = createEntityManager();
            entityManager.persist(version);
            closeEntityManager(entityManager);
        }
    } catch (Exception e) {
        throw new ServiceException(e);
    }
}

From source file:com.appspot.bitlyminous.service.impl.TwitterServiceImpl.java

License:Apache License

public void handleFriendsTimeline() {
    try {/*from w w  w  .java 2 s  .  c o m*/
        Version version = getCurrentVersion();
        Twitter twitter = getTwitterClient();
        List<Status> homeTimeline = twitter.getFriendsTimeline(new Paging(version.getLastHomeTweetId()));
        long lastTweetId = 0L;
        for (Status status : homeTimeline) {
            if (status.getId() > lastTweetId) {
                lastTweetId = status.getId();
            }
            if (!ApplicationConstants.TWITTER_SCREEN_NAME.equalsIgnoreCase(status.getUser().getScreenName())) {
                TwitterContext context = new TwitterContext();
                context.setVersion(version);
                for (TwitterHandler command : getTimelineHandlers(context)) {
                    StatusUpdate reply = command.execute(status);
                    if (reply != null) {
                        twitter.updateStatus(reply);
                    }
                }
                saveUser(context.getUser());
            }
        }
        if (lastTweetId > version.getLastHomeTweetId()) {
            version.setLastHomeTweetId(lastTweetId);
            saveVersion(version);
        }
    } catch (Exception e) {
        throw new ServiceException(e);
    }
}

From source file:com.appspot.bitlyminous.service.impl.TwitterServiceImpl.java

License:Apache License

public void searchPublicTimeline() {
    try {/*  w ww  .  j  a  va 2s .c om*/
        UserService userService = ServiceFactory.newInstance().createUserService();
        for (com.appspot.bitlyminous.entity.User user : userService.getAllAuthenticatedUsers()) {
            Twitter twitter = getTwitterClient(user.getTwitterToken().getValue(),
                    user.getTwitterSecret().getValue());
            List<Status> Status = twitter.getFriendsTimeline();
            long lastTweetId = 0L;
            for (Status tweet : Status) {
                if (tweet.getId() > lastTweetId) {
                    lastTweetId = tweet.getId();
                }
                ScanLinkHandler handler = new ScanLinkHandler(null);
                handler.execute(tweet);
                // if the tweet is from a friend or a friend of friend check for malware 
                // if the tweet is from a friend show related urls and tweets.
                // if the tweet is from a friend add it to the database against user to compute statistics and suggest people.
                // if the tweet is from a friend add to dig if he has signed up.
                //                  IDs retweetedByIDs = twitter.getRetweetedByIDs(tweet.getId());
            }
            if (lastTweetId > user.getLastFriendFeedId()) {
                user.setLastFriendFeedId(lastTweetId);
                saveUser(user);
            }
        }
    } catch (Exception e) {
        throw new ServiceException(e);
    }
}

From source file:com.appspot.socialinquirer.server.service.impl.UserServiceImpl.java

License:Apache License

@Override
public void aggregateQuotes() {
    EntityManager entityManager = createEntityManager();
    try {//from  w  ww  .j  a  v  a2s  . c o  m
        DataAccessObject dao = getDao(entityManager);
        Version version = getCurrentVersion();
        Twitter twitter = getTwitterClient();
        List<Status> homeTimeline = twitter.getUserTimeline(ApplicationConstants.TWITTER_SCREEN_NAME,
                new Paging(version.getLastHomeTweetId()));
        long lastTweetId = 0L;
        for (Status status : homeTimeline) {
            if (status.getId() > lastTweetId) {
                lastTweetId = status.getId();
            }
            if (!status.isRetweet() && status.getInReplyToStatusId() == -1) {
                Quote quote = new Quote();
                quote.setPublishedDate(status.getCreatedAt());
                quote.setText(new Text(status.getText()));
                quote.setTweetId(status.getId());
                dao.saveOrUpdate(quote);
            }
        }
        if (lastTweetId > version.getLastHomeTweetId()) {
            version.setLastHomeTweetId(lastTweetId);
            dao.saveOrUpdate(version);
        }
    } catch (Exception e) {
        throw new ServiceException(e);
    } finally {
        closeEntityManager(entityManager);
    }
}

From source file:com.aremaitch.codestock2010.repository.TweetObj.java

License:Apache License

public static TweetObj createInstance(Status status) {
    TweetObj to = new TweetObj();
    to.setId(status.getId());
    to.setText(status.getText());//from w ww.ja va2 s  . c  om
    to.setToUserId(status.getInReplyToUserId());
    to.setToUser(status.getInReplyToScreenName());
    if (status.getUser() != null) {
        to.setFromUser(status.getUser().getScreenName());
        to.setFromUserId(status.getUser().getId());
        to.setIsoLanguageCode(status.getUser().getLang());
        to.setProfileImageUrl(status.getUser().getProfileBackgroundImageUrl());
    }
    to.setSource(status.getSource());
    to.setCreatedAt(status.getCreatedAt());
    if (status.getGeoLocation() != null) {
        to.setLatitude(status.getGeoLocation().getLatitude());
        to.setLongitude(status.getGeoLocation().getLongitude());
    }
    return to;

}

From source file:com.cafeform.iumfs.twitterfs.files.AbstractTimelineFile.java

License:Apache License

/**
 * Get Status data of requested bytes at requested offset. The base of
 * offset statts from oldest status.//from   ww  w.  j a v  a2 s.  com
 *
 * Procect list by synchronizing, not to read when time line is updated.
 *
 * TODO: CopyOnWriteArrayList would be best, but it doesn't support
 * sorting.. TODO: In current implementation, calculation of offset is very
 * costly. Should I store status as simple text data?
 *
 * @param buf
 * @param size
 * @param offset
 * @return
 */
@Override
public synchronized long read(ByteBuffer buf, long size, long offset) {
    long curr_size = 0;
    long curr_offset = 0;
    long prev_offset = 0;
    long rel_offset; // relative offset within each status 
    int page = 0;

    if (logger.isLoggable(FINER)) {
        stopWatch.start();
    }

    /*
     * OLD                                                         NEW
     * Status             |Status                      |Status     
     * --------offset------------------->|<----------size------------->
     *                     
     * ----prev_offset--->|
     *                    |<-rel_offset->|
     *                    
     * ------------------curr_offset------------------>|
     *                                   |<-copy_size->|
     */
    for (Status status : statusList) {
        try {
            prev_offset = curr_offset;
            String text = statusToFormattedString(status);
            byte[] bytes = text.getBytes("UTF-8");
            long copy_size = 0;
            long status_length = bytes.length;
            rel_offset = 0;

            logger.log(FINER, getUserAndName() + " Read status_list id=" + status.getId() + ", status_length="
                    + status_length);

            logger.log(FINEST, getUserAndName() + text);

            /*
             * Calculate current offset by adding number of characters to 
             * previous offset.
             */
            curr_offset += status_length;
            logger.log(FINER, getUserAndName() + " curr_off=" + curr_offset);
            if (curr_offset < offset) {
                logger.log(FINER, getUserAndName() + " offset not yet reached");
                continue;
            } else if (prev_offset >= offset) {
                logger.log(FINER, getUserAndName() + " prev_offset >= offset");
                /*
                 * Already exceed offset. set relative offset to 0.
                 */
                rel_offset = 0;
            } else {
                logger.log(FINER, getUserAndName() + " prev_offset < offset");
                /*
                 * offset is within this status.
                 * calculate relative offset.
                 */
                rel_offset = offset - prev_offset;
            }
            logger.log(FINER, getUserAndName() + "rel_offset = " + rel_offset);

            if (curr_size + status_length >= size) {
                /*
                 * status size is larger or equal to requested size.
                 * Copy necessary data size and won't read any more. 
                 */
                copy_size = size - curr_size - rel_offset;
                logger.log(FINER,
                        getUserAndName() + " copy_Size = " + copy_size + ". No need to read more Status");
            } else {
                /*
                 * need more data... copy all status data to buffer.
                 */
                copy_size = bytes.length - rel_offset;
                logger.log(FINER,
                        getUserAndName() + " copy_Size = " + copy_size + ". need to read more Status");
            }
            /*
             * write to buffer
             */
            buf.put(bytes, (int) rel_offset, (int) copy_size);
            curr_size += copy_size;
            if (curr_size >= size) {
                logger.log(FINER, getUserAndName() + " currSize >= size");
                break;
            }
            logger.log(FINER, getUserAndName() + " curr_size < size. continue for statement.");
        } catch (UnsupportedEncodingException ex) {
            logger.log(Level.INFO, getUserAndName() + " Cannot decode text in timeline.", ex);
        }
    }
    logger.log(FINE, getUserAndName() + " curr_size = " + curr_size);
    if (logger.isLoggable(FINER)) {
        logger.log(FINER, getUserAndName() + " read took " + stopWatch.stop().toString());
    }
    return curr_size;
}

From source file:com.cafeform.iumfs.twitterfs.files.AbstractTimelineFile.java

License:Apache License

synchronized public void addStatusToList(Status status) {
    if (logger.isLoggable(FINER)) {
        stopWatch.start();/*from   w  w w. j  a  v  a  2s .c  o m*/
    }
    try {
        logger.log(FINER, getUserAndName() + " Adding Status id is " + status.getId());
        logger.log(FINEST, getUserAndName() + statusToFormattedString(status));
        setLength(length() + statusToFormattedString(status).getBytes("UTF-8").length);
        statusList.add(status);
        lastId = status.getId();

        logger.log(FINER, getUserAndName() + " new file size is " + length());
        // Timelie is update. So changed mtime and ctime
        Date now = new Date();
        setMtime(now.getTime());
        setCtime(now.getTime());
    } catch (UnsupportedEncodingException ex) {
        logger.log(SEVERE, getUserAndName() + " Cannot decode string in timeline", ex);
    }
    if (logger.isLoggable(FINER)) {
        logger.log(FINER, getUserAndName() + " addStatusToList took " + stopWatch.stop().toString());
    }
}