dinsdag 5 januari 2010

Groovy and TheMoviedb.org API

How easy is it to use a web-service you might ask? With Groovy it's very easy...

TheMovieDb.org offers a nice interface for searching it's database:

Using it with Groovy appeared to be as simple as:

#!/usr/bin/env groovy

def yourApiKey = "won't share mine..."
def movieDbUrl = "http://api.themoviedb.org/2.1/Movie.search/en/xml"
def searchForTitle = this.args[0]

def xmlString = new URL("${movieDbUrl}/${yourApiKey}/${searchForTitle}").text
def doc = new XmlSlurper().parseText(xmlString)

doc.movies.movie.each{ println "${it.name.text()} - ${it.released.text()}" }

From command line it would look like:

ben@myMachine:~/groovy$ ./moovie.groovy transformers

Transformers: Revenge of the Fallen - 2009-06-24
Transformers - 2007-07-04
The Transformers: The Movie - 1986-08-08
Transformers: Resparked - 2008-03-09
Transformers: Beginnings -
Transformers: Scramble City -

Nice.

Geen opmerkingen:

Een reactie posten