List of usage examples for twitter4j Twitter search
QueryResult search(Query query) throws TwitterException;
From source file:crawling.FoundUsersBySearchGeo.java
License:Apache License
private static void doASearch(Twitter twitter, Query query) throws TwitterException { //try {//from w w w .j a v a2 s . c o m QueryResult result = twitter.search(query); List<Status> tweets = result.getTweets(); int thisCount = 0; for (Status tweet : tweets) { //System.out.println("@" + tweet.getFromUser() + tweet.getId() + " - " + tweet.getText()); // Check the tweet Long id = tweet.getUser().getId(); String screenName = tweet.getUser().getScreenName(); /*String location = ""; if (tweet.getUser().getLocation() != null){ location += tweet.getUser().getLocation(); if (tweet.getGeoLocation() != null){ location += "::" + tweet.getGeoLocation().toString(); }*/ String location = tweet.getUser().getLocation() + "::" + tweet.getGeoLocation(); // If the tweet is a retweet, the source of the tweet is from the target area if (tweet.isRetweet()) { id = tweet.getRetweetedStatus().getUser().getId(); screenName = tweet.getRetweetedStatus().getUser().getScreenName(); //location = tweet.getRetweetedStatus().getUser().getLocation(); location = tweet.getRetweetedStatus().getUser().getLocation() + "::" + tweet.getRetweetedStatus().getGeoLocation(); } if (discoveredUsers.containsKey(id)) { //System.out.println("Already found this user: " + id); long num = discoveredUsers.get(id); discoveredUsers.put(id, num + 1); } else { discoveredUsers.put(id, (long) 1); storeUserID(id, screenName, location); thisCount++; } } if (currentIndex < histCount) { avgUsers[currentIndex] = thisCount; currentIndex++; } else { currentIndex = 0; } /* Calculate the average #users in last 10 times */ double sum = 0; for (int num : avgUsers) sum += num; System.out.print( "\r" + count + ", the average number of users in last " + histCount + " is: " + sum / histCount); //} catch (TwitterException te) { // te.printStackTrace(); // System.out.println("Failed to search tweets: " + te.getMessage()); // System.exit(-1); //} }
From source file:crawling.FoundUsersBySearchHashtag.java
License:Apache License
private static void doASearch(Twitter twitter, Query query) throws TwitterException { //try {/* w w w . j a v a 2s. c o m*/ QueryResult result = twitter.search(query); List<Status> tweets = result.getTweets(); int thisCount = 0; for (Status tweet : tweets) { //System.out.println("@" + tweet.getFromUser() + tweet.getId() + " - " + tweet.getText()); // Check the tweet Long id = tweet.getUser().getId(); String screenName = tweet.getUser().getScreenName(); /*String location = ""; if (tweet.getUser().getLocation() != null){ location += tweet.getUser().getLocation(); if (tweet.getGeoLocation() != null){ location += "::" + tweet.getGeoLocation().toString(); }*/ String location = tweet.getUser().getLocation() + "::" + tweet.getGeoLocation(); // If the tweet is a retweet, the source of the tweet is from the target area /*if (tweet.isRetweet()){ id = tweet.getRetweetedStatus().getUser().getId(); screenName = tweet.getRetweetedStatus().getUser().getScreenName(); //location = tweet.getRetweetedStatus().getUser().getLocation(); location = tweet.getRetweetedStatus().getUser().getLocation() + "::" + tweet.getRetweetedStatus().getGeoLocation(); }*/ if (discoveredUsers.containsKey(id)) { //System.out.println("Already found this user: " + id); long num = discoveredUsers.get(id); discoveredUsers.put(id, num + 1); } else { discoveredUsers.put(id, (long) 1); storeUserID(id, screenName, location); thisCount++; } } if (currentIndex < histCount) { avgUsers[currentIndex] = thisCount; currentIndex++; } else { currentIndex = 0; } /* Calculate the average #users in last 10 times */ double sum = 0; for (int num : avgUsers) sum += num; System.out.print( "\r" + count + ", the average number of users in last " + histCount + " is: " + sum / histCount); //} catch (TwitterException te) { // te.printStackTrace(); // System.out.println("Failed to search tweets: " + te.getMessage()); // System.exit(-1); //} }
From source file:crawling.SearchTweetsHashtag.java
License:Apache License
private static void doASearch(Twitter twitter, Query query) throws TwitterException { //try {/*from w w w. ja v a 2 s.co m*/ QueryResult result = twitter.search(query); List<Status> tweets = result.getTweets(); int thisCount = 0; for (Status tweet : tweets) { // Check the tweet Long id = tweet.getId(); if (discoveredTweets.containsKey(id)) { //System.out.println("Already found this user: " + id); long num = discoveredTweets.get(id); discoveredTweets.put(id, num + 1); } else { discoveredTweets.put(id, (long) 1); storeATweet(tweet); thisCount++; } } if (currentIndex < histCount) { avgUsers[currentIndex] = thisCount; currentIndex++; } else { currentIndex = 0; } /* Calculate the average #users in last 10 times */ double sum = 0; for (int num : avgUsers) sum += num; System.out.print( "\r" + count + ", the average number of tweets in last " + histCount + " is: " + sum / histCount); //} catch (TwitterException te) { // te.printStackTrace(); // System.out.println("Failed to search tweets: " + te.getMessage()); // System.exit(-1); //} }
From source file:de.dev.eth0.retweeter.Distributor.java
License:BEER-WARE LICENSE
/** * Distributes the tweets to all target accounts. * * @return/*from w w w .j a v a 2 s . co m*/ * @throws TwitterException */ public int distribute() throws TwitterException { if (!getConfig().isDistributorConfigValid()) { return Integer.MIN_VALUE; } Twitter twitter = getTwitter(); String search = buildSearchString(); Query query = new Query(search); QueryResult result = twitter.search(query); int count = 0; // Iterate through results for (Tweet tweet : result.getTweets()) { // Check if already distributed if (!distributedTweets.contains(tweet.getId()) && StringUtils.contains(tweet.getText(), getConfig().getDistributorConfig().getHashtag())) { // Retweet each result to all targetaccounts for (String targetaccount : getConfig().getDistributorConfig().getTargetaccounts()) { // Dont distribute to yourself if (!StringUtils.equalsIgnoreCase(tweet.getFromUser(), targetaccount)) { StringBuilder sb = new StringBuilder(); sb.append("@"); sb.append(targetaccount); sb.append(": RT @"); sb.append(tweet.getFromUser()); sb.append(" "); sb.append(tweet.getText()); String text = sb.toString(); // try to tweet, might cause an exception (status duplicate) if already tweeted logger.debug("Distributing tweet {} to {}", tweet.toString(), targetaccount); try { twitter.updateStatus(text.length() < 140 ? text : text.substring(0, 140)); count++; } catch (TwitterException te) { logger.warn("distribute of tweet " + tweet.toString() + "failed " + text, te); } distributedTweets.add(tweet.getId()); } } } } return count; }
From source file:de.dev.eth0.retweeter.Retweeter.java
License:BEER-WARE LICENSE
/** * Performs the retweet-action and returns the number of tweets found with the configured hashtag * * @return number of retweeted tweets//from w ww . j a v a2 s.c om * @throws TwitterException */ public int retweet() throws TwitterException { if (!getConfig().isRetweeterConfigValid()) { return Integer.MIN_VALUE; } Twitter twitter = getTwitter(); String search = buildSearchString(); Query query = new Query(search); QueryResult result = twitter.search(query); List<Status> lastretweets = twitter.getRetweetedByMe(); int count = 0; for (Tweet tweet : result.getTweets()) { // ignore retweets and check if the hashtag is really in the tweet's text if (!StringUtils.startsWith(tweet.getText(), "RT @") && StringUtils.contains(tweet.getText(), getConfig().getRetweeterConfig().getHashtag()) && !ALREADY_RETWEETED.contains(tweet.getId())) { boolean retweeted = false; for (Status retweet : lastretweets) { if (tweet.getId() == retweet.getRetweetedStatus().getId()) { retweeted = true; break; } } if (!retweeted) { // try to retweet, might fail logger.debug("found new tweet to retweet: {}", tweet.toString()); try { twitter.retweetStatus(tweet.getId()); count++; } catch (TwitterException te) { logger.debug("retweet failed", te); } finally { ALREADY_RETWEETED.add(tweet.getId()); } } } } return count; }
From source file:de.jetwick.ese.search.Feeding.java
License:Apache License
public Collection<MyTweet> createReal() { List<MyTweet> tweets = new ArrayList<MyTweet>(); try {// w w w . j ava 2 s . co m // get some tweets about java Twitter twitter4j = new TwitterFactory().getInstance(); for (int i = 0; i < 3; i++) { Query q = new Query("java"); q.setRpp(100); for (Tweet tw : twitter4j.search(q).getTweets()) { MyTweet myTw = new MyTweet(tw.getId(), tw.getFromUser()); myTw.setText(tw.getText()); myTw.setCreatedAt(tw.getCreatedAt()); myTw.setFromUserId(tw.getFromUserId()); tweets.add(myTw); } Thread.sleep(1000); } } catch (Exception ex) { logger.error("Error while grabbing tweets from twitter!", ex); } return tweets; }
From source file:de.jetwick.tw.NewClass.java
License:Apache License
/** A thread using the search API */ public Thread search() { return new Thread() { @Override//from w w w . j a va 2 s . c om public void run() { int MINUTES = 2; Twitter twitter = new TwitterFactory().getInstance(); try { while (!isInterrupted()) { Query query = new Query(queryTerms); // RECENT or POPULAR or MIXED // doesn't make a difference if MIXED or RECENT query.setResultType(Query.MIXED); query.setPage(1); query.setRpp(100); QueryResult res = twitter.search(query); for (Tweet tw : res.getTweets()) { searchMap.put(tw.getId(), tw.getText()); } Thread.sleep(MINUTES * 60 * 1000L); } } catch (Exception ex) { ex.printStackTrace(); } } }; }
From source file:de.vanita5.twittnuker.loader.support.TweetSearchLoader.java
License:Open Source License
@Override public List<Status> getStatuses(final Twitter twitter, final Paging paging) throws TwitterException { if (twitter == null) return null; final Query query = new Query(processQuery(mQuery)); query.setRpp(paging.getCount());//from w w w .jav a2 s. c om if (paging.getMaxId() > 0) { query.setMaxId(paging.getMaxId()); } return Arrays.asList(twitter.search(query).getStatuses()); }
From source file:dhbw.clippinggorilla.external.twitter.TwitterUtils.java
/** * @param query// w w w .j a v a2 s.c om * @return Searches for String Query */ public static QueryResult searchTweets(Query query) { config(); // The factory instance is re-useable and thread safe. Twitter twitter = tf.getInstance(); QueryResult result = null; try { result = twitter.search(query); } catch (TwitterException e) { Log.error("Could not get Tweets", e); } return result; }
From source file:edu.american.student.util.TwitterSpider.java
License:Open Source License
/** * Grabs the last 100 tweet objects (Status) from a handle * @param handle// w w w . jav a2 s . com * @return * @throws SpiderException */ public static List<Status> spider(String handle) throws SpiderException { //connect to Twitter ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey(Constants.getTwitterOAuthConsumerKey()) .setOAuthConsumerSecret(Constants.getTwitterOAuthConsumerSecret()) .setOAuthAccessToken(Constants.getTwitterOAuthAccessToken()) .setOAuthAccessTokenSecret(Constants.getTwitterOAuthAccessTokenSecret()); TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = tf.getInstance(); //get tweets Query query = new Query("from:" + handle.replace("@", "")); query.setCount(100); QueryResult result; try { result = twitter.search(query); List<Status> toReturn = result.getTweets(); return toReturn; } catch (TwitterException e) { String gripe = "Twitter spider failed to...err spider!"; throw new SpiderException(gripe, e); } }