8
\$\begingroup\$

I used some Java code to work out how often MPs and Senators cited their sources in tweets. The results are at here and the GitHub for the whole project is here, and the Java code I use to download the MPs statements from Twitter is below. I'm looking for any feedback on style, possible errors, and anything else you can think of. I'm intending to be running this code quite regularly and I'd like to check I've NOT left something silly in...

import java.util.Map;

import twitter4j.Paging;
import twitter4j.RateLimitStatus;
import twitter4j.ResponseList;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.User;

public class ProcessMp {

    public static Twitter twitter;

    public static void main(String[] args) throws TwitterException, InterruptedException {
        twitter = TwitApplicationFactory.getjoereddingtonTwitter();
        String username = args[0];
        System.out.println(username);
        User user = twitter.showUser(username.replace("@", ""));
        // ht: from http://www.devmanuals.com/tutorials/java/gettingUserId.html
        checkUser(user);
    }

    private static void checkUser(User user) throws TwitterException, InterruptedException {
        int withNumberAndRef = 0;
        int tweets = 0;
        int withNumber = 0;

        for (int i = 1; i < 10; i++) {
            ResponseList<Status> temp = twitter.getUserTimeline(user.getId(), new Paging(i, 100));
            for (Status status : temp) {
                if (status.getText().startsWith("RT")) {
                    continue;
                }
                System.out.println(status.getText());
                tweets++;
                if (status.getText().matches(".* \\d+,*\\d* .*")) {
                    withNumber++;
                    if (status.getText().matches(".*http.*")) {
                        withNumberAndRef++;
                    }
                }
            }
            waitUntilICanMakeAnotherCall();
        }

        System.out.print("XX," + user.getScreenName() + ",");
        System.out.print(user.getName() + ",");
        System.out.print(tweets + ",");
        System.out.print(withNumber + ",");
        System.out.println(withNumberAndRef);

    }

    protected static 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);
                return;
            }
            int secondstosleep = 1 + temp2.getSecondsUntilReset() / temp2.getRemaining();
            Thread.sleep(secondstosleep * 1000);
        }
    }

}
\$\endgroup\$
2
\$\begingroup\$
  • Main is just a driver program. Separate it from the implementation class.
  • If the called function throws an exception , someone has to handle it. Check who should do it and log some helpful message.
  • Variable name temp , temp2.
  • Could we guarantee that user!=null or temp!=null or it may not contain a null status i.e check for user!=null , temp!=null and status!=null.
  • No hard-coded value.
  • Make the twit extraction implementation generic for any pageSize, any user, i.e take that as input parameters or member variable and feed on it.
  • Since the Twit extraction API may be called by anyone, so have the username correction logic there itself.
  • Limit the printlns for debugging purpose only.
\$\endgroup\$

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.