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.mycompany.omnomtweets.Search.java

/**
 * Method to write the tweets to file, base 64 encoded tweet text.
 * @param tweets the tweets to be written
 * @param filename the file to write the tweets into
 * @return true unless something bad happens
 *///from   w  ww  .j  a v  a2s. c  om
public static boolean writeTweetsToFile(List<Status> tweets, String filename) {
    //System.out.println("Writing " + tweets.size() + " tweets");
    boolean success = true;
    try {
        FileWriter addTweets = new FileWriter(new File(filename), true);
        if (tweets != null && tweets.size() > 0) {
            for (Status tweet : tweets) {
                String tweetText;
                String idOfRetweetee = "";
                if (tweet.getRetweetedStatus() != null) {
                    tweetText = "RT " + tweet.getRetweetedStatus().getText();
                    idOfRetweetee = "" + tweet.getRetweetedStatus().getUser().getScreenName();
                    //System.out.println("retweeted" + tweetText);
                } else {
                    tweetText = tweet.getText();
                }
                String urlText = "";
                if (tweet.getURLEntities().length > 0) {
                    for (URLEntity url : tweet.getURLEntities()) {
                        if (url.getExpandedURL() != null) {
                            urlText += url.getExpandedURL() + " ";
                            tweetText = tweetText.replace(url.getURL(), url.getExpandedURL());
                            //System.out.println("Expanded URL " + url.getExpandedURL());
                        } else {
                            urlText += url.getURL() + " ";
                            //System.out.println("URL " + url.getURL());    
                        }
                    }
                }
                if (tweet.getMediaEntities().length > 0) {
                    for (MediaEntity media : tweet.getMediaEntities()) {
                        if (media.getExpandedURL() != null) {
                            urlText += media.getExpandedURL() + " ";
                            tweetText = tweetText.replace(media.getMediaURL(), media.getExpandedURL());
                            //System.out.println("Expanded URL " + media.getExpandedURL());
                        } else {
                            urlText += media.getMediaURL() + " ";
                            //System.out.println("URL " + media.getMediaURL());    
                        }
                    }
                }
                String encodedText = tweetText.replaceAll("\"", "\"\"");
                encodedText = encodedText.replaceAll("\n", " ");
                String writeMe = "\"" + encodedText + "\"," + urlText + "," + tweet.getUser().getId() + ","
                        + tweet.getId() + "," + candidate.name + "," + tweet.getCreatedAt() + ","
                        + idOfRetweetee + "\n";
                //System.out.println(writeMe);
                addTweets.write(writeMe);
            }
        }
        addTweets.close();
    } catch (IOException ex) {
        //System.out.println("Something broke lol");
        success = false;
    }
    return success;
}

From source file:com.mycompany.omnomtweets.TweetsAboutCandidates.java

/**
 * Method to write the tweets to file, base 64 encoded tweet text.
 * @param tweets the tweets to be written
 * @param filename the file to write the tweets into
 * @return true unless something bad happens
 *//*from   w ww.  j  a va2  s.  c o  m*/
public boolean writeTweetsToFile(List<Status> tweets, String filename) {
    //System.out.println("Writing " + tweets.size() + " tweets");
    boolean success = true;
    try {
        FileWriter addTweets = new FileWriter(new File(filename), true);
        if (tweets != null && tweets.size() > 0) {
            for (Status tweet : tweets) {
                String tweetText;
                String idOfRetweetee = "";
                if (tweet.getRetweetedStatus() != null) {
                    tweetText = "RT " + tweet.getRetweetedStatus().getText();
                    idOfRetweetee = "" + tweet.getRetweetedStatus().getUser().getScreenName();
                    //System.out.println("retweeted" + tweetText);
                } else {
                    tweetText = tweet.getText();
                }
                String urlText = "";
                if (tweet.getURLEntities().length > 0) {
                    for (URLEntity url : tweet.getURLEntities()) {
                        if (url.getExpandedURL() != null) {
                            urlText += url.getExpandedURL() + " ";
                            //System.out.println("Expanded URL " + url.getExpandedURL());
                        } else {
                            urlText += url.getURL() + " ";
                            //System.out.println("URL " + url.getURL());    
                        }
                    }
                }
                if (tweet.getMediaEntities().length > 0) {
                    for (MediaEntity media : tweet.getMediaEntities()) {
                        if (media.getExpandedURL() != null) {
                            urlText += media.getExpandedURL() + " ";
                            //System.out.println("Expanded URL " + media.getExpandedURL());
                        } else {
                            urlText += media.getMediaURL() + " ";
                            //System.out.println("URL " + media.getMediaURL());    
                        }
                    }
                }
                String encodedText = tweet.getText().replaceAll("\"", "\"\"");
                String writeMe = "\"" + encodedText + "\"," + urlText + "," + tweet.getUser().getId() + ","
                        + tweet.getId() + "," + candidate.name + "," + tweet.getCreatedAt() + ","
                        + idOfRetweetee + "\n";
                //System.out.println(writeMe);
                addTweets.write(writeMe);
            }
        }
        addTweets.close();
    } catch (IOException ex) {
        System.out.println("Something broke lol");
        success = false;
    }
    return success;
}

