Lighting Talk at SuperHappyDevHouse #40: Develop Netflix Movie Search Application using jQuery, OData, JSONP and Netflix Technologies
I attended SuperHappyDevHouse #40 at the Microsoft Silicon Valley campus last Saturday(Sept 18, 2010). It is the largest SuperHappyDevHouse ever with over 500 developers/hackers/entrepreneurs showed up throughout the day from 1 pm to 1 am. Here are some pictures to share with you.
I presented at the Lighting talk of SuperHappyDevHouse. There were about 11 speakers and each would have only no more than 5 minutes. This talk is inspired by Stephen Walther's blog. My original plan was to demonstrate developing a Netlfix Movie search and play application leveraging the technology of jQuery, OData, JSONP, and Netflix API in a simple HTML file in 5 minutes. However, because of the logistic reason, I can only using slides so I have shared my slide deck with you below. The presentation covers the architecture, OData detail, key steps in developing this application and step by step code.
Develop Netflix Movie Search App using jQuery, OData, JSONP and Netflix Technologies
View more presentations from Doris Chen.
The architecture of this application is illustrated in the slide below. Everything is developed using one simple HTML. A startup page allows you to enter the movie name to lookup through Netflix site. During the look up, it will send a Netflix OData query via a JSONP Ajax call to the Netfllix OData Provider. JSONP is used here for resolving the cross domain issue. As a response, the Netflix will return the matching movie in JSON format. Then using jQuery template approach to display the result. Microsoft has contributed jQuery template jquery.tmpl.js to jQuery project and it will be part of core jQuery 1.5 in the next release. Once you get a list of movies, you can click play button to play the movie in Netflix player. Two things you need to get before you can run this application:
- You need to download jquery.tmpl.js from https://github.com/jquery/jquery-tmpl. Put this file under a separate directory called Scripts
- You need to apply a Netflix Developer API account for the API key as well as a Netflix account to play the movies.
The code in HTML is also illustrated below:
Code Snippet
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="https://www.w3.org/1999/xhtml">
- <head>
- <title>Search Movies</title>
- <style type="text/css">
- #movieTemplateContainer div
- {
- width:500px;
- padding: 10px;
- margin: 10px;
- border: black solid 1px;
- }
- </style>
- </head>
- <body>
- <label>Search Movies:</label>
- <input id="movieName" size="50" />
- <button id="btnLookup">Lookup</button>
- <div id="movieTemplateContainer"></div>
- <script id="movieTemplate" type="text/x-jquery-tmpl">
- <div>
- <img src="${BoxArt.LargeUrl}" />
- <strong>${Name}</strong>
- </br>
- <button id="playButton" movieID=${NetflixApiId} onclick="play(this)">Play Now</button>
- <p>
- {{html Synopsis}}
- </p>
- </div>
- </script>
- <script type="text/javascript" src="https://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js"></script>
- <script type="text/javascript" src="/Scripts/jquery.tmpl.js"></script>
- <script type="text/javascript">
- $("#btnLookup").click(function () {
- // Build OData query
- var movieName = $("#movieName").val();
- var query = "https://odata.netflix.com/Catalog" // netflix base url
- + "/Titles" // top-level resource
- + "?$filter=substringof('" + escape(movieName) + "',Name)" // filter by movie name
- + "&$callback=callback" // jsonp request
- + "&$format=json"; // json request
- // Make JSONP call to Netflix
- $.ajax({
- dataType: "jsonp",
- url: query,
- jsonpCallback: "callback",
- success: callback
- });
- });
- function callback(result) {
- // unwrap result
- var movies = result.d.results;
- $("#movieTemplateContainer").empty();
- $("#movieTemplate").tmpl(movies).appendTo("#movieTemplateContainer");
- }
- function play(mvInfo) {
- var id = $(mvInfo).attr("movieID").substring(45);
- javascript: nflx.openPlayer('https://api.netflix.com/catalog/movie/'+id, 0, 0, 'YOUR NETFLIX DEVELOPER API KEY');
- }
- </script>
- <script src="https://jsapi.netflix.com/us/api/js/api.js"></script>
- </body>
- </html>