Open minds leads to freedom

Communicating my thoughts on software development

Using Scala to update LiveJournal tags – Part I

Posted by rnaufal on May 23rd, 2009

Some days ago I started to use the Scala programming language to update my Livejournal tags using its XML-RPC protocol reference. First I had to check if some tags of mine were entered wrong, so I’ve done this Scala program to list all of them:

   1:import org.apache.xmlrpc.client.XmlRpcClient;
   2:import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
   3:import org.apache.xmlrpc.client.XmlRpcCommonsTransportFactory;
   4:import java.net.URL;
   5:import java.util.Map
   6:import java.util.HashMap
   7:import scala.collection.immutable.TreeSet
   8:
   9:object LJListTag {
  10:     def main(args: Array[String]) {
  11:         val config = new XmlRpcClientConfigImpl()
  12:         config.setEnabledForExtensions(true);
  13:         config.setServerURL(new URL("http://www.livejournal.com/interface/xmlrpc"))
  14:         val client = new XmlRpcClient()
  15:         client.setConfig(config)
  16:         val params = new HashMap[String, String]
  17:         params.put("username", "user")
  18:         params.put("password", "password")
  19:         var paramsToServer = new Array[Object](1)
  20:         paramsToServer(0) = params
  21:         val results = client.execute("LJ.XMLRPC.getusertags", paramsToServer).asInstanceOf[Map[String, String]];
  22:         printEachTag(results)
  23:     }
  24:
  25:     def printEachTag(results: Map[String, String]) {
  26:        var allTags = new TreeSet[String]
  27:        val iterator = results.values().iterator()
  28:           while(iterator.hasNext()) {
  29:             val resultFromRPCData = iterator.next().asInstanceOf[Array[Any]]
  30:             resultFromRPCData.foreach(singleResult => allTags += extractTag(singleResult))
  31:           }
  32:        allTags.foreach(tag => println(tag))
  33:     }
  34:
  35:    def extractTag(singleResult: Any): String = {
  36:        val tag = singleResult.asInstanceOf[HashMap[String, String]]
  37:        return tag.get("name")
  38:    }
  39:}

Just fill your user and password to have all of your LiveJournal tags printed on the standard output. The experience was so amazing, since you can use all the Java libraries (Scala is fully interoperable with Java and runs on top of the JVM). I used a TreeSet because I wanted print my tags sorted according its alphabetical order. I’m continuously studying Scala and its API, so the code above doesn’t use any advanced Scala constructs. If you have any suggestion about the code or how to use better the Scala constructs, post your comments here. It will be all appreciated.

Share and Enjoy:
  • Twitter
  • Digg
  • del.icio.us
  • Reddit
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • FriendFeed
  • Google Buzz
  • MySpace
  • Slashdot
  • http://dserodio.livejournal.com/ dserodio

    Isn’t there a more “native looking” syntax for initializing a HashMap (lines 16-18)? Even Java (using the Double Brace initialization idiom) has a more succinct syntax for this!

  • http://rnaufal.wordpress.com/ rnaufal

    There is a syntatic sugar in Scala when you can create a Map in a more concise way:

    val params = Map("user" -> "user", "password" -> "password")

    But I noted that I cannot send a Map from Scala’s Collections to the server (I’m using the Apache XML-RPC library), so I used the default Java HashMap Collections’ implementation. I think I could then apply your suggestion and use the Double Brace Initialization idiom:

    Map<String, String> params = new HashMap<String, String>() {{
        put("user", "user");
        put("password", "password");
    }};

  • http://www.iforexvideo.com/forex-strategies-%e2%80%93-a-horizon-to-explore/ foreign_exchange_trading

    I like this site very much.

    This is such a outstanding website.
    And it is not like other money orienting site, the content here is truly valuable.

    I am definitely bookmarking it as well as sharin it with my friends.

    :)

  • http://www.wheyproteinsideeffects.net/2009/09/22/mechanism-of-whey-protein-side-effect-function/ Whey Protein side Effects

    Very outstanding website.
    The information here is really helpful.

    I will give it to my friends.

    Cheers

  • http://www.wheyproteinsideeffects.net/2009/09/22/mechanism-of-whey-protein-side-effect-function/ wheyp.roteinsideeffects

    Very extraordinary website.
    The info here is genuinely useful.

    I will invite my friends.

    Cheers

  • http://tvpapa.com freem_ovies.online

    Very good concept, I like how you convey the msg.

  • Microwave Manualunjuctphync

    Hey there thanks regarding your post.I truly enjoy your site.Its extremely informative.On the other hand I actually want you to post how you put social bookmarking below your post.My partner and i like it because it’s a very clean cool blogger mod.
    thank you very much