IMDB API Lookup JQuery Plugin

I spent a few hours last night writing my first ever JQuery plugin, only to realise today that it's completely useless to me! I wanted an easy way to populate the "Add medium" form on RustyShark, as it tends to take me a few hours every time I add a new review (and therefore medium) to the website (y'know, looking up information, finding images, finding box art, editing the review etc), and I wanted to increase my production rate :)

IMDB offers its data for free, for non commercial use, but only in text files. I found an API at http://www.imdbapi.com/, which has gone a long way to simplifying and serializing the information in to something useful, so I decided to use this opportunity to teach myself how to write JQuery plugins. Below is my resulting plugin, queryable by Title + Year, or IMDB ID:

; (function ($) {
    $.fn.imdbLookup = function (options) {
        // Default options
        var defaults = {
            complete: function (imdbData) {
            },
            title: null,
            date: null,
            imdbId: null
        };
        // Overwrite defaults with passed options
        options = $.extend(defaults, options);

        return this.each(function () {
            var $this = $(this);
            $this.click(function () {
                // Get defaults
                var title = (options.title != null && options.title.val().length > 0) ? options.title.val().trim() : null;
                var date = (options.date != null && options.date.val().length > 0) ? $.parseDate(options.date.val().trim()) : null;
                var imdbId = (options.imdbId != null && options.imdbId.val().length > 0) ? options.imdbId.val().trim() : null;

                if ((title == null || date == null) && imdbId == null) return;
                var year = date.getFullYear();

                var qs;

                if (title != null && date != null) {
                    qs = {
                        t: title,
                        y: year
                    };
                } else {
                    qs = {
                        i: imdbId
                    };
                }
                
                $.ajax({
                    url: 'http://www.imdbapi.com/',
                    type: "GET",
                    dataType: "json",
                    data: $.extend({
                        r: 'JSON',
                        plot: 'short',
                        tomatoes: true
                    }, qs),
                    success: function (imdbData) {
                        options.complete(imdbData);
                    }
                });
            });
        });
    };

})(jQuery);

Usage:

    $("#MyElement").imdbLookup({
        complete: function (imdbData) {
            alert(imdbData); // Do something with the resulting object
        },
        title: $("#Title"),
        date: $("#ReleaseDate")
    });

Or:

    $("#MyElement").imdbLookup({
        complete: function (imdbData) {
            alert(imdbData); // Do something with the resulting object
        },
        imdbId: $("#imdbId")
    });

Please read http://www.imdbapi.com/ for notes on usage.

It seems to work for me at the moment, but treat it as completely untested - I've only tried it in Chrome, and I haven't tested the IMDB ID method at all. I just wanted to put the code somewhere before I removed it from the JS in RustyShark :)

Popular posts from this blog

Handling uploads with MVC4, JQuery, Plupload and CKEditor

Getting Unity 3D working with Git on Windows

Generating a self-signed SSL certificate for my QNAP NAS