Search and Display Movie Trailers in ASP.NET with the Internet Video Archive API

There are lots of APIs on the web - RESTful APIs, JSON APIs, XML APIs. Many of these are free and usually only require signing up for a developer key. This lets them know who's doing what and in some cases to apply rate limits. In previous articles I featured using the official Twitter API as well as the Topsy Otter Twitter Database API. Another API that enables the developer to display cool dynamic content is the Internet Video Archive API, which is featured here.

The Internet Video Archive API offers movie trailers and other entertainment - related items. You can find more APIs at Programmable Web.

Internet Video Archive offers a number of different services including Movies and TV with Trailers, Music, Games, and more. They also have a custom Flash player that can be used to display the trailers on your site or blog.

In order to use this API you'll need to sign up for a free DeveloperID. In the case of this sample app, I'm storing this in the appSettings section of the web application's web.config file, so as soon as you get yours, all you need to do to get going is fill it in. Without a DeveloperID, the demo app, by design, will throw an InvalidOperationException.

When I first started with this, I downloaded a sample Movie Title Search XML document and saved it locally. Then I ran XSD.EXE on it to generate a schema, and again XSD.EXE with the /classes option to convert the schema to C# classes. It worked fine, but unfortunately it wouldn't deserialize properly using the XmlSerializer. However, the main class I was interested in works OK for populating the content of search results so at least XSD gave me something useful to work with. In the past, I've worked with XSDObjectGen, which is more sophisticated, but the bottom line with XML Serialization to nested classes is that you need a lot of custom Attributes to make it do exactly what you want, and in this case I felt that the extra time wasn't worth the payback. You might also want to look at XSDToCode on, which looks to be a bit more sophisticated.

The basic process of getting either a Movie Title Search or their "What's in Theaters This week" movie trailers is to make an HTTP request for the respective XML output, embedding your DeveloperId (and optional search term for the Movie Search) and consuming the returned XML. Here is an example of how I did this with the Movie Search API:

private static string TitleSearchUrl = "[DEVELOPERID]&SearchTerm=";
private static string InTheatersThisWeekUrl = "[DEVELOPERID]";
private static string DeveloperID = ConfigurationManager.AppSettings["developerId"];

public static List<itemsItem> TitleSearch (string searchTerm)
if (String.IsNullOrEmpty(DeveloperID))
throw new InvalidOperationException("DeveloperId must be provided in the appSettings element.");
string fullUrl = TitleSearchUrl.Replace("[DEVELOPERID]", DeveloperID) + searchTerm;

WebClient wc = new WebClient();
byte[] b = wc.DownloadData(fullUrl);
MemoryStream ms = new MemoryStream(b);
XmlDocument doc = new XmlDocument();
var nods = doc.SelectNodes("items/item");
List<itemsItem> videos = new List<itemsItem>();
foreach (XmlNode n in nods)
itemsItem itm = new itemsItem();
itm.Description = n.SelectSingleNode("Description").InnerText;
itm.Title = n.SelectNodes("Title")[0].InnerText;
itm.EmbedUrl = n.SelectNodes("EmbedUrl")[0].InnerText;
// add additional fields if desired here
return videos;

The "itemsItem" class is the XSD.EXE constructed container class for the "item" nodes. So all we need to do is get the NodeList of "items/item", iterate over each node, stick whatever properties we want into an instance of the itemsItem class, and add it to our List, "videos". When done, we return the List to the caller.

There are lots of other properties besides Description, Title and EmbedUrl, but those are the only one I wanted for a nice display. Now switching over to the ASP.NET Web page, I have a DataList:

<asp:DataList ID="DataList1" runat="server"
onitemdatabound="DataList1_ItemDataBound" CssClass=AlignCenter>
<table cellspacing=2 cellpadding =2 border=0 width=50% align=center class=AlignCenter>
<td><b><%#Eval("Title") %></b></td>
<tr><td wrap=true align=justify width=50% class=Yellowborder ><%# Eval("Description") %></td></tr>
<tr><td align="center">
<asp:Label id="myContent" Width="320px" runat="server"></asp:Label>
<tr><td><asp:Label ID="EmbedUrl" runat=server></asp:Label></td></tr>
<tr><td class=LightBlueHeader> <br /></td></tr>
<FooterTemplate >

The Title and Description fields are regular bound fields in the ItemTemplate, but the EmbedUrl field requires special treatment. The Flash template to embed each video looks like this:

string scr = "<script>swfobject.embedSWF(\"EmbedUrl\", \"myContent\", \"420\", \"180\", \"9.0.0\", \"expressInstall.swf\", flashvars);</script>";

This needs to be custom processed in the DataList's ItemDataBound eventhandler as follows:

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
string scr = "<script>swfobject.embedSWF(\"EmbedUrl\", \"myContent\", \"420\", \"180\", \"9.0.0\", \"expressInstall.swf\", flashvars);</script>";

if (e.Item.ItemType == ListItemType.Item)
var x = (itemsItem)e.Item.DataItem;
string url = x.EmbedUrl;
var q = (Label)e.Item.Controls[1];

string id = q.ClientID;

q.Text= scr.Replace("myContent", id).Replace("EmbedUrl", url);

catch (Exception ex)

We look to see if the ItemType is ListItemType.Item (not header, footer, etc.). We grab the DataItem and cast it to an instance of our itemsItem class, and get the EmbedUrl property. Then we grab the EmbedUrl Label control that is in the Template, and we do a string replacement on the script string, replacing the EmbedUrl with the actual Url for that video, and the "myContent" ID with the actual ClientId of the Label for that row. In this manner, we have conformed with the script's expectation of a unique id for the control to show the video in, and the revised script becomes the Text property of that row's Label.

The result, after the application of some CSS and formatting, is a nice display of each video's title, description, and (if any) an embedded Flash movie trailer:

This is just a start, there are many other things you can do with the Internet Video Archive API. You can download the complete demo Visual Studio 2010 Solution. Don't forget to request a DeveloperId and plug it into the web.config.

By Peter Bromberg   Popularity  (5084 Views)