示例#1
0
        /// <summary>
        /// Tries to lookup the Movie online and downloads images.
        /// </summary>
        /// <param name="movieInfo">Movie to check</param>
        /// <returns><c>true</c> if successful</returns>
        public virtual async Task <bool> FindAndUpdateMovieAsync(MovieInfo movieInfo)
        {
            try
            {
                // Try online lookup
                if (!await InitAsync().ConfigureAwait(false))
                {
                    return(false);
                }

                MovieInfo movieMatch = null;
                string    movieId    = null;
                bool      matchFound = false;
                TLang     language   = FindBestMatchingLanguage(movieInfo.Languages);

                if (GetMovieId(movieInfo, out movieId))
                {
                    // Prefer memory cache
                    CheckCacheAndRefresh();
                    if (_memoryCache.TryGetValue(movieId, out movieMatch))
                    {
                        matchFound = true;
                    }
                }

                if (!matchFound)
                {
                    MovieMatch match = GetStroredMatch(movieInfo);
                    movieMatch = movieInfo.Clone();
                    if (string.IsNullOrEmpty(movieId))
                    {
                        Logger.Debug(_id + ": Try to lookup movie \"{0}\" from cache: {1}", movieInfo, match != null && !string.IsNullOrEmpty(match.Id));

                        if (match != null)
                        {
                            if (SetMovieId(movieMatch, match.Id))
                            {
                                //If Id was found in cache the online movie info is probably also in the cache
                                if (await _wrapper.UpdateFromOnlineMovieAsync(movieMatch, language, true).ConfigureAwait(false))
                                {
                                    Logger.Debug(_id + ": Found movie {0} in cache", movieInfo.ToString());
                                    matchFound = true;
                                }
                            }
                            else if (string.IsNullOrEmpty(movieId))
                            {
                                //Match was found but with invalid Id probably to avoid a retry
                                //No Id is available so online search will probably fail again
                                return(false);
                            }
                        }
                    }
                    else
                    {
                        if (match != null && movieId != match.Id)
                        {
                            //Id was changed so remove it so it can be updated
                            _storage.TryRemoveMatch(match);
                        }
                    }

                    if (!matchFound)
                    {
                        Logger.Debug(_id + ": Search for movie {0} online", movieInfo.ToString());

                        //Try to update movie information from online source if online Ids are present
                        if (!await _wrapper.UpdateFromOnlineMovieAsync(movieMatch, language, false).ConfigureAwait(false))
                        {
                            //Search for the movie online and update the Ids if a match is found
                            if (await _wrapper.SearchMovieUniqueAndUpdateAsync(movieMatch, language).ConfigureAwait(false))
                            {
                                //Ids were updated now try to update movie information from online source
                                if (await _wrapper.UpdateFromOnlineMovieAsync(movieMatch, language, false).ConfigureAwait(false))
                                {
                                    matchFound = true;
                                }
                            }
                        }
                        else
                        {
                            matchFound = true;
                        }
                    }
                }

                //Always save match even if none to avoid retries
                StoreMovieMatch(movieInfo, movieMatch);

                if (matchFound)
                {
                    movieInfo.MergeWith(movieMatch, true);

                    //Store person matches
                    foreach (PersonInfo person in movieInfo.Actors)
                    {
                        string id;
                        if (GetPersonId(person, out id))
                        {
                            _actorMatcher.StoreNameMatch(id, person.Name, person.Name);
                        }
                    }
                    foreach (PersonInfo person in movieInfo.Directors)
                    {
                        string id;
                        if (GetPersonId(person, out id))
                        {
                            _directorMatcher.StoreNameMatch(id, person.Name, person.Name);
                        }
                    }
                    foreach (PersonInfo person in movieInfo.Writers)
                    {
                        string id;
                        if (GetPersonId(person, out id))
                        {
                            _writerMatcher.StoreNameMatch(id, person.Name, person.Name);
                        }
                    }

                    //Store character matches
                    foreach (CharacterInfo character in movieInfo.Characters)
                    {
                        string id;
                        if (GetCharacterId(character, out id))
                        {
                            _characterMatcher.StoreNameMatch(id, character.Name, character.Name);
                        }
                    }

                    //Store company matches
                    foreach (CompanyInfo company in movieInfo.ProductionCompanies)
                    {
                        string id;
                        if (GetCompanyId(company, out id))
                        {
                            _companyMatcher.StoreNameMatch(id, company.Name, company.Name);
                        }
                    }

                    if (GetMovieId(movieInfo, out movieId))
                    {
                        _memoryCache.TryAdd(movieId, movieInfo);
                    }

                    return(true);
                }

                return(false);
            }
            catch (Exception ex)
            {
                Logger.Debug(_id + ": Exception while processing movie {0}", ex, movieInfo.ToString());
                return(false);
            }
        }