Java tutorial
package de.dev.eth0.retweeter; import java.util.HashSet; import java.util.Set; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import twitter4j.*; /* * ---------------------------------------------------------------------------- * "THE BEER-WARE LICENSE": * <amuthmann@dev-eth0.de> wrote this file. As long as you retain this notice you * can do whatever you want with this stuff. If we meet some day, and you think * this stuff is worth it, you can buy me a beer in return * ---------------------------------------------------------------------------- */ /** * Distributor for tweets * * @author deveth0 */ public class Distributor extends AbstractTwitterClient { private final static Logger logger = LoggerFactory.getLogger(Distributor.class); // List with all ids of tweets, which have already been distributed private Set<Long> distributedTweets = new HashSet<Long>(); /** * Constructor * * @param _config */ public Distributor(Config _config) { super(_config); } /** * Distributes the tweets to all target accounts. * * @return * @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; } /** * Builds the searchstring, format #HASHTAG from user1 OR user2 ... * * @return searchstring */ private String buildSearchString() { StringBuilder sb = new StringBuilder(); Config.DistributorConfig dc = getConfig().getDistributorConfig(); sb.append(dc.getHashtag()); sb.append(" "); for (int i = 0; i < dc.getAccounts().size(); i++) { if (i > 0) { sb.append(" OR "); } sb.append("from:"); sb.append(dc.getAccounts().get(i)); } String ret = sb.toString(); logger.debug("SearchString: " + ret); return ret; } }