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:org.apache.camel.component.twitter.producer.UserProducer.java

License:Apache License

private Status updateStatus(String status) throws Exception {
    if (status.length() > 160) {
        log.warn("Message is longer than 160 characters. Message will be truncated!");
        status = status.substring(0, 160);
    }/*  www  .  j ava  2 s .  co m*/

    Status response = te.getProperties().getTwitter().updateStatus(status);
    log.debug("Updated status: {}", status);
    log.debug("Status id: {}", response.getId());
    return response;
}

From source file:org.apache.flume.sink.solr.morphline.TwitterSource.java

License:Apache License

private Record extractRecord(String idPrefix, Schema avroSchema, Status status) {
    User user = status.getUser();/*w  w w .j av a2s.  c o m*/
    Record doc = new Record(avroSchema);

    doc.put("id", idPrefix + status.getId());
    doc.put("created_at", formatterTo.format(status.getCreatedAt()));
    doc.put("retweet_count", status.getRetweetCount());
    doc.put("retweeted", status.isRetweet());
    doc.put("in_reply_to_user_id", status.getInReplyToUserId());
    doc.put("in_reply_to_status_id", status.getInReplyToStatusId());

    addString(doc, "source", status.getSource());
    addString(doc, "text", status.getText());

    MediaEntity[] mediaEntities = status.getMediaEntities();
    if (mediaEntities.length > 0) {
        addString(doc, "media_url_https", mediaEntities[0].getMediaURLHttps());
        addString(doc, "expanded_url", mediaEntities[0].getExpandedURL());
    }

    doc.put("user_friends_count", user.getFriendsCount());
    doc.put("user_statuses_count", user.getStatusesCount());
    doc.put("user_followers_count", user.getFollowersCount());
    addString(doc, "user_location", user.getLocation());
    addString(doc, "user_description", user.getDescription());
    addString(doc, "user_screen_name", user.getScreenName());
    addString(doc, "user_name", user.getName());
    return doc;
}

From source file:org.apache.solr.handler.dataimport.TwitterEntityProcessor.java

License:Apache License

@Override
public Map<String, Object> nextRow() {

    Map<String, Object> row = new HashMap<>();

    if (twitter == null || query == null)
        return null;

    try {/* w  ww.j a  v a 2s  . c o  m*/
        if (results == null) {
            results = twitter.search(query);
            if (results == null || results.getCount() == 0)
                return null;
        }
        if (tweets == null)
            tweets = (ArrayList<Status>) results.getTweets();

        Status tweet = null;
        if (index < tweets.size()) {
            tweet = (Status) tweets.get(index++);
        } else {
            query = results.nextQuery();
            if (query != null) {
                results = twitter.search(query);
                if (results == null || results.getCount() == 0)
                    return null;
                tweets = (ArrayList<Status>) results.getTweets();
                index = 0;
                tweet = (Status) tweets.get(index++);
            }
        }
        if (tweet == null)
            return null;

        // id
        row.put(MESSAGE_ID, tweet.getId());

        // lang
        row.put(MESSAGE_LANG, tweet.getLang());

        // user
        User user = tweet.getUser();

        // name
        row.put(MESSAGE_USER, user.getName());

        // pseudo
        row.put(MESSAGE_PSEUDO, tweet.getUser().getScreenName());

        // text
        row.put(MESSAGE_TEXT, tweet.getText());

        // date
        Date date = tweet.getCreatedAt();
        row.put(MESSAGE_DATE, date.toString());

    } catch (TwitterException e) {
        e.printStackTrace();
        return null;
    }

    return row;
}

From source file:org.bireme.interop.toJson.TweetLoader.java

License:Open Source License

public Status getNextStatus() throws TwitterException {
    final Status status;

    if ((retweetIterator != null) && (retweetIterator.hasNext())) {
        status = retweetIterator.next();
    } else {/*from w  w  w . j a v  a 2  s. co m*/
        if (tweetIterator == null) {
            getNextIterator();
        }
        if (tweetIterator == null) {
            status = null;
        } else if (++curTweet > total) {
            status = null;
        } else if (tweetIterator.hasNext()) {
            status = tweetIterator.next();
            if (useRetweets) {
                final ResponseList<Status> retweets = twitter.getRetweets(status.getId());
                if (retweets != null) {
                    retweetIterator = retweets.iterator();
                }
            }
        } else {
            tweetIterator = null;
            retweetIterator = null;
            curTweet--;
            status = getNextStatus();
        }
    }
    if (status != null) {
        final long id = status.getId();
        if (id < max_id) {
            max_id = id - 1;
        }
    }
    return status;
}

From source file:org.bireme.interop.toJson.Twitter2Json.java

License:Open Source License

