Excercise 3 - Exploring Twitter using Java and TwitterAPI

This question is written by Scott Sanner. It uses Java and the Twitter4j wrapper of the Twitter API.

For this section, simply submit your short code snippets in the google form. You do not have to show this code running in lab — it will be obvious from inspection whether your answers are correct. Provide code snippets using Twitter4j to answer the following questions (omitting any enclosing main method and try/catch blocks); an example follows:

// Get the last 50 Tweets that mention “Obama”:
        Twitter twitter = new TwitterFactory().getInstance();
        QueryResult result = twitter.search(new Query("Obama").rpp(50));
        List<Tweet> tweets = result.getTweets();
        for (Tweet tweet : tweets)
           System.out.println(tweet);
  1. Display the last 500 Tweets that mention “Obama” (hint: while Twitter limits to 100 tweets per query, you can use paging to see up to the last 1500 tweets).

  2. Write a loop that repeats 10 times and repeatedly queries for the most recent 100 Tweets and displays them, ensuring that the same Tweet is never retrieved again once retrieved and shown in a previous query (hint: Id’s for Tweets are strictly increasing and you can restrict the search by Id).

  3. Display the last 25 Tweets within roughly 40 miles of downtown New York City (hint: use Twitter geolocation and search restriction facilities, but be careful that the decimal format for latitude and longitude required by Twitter does not consist of the minute/second format for latitude and longitude that many websites display).

  4. Display the number of times the most recent Tweet on “Obama” has been retweeted — don’t show any additional information.

You are encouraged to execute and test your code before providing the answer (please refer to the WWW Lab on how to get started using Twitter4j). It is expected that your code would not throw a NullPointerException or IndexOutOfBoundsException.

How does the Twitter4j API work? Ask Google and use online resources like the Twitter4j API documentation and examples that come with the distribution.

CSS2013 20 April 2013 Canberra, Australia