From source file:com.narvis.frontend.twitter.input.Input.java

License:Open Source License

private TwitterMessageInOut createMessageFromTweet(Status status) throws IllegalKeywordException {
    return new TwitterMessageInOut(this.accessTwitter.getConf().getName(), getCleanTweet(status.getText()),
            status.getUser().getScreenName() + getOtherResponseName(status.getText()), this.accessTwitter,
            status.getId());
}

From source file:com.ocpsoft.hatchling.twitter.PersistentStatusListener.java

License:Open Source License

@Override
public void onStatus(final Status status) {
    Tweet t = new Tweet();
    t.setReceived(status.getCreatedAt());
    t.setText(status.getText());/* www  . j a v a  2s  . com*/
    t.setScreenName(status.getUser().getScreenName());
    t.setUserName(status.getUser().getName());
    t.setUserProfileImageURL(status.getUser().getProfileImageURL());
    t.setProfileURL(null);
    t.setTweetId(status.getId());

    URLEntity[] urlEntities = status.getURLEntities();
    if (urlEntities != null) {
        for (URLEntity url : urlEntities) {
            TweetURL tweetURL = new TweetURL();
            if (url.getExpandedURL() != null) {
                tweetURL.setURL(url.getExpandedURL());
            } else if (url.getURL() != null) {
                tweetURL.setURL(url.getURL());
            }
            t.getURLs().add(tweetURL);
        }
    }

    buffer.add(t);
}

From source file:com.ontotext.s4.TwitterVisualization.downloadTweets.SearchTweets.java

License:Open Source License

/**
 * Saves a Tweet into file. The name of the file will be the id of the Tweet.
 * //w  ww. j av  a 2 s  .co  m
 * @param tweet
 *            Tweet message to save.
 */
private void saveTweetIntoFile(Status tweet) {
    /*
     * checks if data folder exist. If not creates it.
     */
    File files = new File(dataFolder);
    if (!files.exists()) {
        files.mkdirs();
    }

    BufferedWriter writer = null;
    try {
        /*
         * create file into data folder with id for name, json for extension
         * and UTF-8 encoding
         */
        writer = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(dataFolder + "/" + tweet.getId() + ".json"), "UTF-8"));

        /*
         * appends raw Json from tweet and then add new line into file
         */
        writer.append(TwitterObjectFactory.getRawJSON(tweet) + "\n");
    } catch (IOException e) {
        logger.debug(e);
    } finally {
        try {
            writer.close();
        } catch (IOException e) {
            logger.debug("Something went wrong when closing tweet file.", e);
        }
    }
}

From source file:com.projectlaver.util.TwitterListingResponseHandler.java

License:Open Source License

public void processStatus(Status status) {

    if (this.logger.isDebugEnabled()) {
        this.logger.debug("+processStatus() with status id: " + status.getId());
    }//ww w  .  j  a  v a2  s . c  o  m

    String twitterId = this.longToString(status.getId());
    User profile = status.getUser();

    // Convert twitterIds to Strings here because these fields are treated as varchars by the database
    String providerUserId = this.longToString(profile.getId());
    String inReplyToStatusId = this.longToString(status.getInReplyToStatusId());
    String tweetText = status.getText();
    Boolean isRetweet = (status.getRetweetedStatus() != null);

    // these two fields are required; without an @mention and a #hashtag we do not need to persist this tweet
    String mentionedProviderUserId = this.getMentionedProviderUserId(status.getUserMentionEntities());
    String hashtag = this.getMentionedHashtag(status.getHashtagEntities());

    if (StringUtils.isNoneBlank(mentionedProviderUserId, hashtag)) {

        ReplyMessageDTO dto = new ReplyMessageDTO(SocialProviders.TWITTER, providerUserId, twitterId,
                inReplyToStatusId, null, isRetweet, tweetText, hashtag, INITIAL_STATUS, status.getCreatedAt());

        // Use a try catch here to trap excptions
        try {
            super.processUserMessage(mentionedProviderUserId, dto, false);

            // Retry on deadlock
        } catch (DeadlockLoserDataAccessException e) {

            this.logger.error("Lost deadlock trying to insert tweet. Falling back on retry logic.");
            this.retryProcessAfterDeadlock(mentionedProviderUserId, dto);

            // Log on uncaught exception
        } catch (Exception e) {
            this.logger.error(String.format(
                    "Valid message with id: %s read by stream, but processing failed with an exception.",
                    twitterId), e);
            this.logger.error("UNINSERTED TWEET: " + ToStringBuilder.reflectionToString(dto));
        }
    }

    if (this.logger.isDebugEnabled()) {
        this.logger.debug("-processStatus() with status id: " + status.getId());
    }
}

