Over a million developers have joined DZone.

Internetmusic With Java

· Java Zone

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

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

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}