Example #1
0
        private static bool ExtractMovieData(ILocalFsResourceAccessor lfsra, IDictionary <Guid, MediaItemAspect> extractedAspectData)
        {
            string[] pathsToTest = new [] { lfsra.LocalFileSystemPath, lfsra.CanonicalLocalResourcePath.ToString() };
            string   title;

            if (MediaItemAspect.TryGetAttribute(extractedAspectData, MediaAspect.ATTR_TITLE, out title) && !string.IsNullOrEmpty(title))
            {
                MovieInfo movieInfo = new MovieInfo
                {
                    MovieName = title,
                };

                // Allow the online lookup to choose best matching language for metadata
                ICollection <string> movieLanguages;
                if (MediaItemAspect.TryGetAttribute(extractedAspectData, VideoAspect.ATTR_AUDIOLANGUAGES, out movieLanguages) && movieLanguages.Count > 0)
                {
                    movieInfo.Languages.AddRange(movieLanguages);
                }

                // Try to use an existing IMDB id for exact mapping
                string imdbId;
                if (MediaItemAspect.TryGetAttribute(extractedAspectData, MovieAspect.ATTR_IMDB_ID, out imdbId) ||
                    pathsToTest.Any(path => ImdbIdMatcher.TryMatchImdbId(path, out imdbId)) ||
                    NfoReader.TryMatchImdbId(lfsra, out imdbId))
                {
                    movieInfo.ImdbId = imdbId;
                }

                // Also test the full path year, using a dummy. This is useful if the path contains the real name and year.
                foreach (string path in pathsToTest)
                {
                    MovieInfo dummy = new MovieInfo {
                        MovieName = path
                    };
                    if (NamePreprocessor.MatchTitleYear(dummy))
                    {
                        movieInfo.MovieName = dummy.MovieName;
                        movieInfo.Year      = dummy.Year;
                        break;
                    }
                }

                // When searching movie title, the year can be relevant for multiple titles with same name but different years
                DateTime recordingDate;
                if (MediaItemAspect.TryGetAttribute(extractedAspectData, MediaAspect.ATTR_RECORDINGTIME, out recordingDate))
                {
                    movieInfo.Year = recordingDate.Year;
                }

                if (MovieTheMovieDbMatcher.Instance.FindAndUpdateMovie(movieInfo))
                {
                    movieInfo.SetMetadata(extractedAspectData);
                    return(true);
                }
            }
            return(false);
        }
        /// <summary>
        /// Tries to lookup the Movie from TheMovieDB and updates the given <paramref name="movieInfo"/> with the online information.
        /// </summary>
        /// <param name="movieInfo">Movie to check</param>
        /// <returns><c>true</c> if successful</returns>
        public bool FindAndUpdateMovie(MovieInfo movieInfo)
        {
            /* Clear the names from unwanted strings */
            NamePreprocessor.CleanupTitle(movieInfo);
            string preferredLookupLanguage = FindBestMatchingLanguage(movieInfo);
            Movie  movieDetails;

            if (
                /* Best way is to get details by an unique IMDB id */
                MatchByImdbId(movieInfo, out movieDetails) ||
                TryMatch(movieInfo.MovieName, movieInfo.Year, preferredLookupLanguage, false, out movieDetails) ||
                /* Prefer passed year, if no year given, try to process movie title and split between title and year */
                (movieInfo.Year != 0 || NamePreprocessor.MatchTitleYear(movieInfo)) && TryMatch(movieInfo.MovieName, movieInfo.Year, preferredLookupLanguage, false, out movieDetails)
                )
            {
                int movieDbId = 0;
                if (movieDetails != null)
                {
                    movieDbId              = movieDetails.Id;
                    movieInfo.MovieName    = movieDetails.Title;
                    movieInfo.OriginalName = movieDetails.OriginalTitle;
                    movieInfo.Summary      = movieDetails.Overview;
                    movieInfo.Tagline      = movieDetails.Tagline;
                    if (movieDetails.Budget.HasValue)
                    {
                        movieInfo.Budget = movieDetails.Budget.Value;
                    }
                    if (movieDetails.Revenue.HasValue)
                    {
                        movieInfo.Revenue = movieDetails.Revenue.Value;
                    }
                    if (movieDetails.Runtime.HasValue)
                    {
                        movieInfo.Runtime = movieDetails.Runtime.Value;
                    }
                    if (movieDetails.Popularity.HasValue)
                    {
                        movieInfo.Popularity = movieDetails.Popularity.Value;
                    }
                    if (movieDetails.Rating.HasValue)
                    {
                        movieInfo.TotalRating = movieDetails.Rating.Value;
                    }
                    if (movieDetails.RatingCount.HasValue)
                    {
                        movieInfo.RatingCount = movieDetails.RatingCount.Value;
                    }
                    movieInfo.ImdbId    = movieDetails.ImdbId;
                    movieInfo.MovieDbId = movieDetails.Id;
                    if (movieDetails.Genres.Count > 0)
                    {
                        movieInfo.Genres.Clear();
                        movieInfo.Genres.AddRange(movieDetails.Genres.Select(p => p.Name));
                    }

                    MovieCasts movieCasts;
                    if (_movieDb.GetMovieCast(movieDbId, out movieCasts))
                    {
                        movieInfo.Actors.Clear();
                        movieInfo.Actors.AddRange(movieCasts.Cast.Select(p => p.Name));
                        movieInfo.Directors.Clear();
                        movieInfo.Directors.AddRange(movieCasts.Crew.Where(p => p.Job == "Director").Select(p => p.Name));
                        movieInfo.Writers.Clear();
                        movieInfo.Writers.AddRange(movieCasts.Crew.Where(p => p.Job == "Author").Select(p => p.Name));
                    }
                    if (movieDetails.ReleaseDate.HasValue)
                    {
                        int year = movieDetails.ReleaseDate.Value.Year;
                        if (year > 0)
                        {
                            movieInfo.Year = year;
                        }
                    }

                    if (movieDetails.Collection != null &&
                        movieDetails.Collection.Id > 0)
                    {
                        movieInfo.CollectionMovieDbId = movieDetails.Collection.Id;
                        movieInfo.CollectionName      = movieDetails.Collection.Name;
                    }
                }

                if (movieDbId > 0)
                {
                    ScheduleDownload(movieDbId);
                }
                return(true);
            }
            return(false);
        }