Some Twitter code (Warning, coders only)

So in a previous post I promised to share the code that lets me keep track of the people that actively engage on Twitter – that reply or retweet. As I promised that six months ago I thought it was time to dig out the code. It’s relatively straightforward, mostly because I use the Twitter4J library to do most of the heavy lifting. Hope this is useful for someone.

If you are willing to do some coding, here is how it would work in java…

package twitone;

import java.util.ArrayList;
import java.util.Map;

import twitone.structure.BaseTwitterClass;
import twitone.structure.TwitApplicationFactory;
import twitter4j.Paging;
import twitter4j.RateLimitStatus;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;

public class MyRetweeters extends BaseTwitterClass {

  private Twitter twitter;

  public MyRetweeters(Twitter twitter) {
    this.twitter = twitter;

  public static void main(String[] args) throws TwitterException {

    Twitter twitter = TwitApplicationFactory.getjoereddingtonTwitter();
    MyRetweeters unit = new MyRetweeters(twitter);
    String temp[] = unit.get();
    for (String string : temp) {

  public String[] get() {
    ArrayList names = new ArrayList();
    try {
      for (Status status : twitter.getUserTimeline(new Paging(1, 200))) {
        if (status.getText().startsWith("RT")) {
          // skip
        } else if (status.getRetweetCount() > 0) {
          // Because I don't want to breach
          // Twitter's rate limits
          // okay the below has been added to keep within the rate
          // limited.
          // end keeping within rate limit code.
          for (Status rt : twitter.getRetweets(status.getId())) {
            System.out.println("---" + rt.getUser().getScreenName());
    } catch (TwitterException e) {
    } catch (InterruptedException e) {
    return names.toArray(new String[names.size()]);

   * @throws TwitterException
   * @throws InterruptedException
  protected void waitUntilICanMakeAnotherCall() throws TwitterException, InterruptedException {
      Map<String, RateLimitStatus> temp = twitter.getRateLimitStatus();

      RateLimitStatus temp2 = temp.get("/statuses/retweets/:id");
      if (temp2.getRemaining() == 0) {
        Thread.sleep((temp2.getSecondsUntilReset() + 5) * 1000);
      int secondstosleep = 1 + temp2.getSecondsUntilReset() / temp2.getRemaining();
      Thread.sleep(secondstosleep * 1000);

This code will print out every tweet you’ve made recently, along with the IDs of the people who retweeted it. A couple of quick things to note – mostly that this line:

Twitter twitter = TwitApplicationFactory.getjoereddingtonTwitter();

won’t work for you – that’s me getting my own API key and so on, you’ll have to write your own…

You should also know that this only works on the last 200 tweets, largely because that’s all I ever needed, it would be fairly simple to loop over the pages.

If you want replies rather than retweets, that’s actually much quicker and easier:

private String[] getReplies() {
		ArrayList names = new ArrayList();
		try {
			for (Status status : twitter.getMentionsTimeline(new Paging(1, 200))) {
				if (status.getInReplyToStatusId() == -1) {
					// skip, possibly/likely spam, I'll pick up the genuine
					// ones by hand
				} else {
		} catch (TwitterException e) {
		return names.toArray(new String[names.size()]);
	} // TODO Auto-generated method stub

Leave a Reply