private JSONObject getDocument(final Status status) {
    assert status != null;

    final JSONObject obj = new JSONObject();
    final GeoLocation geo = status.getGeoLocation();
    final Place place = status.getPlace();
    final User user = status.getUser();

    obj.put("createdAt", status.getCreatedAt()).put("id", status.getId()).put("lang", status.getLang());
    if (geo != null) {
        obj.put("location_latitude", geo.getLatitude()).put("location_longitude", geo.getLongitude());
    }/*from   www . j  a  v a  2  s .c  om*/
    if (place != null) {
        obj.put("place_country", place.getCountry()).put("place_fullName", place.getFullName())
                .put("place_id", place.getId()).put("place_name", place.getName())
                .put("place_type", place.getPlaceType()).put("place_streetAddress", place.getStreetAddress())
                .put("place_url", place.getURL());
    }
    obj.put("source", status.getSource()).put("text", status.getText());
    if (user != null) {
        obj.put("user_description", user.getDescription()).put("user_id", user.getId())
                .put("user_lang", user.getLang()).put("user_location", user.getLocation())
                .put("user_name", user.getName()).put("user_url", user.getURL());
    }
    obj.put("isTruncated", status.isTruncated()).put("isRetweet", status.isRetweet());

    return obj;
}

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

License:Open Source License

/**
 * Check status.//from   ww  w  . j  a  v a  2s .  c  o  m
 */
public void checkStatus() {
    if (!getProcessStatus()) {
        return;
    }
    log("Checking status", Level.FINE);
    try {
        Network memory = getBot().memory().newMemory();
        Vertex twitter = memory.createVertex(getPrimitive());
        Vertex vertex = twitter.getRelationship(Primitive.LASTTIMELINE);
        long last = 0;
        if (vertex != null) {
            last = ((Number) vertex.getData()).longValue();
        }
        long max = 0;
        ResponseList<Status> timeline = null;
        boolean more = true;
        int page = 1;
        int count = 0;
        this.errors = 0;
        while (more && (count <= this.maxStatus) && page <= this.maxPage) {
            if (last == 0) {
                timeline = getConnection().getHomeTimeline();
                more = false;
            } else {
                Paging paging = new Paging(page, last);
                timeline = getConnection().getHomeTimeline(paging);
                if ((timeline == null) || (timeline.size() < 20)) {
                    more = false;
                }
                page++;
            }
            if ((timeline == null) || timeline.isEmpty()) {
                break;
            }
            log("Processing status", Level.INFO, timeline.size());
            for (int index = timeline.size() - 1; index >= 0; index--) {
                if (count >= this.maxStatus) {
                    break;
                }
                if (this.errors > this.maxErrors) {
                    break;
                }
                Status status = timeline.get(index);
                String name = status.getUser().getScreenName();
                if (!name.equals(this.userName)) {
                    long statusTime = status.getCreatedAt().getTime();
                    long statusId = status.getId();
                    if (statusId > max) {
                        max = statusId;
                    }
                    if ((System.currentTimeMillis() - statusTime) > DAY) {
                        log("Day old status", Level.INFO, statusId, statusTime);
                        more = false;
                        continue;
                    }
                    if (statusId > last) {
                        if (Utils.checkProfanity(status.getText())) {
                            continue;
                        }
                        boolean match = false;
                        List<String> statusWords = new TextStream(status.getText().toLowerCase()).allWords();
                        if (getListenStatus()) {
                            this.languageState = LanguageState.Listening;
                            match = true;
                        } else {
                            for (String text : getStatusKeywords()) {
                                List<String> keywords = new TextStream(text.toLowerCase()).allWords();
                                if (!keywords.isEmpty() && statusWords.containsAll(keywords)) {
                                    match = true;
                                    break;
                                }
                            }
                        }
                        if (getLearn()) {
                            learnTweet(status, true, true, memory);
                        }
                        if (match) {
                            count++;
                            input(status);
                            Utils.sleep(500);
                        } else {
                            log("Skipping status, missing keywords", Level.FINE, status.getText());
                            if (!status.isRetweet() && !status.getUser().isProtected()
                                    && !status.isRetweetedByMe()) {
                                boolean retweeted = false;
                                // Check retweet.
                                for (String keywords : getRetweet()) {
                                    List<String> keyWords = new TextStream(keywords.toLowerCase()).allWords();
                                    if (!keyWords.isEmpty()) {
                                        if (statusWords.containsAll(keyWords)) {
                                            retweeted = true;
                                            count++;
                                            retweet(status);
                                            Utils.sleep(500);
                                            break;
                                        }
                                    }
                                }
                                if (!retweeted) {
                                    log("Skipping rewteet, missing keywords", Level.FINE, status.getText());
                                }
                            } else if (!getRetweet().isEmpty()) {
                                if (status.isRetweet()) {
                                    log("Skipping rewteet", Level.FINE, status.getText());
                                } else if (status.getUser().isProtected()) {
                                    log("Skipping protected user", Level.FINE, status.getText());
                                } else if (status.isRetweetedByMe()) {
                                    log("Skipping already retweeted", Level.FINE, status.getText());
                                }
                            }
                        }
                    } else {
                        log("Old status", Level.INFO, statusId, statusTime);
                    }
                }
            }
        }
        if (max != 0) {
            twitter.setRelationship(Primitive.LASTTIMELINE, memory.createVertex(max));
            memory.save();
        }
    } catch (Exception exception) {
        log(exception);
    }
    // Wait for language processing.
    int count = 0;
    while (count < 60 && !getBot().memory().getActiveMemory().isEmpty()) {
        Utils.sleep(1000);
    }
}

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

License:Open Source License

/**
 * Learn responses from the tweet search.
 *//*from   www  . j  ava  2 s  .c o  m*/
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);
    }
}

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

License:Open Source License

/**
 * Learn from the profiles posts./*from w w w .ja  v  a2 s  .  com*/
 */
public void checkLearning() {
    if (!getLearnFromSelf()) {
        return;
    }
    log("Checking learning", Level.FINE);
    try {
        Network memory = getBot().memory().newMemory();
        Vertex twitter = memory.createVertex(getPrimitive());
        Vertex vertex = twitter.getRelationship(Primitive.LASTLEARN);
        long last = 0;
        if (vertex != null) {
            last = ((Number) vertex.getData()).longValue();
        }
        long max = 0;
        ResponseList<Status> timeline = getConnection().getUserTimeline();
        if ((timeline == null) || timeline.isEmpty()) {
            return;
        }
        log("Processing status", Level.INFO, timeline.size());
        for (int index = timeline.size() - 1; index >= 0; index--) {
            Status tweet = timeline.get(index);
            long statusTime = tweet.getCreatedAt().getTime();
            long statusId = tweet.getId();
            if (statusId > max) {
                max = statusId;
            }
            if ((System.currentTimeMillis() - statusTime) > DAY) {
                log("Day old status", Level.INFO, statusId, statusTime);
                continue;
            }
            if (statusId > last) {
                learnTweet(tweet, true, true, memory);
            } else {
                log("Old status", Level.INFO, statusId, statusTime);
            }
        }
        if (max != 0) {
            twitter.setRelationship(Primitive.LASTTIMELINE, memory.createVertex(max));
            memory.save();
        }
    } catch (Exception exception) {
        log(exception);
    }
}

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

License:Open Source License

/**
 * Check messages to this user./*from   www  .  j  av  a 2  s.c o  m*/
 */
public void checkMentions() {
    if (!getReplyToMentions()) {
        return;
    }
    try {
        log("Checking mentions", Level.FINE);
        Network memory = getBot().memory().newMemory();
        Vertex twitter = memory.createVertex(getPrimitive());
        Vertex vertex = twitter.getRelationship(Primitive.LASTMENTION);
        long last = 0;
        if (vertex != null) {
            last = ((Number) vertex.getData()).longValue();
        }
        long max = 0;
        ResponseList<Status> mentions = null;
        boolean more = true;
        int page = 1;
        while (more) {
            if (last == 0) {
                mentions = getConnection().getMentionsTimeline();
                more = false;
            } else {
                Paging paging = new Paging(page, last);
                mentions = getConnection().getMentionsTimeline(paging);
                if ((mentions == null) || (mentions.size() < 20)) {
                    more = false;
                }
                page++;
            }
            if ((mentions == null) || mentions.isEmpty()) {
                break;
            }
            log("Processing mentions", Level.FINE, mentions.size());
            for (int index = mentions.size() - 1; index >= 0; index--) {
                Status tweet = mentions.get(index);
                long statusTime = tweet.getCreatedAt().getTime();
                long statusId = tweet.getId();
                if (statusId > max) {
                    max = statusId;
                }
                if ((System.currentTimeMillis() - statusTime) > DAY) {
                    log("Day old mention", Level.INFO, statusId, statusTime);
                    more = false;
                    continue;
                }
                // Exclude self
                if (tweet.getUser().getScreenName().equals(getUserName())) {
                    continue;
                }
                if (statusId > last) {
                    log("Processing mention", Level.INFO, tweet.getText(), tweet.getUser().getScreenName());
                    input(tweet);
                    Utils.sleep(100);
                } else {
                    log("Old mention", Level.INFO, statusId, statusTime);
                }
            }
        }
        if (max != 0) {
            twitter.setRelationship(Primitive.LASTMENTION, memory.createVertex(max));
            memory.save();
        }
    } catch (Exception exception) {
        log(exception);
    }
    // Wait for language processing.
    int count = 0;
    while (count < 60 && !getBot().memory().getActiveMemory().isEmpty()) {
        Utils.sleep(1000);
    }
    this.languageState = LanguageState.Discussion;
}

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

