List of usage examples for twitter4j Status getText
String getText();
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()); }// w w w .ja va 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./* www .j av a2s . c om*/ */ 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 ww w . j a va 2 s . co 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
public void learnTweet(Status tweet, boolean processTweets, boolean processReplies, Network memory) throws Exception { String text = tweet.getText(); // Exclude retweets if (tweet.isRetweet()) { log("Tweet is retweet", Level.FINER, tweet.getText()); return;//from w ww. ja va2 s . c o m } if (Utils.checkProfanity(text)) { log("Ignoring profanity", Level.INFO, text); return; } // Exclude protected if (tweet.getUser().isProtected() && !tweet.getUser().getScreenName().equals(getUserName())) { log("Tweet is protected", Level.FINER, tweet.getText()); return; } log("Learning status", Level.INFO, text); // Exclude replies/mentions if (tweet.getText().indexOf('@') != -1) { log("Tweet is reply", Level.FINER, tweet.getText()); if (!processReplies) { return; } long id = tweet.getInReplyToStatusId(); if (id > 0) { try { Status reply = getConnection().showStatus(id); String replyText = reply.getText(); if (replyText != null && !replyText.isEmpty()) { // Filter out @users for (String word : new TextStream(text).allWords()) { if (word.startsWith("@")) { text = text.replace(word, ""); } } for (String word : new TextStream(replyText).allWords()) { if (word.startsWith("@")) { replyText = replyText.replace(word, ""); } } Vertex question = memory.createSentence(replyText.trim()); Vertex sentence = memory.createSentence(text.trim()); Language.addResponse(question, sentence, memory); } } catch (Exception ignore) { log(ignore.toString(), Level.WARNING); } } return; } if (!processTweets) { return; } Vertex sentence = memory.createSentence(text); String keywords = ""; for (String word : new TextStream(text).allWords()) { if (word.startsWith("#")) { keywords = keywords + " " + word + " " + word.substring(1, word.length()); } } Language.addResponse(sentence, sentence, null, keywords, null, memory); }
From source file:org.botlibre.sense.twitter.Twitter.java
License:Open Source License
/** * Check messages to this user.//from w w w . j a v a2s . 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./*from ww w. j a v a 2 s . c om*/ */ 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); } }
From source file:org.botlibre.sense.twitter.Twitter.java
License:Open Source License
/** * Check search keywords./* www.j av a 2 s. co m*/ */ public void checkAutoFollowSearch(int friendCount) { if (getAutoFollowSearch().isEmpty()) { return; } log("Processing autofollow search", Level.FINE, getAutoFollowSearch()); try { Network memory = getBot().memory().newMemory(); Vertex twitter = memory.createVertex(getPrimitive()); Vertex vertex = twitter.getRelationship(Primitive.LASTAUTOFOLLOWSEARCH); long last = 0; long max = 0; int count = 0; if (vertex != null) { last = ((Number) vertex.getData()).longValue(); } for (String followSearch : getAutoFollowSearch()) { Query query = new Query(followSearch); if (vertex != null) { query.setSinceId(last); } SearchResource search = getConnection().search(); QueryResult result = search.search(query); List<Status> tweets = result.getTweets(); if (tweets != null) { for (Status tweet : tweets) { if (count > this.maxSearch) { break; } if (tweet.getId() > last) { log("Autofollow search", Level.FINE, tweet.getText(), tweet.getUser().getScreenName(), followSearch); if (checkFriendship(tweet.getUser().getId(), false)) { friendCount++; if (friendCount >= getMaxFriends()) { log("Max friend limit", Level.FINE, getMaxFriends()); return; } } count++; if (tweet.getId() > max) { max = tweet.getId(); } } } } if (count > this.maxSearch) { break; } } if (max != 0) { twitter.setRelationship(Primitive.LASTAUTOFOLLOWSEARCH, memory.createVertex(max)); memory.save(); } } catch (Exception exception) { log(exception); } }
From source file:org.botlibre.sense.twitter.Twitter.java
License:Open Source License
/** * Return the time-line./*from w w w .j a v a2s . c o m*/ */ public List<String> getTimeline() { List<String> timeline = new ArrayList<String>(); try { ResponseList<Status> statuses = getConnection().getHomeTimeline(); for (Status status : statuses) { timeline.add(status.getCreatedAt() + " - <b>" + status.getUser().getScreenName() + "</b>: " + status.getText()); } } catch (Exception exception) { log(exception); throw new BotException(exception); } return timeline; }
From source file:org.botlibre.sense.twitter.Twitter.java
License:Open Source License
/** * Retweet the tweet./* ww w. ja v a 2 s . co m*/ */ public void retweet(Status tweet) { if (tweet.isRetweet()) { tweet = tweet.getRetweetedStatus(); } if (tweet.getUser().isProtected()) { log("Cannot retweet protected user", Level.INFO, tweet.getUser().getScreenName(), tweet.getText()); return; } this.retweets++; log("Retweeting:", Level.INFO, tweet.getText(), tweet.getUser().getScreenName()); try { if (getConnection() == null) { connect(); } getConnection().retweetStatus(tweet.getId()); } catch (Exception exception) { if (exception.getMessage() != null && exception.getMessage().contains("authorized") && exception.getMessage().contains("endpoint")) { this.errors = this.errors + 5; } this.errors++; log(exception.toString(), Level.WARNING, tweet.getText()); } }
From source file:org.botlibre.sense.twitter.Twitter.java
License:Open Source License
/** * Process the email message.//from w ww . j a v a 2 s.c om */ @Override public void input(Object input, Network network) { if (!isEnabled()) { return; } try { if (input instanceof Status) { Status tweet = (Status) input; log("Processing status", Bot.FINE, tweet.getText(), tweet.getId()); if ((System.currentTimeMillis() - tweet.getCreatedAt().getTime()) > DAY) { log("Day old status", Bot.FINE, tweet.getId(), tweet.getCreatedAt().getTime()); return; } if (this.processedTweets.contains(tweet.getId())) { log("Already processed status", Bot.FINE, tweet.getText(), tweet.getId()); return; } this.processedTweets.add(tweet.getId()); String name = tweet.getUser().getScreenName(); String replyTo = tweet.getInReplyToScreenName(); String text = tweet.getText().trim(); TextStream stream = new TextStream(text); String firstWord = null; if (getIgnoreReplies()) { if (stream.peek() == '@') { stream.next(); String replyTo2 = stream.nextWord(); firstWord = stream.peekWord(); text = stream.upToEnd().trim(); if (!replyTo2.equals(replyTo)) { log("Reply to does not match:", Bot.FINE, replyTo2, replyTo); } replyTo = replyTo2; if (replyTo.equals(this.userName) && getFollowMessages()) { if ("follow".equals(firstWord)) { log("Adding friend", Level.INFO, tweet.getUser().getScreenName()); getConnection().createFriendship(tweet.getUser().getId()); } else if ("unfollow".equals(firstWord)) { log("Removing friend", Level.INFO, tweet.getUser().getScreenName()); getConnection().destroyFriendship(tweet.getUser().getId()); } } } } else { // Ignore the reply user, force the bot to reply. replyTo = null; } if (!tweet.isRetweet() && !tweet.getUser().isProtected()) { stream.reset(); List<String> words = stream.allWords(); for (String keywords : getRetweet()) { List<String> keyWords = new TextStream(keywords).allWords(); if (!keyWords.isEmpty()) { if (words.containsAll(keyWords)) { retweet(tweet); break; } } } } log("Input status", Level.FINE, tweet.getText(), name, replyTo); this.tweetsProcessed++; inputSentence(text, name, replyTo, tweet, network); } } catch (Exception exception) { log(exception); } }