private bool LoadFromTheMovieDb(bool ForceRefresh = false) { try { Logger.Log("Loading from TheMovieDb: {0}", this.Filename); TheMovieDb.TmdbApi api = new TheMovieDb.TmdbApi("1f5e5026bd038bfcfe62261e6da0634f"); string foldername = ""; foreach (Match match in Regex.Matches(Filename, @"(?<=((\\|/)))[^\\/]+(?=(\\|/))")) foldername = match.Value; // see if there is year in the foldername string year = null; if (Regex.IsMatch(foldername, @"(?<=(\())[\d]{4}(?=(\)))")) year = Regex.Match(foldername, @"(?<=(\())[\d]{4}(?=(\)))").Value; string movieTitle = Regex.Match(foldername, @"^[^(\[]+").Value.Trim(); string matchTitle = Regex.Replace(movieTitle.ToLower(), "[^a-z0-9]", ""); string searchStr = movieTitle.Replace(" - ", " ").Replace(".", " ").Replace("_", " "); Logger.Log("Searching Str: {0}", searchStr); IEnumerable<TheMovieDb.TmdbMovie> results = api.MovieSearch(searchStr, ForceRefresh); string[] names = (from r in results select r.Name).ToArray(); TheMovieDb.TmdbMovie movie = null; foreach (TheMovieDb.TmdbMovie result in results) { Logger.Log("Search result: {0} ({1})", result.Name, result.Released); if (movie == null) movie = result; List<string> resultMatchTitles = new string[] { Regex.Replace(result.Name.ToLower(), "[^a-z0-9]", "") }.ToList(); if (!String.IsNullOrEmpty(result.AlternativeName)) resultMatchTitles.Add(Regex.Replace(result.AlternativeName.ToLower(), "[^a-z0-9]", "")); if (year != null && result.Released != null && Regex.Match(result.Released, @"[\d]{4}").Value == year) { // likely to be this if (resultMatchTitles.Contains(matchTitle)) { // exact match!!! movie = result; break; } } else if (String.IsNullOrEmpty(year) && resultMatchTitles.Contains(matchTitle)) // if year is specified, then the year MUST match { movie = result; } } if (movie != null) { Logger.Log("Found movie id: {0}", movie.Id); // get complete data movie = api.GetMovieInfo(movie.Id, ForceRefresh); MatchCollection tags = Regex.Matches(foldername, @"(?<=(\[))[^\]]+(?=(\]))"); this.Tags = new string[tags.Count]; for (int i = 0; i < tags.Count; i++) this.Tags[i] = tags[i].Value; this.Actors = (from c in movie.Cast ?? new List<TheMovieDb.TmdbCastPerson>() where new string[] { "acting", "actor", "actors" }.Contains(c.Department.ToLower()) select new KeyValuePair<string, string>(c.Name, c.Character)).ToArray(); this.Directors = (from c in movie.Cast ?? new List<TheMovieDb.TmdbCastPerson>() where new string[] { "directing", "director" }.Contains(c.Department.ToLower()) select c.Name).ToArray(); this.Writers = (from c in movie.Cast ?? new List<TheMovieDb.TmdbCastPerson>() where new string[] { "writing", "writer" }.Contains(c.Department.ToLower()) select c.Name).ToArray(); this.Genres = (from g in movie.Genres ?? new List<TheMovieDb.TmdbGenre>() select g.Name).ToArray(); this.Id = movie.ImdbId; this.Mpaa = movie.Certification; this.OriginalTitle = movie.OriginalName ?? movie.Name; //this.Outline = movie.Overview; this.Plot = movie.Overview; this.Rating = float.Parse(movie.Rating ?? "0"); int runtime = 0; if(int.TryParse(movie.Runtime, out runtime)) this.Runtime = runtime * 60; // convert to seconds this.SortTitle = movie.Name; this.TagLine = movie.Tagline; this.Title = movie.Name; this.Trailer = movie.Trailer; this.Votes = int.Parse(movie.Votes ?? "0"); if (Regex.IsMatch(movie.Released ?? "", @"[\d]{4}")) this.Year = int.Parse(Regex.Match(movie.Released, @"[\d]{4}").Value ?? "0"); if (Regex.IsMatch(movie.Released ?? "", @"[\d]{4}-[\d]{2}-[\d]{2}")) this.ReleaseDate = DateTime.ParseExact(Regex.Match(movie.Released, @"[\d]{4}-[\d]{2}-[\d]{2}").Value, "yyyy-MM-dd", new CultureInfo("en-us")); List<TheMovieDb.TmdbImage> posters = (from p in movie.Posters ?? new List<TheMovieDb.TmdbImage>() where p.ImageInfo.Size == "mid" select p).ToList(); if (posters != null && posters.Count > 0) { this.PosterUrl = posters[0].ImageInfo.Url; if (posters.Count > 1 && this.Tags != null && this.Tags.Length > 0) this.PosterUrl = posters[1].ImageInfo.Url; } // backgrounds TheMovieDb.TmdbImage[] backdrops = (from bd in movie.Backdrops ?? new List<TheMovieDb.TmdbImage>() where bd.ImageInfo.Size == "w1280" select bd).ToArray(); if (backdrops.Length > 0) { this.FanArtUrls = new string[backdrops.Length]; for (int i = 0; i < backdrops.Length; i++) this.FanArtUrls[i] = backdrops[i].ImageInfo.Url; } // movie file meta this.FileInfo = VideoFileMeta.Load(Filename); // rename it. MovieRenamer.Rename(this); return true; } } catch(Exception ex) { Logger.Log(ex.Message + Environment.NewLine + ex.StackTrace); } return false; }
private bool LoadFromTheMovieDb(bool ForceRefresh = false) { try { Logger.Log("Loading from TheMovieDb: {0}", this.Filename); TheMovieDb.TmdbApi api = new TheMovieDb.TmdbApi("1f5e5026bd038bfcfe62261e6da0634f"); string foldername = ""; foreach (Match match in Regex.Matches(Filename, @"(?<=((\\|/)))[^\\/]+(?=(\\|/))")) { foldername = match.Value; } // see if there is year in the foldername string year = null; if (Regex.IsMatch(foldername, @"(?<=(\())[\d]{4}(?=(\)))")) { year = Regex.Match(foldername, @"(?<=(\())[\d]{4}(?=(\)))").Value; } string movieTitle = Regex.Match(foldername, @"^[^(\[]+").Value.Trim(); string matchTitle = Regex.Replace(movieTitle.ToLower(), "[^a-z0-9]", ""); string searchStr = movieTitle.Replace(" - ", " ").Replace(".", " ").Replace("_", " "); Logger.Log("Searching Str: {0}", searchStr); IEnumerable <TheMovieDb.TmdbMovie> results = api.MovieSearch(searchStr, ForceRefresh); string[] names = (from r in results select r.Name).ToArray(); TheMovieDb.TmdbMovie movie = null; foreach (TheMovieDb.TmdbMovie result in results) { Logger.Log("Search result: {0} ({1})", result.Name, result.Released); if (movie == null) { movie = result; } List <string> resultMatchTitles = new string[] { Regex.Replace(result.Name.ToLower(), "[^a-z0-9]", "") }.ToList(); if (!String.IsNullOrEmpty(result.AlternativeName)) { resultMatchTitles.Add(Regex.Replace(result.AlternativeName.ToLower(), "[^a-z0-9]", "")); } if (year != null && result.Released != null && Regex.Match(result.Released, @"[\d]{4}").Value == year) { // likely to be this if (resultMatchTitles.Contains(matchTitle)) { // exact match!!! movie = result; break; } } else if (String.IsNullOrEmpty(year) && resultMatchTitles.Contains(matchTitle)) // if year is specified, then the year MUST match { movie = result; } } if (movie != null) { Logger.Log("Found movie id: {0}", movie.Id); // get complete data movie = api.GetMovieInfo(movie.Id, ForceRefresh); MatchCollection tags = Regex.Matches(foldername, @"(?<=(\[))[^\]]+(?=(\]))"); this.Tags = new string[tags.Count]; for (int i = 0; i < tags.Count; i++) { this.Tags[i] = tags[i].Value; } this.Actors = (from c in movie.Cast ?? new List <TheMovieDb.TmdbCastPerson>() where new string[] { "acting", "actor", "actors" }.Contains(c.Department.ToLower()) select new KeyValuePair <string, string>(c.Name, c.Character)).ToArray(); this.Directors = (from c in movie.Cast ?? new List <TheMovieDb.TmdbCastPerson>() where new string[] { "directing", "director" }.Contains(c.Department.ToLower()) select c.Name).ToArray(); this.Writers = (from c in movie.Cast ?? new List <TheMovieDb.TmdbCastPerson>() where new string[] { "writing", "writer" }.Contains(c.Department.ToLower()) select c.Name).ToArray(); this.Genres = (from g in movie.Genres ?? new List <TheMovieDb.TmdbGenre>() select g.Name).ToArray(); this.Id = movie.ImdbId; this.Mpaa = movie.Certification; this.OriginalTitle = movie.OriginalName ?? movie.Name; //this.Outline = movie.Overview; this.Plot = movie.Overview; this.Rating = float.Parse(movie.Rating ?? "0"); int runtime = 0; if (int.TryParse(movie.Runtime, out runtime)) { this.Runtime = runtime * 60; // convert to seconds } this.SortTitle = movie.Name; this.TagLine = movie.Tagline; this.Title = movie.Name; this.Trailer = movie.Trailer; this.Votes = int.Parse(movie.Votes ?? "0"); if (Regex.IsMatch(movie.Released ?? "", @"[\d]{4}")) { this.Year = int.Parse(Regex.Match(movie.Released, @"[\d]{4}").Value ?? "0"); } if (Regex.IsMatch(movie.Released ?? "", @"[\d]{4}-[\d]{2}-[\d]{2}")) { this.ReleaseDate = DateTime.ParseExact(Regex.Match(movie.Released, @"[\d]{4}-[\d]{2}-[\d]{2}").Value, "yyyy-MM-dd", new CultureInfo("en-us")); } List <TheMovieDb.TmdbImage> posters = (from p in movie.Posters ?? new List <TheMovieDb.TmdbImage>() where p.ImageInfo.Size == "mid" select p).ToList(); if (posters != null && posters.Count > 0) { this.PosterUrl = posters[0].ImageInfo.Url; if (posters.Count > 1 && this.Tags != null && this.Tags.Length > 0) { this.PosterUrl = posters[1].ImageInfo.Url; } } // backgrounds TheMovieDb.TmdbImage[] backdrops = (from bd in movie.Backdrops ?? new List <TheMovieDb.TmdbImage>() where bd.ImageInfo.Size == "w1280" select bd).ToArray(); if (backdrops.Length > 0) { this.FanArtUrls = new string[backdrops.Length]; for (int i = 0; i < backdrops.Length; i++) { this.FanArtUrls[i] = backdrops[i].ImageInfo.Url; } } // movie file meta this.FileInfo = VideoFileMeta.Load(Filename); // rename it. MovieRenamer.Rename(this); return(true); } } catch (Exception ex) { Logger.Log(ex.Message + Environment.NewLine + ex.StackTrace); } return(false); }