This question is written by . 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);
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).
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).
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).
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.