List of usage examples for twitter4j Query Query
public Query(String query)
From source file:org.apache.camel.component.twitter.consumer.search.SearchConsumer.java
License:Apache License
public List<Status> directConsume() throws TwitterException { String keywords = te.getProperties().getKeywords(); if (keywords == null || keywords.trim().length() == 0) { return Collections.emptyList(); }//from w ww. j ava2 s. com Query query = new Query(keywords); LOG.debug("Searching twitter with keywords: {}", keywords); return search(query); }
From source file:org.apache.camel.component.twitter.producer.SearchProducer.java
License:Apache License
@Override public void process(Exchange exchange) throws Exception { long myLastId = lastId; // KEYWORDS// w ww. jav a 2 s. c o m // keywords from header take precedence String keywords = exchange.getIn().getHeader(TwitterConstants.TWITTER_KEYWORDS, String.class); if (keywords == null) { keywords = te.getProperties().getKeywords(); } if (keywords == null) { throw new CamelExchangeException("No keywords to use for query", exchange); } Query query = new Query(keywords); // filter of older tweets if (te.getProperties().isFilterOld() && myLastId != 0) { query.setSinceId(myLastId); } // language String lang = exchange.getIn().getHeader(TwitterConstants.TWITTER_SEARCH_LANGUAGE, String.class); if (lang == null) { lang = te.getProperties().getLang(); } if (ObjectHelper.isNotEmpty(lang)) { query.setLang(lang); } // number of elemnt per page Integer count = exchange.getIn().getHeader(TwitterConstants.TWITTER_COUNT, Integer.class); if (count == null) { count = te.getProperties().getCount(); } if (ObjectHelper.isNotEmpty(count)) { query.setCount(count); } // number of pages Integer numberOfPages = exchange.getIn().getHeader(TwitterConstants.TWITTER_NUMBER_OF_PAGES, Integer.class); if (numberOfPages == null) { numberOfPages = te.getProperties().getNumberOfPages(); } Twitter twitter = te.getProperties().getTwitter(); log.debug("Searching twitter with keywords: {}", keywords); QueryResult results = twitter.search(query); List<Status> list = results.getTweets(); for (int i = 1; i < numberOfPages; i++) { if (!results.hasNext()) { break; } log.debug("Fetching page"); results = twitter.search(results.nextQuery()); list.addAll(results.getTweets()); } if (te.getProperties().isFilterOld()) { for (Status t : list) { long newId = t.getId(); if (newId > myLastId) { myLastId = newId; } } } exchange.getIn().setBody(list); // update the lastId after finished the processing if (myLastId > lastId) { lastId = myLastId; } }
From source file:org.apache.solr.handler.dataimport.TwitterEntityProcessor.java
License:Apache License
public void init(Context context) { super.init(context); // get parameters String consumerKey = getStringFromContext("consumerKey", null); String consumerSecret = getStringFromContext("consumerSecret", null); String accessToken = getStringFromContext("accessToken", null); String accessTokenSecret = getStringFromContext("accessTokenSecret", null); // connect to twitter ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setOAuthConsumerKey(consumerKey); cb.setOAuthConsumerSecret(consumerSecret); cb.setOAuthAccessToken(accessToken); cb.setOAuthAccessTokenSecret(accessTokenSecret); twitter = new TwitterFactory(cb.build()).getInstance(); // initiate the search String criteria = getStringFromContext("criteria", null); since = context.replaceTokens("${dih.last_index_time}"); query = new Query(criteria); //query.setSince(since.substring(0, since.indexOf(" ")).trim()); query.setSince("2015-02-12"); index = 0;//from w w w . j a v a 2 s.c om logConfig(); }
From source file:org.bireme.interop.Twitter2Couch.java
License:Open Source License
public static void main(final String[] args) throws Exception { final int len = args.length; if (len < 3) { usage();/*from w w w . j ava 2s . co m*/ } final String idQuery = args[0]; final String couchHost = args[1]; final String couchDbName = args[2]; final String twitterUserId = idQuery.startsWith("--twitteruserid=") ? idQuery.substring(16) : null; final Query twitterQuery = idQuery.startsWith("--twitterquery=") ? new Query(idQuery.substring(15)) : null; int twitterTotal = TweetLoader.MAX_PAGE_SIZE; Date twitterLowerDate = null; String couchPort = Integer.toString(Json2Couch.DEFAULT_COUCH_PORT); String couchUser = null; String couchPswd = null; boolean useRetweets = false; boolean append = false; int tell = Integer.MAX_VALUE; if ((twitterUserId == null) && (twitterQuery == null)) { usage(); } if ((couchHost == null) && (couchDbName == null)) { usage(); } for (int idx = 3; idx < len; idx++) { if (args[idx].startsWith("--twittertotal=")) { twitterTotal = Integer.parseInt(args[idx].substring(15)); } else if (args[idx].startsWith("--twitterlowerdate=")) { twitterLowerDate = new Date(args[idx].substring(19)); } else if (args[idx].startsWith("--couchport=")) { couchPort = args[idx].substring(12); } else if (args[idx].startsWith("--couchuser=")) { couchUser = args[idx].substring(12); } else if (args[idx].startsWith("--couchpswd=")) { couchPswd = args[idx].substring(12); } else if (args[idx].startsWith("--tell=")) { tell = Integer.parseInt(args[idx].substring(7)); } else if (args[idx].equals("--useretweets")) { useRetweets = true; } else if (args[idx].equals("--append")) { append = true; } else { usage(); } } final Twitter2Json t2j = new Twitter2Json(twitterUserId, twitterQuery, twitterTotal, twitterLowerDate, useRetweets); final Json2Couch j2c = new Json2Couch(couchHost, couchPort, couchUser, couchPswd, couchDbName, append); new Twitter2Couch(t2j, j2c).export(tell); }
From source file:org.bireme.interop.Twitter2Isis.java
License:Open Source License
public static void main(final String[] args) throws Exception { final int len = args.length; if (len < 2) { usage();/*from w ww. j av a2 s . co m*/ } final String idQuery = args[0]; final String mstName = args[1]; final boolean ffi = true; final String twitterUserId = idQuery.startsWith("--twitteruserid=") ? idQuery.substring(16) : null; final String twitterQuery = idQuery.startsWith("--twitterquery=") ? idQuery.substring(15) : null; int twitterTotal = TweetLoader.MAX_PAGE_SIZE; Date twitterLowerDate = null; String encoding = Master.DEFAULT_ENCODING; Map<String, Integer> convTable = null; boolean useRetweets = false; boolean append = false; int tell = Integer.MAX_VALUE; if ((twitterUserId == null) && (twitterQuery == null)) { usage(); } for (int idx = 2; idx < len; idx++) { if (args[idx].startsWith("--twittertotal=")) { twitterTotal = Integer.parseInt(args[idx].substring(15)); } else if (args[idx].startsWith("--twitterlowerdate=")) { twitterLowerDate = new Date(args[idx].substring(19)); } else if (args[idx].startsWith("--isisencoding=")) { encoding = args[idx].substring(15); } else if (args[idx].startsWith("--convTable=")) { convTable = getConvTable(args[idx].substring(12)); } else if (args[idx].startsWith("--tell=")) { tell = Integer.parseInt(args[idx].substring(7)); } else if (args[idx].equals("--useretweets")) { useRetweets = true; } else if (args[idx].equals("--append")) { append = true; } else { usage(); } } final Twitter2Json t2j = new Twitter2Json(twitterUserId, new Query(twitterQuery), twitterTotal, twitterLowerDate, useRetweets); final Json2Isis j2i = new Json2Isis(mstName, convTable, encoding, null, ffi, append); new Twitter2Isis(t2j, j2i).export(tell); }
From source file:org.bireme.interop.Twitter2Lucene.java
License:Open Source License
public static void main(final String[] args) throws Exception { final int len = args.length; if (len < 2) { usage();/*from w w w. ja v a 2 s . c om*/ } final String idQuery = args[0]; final String luceneDir = args[1]; final String twitterUserId = idQuery.startsWith("--twitteruserid=") ? idQuery.substring(16) : null; final String twitterQuery = idQuery.startsWith("--twitterquery=") ? idQuery.substring(15) : null; int twitterTotal = TweetLoader.MAX_PAGE_SIZE; Date twitterLowerDate = null; boolean useRetweets = false; boolean store = false; boolean append = false; int tell = Integer.MAX_VALUE; if ((twitterUserId == null) && (twitterQuery == null)) { usage(); } for (int idx = 2; idx < len; idx++) { if (args[idx].startsWith("--twittertotal=")) { twitterTotal = Integer.parseInt(args[idx].substring(15)); } else if (args[idx].startsWith("--twitterlowerdate=")) { twitterLowerDate = new Date(args[idx].substring(19)); } else if (args[idx].equals("--store")) { store = true; } else if (args[idx].startsWith("--tell=")) { tell = Integer.parseInt(args[idx].substring(7)); } else if (args[idx].equals("--useretweets")) { useRetweets = true; } else if (args[idx].equals("--append")) { append = true; } else { usage(); } } final Twitter2Json t2j = new Twitter2Json(twitterUserId, new Query(twitterQuery), twitterTotal, twitterLowerDate, useRetweets); final Json2Lucene j2l = new Json2Lucene(luceneDir, store, append); new Twitter2Lucene(t2j, j2l).export(tell); }
From source file:org.bireme.interop.Twitter2Mongo.java
License:Open Source License
public static void main(final String[] args) throws Exception { final int len = args.length; if (len < 4) { usage();//from w ww. jav a 2 s .c o m } final String idQuery = args[0]; final String mongoHost = args[1]; final String mongoDbName = args[2]; final String mongoColName = args[3]; final String twitterUserId = idQuery.startsWith("--twitteruserid=") ? idQuery.substring(16) : null; final String twitterQuery = idQuery.startsWith("--twitterquery=") ? idQuery.substring(15) : null; int twitterTotal = TweetLoader.MAX_PAGE_SIZE; Date twitterLowerDate = null; String mongoPort = Integer.toString(Json2Mongo.DEFAULT_MONGO_PORT); String mongoUser = null; String mongoPswd = null; boolean useRetweets = false; boolean append = false; int tell = Integer.MAX_VALUE; if ((twitterUserId == null) && (twitterQuery == null)) { usage(); } for (int idx = 4; idx < len; idx++) { if (args[idx].startsWith("--twittertotal=")) { twitterTotal = Integer.parseInt(args[idx].substring(15)); } else if (args[idx].startsWith("--twitterlowerdate=")) { twitterLowerDate = new Date(args[idx].substring(19)); } else if (args[idx].startsWith("--mongoport=")) { mongoPort = args[idx].substring(12); } else if (args[idx].startsWith("--mongouser=")) { mongoUser = args[idx].substring(12); } else if (args[idx].startsWith("--mongopsw=")) { mongoPswd = args[idx].substring(12); } else if (args[idx].startsWith("--tell=")) { tell = Integer.parseInt(args[idx].substring(7)); } else if (args[idx].equals("--useretweets")) { useRetweets = true; } else if (args[idx].equals("--append")) { append = true; } else { usage(); } } final Twitter2Json t2j = new Twitter2Json(twitterUserId, new Query(twitterQuery), twitterTotal, twitterLowerDate, useRetweets); final Json2Mongo j2m = new Json2Mongo(mongoHost, mongoPort, mongoUser, mongoPswd, mongoDbName, mongoColName, append); new Twitter2Mongo(t2j, j2m).export(tell); }
From source file:org.botlibre.sense.twitter.Twitter.java
License:Open Source License
/** * Learn responses from the tweet search. *///w w w . jav a 2s . c om 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./* ww w . ja va 2s . 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); } }
From source file:org.botlibre.sense.twitter.Twitter.java
License:Open Source License
/** * Check search keywords./*w w w .java 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); } }