From source file:com.rackspace.spark.TwitterFilterFunction.java

License:Open Source License

@Override
public Tuple2<Long, String> call(Status status) {
    try {/*from   w  ww.  j a  v  a2 s.  c  om*/
        if (status != null && status.getText() != null) {
            long id = status.getId();
            String text = status.getText();
            return new Tuple2<Long, String>(id, text);
        }
        return null;
    } catch (Exception ex) {
        Logger LOG = Logger.getLogger(this.getClass());
        LOG.error("IO error while filtering tweets", ex);
        LOG.trace(null, ex);
    }
    return null;
}

From source file:com.raythos.sentilexo.files.twitter.TwitterJSONLoaderToKafka.java

@Override
protected void handleStatusObject(int lineNo, Status status, String rawJSONLine) {
    log.trace("Posting Status with id " + status.getId() + "from File" + filename + " line #" + lineNo);
    try {/*from  w  w  w.  ja v  a2s .c  o  m*/
        TwitterQueryResultItemAvro tqri = new TwitterQueryResultItemAvro();

        tqri = QueryResultItemMapper.mapItem(queryOwner, queryName, queryTerms, status);
        byte[] data = QueryResultItemMapper.getAvroSerialized(tqri);
        twitterResultItemTopic.postBinary(data);
        twitterJsonTopic.postPropertyValuePair(tqri.getStatusId().toString(), rawJSONLine);
        log.trace("Status with id " + status.getId() + "posted to Kafka topic  from file" + getFilename()
                + " line #" + lineNo);
    } catch (Exception e) {
        log.error("error when posting status to Kafka" + e);

    }
}

From source file:com.raythos.sentilexo.trident.twitter.TextLineToResultItemFunction.java

License:Apache License

@Override
public void execute(TridentTuple tuple, TridentCollector tc) {
    String owner = tuple.getString(0);
    String queryName = tuple.getString(1);
    String queryTerms = tuple.getString(2);
    String jsonText = tuple.getString(3);
    Status status = null;
    try {//from   w ww  .j a v a 2  s  .com
        log.trace("processing json " + jsonText);
        if (jsonText.startsWith("{\"created_at")) {
            status = TwitterObjectFactory.createStatus(jsonText);
            log.trace("Status object was created from " + jsonText);

            ResultJson jsonDataItem = new ResultJson(status.getId(), jsonText);
            jsonDataItem.save();
            log.trace("JsonText was saved in jsonLog for " + status.getId());

            TwitterQueryResultItemAvro avroObject = QueryResultItemMapper.mapItem(owner, queryName, queryTerms,
                    status);
            log.trace("Text was deserialised into Avro object with status id = " + avroObject.getStatusId());
            ResultItem dataItem = new ResultItem(avroObject);
            dataItem.save();
            log.info("Result Item StatusId = " + avroObject.getStatusId() + " written to Cassandra");
            tc.emit(new Values(dataItem));
            log.info("Result Item was emmited for " + status.getId());
        } else {
            log.warn("No twitter status found in: " + jsonText);
        }
    } catch (Exception ex) {
        log.error("Exception was raised: " + ex);
    }
}

From source file:com.raythos.sentilexo.twitter.domain.QueryResultItemMapper.java

License:Apache License

