From b827f4058da428e5688b249ba1e7fff3c91f83a7 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 1 Dec 2013 13:57:52 -0500 Subject: [PATCH] improve movie matching by looking for titles with same year first --- .../Movies/MovieDbProvider.cs | 91 +++++++++++++------ 1 file changed, 63 insertions(+), 28 deletions(-) diff --git a/MediaBrowser.Providers/Movies/MovieDbProvider.cs b/MediaBrowser.Providers/Movies/MovieDbProvider.cs index b4903ba69f..fe409c0900 100644 --- a/MediaBrowser.Providers/Movies/MovieDbProvider.cs +++ b/MediaBrowser.Providers/Movies/MovieDbProvider.cs @@ -441,38 +441,73 @@ namespace MediaBrowser.Providers.Movies if (searchResult != null) { - foreach (var possible in searchResult.results) - { - string matchedName = possible.title ?? possible.name; - string id = possible.id.ToString(CultureInfo.InvariantCulture); - - if (matchedName != null) - { - Logger.Debug("Match " + matchedName + " for " + name); - if (year != null) - { - DateTime r; - - //These dates are always in this exact format - if (DateTime.TryParseExact(possible.release_date, "yyyy-MM-dd", EnUs, DateTimeStyles.None, out r)) - { - if (Math.Abs(r.Year - year.Value) > 1) // allow a 1 year tolerance on release date - { - Logger.Debug("Result " + matchedName + " released on " + r + " did not match year " + year); - continue; - } - } - } - //matched name and year - return id; - } - - } + return FindIdOfBestResult(searchResult.results, name, year); } return null; } + private string FindIdOfBestResult(List results, string name, int? year) + { + if (year.HasValue) + { + // Take the first result from the same year + var id = results.Where(i => + { + // Make sure it has a name + if (!string.IsNullOrEmpty(i.title ?? i.name)) + { + DateTime r; + + // These dates are always in this exact format + if (DateTime.TryParseExact(i.release_date, "yyyy-MM-dd", EnUs, DateTimeStyles.None, out r)) + { + return r.Year == year.Value; + } + } + + return false; + }) + .Select(i => i.id.ToString(CultureInfo.InvariantCulture)) + .FirstOrDefault(); + + if (!string.IsNullOrEmpty(id)) + { + return id; + } + + // Take the first result within one year + id = results.Where(i => + { + // Make sure it has a name + if (!string.IsNullOrEmpty(i.title ?? i.name)) + { + DateTime r; + + // These dates are always in this exact format + if (DateTime.TryParseExact(i.release_date, "yyyy-MM-dd", EnUs, DateTimeStyles.None, out r)) + { + return Math.Abs(r.Year - year.Value) <= 1; + } + } + + return false; + }) + .Select(i => i.id.ToString(CultureInfo.InvariantCulture)) + .FirstOrDefault(); + + if (!string.IsNullOrEmpty(id)) + { + return id; + } + } + + // Just take the first one + return results.Where(i => !string.IsNullOrEmpty(i.title ?? i.name)) + .Select(i => i.id.ToString(CultureInfo.InvariantCulture)) + .FirstOrDefault(); + } + /// /// URLs the encode. /// @@ -601,7 +636,7 @@ namespace MediaBrowser.Providers.Movies return path; } - + internal string GetImagesDataFilePath(BaseItem item) { var path = GetDataFilePath(item);