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…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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) {
      System.out.println(string);
    }
  }
 
  public String[] get() {
    ArrayList names = new ArrayList();
    try {
      for (Status status : twitter.getUserTimeline(new Paging(1, 200))) {
        System.out.println(status.getText());
        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.
          waitUntilICanMakeAnotherCall();
          // end keeping within rate limit code.
          for (Status rt : twitter.getRetweets(status.getId())) {
            names.add(rt.getUser().getScreenName());
            System.out.println("---" + rt.getUser().getScreenName());
          }
        }
      }
    } catch (TwitterException e) {
      e.printStackTrace();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    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");
      System.out.println(temp2.getRemaining());
      if (temp2.getRemaining() == 0) {
        Thread.sleep((temp2.getSecondsUntilReset() + 5) * 1000);
        return;
      }
      System.out.println(temp2.getSecondsUntilReset());
      int secondstosleep = 1 + temp2.getSecondsUntilReset() / temp2.getRemaining();
      System.out.println(secondstosleep);
      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:

1
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 {
					names.add(status.getUser().getScreenName());
				}
			}
		} catch (TwitterException e) {
			e.printStackTrace();
		}
		return names.toArray(new String[names.size()]);
	} // TODO Auto-generated method stub

Leave a Reply