public static Map getFieldsMapFromStatus(String queryOwner, String queryName, String queryString,
        Status status) {

    if (queryName != null)
        queryName = queryName.toLowerCase();
    if (queryOwner != null)
        queryOwner = queryOwner.toLowerCase();
    Map m = StatusArraysHelper.getUserMentionMap(status);
    Map newMap = new HashMap();
    for (Object key : m.keySet()) {
        newMap.put(key.toString(), (Long) m.get(key));
    }//from   ww w.ja v  a2 s  .co  m
    Double longitude = null;
    Double lattitude = null;
    if (status.getGeoLocation() != null) {
        longitude = status.getGeoLocation().getLongitude();
        lattitude = status.getGeoLocation().getLatitude();
    }
    String place = null;
    if (status.getPlace() != null) {
        place = status.getPlace().getFullName();
    }

    boolean isRetweet = status.getRetweetedStatus() != null;
    Long retweetedId = null;
    String retweetedText = null;
    if (isRetweet) {
        retweetedId = status.getRetweetedStatus().getId();
        retweetedText = status.getRetweetedStatus().getText();
    }

    Map<String, Object> result = new HashMap<>();
    result.put(QueryResultItemFieldNames.STATUS_ID, status.getId());
    result.put(QueryResultItemFieldNames.CREATED_AT, status.getCreatedAt());
    result.put(QueryResultItemFieldNames.CURRENT_USER_RETWEET_ID, status.getCurrentUserRetweetId());
    result.put(QueryResultItemFieldNames.FAVOURITE_COUNT, status.getFavoriteCount());
    result.put(QueryResultItemFieldNames.FAVOURITED, status.isFavorited());
    result.put(QueryResultItemFieldNames.HASHTAGS, StatusArraysHelper.getHashTagsList(status));
    result.put(QueryResultItemFieldNames.IN_REPLY_TO_SCREEN_NAME, (status.getInReplyToScreenName()));
    result.put(QueryResultItemFieldNames.IN_REPLY_TO_STATUS_ID, status.getInReplyToStatusId());
    result.put(QueryResultItemFieldNames.IN_REPLY_TO_USER_ID, status.getInReplyToUserId());
    result.put(QueryResultItemFieldNames.LATITUDE, lattitude);
    result.put(QueryResultItemFieldNames.LONGITUDE, longitude);
    result.put(QueryResultItemFieldNames.MENTIONS, newMap);
    result.put(QueryResultItemFieldNames.LANGUAGE, status.getLang());
    result.put(QueryResultItemFieldNames.PLACE, place);
    result.put(QueryResultItemFieldNames.POSSIBLY_SENSITIVE, status.isPossiblySensitive());
    result.put(QueryResultItemFieldNames.QUERY_NAME, queryName);
    result.put(QueryResultItemFieldNames.QUERY_OWNER, queryOwner);
    result.put(QueryResultItemFieldNames.QUERY, queryString);
    result.put(QueryResultItemFieldNames.RELEVANT_QUERY_TERMS,
            TwitterUtils.relevantQueryTermsFromStatus(queryString, status));
    result.put(QueryResultItemFieldNames.RETWEET, isRetweet);
    result.put(QueryResultItemFieldNames.RETWEET_COUNT, status.getRetweetCount());
    result.put(QueryResultItemFieldNames.RETWEETED, status.isRetweeted());
    result.put(QueryResultItemFieldNames.RETWEETED_BY_ME, status.isRetweetedByMe());
    result.put(QueryResultItemFieldNames.RETWEET_STATUS_ID, retweetedId);
    result.put(QueryResultItemFieldNames.RETWEETED_TEXT, retweetedText);
    result.put(QueryResultItemFieldNames.SCOPES, StatusArraysHelper.getScopesList(status));
    result.put(QueryResultItemFieldNames.SCREEN_NAME, status.getUser().getScreenName());
    result.put(QueryResultItemFieldNames.SOURCE, (status.getSource()));
    result.put(QueryResultItemFieldNames.TEXT, (status.getText()));
    result.put(QueryResultItemFieldNames.TRUNCATED, status.isTruncated());
    result.put(QueryResultItemFieldNames.URLS, StatusArraysHelper.getUrlsList(status));
    result.put(QueryResultItemFieldNames.USER_ID, status.getUser().getId());
    result.put(QueryResultItemFieldNames.USER_NAME, (status.getUser().getName()));
    result.put(QueryResultItemFieldNames.USER_DESCRIPTION, (status.getUser().getDescription()));
    result.put(QueryResultItemFieldNames.USER_LOCATION, (status.getUser().getLocation()));
    result.put(QueryResultItemFieldNames.USER_URL, (status.getUser().getURL()));
    result.put(QueryResultItemFieldNames.USER_IS_PROTECTED, status.getUser().isProtected());
    result.put(QueryResultItemFieldNames.USER_FOLLOWERS_COUNT, status.getUser().getFollowersCount());
    result.put(QueryResultItemFieldNames.USER_CREATED_AT, status.getUser().getCreatedAt());
    result.put(QueryResultItemFieldNames.USER_FRIENDS_COUNT, status.getUser().getFriendsCount());
    result.put(QueryResultItemFieldNames.USER_LISTED_COUNT, status.getUser().getListedCount());
    result.put(QueryResultItemFieldNames.USER_STATUSES_COUNT, status.getUser().getStatusesCount());
    result.put(QueryResultItemFieldNames.USER_FAVOURITES_COUNT, status.getUser().getFavouritesCount());
    return result;
}