License:Open Source License

/**
 * Check search keywords.//w ww .java2s.c o m
 */
public void checkSearch() {
    if (getTweetSearch().isEmpty()) {
        return;
    }
    log("Processing search", Level.FINE, getTweetSearch());
    try {
        Network memory = getBot().memory().newMemory();
        Vertex twitter = memory.createVertex(getPrimitive());
        Vertex vertex = twitter.getRelationship(Primitive.LASTSEARCH);
        long last = 0;
        long max = 0;
        int count = 0;
        this.errors = 0;
        if (vertex != null) {
            last = ((Number) vertex.getData()).longValue();
        }
        Set<Long> processed = new HashSet<Long>();
        for (String tweetSearch : getTweetSearch()) {
            Query query = new Query(tweetSearch);
            if (vertex != null) {
                query.setSinceId(last);
            }
            SearchResource search = getConnection().search();
            QueryResult result = search.search(query);
            List<Status> tweets = result.getTweets();
            if (tweets != null) {
                log("Processing search results", Level.FINE, tweets.size(), tweetSearch);
                for (Status tweet : tweets) {
                    if (count > this.maxSearch) {
                        log("Max search results processed", Level.FINE, this.maxSearch);
                        break;
                    }
                    if (tweet.getId() > last && !processed.contains(tweet.getId())) {
                        if (tweet.getId() > max) {
                            max = tweet.getId();
                        }
                        boolean match = false;
                        // Exclude replies/mentions
                        if (getIgnoreReplies() && tweet.getText().indexOf('@') != -1) {
                            log("Ignoring: Tweet is reply", Level.FINER, tweet.getText());
                            continue;
                        }
                        // Exclude retweets
                        if (tweet.isRetweet()) {
                            log("Ignoring: Tweet is retweet", Level.FINER, tweet.getText());
                            continue;
                        }
                        // Exclude protected
                        if (tweet.getUser().isProtected()) {
                            log("Ignoring: Tweet is protected", Level.FINER, tweet.getText());
                            continue;
                        }
                        // Exclude self
                        if (tweet.getUser().getScreenName().equals(getUserName())) {
                            log("Ignoring: Tweet is from myself", Level.FINER, tweet.getText());
                            continue;
                        }
                        // Ignore profanity
                        if (Utils.checkProfanity(tweet.getText())) {
                            log("Ignoring: Tweet contains profanity", Level.FINER, tweet.getText());
                            continue;
                        }
                        List<String> statusWords = new TextStream(tweet.getText().toLowerCase()).allWords();
                        for (String text : getStatusKeywords()) {
                            List<String> keywords = new TextStream(text.toLowerCase()).allWords();
                            if (statusWords.containsAll(keywords)) {
                                match = true;
                                break;
                            }
                        }
                        if (getLearn()) {
                            learnTweet(tweet, true, true, memory);
                        }
                        if (match) {
                            processed.add(tweet.getId());
                            log("Processing search", Level.INFO, tweet.getUser().getScreenName(), tweetSearch,
                                    tweet.getText());
                            input(tweet);
                            Utils.sleep(500);
                            count++;
                        } else {
                            if (!tweet.isRetweetedByMe()) {
                                boolean found = false;
                                // Check retweet.
                                for (String keywords : getRetweet()) {
                                    List<String> keyWords = new TextStream(keywords).allWords();
                                    if (!keyWords.isEmpty()) {
                                        if (statusWords.containsAll(keyWords)) {
                                            found = true;
                                            processed.add(tweet.getId());
                                            count++;
                                            retweet(tweet);
                                            Utils.sleep(500);
                                            break;
                                        }
                                    }
                                }
                                if (!found) {
                                    log("Missing keywords", Level.FINER, tweet.getText());
                                }
                            } else {
                                log("Already retweeted", Level.FINER, tweet.getText());
                            }
                        }
                    }
                }
            }
            if (count > this.maxSearch) {
                break;
            }
            if (this.errors > this.maxErrors) {
                break;
            }
        }
        if (max != 0) {
            twitter.setRelationship(Primitive.LASTSEARCH, memory.createVertex(max));
            memory.save();
        }
    } catch (Exception exception) {
        log(exception);
    }
    // Wait for language processing.
    int count = 0;
    while (count < 60 && !getBot().memory().getActiveMemory().isEmpty()) {
        Utils.sleep(1000);
    }
}