Netflix Title Search in C#

9/24/2009

I showed in my last post how to do a basic auto complete sort of functionality using Netflix. Today I'm expanding on that a bit and showing some basic search functionality. Using the OAuth class that I created as a base class, we can easily create a set of classes to pull information from Netflix.

   1: /// <summary>
   2: /// Helper class to be used with the Netflix API
   3: /// </summary>
   4: public class Netflix : OAuth.OAuth
   5: {
   6:     #region Constructor
   7:  
   8:     /// <summary>
   9:     /// Constructor
  10:     /// </summary>
  11:     public Netflix()
  12:         : base()
  13:     {
  14:         this.SignatureType = Signature.HMACSHA1;
  15:     }
  16:  
  17:     #endregion
  18:  
  19:     #region Public Functions
  20:  
  21:     /// <summary>
  22:     /// Calls netflix's auto complete function
  23:     /// </summary>
  24:     /// <param name="ConsumerKey">Consumer key</param>
  25:     /// <param name="Term">Your search term</param>
  26:     /// <returns>A list of possible matches</returns>
  27:     public List<string> AutoComplete(string ConsumerKey, string Term)
  28:     {
  29:         List<string> Results = new List<string>();
  30:         string Content = FileManager.GetFileContents(new Uri("http://api.netflix.com/catalog/titles/autocomplete?oauth_consumer_key=" + ConsumerKey + "&term=" + Term));
  31:         XmlDocument Document = new XmlDocument();
  32:         Document.LoadXml(Content);
  33:         foreach (XmlNode Children in Document.ChildNodes)
  34:         {
  35:             if (Children.Name.Equals("autocomplete", StringComparison.CurrentCultureIgnoreCase))
  36:             {
  37:                 foreach (XmlNode Child in Children.ChildNodes)
  38:                 {
  39:                     if (Child.Name.Equals("autocomplete_item", StringComparison.CurrentCultureIgnoreCase))
  40:                     {
  41:  
  42:                         foreach (XmlNode Title in Child.ChildNodes)
  43:                         {
  44:                             if (Title.Name.Equals("title", StringComparison.CurrentCultureIgnoreCase))
  45:                             {
  46:                                 Results.Add(Title.Attributes["short"].Value);
  47:                             }
  48:                         }
  49:                     }
  50:                 }
  51:             }
  52:         }
  53:         return Results;  
  54:     }
  55:  
  56:     /// <summary>
  57:     /// Title search
  58:     /// </summary>
  59:     /// <param name="ConsumerKey">Consumer key</param>
  60:     /// <param name="ConsumerKeySecret">Consumer key secret</param>
  61:     /// <param name="Term">Term to search for</param>
  62:     /// <param name="MaxResults">Max results to return</param>
  63:     /// <param name="StartIndex">The starting index</param>
  64:     /// <returns>List of title information</returns>
  65:     public Titles TitleSearch(string ConsumerKey, string ConsumerKeySecret, string Term, int MaxResults, int StartIndex)
  66:     {
  67:         AddParameter("term", Term);
  68:         AddParameter("max_results", MaxResults.ToString());
  69:         AddParameter("start_index", StartIndex.ToString());
  70:         this.ConsumerKey = ConsumerKey;
  71:         this.ConsumerKeySecret = ConsumerKeySecret;
  72:         this.Token = "";
  73:         this.TokenSecret = "";
  74:         this.Method = HTTPMethod.GET;
  75:         this.Url = new Uri("http://api.netflix.com/catalog/titles/");
  76:         return new Titles(FileManager.GetFileContents(new Uri(GenerateRequest())));
  77:     }
  78:  
  79:     /// <summary>
  80:     /// Title synopsis
  81:     /// </summary>
  82:     /// <param name="ConsumerKey">Consumer key</param>
  83:     /// <param name="ConsumerKeySecret">Consumer key secret</param>
  84:     /// <param name="Title">Title to search for</param>
  85:     /// <returns>The synopsis info</returns>
  86:     public string TitleSynopsis(string ConsumerKey, string ConsumerKeySecret, Title Title)
  87:     {
  88:         this.ConsumerKey = ConsumerKey;
  89:         this.ConsumerKeySecret = ConsumerKeySecret;
  90:         this.Token = "";
  91:         this.TokenSecret = "";
  92:         this.Method = HTTPMethod.GET;
  93:         this.Url = new Uri(Title.SynopsisLink);
  94:         string Content = FileManager.GetFileContents(new Uri(GenerateRequest()));
  95:  
  96:         XmlDocument Document = new XmlDocument();
  97:         Document.LoadXml(Content);
  98:         foreach (XmlNode Children in Document.ChildNodes)
  99:         {
 100:             if (Children.Name.Equals("synopsis", StringComparison.CurrentCultureIgnoreCase))
 101:             {
 102:                 return Children.InnerText;
 103:             }
 104:         }
 105:  
 106:         return "";
 107:     }
 108:  
 109:     /// <summary>
 110:     /// Cast search
 111:     /// </summary>
 112:     /// <param name="ConsumerKey">Consumer key</param>
 113:     /// <param name="ConsumerKeySecret">Consumer key secret</param>
 114:     /// <param name="Title">Title to search for</param>
 115:     /// <returns>List of people information</returns>
 116:     public People CastLookup(string ConsumerKey, string ConsumerKeySecret, Title Title)
 117:     {
 118:         this.ConsumerKey = ConsumerKey;
 119:         this.ConsumerKeySecret = ConsumerKeySecret;
 120:         this.Token = "";
 121:         this.TokenSecret = "";
 122:         this.Method = HTTPMethod.GET;
 123:         this.Url = new Uri(Title.CastLink);
 124:         return new People(FileManager.GetFileContents(new Uri(GenerateRequest())));
 125:     }
 126:  
 127:     /// <summary>
 128:     /// Director search
 129:     /// </summary>
 130:     /// <param name="ConsumerKey">Consumer key</param>
 131:     /// <param name="ConsumerKeySecret">Consumer key secret</param>
 132:     /// <param name="Title">Title to search for</param>
 133:     /// <returns>List of people information</returns>
 134:     public People DirectorLookup(string ConsumerKey, string ConsumerKeySecret, Title Title)
 135:     {
 136:         this.ConsumerKey = ConsumerKey;
 137:         this.ConsumerKeySecret = ConsumerKeySecret;
 138:         this.Token = "";
 139:         this.TokenSecret = "";
 140:         this.Method = HTTPMethod.GET;
 141:         this.Url = new Uri(Title.DirectorsLink);
 142:         return new People(FileManager.GetFileContents(new Uri(GenerateRequest())));
 143:     }
 144:  
 145:     /// <summary>
 146:     /// Similar title search
 147:     /// </summary>
 148:     /// <param name="ConsumerKey">Consumer key</param>
 149:     /// <param name="ConsumerKeySecret">Consumer key secret</param>
 150:     /// <param name="Title">Title to use as our search base</param>
 151:     /// <returns>List of title information</returns>
 152:     public Titles SimilarTitles(string ConsumerKey, string ConsumerKeySecret, Title Title)
 153:     {
 154:         this.ConsumerKey = ConsumerKey;
 155:         this.ConsumerKeySecret = ConsumerKeySecret;
 156:         this.Token = "";
 157:         this.TokenSecret = "";
 158:         this.Method = HTTPMethod.GET;
 159:         this.Url = new Uri(Title.SimilarTitleLink);
 160:         return new Titles(FileManager.GetFileContents(new Uri(GenerateRequest())));
 161:     }
 162:  
 163:     /// <summary>
 164:     /// Formats available for the title
 165:     /// </summary>
 166:     /// <param name="ConsumerKey">Consumer key</param>
 167:     /// <param name="ConsumerKeySecret">Consumer key secret</param>
 168:     /// <param name="Title">Title to use as our search base</param>
 169:     /// <returns>List of available formats</returns>
 170:     public List<string> FormatsAvailable(string ConsumerKey, string ConsumerKeySecret, Title Title)
 171:     {
 172:         this.ConsumerKey = ConsumerKey;
 173:         this.ConsumerKeySecret = ConsumerKeySecret;
 174:         this.Token = "";
 175:         this.TokenSecret = "";
 176:         this.Method = HTTPMethod.GET;
 177:         this.Url = new Uri(Title.FormatsAvailableLink);
 178:         string Content = FileManager.GetFileContents(new Uri(GenerateRequest()));
 179:  
 180:         List<string> Results = new List<string>();
 181:         XmlDocument Document = new XmlDocument();
 182:         Document.LoadXml(Content);
 183:         foreach (XmlNode Children in Document.ChildNodes)
 184:         {
 185:             if (Children.Name.Equals("delivery_formats", StringComparison.CurrentCultureIgnoreCase))
 186:             {
 187:                 foreach (XmlNode Child in Children.ChildNodes)
 188:                 {
 189:                     if (Child.Name.Equals("availability", StringComparison.CurrentCultureIgnoreCase))
 190:                     {
 191:                         foreach (XmlNode Category in Child.ChildNodes)
 192:                         {
 193:                             if (Category.Name.Equals("category", StringComparison.CurrentCultureIgnoreCase))
 194:                             {
 195:                                 Results.Add(Category.Attributes["term"].Value);
 196:                             }
 197:                         }
 198:                     }
 199:                 }
 200:             }
 201:         }
 202:         return Results;
 203:     }
 204:  
 205:     #endregion
 206: }

The code above has a number of functions that can be used to pull information. The main function that you'd be interested in is TitleSearch. It takes your consumer key, secret, search term, starting index, etc. The consumer key and secret key can be retrieved by signing up at netflix's developer center. But with that function, a helper class (Titles) is used to parse the information and return it so that it can be used. That along with the other helpers can be found below:

   1: /// <summary>
   2: /// List of titles
   3: /// </summary>
   4: public class Titles
   5: {
   6:     #region Constructor
   7:  
   8:     /// <summary>
   9:     /// Constructor
  10:     /// </summary>
  11:     /// <param name="XMLContent">XML Content</param>
  12:     public Titles(string XMLContent)
  13:     {
  14:         XmlDocument Document = new XmlDocument();
  15:         Document.LoadXml(XMLContent);
  16:         TitleList = new List<Title>();
  17:  
  18:         foreach (XmlNode Children in Document.ChildNodes)
  19:         {
  20:             if (Children.Name.Equals("catalog_titles", StringComparison.CurrentCultureIgnoreCase)
  21:                 || Children.Name.Equals("similars", StringComparison.CurrentCultureIgnoreCase))
  22:             {
  23:                 foreach (XmlNode Child in Children.ChildNodes)
  24:                 {
  25:                     if (Child.Name.Equals("catalog_title", StringComparison.CurrentCultureIgnoreCase)
  26:                         || Child.Name.Equals("similars_item", StringComparison.CurrentCultureIgnoreCase))
  27:                     {
  28:                         TitleList.Add(new Title((XmlElement)Child));
  29:                     }
  30:                     else if (Child.Name.Equals("number_of_results", StringComparison.CurrentCultureIgnoreCase))
  31:                     {
  32:                         NumberOfResults = int.Parse(Child.InnerText);
  33:                     }
  34:                     else if (Child.Name.Equals("start_index", StringComparison.CurrentCultureIgnoreCase))
  35:                     {
  36:                         StartIndex = int.Parse(Child.InnerText);
  37:                     }
  38:                     else if (Child.Name.Equals("results_per_page", StringComparison.CurrentCultureIgnoreCase))
  39:                     {
  40:                         ResultsPerPage = int.Parse(Child.InnerText);
  41:                     }
  42:                 }
  43:             }
  44:         }
  45:     }
  46:  
  47:     #endregion
  48:  
  49:     #region Properties
  50:  
  51:     /// <summary>
  52:     /// Number of results
  53:     /// </summary>
  54:     public int NumberOfResults { get; set; }
  55:  
  56:     /// <summary>
  57:     /// Start index
  58:     /// </summary>
  59:     public int StartIndex { get; set; }
  60:  
  61:     /// <summary>
  62:     /// Results per page
  63:     /// </summary>
  64:     public int ResultsPerPage { get; set; }
  65:  
  66:     /// <summary>
  67:     /// List of titles
  68:     /// </summary>
  69:     public List<Title> TitleList { get; set; }
  70:  
  71:     #endregion
  72: } 
  73:  
  74:   
  75:  
  76: /// <summary>
  77: /// Title information
  78: /// </summary>
  79: public class Title
  80: {
  81:     #region Constructor
  82:  
  83:     /// <summary>
  84:     /// Constructor
  85:     /// </summary>
  86:     /// <param name="Element">Title information</param>
  87:     public Title(XmlElement Element)
  88:     {
  89:         Categories = new List<string>();
  90:         foreach (XmlNode Children in Element.ChildNodes)
  91:         {
  92:             if (Children.Name.Equals("id", StringComparison.CurrentCultureIgnoreCase))
  93:             {
  94:                 ID = Children.InnerText;
  95:             }
  96:             else if (Children.Name.Equals("title", StringComparison.CurrentCultureIgnoreCase))
  97:             {
  98:                 TitleInformation = new TitleInformation((XmlElement)Children);
  99:             }
 100:             else if (Children.Name.Equals("box_art", StringComparison.CurrentCultureIgnoreCase))
 101:             {
 102:                 BoxArt = new BoxArt((XmlElement)Children);
 103:             }
 104:             else if (Children.Name.Equals("release_year", StringComparison.CurrentCultureIgnoreCase))
 105:             {
 106:                 ReleaseYear = Children.InnerText;
 107:             }
 108:             else if (Children.Name.Equals("runtime", StringComparison.CurrentCultureIgnoreCase))
 109:             {
 110:                 RunTime = int.Parse(Children.InnerText);
 111:             }
 112:             else if (Children.Name.Equals("average_rating", StringComparison.CurrentCultureIgnoreCase))
 113:             {
 114:                 AverageRating = double.Parse(Children.InnerText);
 115:             }
 116:  
 117:             else if (Children.Name.Equals("category", StringComparison.CurrentCultureIgnoreCase))
 118:             {
 119:                 if (Children.Attributes["scheme"] != null
 120:                     && (Children.Attributes["scheme"].Value.Equals("http://api.netflix.com/categories/mpaa_ratings",
 121:                         StringComparison.CurrentCultureIgnoreCase)
 122:                         || Children.Attributes["scheme"].Value.Equals("http://api.netflix.com/categories/tv_ratingss",
 123:                         StringComparison.CurrentCultureIgnoreCase)))
 124:                 {
 125:                     Rating = Children.Attributes["label"].Value;
 126:                 }
 127:                 else if (Children.Attributes["scheme"].Value.Equals("http://api.netflix.com/categories/genres",
 128:                     StringComparison.CurrentCultureIgnoreCase))
 129:                 {
 130:                     Categories.Add(Children.Attributes["label"].Value);
 131:                 }
 132:             }
 133:             else if (Children.Name.Equals("link", StringComparison.CurrentCultureIgnoreCase))
 134:             {
 135:                 if (Children.Attributes["rel"] != null
 136:                     && Children.Attributes["rel"].Value.Equals("http://schemas.netflix.com/catalog/titles/synopsis",
 137:                         StringComparison.CurrentCultureIgnoreCase))
 138:                 {
 139:                     SynopsisLink = Children.Attributes["href"].Value;
 140:                 }
 141:                 else if (Children.Attributes["rel"] != null
 142:                     && Children.Attributes["rel"].Value.Equals("http://schemas.netflix.com/catalog/people.cast",
 143:                         StringComparison.CurrentCultureIgnoreCase))
 144:                 {
 145:                     CastLink = Children.Attributes["href"].Value;
 146:                 }
 147:                 else if (Children.Attributes["rel"] != null
 148:                     && Children.Attributes["rel"].Value.Equals("http://schemas.netflix.com/catalog/people.directors",
 149:                         StringComparison.CurrentCultureIgnoreCase))
 150:                 {
 151:                     DirectorsLink = Children.Attributes["href"].Value;
 152:                 }
 153:                 else if (Children.Attributes["rel"] != null
 154:                     && Children.Attributes["rel"].Value.Equals("http://schemas.netflix.com/catalog/titles/format_availability",
 155:                         StringComparison.CurrentCultureIgnoreCase))
 156:                 {
 157:                     FormatsAvailableLink = Children.Attributes["href"].Value;
 158:                 }
 159:                 else if (Children.Attributes["rel"] != null
 160:                     && Children.Attributes["rel"].Value.Equals("http://schemas.netflix.com/catalog/titles/screen_formats",
 161:                         StringComparison.CurrentCultureIgnoreCase))
 162:                 {
 163:                     ScreenFormatsLink = Children.Attributes["href"].Value;
 164:                 }
 165:                 else if (Children.Attributes["rel"] != null
 166:                     && Children.Attributes["rel"].Value.Equals("http://schemas.netflix.com/catalog/titles/languages_and_audio",
 167:                         StringComparison.CurrentCultureIgnoreCase))
 168:                 {
 169:                     LanguagesLink = Children.Attributes["href"].Value;
 170:                 }
 171:                 else if (Children.Attributes["rel"] != null
 172:                     && Children.Attributes["rel"].Value.Equals("http://schemas.netflix.com/catalog/titles.similars",
 173:                         StringComparison.CurrentCultureIgnoreCase))
 174:                 {
 175:                     SimilarTitleLink = Children.Attributes["href"].Value;
 176:                 }
 177:                 else if (Children.Attributes["rel"] != null
 178:                     && Children.Attributes["rel"].Value.Equals("alternate",
 179:                         StringComparison.CurrentCultureIgnoreCase))
 180:                 {
 181:                      this.NetflixWebPage= Children.Attributes["href"].Value;
 182:                 }
 183:                 else if (Children.Attributes["rel"] != null
 184:                     && Children.Attributes["rel"].Value.Equals("http://schemas.netflix.com/catalog/titles.series",
 185:                         StringComparison.CurrentCultureIgnoreCase))
 186:                 {
 187:                     this.SeriesLink = Children.Attributes["href"].Value;
 188:                 }
 189:                 else if (Children.Attributes["rel"] != null
 190:                     && Children.Attributes["rel"].Value.Equals("http://schemas.netflix.com/catalog/programs",
 191:                         StringComparison.CurrentCultureIgnoreCase))
 192:                 {
 193:                     this.EpisodeLink = Children.Attributes["href"].Value;
 194:                 }
 195:                 else if (Children.Attributes["rel"] != null
 196:                     && Children.Attributes["rel"].Value.Equals("http://schemas.netflix.com/catalog/titles/official_url",
 197:                         StringComparison.CurrentCultureIgnoreCase))
 198:                 {
 199:                     this.StudioWebPage = Children.Attributes["href"].Value;
 200:                 }
 201:             }
 202:         }
 203:     }
 204:  
 205:     #endregion
 206:  
 207:     #region Properties
 208:  
 209:     /// <summary>
 210:     /// Title ID
 211:     /// </summary>
 212:     public string ID { get; set; }
 213:  
 214:     /// <summary>
 215:     /// Title information
 216:     /// </summary>
 217:     public TitleInformation TitleInformation { get; set; }
 218:  
 219:     /// <summary>
 220:     /// Box artwork
 221:     /// </summary>
 222:     public BoxArt BoxArt { get; set; }
 223:  
 224:     /// <summary>
 225:     /// Synopsis link
 226:     /// </summary>
 227:     public string SynopsisLink { get; set; }
 228:  
 229:     /// <summary>
 230:     /// Year released
 231:     /// </summary>
 232:     public string ReleaseYear { get; set; }
 233:  
 234:     /// <summary>
 235:     /// Average rating
 236:     /// </summary>
 237:     public string Rating { get; set; }
 238:  
 239:     /// <summary>
 240:     /// list of categories
 241:     /// </summary>
 242:     public List<string> Categories { get; set; }
 243:  
 244:     /// <summary>
 245:     /// Run time in seconds
 246:     /// </summary>
 247:     public int RunTime { get; set; }
 248:  
 249:     /// <summary>
 250:     /// Link to cast information
 251:     /// </summary>
 252:     public string CastLink { get; set; }
 253:  
 254:     /// <summary>
 255:     /// Link to directories information
 256:     /// </summary>
 257:     public string DirectorsLink { get; set; }
 258:  
 259:     /// <summary>
 260:     /// Formats available link
 261:     /// </summary>
 262:     public string FormatsAvailableLink { get; set; }
 263:  
 264:     /// <summary>
 265:     /// Screen formats available link
 266:     /// </summary>
 267:     public string ScreenFormatsLink { get; set; }
 268:  
 269:     /// <summary>
 270:     /// Language information link
 271:     /// </summary>
 272:     public string LanguagesLink { get; set; }
 273:  
 274:     /// <summary>
 275:     /// Average rating
 276:     /// </summary>
 277:     public double AverageRating { get; set; }
 278:  
 279:     /// <summary>
 280:     /// Similar titles to this one link
 281:     /// </summary>
 282:     public string SimilarTitleLink { get; set; }
 283:  
 284:     /// <summary>
 285:     /// Link to netflix webpage
 286:     /// </summary>
 287:     public string NetflixWebPage { get; set; }
 288:  
 289:     /// <summary>
 290:     /// Link to studio web page
 291:     /// </summary>
 292:     public string StudioWebPage { get; set; }
 293:  
 294:     /// <summary>
 295:     /// Link to awards
 296:     /// </summary>
 297:     public string Awards { get; set; }
 298:  
 299:     /// <summary>
 300:     /// Link to bonus material
 301:     /// </summary>
 302:     public string BonusMaterial { get; set; }
 303:  
 304:     /// <summary>
 305:     /// Link for series info
 306:     /// </summary>
 307:     public string SeriesLink { get; set; }
 308:  
 309:     /// <summary>
 310:     /// Link for episodes
 311:     /// </summary>
 312:     public string EpisodeLink { get; set; }
 313:  
 314:     /// <summary>
 315:     /// Season info link
 316:     /// </summary>
 317:     public string SeasonLink { get; set; }
 318:  
 319:     #endregion
 320: } 
 321:  
 322:   
 323:  
 324: /// <summary>
 325: /// Title information
 326: /// </summary>
 327: public class TitleInformation
 328: {
 329:     #region Constructor
 330:  
 331:     /// <summary>
 332:     /// Constructor
 333:     /// </summary>
 334:     /// <param name="Element">Element information</param>
 335:     public TitleInformation(XmlElement Element)
 336:     {
 337:         if (Element.Attributes["short"] != null)
 338:         {
 339:             ShortName = Element.Attributes["short"].Value;
 340:         }
 341:         if (Element.Attributes["regular"] != null)
 342:         {
 343:             RegularName = Element.Attributes["regular"].Value;
 344:         }
 345:     }
 346:  
 347:     #endregion
 348:  
 349:     #region Public Properties
 350:  
 351:     /// <summary>
 352:     /// Short name
 353:     /// </summary>
 354:     public string ShortName { get; set; }
 355:  
 356:     /// <summary>
 357:     /// Regular name
 358:     /// </summary>
 359:     public string RegularName { get; set; }
 360:  
 361:     #endregion
 362: } 
 363:  
 364:   
 365:  
 366: /// <summary>
 367: /// Box art information
 368: /// </summary>
 369: public class BoxArt
 370: {
 371:     #region Constructor
 372:  
 373:     /// <summary>
 374:     /// Constructor
 375:     /// </summary>
 376:     /// <param name="Element">Element information</param>
 377:     public BoxArt(XmlElement Element)
 378:     {
 379:         if (Element.Attributes["small"] != null)
 380:         {
 381:             SmallPicture = Element.Attributes["small"].Value;
 382:         }
 383:         if (Element.Attributes["medium"] != null)
 384:         {
 385:             MediumPicture = Element.Attributes["medium"].Value;
 386:         }
 387:         if (Element.Attributes["large"] != null)
 388:         {
 389:             LargePicture = Element.Attributes["large"].Value;
 390:         }
 391:     }
 392:  
 393:     #endregion
 394:  
 395:     #region Public Properties
 396:  
 397:     /// <summary>
 398:     /// Small picture
 399:     /// </summary>
 400:     public string SmallPicture { get; set; }
 401:  
 402:     /// <summary>
 403:     /// Medium picture
 404:     /// </summary>
 405:     public string MediumPicture { get; set; }
 406:  
 407:     /// <summary>
 408:     /// Large picture
 409:     /// </summary>
 410:     public string LargePicture { get; set; }
 411:  
 412:     #endregion
 413: } 
 414:  
 415:   
 416:  
 417: /// <summary>
 418: /// People list
 419: /// </summary>
 420: public class People
 421: {
 422:     #region Constructor
 423:  
 424:     /// <summary>
 425:     /// Constructor
 426:     /// </summary>
 427:     /// <param name="XMLContent">XML content</param>
 428:     public People(string XMLContent)
 429:     {
 430:         XmlDocument Document = new XmlDocument();
 431:         Document.LoadXml(XMLContent);
 432:         PeopleList = new List<Person>();
 433:  
 434:         foreach (XmlNode Children in Document.ChildNodes)
 435:         {
 436:             if (Children.Name.Equals("people", StringComparison.CurrentCultureIgnoreCase))
 437:             {
 438:                 foreach (XmlNode Child in Children.ChildNodes)
 439:                 {
 440:                     if (Child.Name.Equals("person", StringComparison.CurrentCultureIgnoreCase))
 441:                     {
 442:                         PeopleList.Add(new Person((XmlElement)Child));
 443:                     }
 444:                     else if (Child.Name.Equals("number_of_results", StringComparison.CurrentCultureIgnoreCase))
 445:                     {
 446:                         NumberOfResults = int.Parse(Child.InnerText);
 447:                     }
 448:                     else if (Child.Name.Equals("start_index", StringComparison.CurrentCultureIgnoreCase))
 449:                     {
 450:                         StartIndex = int.Parse(Child.InnerText);
 451:                     }
 452:                     else if (Child.Name.Equals("results_per_page", StringComparison.CurrentCultureIgnoreCase))
 453:                     {
 454:                         ResultsPerPage = int.Parse(Child.InnerText);
 455:                     }
 456:                 }
 457:             }
 458:         }
 459:     }
 460:  
 461:     #endregion
 462:  
 463:     #region Properties
 464:  
 465:     /// <summary>
 466:     /// List of people
 467:     /// </summary>
 468:     public List<Person> PeopleList { get; set; }
 469:  
 470:     /// <summary>
 471:     /// Number of results
 472:     /// </summary>
 473:     public int NumberOfResults { get; set; }
 474:  
 475:     /// <summary>
 476:     /// Start index
 477:     /// </summary>
 478:     public int StartIndex { get; set; }
 479:  
 480:     /// <summary>
 481:     /// Number of results per page
 482:     /// </summary>
 483:     public int ResultsPerPage { get; set; }
 484:  
 485:     #endregion
 486: } 
 487:  
 488:   
 489:  
 490: /// <summary>
 491: /// Person class
 492: /// </summary>
 493: public class Person
 494: {
 495:     #region Constructor
 496:  
 497:     /// <summary>
 498:     /// Constructor
 499:     /// </summary>
 500:     /// <param name="Element">person node</param>
 501:     public Person(XmlElement Element)
 502:     {
 503:         foreach (XmlNode Children in Element.ChildNodes)
 504:         {
 505:             if (Children.Name.Equals("id", StringComparison.CurrentCultureIgnoreCase))
 506:             {
 507:                 ID = Children.InnerText;
 508:             }
 509:             else if (Children.Name.Equals("name", StringComparison.CurrentCultureIgnoreCase))
 510:             {
 511:                 Name = Children.InnerText;
 512:             }
 513:             else if (Children.Name.Equals("bio", StringComparison.CurrentCultureIgnoreCase))
 514:             {
 515:                 Bio = Children.InnerText;
 516:             }
 517:             else if (Children.Name.Equals("link", StringComparison.CurrentCultureIgnoreCase))
 518:             {
 519:                 if (Children.Attributes["rel"] != null
 520:                     && Children.Attributes["rel"].Value.Equals("http://schemas.netflix.com/catlog/person/filmography",
 521:                         StringComparison.CurrentCultureIgnoreCase))
 522:                 {
 523:                     Filmography = Children.Attributes["href"].Value;
 524:                 }
 525:                 else if (Children.Attributes["rel"] != null
 526:                     && Children.Attributes["rel"].Value.Equals("alternate",
 527:                         StringComparison.CurrentCultureIgnoreCase))
 528:                 {
 529:                     Webpage = Children.Attributes["href"].Value;
 530:                 }
 531:             }
 532:         }
 533:     }
 534:  
 535:     #endregion
 536:  
 537:     #region Properties
 538:  
 539:     /// <summary>
 540:     /// ID for the person
 541:     /// </summary>
 542:     public string ID { get; set; }
 543:  
 544:     /// <summary>
 545:     /// Name of the person
 546:     /// </summary>
 547:     public string Name { get; set; }
 548:  
 549:     /// <summary>
 550:     /// Bio information for the person
 551:     /// </summary>
 552:     public string Bio { get; set; }
 553:  
 554:     /// <summary>
 555:     /// Filmography link
 556:     /// </summary>
 557:     public string Filmography { get; set; }
 558:  
 559:     /// <summary>
 560:     /// Webpage link
 561:     /// </summary>
 562:     public string Webpage { get; set; }
 563:  
 564:     #endregion
 565: }

I realize that's a lot of code, but all it does is takes the XML that is returned and parses it out so that we can use the information. Most of the items are going to be rather obvious with one exception. Netflix doesn't return information directly in a normal search (well they can but it requires an extra couple of parameters). Instead what they do is they return a link that can be used for another query that will return that information. The reason for this is to save space. For instance, you may do a query and have 1000+ items returned. To save space, it's easier if you return the link instead of the whole description for a movie (or the cast, with all of their information, etc.).

So when you call TitleSearch, you're getting the basic information for the items and links to everything else that you would want. Which is why there are functions for things link pulling cast information, formats that it's available in, etc. As I said, we can actually have Netflix expand that information for us but once again I'd just go with the multiple queries unless it's information you know that you will need. Anyway, hopefully the code is of use to someone out there. So try it out, leave feedback, and happy coding.



Comments