Monday, August 23, 2004

Making my ultra-cool Mozilla WebService IMDB query button...

I had been looking at the Mozilla Babelfish webservice query button - wondering why this extremely cool feature of Mozilla wasn't everywhere - and then I rented "The Whole 10 Yards" - which, had I checked IMDB, would have seen that this movie wasn't worth renting... It would have saved $5 and my feeling of embarrassment for the actors/actresses in this terrible movie... But I digress...

What I really want, is special glasses that overlay IMDB ratings whenever I look at a movie poster / ad / box / whatever - but until that time, I would settle for the ability to high-light movie titles at my video stores website and press a button to get the IMDB rating...

After some quick searching, I found that there was no IMDB web service - so I would have to create one - I did, however, find a very complete perl module that let me basically get a movie ranking with about 3 lines of perl code... This great library would save me a great deal of time, but would almost get me ranting about crazy software dependencies, but I'll save that for another time...

The components of my micro-project were:
(1) Something that let me get an IMDB movie rating from a movie title...
(2) Wrappering that in a web service
(3) Writing some javascript to use Mozilla's WebServiceObject...

(1) Ended up being pretty quick once I got IMDB::Film installed and running (it had 2 dependencies, which had 2 more dependencies each, but after I downloaded 10+ modules from CPAN I was laughing... thank god for CPAN or this would have taken numerous hours rather than just a single hour to get running...

(2) I was hoping to just run the webservice locally on my machine using the SOAP::Lite daemon example code - which isn't a full webserver, but something that just handles soap requests [ie - I couldn't serve my wsdl file from it] , and access it via (http://127.0.0.1/...) - but I later discovered that:
a) Mozilla and SOAP::Lite wants to get the WSDL file from a fully qualified URL, and didn't like getting things from files...
b) Mozilla checks for another file on the server which essentially allows it to make web service calls while I'm browsing other sites...
So I had created my web service using the SOAP::Lite daemon, and was able to use a SOAP::Lite client stubbed out to make calls, but Mozilla wasn't going to work, so I had to deploy to a real box, with a real webserver, etc, etc... This added a couple of unplanned hours to my task...

(3) The javascript was mostly a cut-and-paste job from Mozilla's existing example...

Long story short, I got everything working in about 6 hours... having to deploy IMDB::Film to a different machine (my laptop uses Windows, and has Perl 5.8 - Then I deployed to an old machine I had, with Perl 5.005 - had some additional dependencies, and lots of fun installation goodness...) I got my button working... Once I get a chance to make sure that the SOAP::Lite cgi isn't a security concern, I'll publish the real URL for what I have implemented...

Here's the flow of events:



Here's all of the code that makes it happen... You can't just start using this right away - I have changed the URL for my web-service so that it does not get flooded... The web service is not really at www.XYZ.com ....

This is the WSDL - basically - one method: getRating(movieTitle) - returns a single string containing "movie title:year:rating":

This is the web service code - cut and paste job from the Perl SOAP::Lite example code...

Here's the perl module... Note that it relies on the terrific IMDB::Film module...

Here's the page w/ javascript

Takes about 10 seconds to respond to the first movie query, seems faster afterwards (the javascript uses the existing web service proxy object...) I can have several queries going at once ... As a general rule, I don't rent movies with IMDB ratings under 5.0... Make donations of saved money to your favourite local charity... :)

Friday, August 20, 2004

Cool Web Service functions in Mozilla...

This Mozilla WSDL Example page shows how cool Mozilla can be... There is an on page example in which I can type some text, press a button, and it uses the BabelFish web service on xmethods to translate the text... Neat trick you say... This is cooler than you think because it's using Mozilla's built in web service functionality to make the request - There is no server side interface to xmethods... The browser is acting as a web service client to the babel fish service...

So that's nice and all - but the truly cool thing: There are links at the bottom of the page that I can drag onto my Mozilla toolbar (with my bookmarks...) ... I can then simply select some text on a page, and press the "bookmark" which translates the text I've highlighted! What's going on... Well the link that I dragged to my toolbar is some javascript:

javascript:
var proxy= null;
var wsdl_uri = "http://www.xmethods.net/sd/2001/BabelFishService.wsdl";
function Translate (aValue) {
if (!proxy) {
var listener = {
onLoad: function (aProxy) {
proxy = aProxy;
proxy.setListener(listener);
requestTranslation(aValue);
},
onError: function (aError) { },
BabelFishCallback : function (aTranslatedValue) { alert(aTranslatedValue); }
};
createProxy(listener);
} else {
requestTranslation(aValue);
}
}
function createProxy(aCreationListener) {
try {
var factory = new WebServiceProxyFactory();
factory.createProxyAsync(wsdl_uri, "BabelFishPort", "", true, aCreationListener);
} catch (ex) { alert(ex); }
}
function requestTranslation (value) {
if (proxy) {
proxy.BabelFish("en_fr", value);
} else {
alert("Error: Proxy set up not complete!");
}
}

if(window.getSelection()!="")Translate(window.getSelection());
else alert("please select a text before");void(0);


This is interesting on about 3 different levels:
1) I can execute javascript in Mozilla by clicking a bookmark in my toolbar...
2) This bit of code is truly interesting:

try {
var factory = new WebServiceProxyFactory();
factory.createProxyAsync(wsdl_uri, "BabelFishPort", "", true, aCreationListener);
} catch (ex) { alert(ex); }

I can now write javascript to more or less call any webservice I like (haven't tried it with complex data structures - but who knows...)
3) Why aren't there 1000 Mozilla webservice bookmarks out there for me to drop on my toolbar...

I'll be playing around with some of this today to see if I can get my own web service running using javascript, Perl, SOAP::Lite, IMDB::Film (!!) - so that as I'm looking at my video stores website, I can just highlight movie titles and get the IMDB movie rating... Will post an update later once I've got something working...