Auto Complete Search of the Netflix API in C#

9/22/2009

In my last post I talked about OAuth and the fact that I was using it when dealing with the Netflix API. Well it turns out that not everything in the API requires OAuth. There is one portion of the API that requires very little (with the exception of a consumer key which you can get from the Netflix developer section). Specifically the auto complete functionality of the API. In order to do their auto complete search all you need is the consumer key and a search term. It gets fed to the system like OAuth does (a simple URL with the appropriate query string), but we don't have to deal with a signature, etc. So lets look at the code to get this done:

   1: public List<string> AutoComplete(string ConsumerKey, string Term)
   2: {
   3:     List<string> Results = new List<string>();
   4:     string Content = FileManager.GetFileContents(new Uri("http://api.netflix.com/catalog/titles/autocomplete?oauth_consumer_key=" + ConsumerKey + "&term=" + Term));
   5:     XmlDocument Document = new XmlDocument();
   6:     Document.LoadXml(Content);
   7:     foreach (XmlNode Children in Document.ChildNodes)
   8:     {
   9:         if (Children.Name.Equals("autocomplete", StringComparison.CurrentCultureIgnoreCase))
  10:         {
  11:             foreach (XmlNode Child in Children.ChildNodes)
  12:             {
  13:                 if (Child.Name.Equals("autocomplete_item", StringComparison.CurrentCultureIgnoreCase))
  14:                 {
  15:  
  16:                     foreach (XmlNode Title in Child.ChildNodes)
  17:                     {
  18:                         if (Title.Name.Equals("title", StringComparison.CurrentCultureIgnoreCase))
  19:                         {
  20:                             Results.Add(Title.Attributes["short"].Value);
  21:                         }
  22:                     }
  23:                 }
  24:             }
  25:         }
  26:     }
  27:     return Results;
  28: }

The code above does the call to the API using the FileManager class from my utility library. Once the response is read, it loads the XML that is returned and parses it, looking for the title content. I then put those values into a list of strings and return it. That's all there is to it really. So give it a try, leave feedback, and happy coding.



Comments