List of usage examples for twitter4j.api SearchResource search
QueryResult search(Query query) throws TwitterException;
From source file:org.botlibre.sense.twitter.Twitter.java
License:Open Source License
/** * Learn responses from the tweet search. */// w w w .j a va2 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
/** * Check search keywords./*www. ja v a2 s . co 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); } }
From source file:org.botlibre.sense.twitter.Twitter.java
License:Open Source License
/** * Check search keywords./*from ww w . ja v a 2 s .c o 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); } }