Hacking on GraphHopper - a Java road routing engine. Peter has posted 62 posts at DZone. You can read more from them at their website. View Full User Profile

Internetmusic With Java

02.15.2010
| 3518 views |
  • submit to reddit

A small group on twitter started a funny (or stupid?) mem #internetmusiker (which is internet-musician in English). The initial idea came from FR31H31T. The task was to slightly change a name of a musician, band or song and get a phrase which sounds like an internet- or computer-technical phrase. The results were sometimes very funny and my favourite examples are:

Nine inch Mails, Modem Talking, Deftunes, Wii valdi oder iValdi and Sudo Lindenberg (look here for the full list)

Now I wanted to know which phrases were the most retweeted once and I wrote a small Java application which uses the twitter4j API to do this task.

Twitter twitter = new Twitter(login, pw);
for (int page = 1; page < 1500; page++) {
Query query = new Query("internetmusiker");
query.setPage(page);
query.setRpp(50);

try {
QueryResult res = twitter.search(query);
if (res.getTweets().size() == 0) {
log("No more tweets found");
break;
}
log("page:" + page + " found " + res.getTweets().size() + " tweets");
for (Tweet twe : res.getTweets()) {
allTweets.add(twe);
}
} catch (TwitterException ex) {
log("End of search reached", ex);
break;
}
}
To avoid a full search for the next time I saved the results via my small helper object StoreableTweet to a text file and used ‘query.setSinceId(maxId);’ while querying.

To calculate the retweets there is no API provided (or am I wrong?) so I needed to parse for a ‘RT @name text’ and look up the text within all the tweets. For that I created a hash map with the text content as keys.

Map<String, StorableTweet> mappedTweets = new HashMap();

for (StorableTweet stw : allTweets) {
String key = stw.getTextToRetweet();
if (mappedTweets.containsKey(key))
log("Mapped tweets collection already contains tweet:"
+ stw.toPersistentString());

mappedTweets.put(key, stw);
}

for (StorableTweet stw : allTweets) {
if (stw.getOrigRetweetedText().length() > 0) {
StorableTweet retweet = mappedTweets.get(
stw.getOrigRetweetedText());
if (retweet == null)
log("Skip retweet: cannot find retweet "
+ stw.toPersistentString());
else
retweet.addRetweet(stw);
}
}

 

Again you can look here for the resulting html file. So start your own with #internetmusiker oder even #internetmusician?

From http://karussell.wordpress.com

Published at DZone with permission of its author, Peter Karussell.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: