/// <summary> /// Fills the local results with movies /// </summary> /// <param name="searchQuery"></param> private void SearchForMovies(string searchQuery, int maxResults) { UriBuilder uri = new UriBuilder(API_URL_SEARCH); uri.Query = "api_key=" + API_KEY + "&title=" + searchQuery; results = new List <TheMovieDbResult>(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri.Uri); // execute the request using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { // we will read data via the response stream using (Stream resStream = response.GetResponseStream()) { XmlTextReader reader = new XmlTextReader(resStream); reader.WhitespaceHandling = WhitespaceHandling.None; while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { if (reader.Name == "movie") { TheMovieDbResult title = GetTitleFromMovieNode(reader); if (title != null) { results.Add(title); } if (results.Count >= maxResults) { break; } } } } } } // load up all the titles with images foreach (TheMovieDbResult title in results) { if (title != null) { if (title.ImageUrlThumb != null) { title.Title.FrontCoverPath = title.ImageUrlThumb; } } //DownloadImage(title.Title, title.ImageUrlThumb); } }
private OMLSDKTitle GetMovieDetails(int movieId) { UriBuilder uri = new UriBuilder(API_URL_INFO); uri.Query = "api_key=" + API_KEY + "&id=" + movieId.ToString(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri.Uri); TheMovieDbResult title = null; // execute the request using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { // we will read data via the response stream using (Stream resStream = response.GetResponseStream()) { XmlTextReader reader = new XmlTextReader(resStream); reader.WhitespaceHandling = WhitespaceHandling.None; while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { if (reader.Name == "movie") { title = GetTitleFromMovieNode(reader); break; } } } } } // load up the big image if (title != null) { DownloadImage(title.Title, title.ImageUrl); } return((title != null) ? title.Title : null); }
/// <summary> /// Creates a result object from the movie result node - returns null if it's not a valid result /// </summary> /// <param name="reader"></param> /// <returns></returns> private TheMovieDbResult GetTitleFromMovieNode(XmlTextReader reader) { this.BackDrops = null; bool notMovie = false; TheMovieDbResult result = new TheMovieDbResult(); while (reader.Read()) { if (reader.Value == "Your query didn't return any results.") return null; if (reader.NodeType == XmlNodeType.Element) { switch (reader.Name) { case "id": result.Id = int.Parse(GetElementValue(reader)); break; case "title": result.Title.Name = GetElementValue(reader); break; case "type": if (!GetElementValue(reader).Equals("movie", StringComparison.OrdinalIgnoreCase)) { notMovie = true; } break; case "short_overview": result.Title.Synopsis = GetElementValue(reader); break; case "release": DateTime releaseDate; if (DateTime.TryParse(GetElementValue(reader), CultureInfo.CreateSpecificCulture("en-GB"), DateTimeStyles.None, out releaseDate)) result.Title.ReleaseDate = releaseDate; break; case "poster": if (IsAttributeValue(reader, "original")) { // only keep the first one if (result.ImageUrl != null) continue; result.ImageUrl = GetElementValue(reader); } else if (IsAttributeValue(reader, "thumb")) { // only keep the first one if (result.ImageUrlThumb != null) continue; result.ImageUrlThumb = GetElementValue(reader); } break; case "backdrop": if (IsAttributeValue(reader, "original")) { if (this.BackDrops == null) this.BackDrops = new List<string>(); this.BackDrops.Add(GetElementValue(reader)); } break; case "runtime": result.Title.Runtime = int.Parse(GetElementValue(reader)); break; case "homepage": result.Title.OfficialWebsiteURL = GetElementValue(reader); break; case "production_countries": // todo : solomon : this needs to be filled in break; case "categories": while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "category") { string genre = GetElementValue(reader); if (result.Title.Genres != null && result.Title.Genres.Contains(genre)) { // the moviedb has duplicates genres for some reason continue; } result.Title.AddGenre(genre); } else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "categories") break; } break; case "people": while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "person") { // add the actor switch (reader.GetAttribute(0)) { case "actor": result.Title.AddActingRole(GetElementValue(reader), string.Empty); break; case "director": result.Title.AddDirector(new OMLSDKPerson(GetElementValue(reader))); break; case "author": case "screenplay": case "story": result.Title.AddWriter(new OMLSDKPerson(GetElementValue(reader))); break; case "producer": result.Title.AddProducer(new OMLSDKPerson(GetElementValue(reader))); break; case "original_music_composer": // unused break; case "director_of_photography": // unused break; case "editor": // unused break; case "casting": // unused break; } } else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "people") break; } break; } } else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "movie") break; // if we're not a movie let's move on if (notMovie) break; } return (notMovie) ? null : result; }
/// <summary> /// Creates a result object from the movie result node - returns null if it's not a valid result /// </summary> /// <param name="reader"></param> /// <returns></returns> private TheMovieDbResult GetTitleFromMovieNode(XmlTextReader reader) { this.BackDrops = null; bool notMovie = false; TheMovieDbResult result = new TheMovieDbResult(); while (reader.Read()) { if (reader.Value == "Your query didn't return any results.") { return(null); } if (reader.NodeType == XmlNodeType.Element) { switch (reader.Name) { case "id": result.Id = int.Parse(GetElementValue(reader)); break; case "title": result.Title.Name = GetElementValue(reader); break; case "type": if (!GetElementValue(reader).Equals("movie", StringComparison.OrdinalIgnoreCase)) { notMovie = true; } break; case "short_overview": result.Title.Synopsis = GetElementValue(reader); break; case "release": DateTime releaseDate; if (DateTime.TryParse(GetElementValue(reader), CultureInfo.CreateSpecificCulture("en-GB"), DateTimeStyles.None, out releaseDate)) { result.Title.ReleaseDate = releaseDate; } break; case "poster": if (IsAttributeValue(reader, "original")) { // only keep the first one if (result.ImageUrl != null) { continue; } result.ImageUrl = GetElementValue(reader); } else if (IsAttributeValue(reader, "thumb")) { // only keep the first one if (result.ImageUrlThumb != null) { continue; } result.ImageUrlThumb = GetElementValue(reader); } break; case "backdrop": if (IsAttributeValue(reader, "original")) { if (this.BackDrops == null) { this.BackDrops = new List <string>(); } this.BackDrops.Add(GetElementValue(reader)); } break; case "runtime": result.Title.Runtime = int.Parse(GetElementValue(reader)); break; case "homepage": result.Title.OfficialWebsiteURL = GetElementValue(reader); break; case "production_countries": // todo : solomon : this needs to be filled in break; case "categories": while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "category") { string genre = GetElementValue(reader); if (result.Title.Genres != null && result.Title.Genres.Contains(genre)) { // the moviedb has duplicates genres for some reason continue; } result.Title.AddGenre(genre); } else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "categories") { break; } } break; case "people": while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "person") { // add the actor switch (reader.GetAttribute(0)) { case "actor": result.Title.AddActingRole(GetElementValue(reader), string.Empty); break; case "director": result.Title.AddDirector(new OMLSDKPerson(GetElementValue(reader))); break; case "author": case "screenplay": case "story": result.Title.AddWriter(new OMLSDKPerson(GetElementValue(reader))); break; case "producer": result.Title.AddProducer(new OMLSDKPerson(GetElementValue(reader))); break; case "original_music_composer": // unused break; case "director_of_photography": // unused break; case "editor": // unused break; case "casting": // unused break; } } else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "people") { break; } } break; } } else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "movie") { break; } // if we're not a movie let's move on if (notMovie) { break; } } return((notMovie) ? null : result); }