2
\$\begingroup\$

I'm looking for feedback (both general Java, and Twitter specific) on how I'm getting the Twitter handles of everyone who retweeted me over the last 200 Tweets. Comments welcome.

package twitone;

import java.util.ArrayList;
import twitone.structure.BaseTwitterClass;
import twitone.structure.TwitApplicationFactory;
import twitter4j.Paging;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;

public class getmyretweeters extends BaseTwitterClass {

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

        Twitter twitter = TwitApplicationFactory.getjoereddingtonTwitter();
        String temp[] = getRetweeters(twitter);
        for (String string : temp) {
            System.out.println(string);
        }
    }

    public static String[] getRetweeters(Twitter twitter) {
        ArrayList<String> names = new ArrayList<String>();
        try {
            for (Status status : twitter.getUserTimeline(new Paging(1, 200))) {
                System.out.println(status.getText());
                if (status.getRetweetCount() > 0) {
                    Thread.sleep(120000);// Because I don't want to breach
                                        // Twitter's rate limits
                    for (Status rt : twitter.getRetweets(status.getId())) {
                        names.add(rt.getUser().getScreenName());
                    }
                }
            }
        } catch (TwitterException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return names.toArray(new String[names.size()]);
    }
}
\$\endgroup\$
3
\$\begingroup\$

GOing through some general style issues first:

  • getmyretweeters is a class, and should have Camel-Case capitalization -> GetMyRetweeters. I would prefer MyRetweeters with a method get().
  • Your class should not have the static methods you have, but shouild rather have a constructor that takes a twitter name, and then initializes itself to that: public MyRetweeters(Twitter account) {...}
  • your getRetweeters(...) method is static, but should really be an instance method, and the name is implied by the class-name (Retweeters). get() is just fine. Since the class should have been constructed with a Twitter handle, there is no need for any arguments either...

Note about the twitter-API rate limits. Sleeping for 2 minutes is a bad idea in your code. If you really have to be doing this sleeping, then you should change your code to do a background mechanism for accessing Twitter, and have a callback to your application with the results of your query. Also, you should have some sort of caching mechanism so that you do not need to re-query data you already have from Twitter. A limit of 1 query every 2 minutes is a really tight limit.

You should also consider reading up on the Twitter API rate limits... they are not as harsh as you have allowed for. Also, there are headers returned as part of tyour request that shows you how many requests are available in the current window, and when the window ends. Bottom line is that you should be smarter about your rate limiting.

Finally, your factory that gets Twitter sessions should not have a method TwitApplicationFactory.getjoereddingtonTwitter() but should instead have something like: TwitApplicationFactory.getTwitter("joereddington");

Your main method becomes:

public static void main(String[] args) throws TwitterException {
    Twitter twitter = TwitApplicationFactory.getTwitter("joereddington");
    Retweeters retweets = new Retweeters(twitter);
    String temp[] = retweeters.get();
    for (String string : temp) {
        System.out.println(string);
    }
}
\$\endgroup\$
  • \$\begingroup\$ Lovely! thank you for all of that - very thorough! : ) \$\endgroup\$ – Joe Dec 22 '13 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.