public async Task <bool> UpdateCollectionAsync(MovieCollectionInfo collectionInfo, bool updateMovieList)
        {
            bool success = false;

            foreach (IMovieMatcher matcher in MOVIE_MATCHERS.Where(m => m.Enabled))
            {
                success |= await matcher.UpdateCollectionAsync(collectionInfo, updateMovieList).ConfigureAwait(false);
            }

            if (updateMovieList)
            {
                if (collectionInfo.Movies.Count == 0)
                {
                    return(false);
                }

                for (int i = 0; i < collectionInfo.Movies.Count; i++)
                {
                    //MovieInfo movieInfo = collectionInfo.Movies[i];
                    //foreach (IMovieMatcher matcher in MOVIE_MATCHERS.Where(m => m.Enabled))
                    //{
                    //  success |= matcher.FindAndUpdateMovie(movieInfo, importOnly);
                    //}
                }
            }
            return(success);
        }
Пример #2
0
        public bool UpdateCollection(MovieCollectionInfo collectionInfo, bool updateMovieList, bool importOnly)
        {
            bool success = false;

            foreach (IMovieMatcher matcher in MOVIE_MATCHERS.OrderByDescending(m => m.Primary).Where(m => m.Enabled))
            {
                success |= matcher.UpdateCollection(collectionInfo, updateMovieList, importOnly);
            }

            if (updateMovieList)
            {
                if (collectionInfo.Movies.Count == 0)
                {
                    return(false);
                }

                for (int i = 0; i < collectionInfo.Movies.Count; i++)
                {
                    //MovieInfo movieInfo = collectionInfo.Movies[i];
                    //foreach (IMovieMatcher matcher in MOVIE_MATCHERS.OrderByDescending(m => m.Primary).Where(m => m.Enabled))
                    //{
                    //  success |= matcher.FindAndUpdateMovie(movieInfo, importOnly);
                    //}
                }
            }
            return(success);
        }
        public void CacheExtractedItem(Guid extractedItemId, IDictionary <Guid, IList <MediaItemAspect> > extractedAspects)
        {
            MovieCollectionInfo collection = new MovieCollectionInfo();

            collection.FromMetadata(extractedAspects);
            AddToCache(extractedItemId, collection, false);
        }
Пример #4
0
        public virtual async Task <bool> UpdateCollectionAsync(MovieCollectionInfo movieCollectionInfo, bool updateMovieList)
        {
            try
            {
                // Try online lookup
                if (!await InitAsync().ConfigureAwait(false))
                {
                    return(false);
                }

                TLang language = FindBestMatchingLanguage(movieCollectionInfo.Languages);
                bool  updated  = false;
                MovieCollectionInfo movieCollectionMatch = movieCollectionInfo.Clone();
                movieCollectionMatch.Movies.Clear();
                //Try updating from cache
                if (!await _wrapper.UpdateFromOnlineMovieCollectionAsync(movieCollectionMatch, language, true).ConfigureAwait(false))
                {
                    Logger.Debug(_id + ": Search for collection {0} online", movieCollectionInfo.ToString());

                    //Try to update movie collection information from online source
                    if (await _wrapper.UpdateFromOnlineMovieCollectionAsync(movieCollectionMatch, language, false).ConfigureAwait(false))
                    {
                        updated = true;
                    }
                }
                else
                {
                    Logger.Debug(_id + ": Found collection {0} in cache", movieCollectionInfo.ToString());
                    updated = true;
                }

                if (updated)
                {
                    movieCollectionInfo.MergeWith(movieCollectionMatch, true, updateMovieList);

                    if (updateMovieList)
                    {
                        foreach (MovieInfo movie in movieCollectionMatch.Movies)
                        {
                            IGenreConverter converter = ServiceRegistration.Get <IGenreConverter>();
                            foreach (var genre in movie.Genres)
                            {
                                if (!genre.Id.HasValue && converter.GetGenreId(genre.Name, GenreCategory.Movie, null, out int genreId))
                                {
                                    genre.Id = genreId;
                                }
                            }
                        }
                    }
                }

                return(updated);
            }
            catch (Exception ex)
            {
                Logger.Debug(_id + ": Exception while processing collection {0}", ex, movieCollectionInfo.ToString());
                return(false);
            }
        }
 protected override bool SetMovieCollectionId(MovieCollectionInfo movieCollection, string id)
 {
     if (!string.IsNullOrEmpty(id))
     {
         movieCollection.MovieDbId = Convert.ToInt32(id);
         return(true);
     }
     return(false);
 }
 protected override bool GetMovieCollectionId(MovieCollectionInfo movieCollection, out string id)
 {
     id = null;
     if (movieCollection.MovieDbId > 0)
     {
         id = movieCollection.MovieDbId.ToString();
     }
     return(id != null);
 }
Пример #7
0
        public override void Update(MediaItem mediaItem)
        {
            base.Update(mediaItem);
            if (mediaItem == null)
            {
                return;
            }

            MovieCollectionInfo movieCollection = new MovieCollectionInfo();

            if (!movieCollection.FromMetadata(mediaItem.Aspects))
            {
                return;
            }

            CollectionName = movieCollection.CollectionName.Text ?? "";

            int?count;

            if (mediaItem.Aspects.ContainsKey(MovieCollectionAspect.ASPECT_ID))
            {
                if (MediaItemAspect.TryGetAttribute(mediaItem.Aspects, MovieCollectionAspect.ATTR_AVAILABLE_MOVIES, out count))
                {
                    AvailableMovies = count.Value.ToString();
                }
                else
                {
                    AvailableMovies = "";
                }

                if (MediaItemAspect.TryGetAttribute(mediaItem.Aspects, MovieCollectionAspect.ATTR_NUM_MOVIES, out count))
                {
                    TotalMovies = count.Value.ToString();
                }
                else
                {
                    TotalMovies = "";
                }

                if (VirtualMediaHelper.ShowVirtualMovieMedia)
                {
                    Movies = TotalMovies;
                }
                else
                {
                    Movies = AvailableMovies;
                }
            }

            FireChange();
        }
        public static MovieCollectionInfo AsMovieCollection(this BaseInfo info)
        {
            MovieCollectionInfo collection = info as MovieCollectionInfo;

            if (collection != null)
            {
                return(collection);
            }
            MovieInfo movie = info as MovieInfo;

            if (movie != null)
            {
                return(movie.CloneBasicInstance <MovieCollectionInfo>());
            }
            return(null);
        }
        public bool TryExtractRelationships(IDictionary <Guid, IList <MediaItemAspect> > aspects, bool importOnly, out IList <RelationshipItem> extractedLinkedAspects)
        {
            extractedLinkedAspects = null;

            if (!importOnly) //Only during import
            {
                return(false);
            }

            MovieInfo movieInfo = new MovieInfo();

            if (!movieInfo.FromMetadata(aspects))
            {
                return(false);
            }

            MovieCollectionInfo collectionInfo = movieInfo.CloneBasicInstance <MovieCollectionInfo>();

            if (collectionInfo.CollectionName.IsEmpty || collectionInfo.HasExternalId)
            {
                return(false);
            }

            extractedLinkedAspects = new List <RelationshipItem>();

            IDictionary <Guid, IList <MediaItemAspect> > collectionAspects = new Dictionary <Guid, IList <MediaItemAspect> >();

            //Create custom collection
            collectionInfo.AssignNameId();
            collectionInfo.SetMetadata(collectionAspects);

            bool movieVirtual = true;

            if (MediaItemAspect.TryGetAttribute(aspects, MediaAspect.ATTR_ISVIRTUAL, false, out movieVirtual))
            {
                MediaItemAspect.SetAttribute(collectionAspects, MediaAspect.ATTR_ISVIRTUAL, movieVirtual);
            }

            if (collectionAspects.ContainsKey(ExternalIdentifierAspect.ASPECT_ID))
            {
                extractedLinkedAspects.Add(new RelationshipItem(collectionAspects, Guid.Empty));
            }

            return(extractedLinkedAspects.Count > 0);
        }
Пример #10
0
        public List <MovieCollectionInfo> GetLastChangedMovieCollections()
        {
            List <MovieCollectionInfo> collections = new List <MovieCollectionInfo>();

            if (!InitAsync().Result)
            {
                return(collections);
            }

            foreach (string id in _config.LastUpdatedMovieCollections)
            {
                MovieCollectionInfo c = new MovieCollectionInfo();
                if (SetMovieCollectionId(c, id) && !collections.Contains(c))
                {
                    collections.Add(c);
                }
            }
            return(collections);
        }
Пример #11
0
        public async Task <bool> TryExtractRelationshipsAsync(IResourceAccessor mediaItemAccessor, IDictionary <Guid, IList <MediaItemAspect> > aspects, IList <IDictionary <Guid, IList <MediaItemAspect> > > extractedLinkedAspects)
        {
            MovieInfo movieInfo = new MovieInfo();

            if (!movieInfo.FromMetadata(aspects))
            {
                return(false);
            }

            MovieCollectionInfo collectionInfo = RelationshipExtractorUtils.TryCreateInfoFromLinkedAspects(extractedLinkedAspects, out List <MovieCollectionInfo> collection) ?
                                                 collection[0] : movieInfo.CloneBasicInstance <MovieCollectionInfo>();

            if (!MovieMetadataExtractor.SkipOnlineSearches && collectionInfo.HasExternalId)
            {
                await OnlineMatcherService.Instance.UpdateCollectionAsync(collectionInfo, false).ConfigureAwait(false);
            }

            IDictionary <Guid, IList <MediaItemAspect> > collectionAspects = collectionInfo.LinkedAspects != null ?
                                                                             collectionInfo.LinkedAspects : new Dictionary <Guid, IList <MediaItemAspect> >();

            collectionInfo.SetMetadata(collectionAspects);

            bool movieVirtual = true;

            if (MediaItemAspect.TryGetAttribute(aspects, MediaAspect.ATTR_ISVIRTUAL, false, out movieVirtual))
            {
                MediaItemAspect.SetAttribute(collectionAspects, MediaAspect.ATTR_ISVIRTUAL, movieVirtual);
            }

            if (!collectionAspects.ContainsKey(ExternalIdentifierAspect.ASPECT_ID))
            {
                return(false);
            }

            if (collectionInfo.LinkedAspects == null)
            {
                extractedLinkedAspects.Add(collectionAspects);
            }
            return(extractedLinkedAspects.Count > 0);
        }
Пример #12
0
        public List <MovieCollectionInfo> GetMovieCollection(ObjectId userId)
        {
            var movieCollectionInfo = new List <MovieCollectionInfo>();

            List <MovieCollection> movieCollection = _movieCollections.Find(collection => collection.UserId == userId).ToList();

            foreach (var item in movieCollection)
            {
                var movie = _movies.Find(x => x.ImdbId == item.MovieId).SingleOrDefault();
                if (movie != null)
                {
                    var newMovieInfo = new MovieCollectionInfo()
                    {
                        Movie           = movie,
                        MovieCollection = item
                    };
                    movieCollectionInfo.Add(newMovieInfo);
                }
            }

            return(movieCollectionInfo);
        }
Пример #13
0
        public List <MovieCollectionInfo> GetFacebookFriendsMovieCollection(long facebookId)
        {
            var movieCollectionInfo = new List <MovieCollectionInfo>();
            var user = _users.Find(x => x.FacebookId == facebookId).SingleOrDefault();

            List <MovieCollection> movieCollection = _movieCollections.Find(collection => collection.UserId == user.Id).ToList();

            foreach (var item in movieCollection)
            {
                var movie = _movies.Find(x => x.ImdbId == item.MovieId).SingleOrDefault();
                if (movie != null)
                {
                    var newMovieInfo = new MovieCollectionInfo()
                    {
                        Movie           = movie,
                        MovieCollection = item
                    };
                    movieCollectionInfo.Add(newMovieInfo);
                }
            }

            return(movieCollectionInfo);
        }
Пример #14
0
        public async Task <bool> TryExtractRelationshipsAsync(IResourceAccessor mediaItemAccessor, IDictionary <Guid, IList <MediaItemAspect> > aspects, IList <IDictionary <Guid, IList <MediaItemAspect> > > extractedLinkedAspects)
        {
            if (MovieMetadataExtractor.OnlyLocalMedia)
            {
                return(false);
            }

            MovieCollectionInfo collectionInfo = new MovieCollectionInfo();

            if (!collectionInfo.FromMetadata(aspects))
            {
                return(false);
            }

            if (!MovieMetadataExtractor.SkipOnlineSearches && collectionInfo.HasExternalId)
            {
                await OnlineMatcherService.Instance.UpdateCollectionAsync(collectionInfo, true, _category).ConfigureAwait(false);
            }

            for (int i = 0; i < collectionInfo.Movies.Count; i++)
            {
                MovieInfo movieInfo = collectionInfo.Movies[i];
                movieInfo.CollectionNameId = collectionInfo.NameId;

                IDictionary <Guid, IList <MediaItemAspect> > movieAspects = new Dictionary <Guid, IList <MediaItemAspect> >();
                if (movieInfo.SetMetadata(movieAspects))
                {
                    MediaItemAspect.SetAttribute(movieAspects, MediaAspect.ATTR_ISVIRTUAL, true);
                    if (movieAspects.ContainsKey(ExternalIdentifierAspect.ASPECT_ID))
                    {
                        extractedLinkedAspects.Add(movieAspects);
                    }
                }
            }
            return(extractedLinkedAspects.Count > 0);
        }
Пример #15
0
 protected virtual bool SetMovieCollectionId(MovieCollectionInfo movieCollection, string id)
 {
     return(false);
 }
Пример #16
0
 protected virtual bool GetMovieCollectionId(MovieCollectionInfo movieCollection, out string id)
 {
     id = null;
     return(false);
 }
 public override bool UpdateCollection(MovieCollectionInfo movieCollectionInfo, bool updateMovieList, bool importOnly)
 {
     return(false);
 }
Пример #18
0
 public override Task <bool> UpdateCollectionAsync(MovieCollectionInfo movieCollectionInfo, bool updateMovieList)
 {
     return(Task.FromResult(false));
 }
        public bool TryExtractRelationships(IDictionary <Guid, IList <MediaItemAspect> > aspects, bool importOnly, out IList <RelationshipItem> extractedLinkedAspects)
        {
            extractedLinkedAspects = null;

            MovieInfo movieInfo = new MovieInfo();

            if (!movieInfo.FromMetadata(aspects))
            {
                return(false);
            }

            MovieCollectionInfo cachedCollection;
            Guid collectionId;
            MovieCollectionInfo collectionInfo = movieInfo.CloneBasicInstance <MovieCollectionInfo>();

            if (TryGetInfoFromCache(collectionInfo, out cachedCollection, out collectionId))
            {
                collectionInfo = cachedCollection;
            }
            else if (!MovieMetadataExtractor.SkipOnlineSearches && collectionInfo.HasExternalId)
            {
                OnlineMatcherService.Instance.UpdateCollection(collectionInfo, false, false);
            }

            bool hasId = false;

            if (!MovieMetadataExtractor.SkipOnlineSearches)
            {
                hasId = collectionInfo.HasExternalId || !string.IsNullOrEmpty(movieInfo.CollectionNameId);
            }
            else
            {
                hasId = !string.IsNullOrEmpty(movieInfo.CollectionNameId);
            }

            if (!BaseInfo.HasRelationship(aspects, LinkedRole) && hasId)
            {
                collectionInfo.HasChanged = true; //Force save if no relationship exists
            }
            if (!collectionInfo.HasChanged && !importOnly)
            {
                return(false);
            }

            extractedLinkedAspects = new List <RelationshipItem>();

            IDictionary <Guid, IList <MediaItemAspect> > collectionAspects = new Dictionary <Guid, IList <MediaItemAspect> >();

            if (collectionId != Guid.Empty)
            {
                collectionInfo.SetMetadata(collectionAspects);

                bool movieVirtual = true;
                if (MediaItemAspect.TryGetAttribute(aspects, MediaAspect.ATTR_ISVIRTUAL, false, out movieVirtual))
                {
                    MediaItemAspect.SetAttribute(collectionAspects, MediaAspect.ATTR_ISVIRTUAL, movieVirtual);
                }

                if (collectionAspects.ContainsKey(ExternalIdentifierAspect.ASPECT_ID))
                {
                    extractedLinkedAspects.Add(new RelationshipItem(collectionAspects, collectionId));
                }
            }
            else
            {
                //Create custom collection
                if (!string.IsNullOrEmpty(movieInfo.CollectionNameId) && !collectionInfo.HasExternalId)
                {
                    collectionInfo            = movieInfo.CloneBasicInstance <MovieCollectionInfo>();
                    collectionInfo.HasChanged = true;
                }
                collectionInfo.SetMetadata(collectionAspects);

                bool movieVirtual = true;
                if (MediaItemAspect.TryGetAttribute(aspects, MediaAspect.ATTR_ISVIRTUAL, false, out movieVirtual))
                {
                    MediaItemAspect.SetAttribute(collectionAspects, MediaAspect.ATTR_ISVIRTUAL, movieVirtual);
                }

                if (collectionAspects.ContainsKey(ExternalIdentifierAspect.ASPECT_ID))
                {
                    extractedLinkedAspects.Add(new RelationshipItem(collectionAspects, Guid.Empty));
                }
            }

            return(extractedLinkedAspects.Count > 0);
        }
Пример #20
0
        protected async Task ExtractMovieFanArt(Guid mediaItemId, IDictionary <Guid, IList <MediaItemAspect> > aspects)
        {
            bool             shouldCacheLocal = false;
            IResourceLocator mediaItemLocator = null;

            if (!BaseInfo.IsVirtualResource(aspects))
            {
                mediaItemLocator = GetResourceLocator(aspects);
                if (mediaItemLocator == null)
                {
                    return;
                }

                //Whether local fanart should be stored in the fanart cache
                shouldCacheLocal = ShouldCacheLocalFanArt(mediaItemLocator.NativeResourcePath,
                                                          MovieMetadataExtractor.CacheLocalFanArt, MovieMetadataExtractor.CacheOfflineFanArt);
            }

            if (!shouldCacheLocal && MovieMetadataExtractor.SkipFanArtDownload)
            {
                return; //Nothing to do
            }
            MovieInfo movieInfo = new MovieInfo();

            movieInfo.FromMetadata(aspects);

            //Movie fanart
            if (AddToCache(mediaItemId))
            {
                //Actor fanart may be stored in the movie directory, so get the actors now
                IList <Tuple <Guid, string> > actors = null;
                if (MediaItemAspect.TryGetAspect(aspects, VideoAspect.Metadata, out SingleMediaItemAspect videoAspect))
                {
                    var actorNames = videoAspect.GetCollectionAttribute <string>(VideoAspect.ATTR_ACTORS);
                    if (actorNames != null)
                    {
                        RelationshipExtractorUtils.TryGetMappedLinkedIds(PersonAspect.ROLE_ACTOR, aspects, actorNames.ToList(), out actors);
                    }
                }
                if (shouldCacheLocal)
                {
                    await ExtractMovieFolderFanArt(mediaItemLocator, mediaItemId, movieInfo.ToString(), actors).ConfigureAwait(false);
                }
                if (!MovieMetadataExtractor.SkipFanArtDownload)
                {
                    await OnlineMatcherService.Instance.DownloadMovieFanArtAsync(mediaItemId, movieInfo).ConfigureAwait(false);
                }
            }

            //Collection fanart
            if (RelationshipExtractorUtils.TryGetLinkedId(MovieCollectionAspect.ROLE_MOVIE_COLLECTION, aspects, out Guid collectionMediaItemId) &&
                AddToCache(collectionMediaItemId))
            {
                MovieCollectionInfo collectionInfo = movieInfo.CloneBasicInstance <MovieCollectionInfo>();
                if (shouldCacheLocal)
                {
                    await ExtractCollectionFolderFanArt(mediaItemLocator, collectionMediaItemId, collectionInfo.ToString()).ConfigureAwait(false);
                }
                if (!MovieMetadataExtractor.SkipFanArtDownload)
                {
                    await OnlineMatcherService.Instance.DownloadMovieFanArtAsync(collectionMediaItemId, collectionInfo).ConfigureAwait(false);
                }
            }
        }
Пример #21
0
        protected override bool TryGetFanArtInfo(BaseInfo info, out TLang language, out string fanArtMediaType, out bool includeThumbnails)
        {
            language          = default(TLang);
            fanArtMediaType   = null;
            includeThumbnails = true;

            MovieInfo movieInfo = info as MovieInfo;

            if (movieInfo != null)
            {
                language          = FindBestMatchingLanguage(movieInfo.Languages);
                fanArtMediaType   = FanArtMediaTypes.Movie;
                includeThumbnails = false;
                return(true);
            }

            MovieCollectionInfo movieCollectionInfo = info as MovieCollectionInfo;

            if (movieCollectionInfo != null)
            {
                language        = FindBestMatchingLanguage(movieCollectionInfo.Languages);
                fanArtMediaType = FanArtMediaTypes.MovieCollection;
                return(true);
            }

            if (OnlyBasicFanArt)
            {
                return(false);
            }

            CompanyInfo companyInfo = info as CompanyInfo;

            if (companyInfo != null)
            {
                language        = FindMatchingLanguage(string.Empty);
                fanArtMediaType = FanArtMediaTypes.Company;
                return(true);
            }

            CharacterInfo characterInfo = info as CharacterInfo;

            if (characterInfo != null)
            {
                language        = FindMatchingLanguage(string.Empty);
                fanArtMediaType = FanArtMediaTypes.Character;
                return(true);
            }

            PersonInfo personInfo = info as PersonInfo;

            if (personInfo != null)
            {
                if (personInfo.Occupation == PersonAspect.OCCUPATION_ACTOR)
                {
                    fanArtMediaType = FanArtMediaTypes.Actor;
                }
                else if (personInfo.Occupation == PersonAspect.OCCUPATION_DIRECTOR)
                {
                    fanArtMediaType = FanArtMediaTypes.Director;
                }
                else if (personInfo.Occupation == PersonAspect.OCCUPATION_WRITER)
                {
                    fanArtMediaType = FanArtMediaTypes.Writer;
                }
                else
                {
                    return(false);
                }
                language = FindMatchingLanguage(string.Empty);
                return(true);
            }
            return(false);
        }
        public bool TryExtractRelationships(IDictionary <Guid, IList <MediaItemAspect> > aspects, bool importOnly, out IList <RelationshipItem> extractedLinkedAspects)
        {
            extractedLinkedAspects = null;

            if (importOnly)
            {
                return(false);
            }

            if (MovieMetadataExtractor.OnlyLocalMedia)
            {
                return(false);
            }

            MovieCollectionInfo collectionInfo = new MovieCollectionInfo();

            if (!collectionInfo.FromMetadata(aspects))
            {
                return(false);
            }

            if (CheckCacheContains(collectionInfo))
            {
                return(false);
            }

            if (!MovieMetadataExtractor.SkipOnlineSearches && collectionInfo.HasExternalId)
            {
                OnlineMatcherService.Instance.UpdateCollection(collectionInfo, true, importOnly);
            }

            if (collectionInfo.Movies.Count == 0)
            {
                return(false);
            }

            if (BaseInfo.CountRelationships(aspects, LinkedRole) < collectionInfo.Movies.Count)
            {
                collectionInfo.HasChanged = true; //Force save for new movies
            }
            else
            {
                return(false);
            }

            if (!collectionInfo.HasChanged)
            {
                return(false);
            }

            AddToCheckCache(collectionInfo);

            extractedLinkedAspects = new List <RelationshipItem>();
            for (int i = 0; i < collectionInfo.Movies.Count; i++)
            {
                MovieInfo movieInfo = collectionInfo.Movies[i];
                movieInfo.CollectionNameId = collectionInfo.NameId;

                IDictionary <Guid, IList <MediaItemAspect> > movieAspects = new Dictionary <Guid, IList <MediaItemAspect> >();
                movieInfo.SetMetadata(movieAspects);
                MediaItemAspect.SetAttribute(movieAspects, MediaAspect.ATTR_ISVIRTUAL, true);

                if (movieAspects.ContainsKey(ExternalIdentifierAspect.ASPECT_ID))
                {
                    extractedLinkedAspects.Add(new RelationshipItem(movieAspects, Guid.Empty));
                }
            }
            return(extractedLinkedAspects.Count > 0);
        }
Пример #23
0
        protected async Task ExtractMovieFanArt(Guid mediaItemId, IDictionary <Guid, IList <MediaItemAspect> > aspects)
        {
            bool             shouldCacheLocal = false;
            IResourceLocator mediaItemLocator = null;

            if (!BaseInfo.IsVirtualResource(aspects))
            {
                mediaItemLocator = GetResourceLocator(aspects);

                //Whether local fanart should be stored in the fanart cache
                shouldCacheLocal = ShouldCacheLocalFanArt(mediaItemLocator.NativeResourcePath,
                                                          MovieMetadataExtractor.CacheLocalFanArt, MovieMetadataExtractor.CacheOfflineFanArt);
            }

            if (mediaItemLocator == null)
            {
                return;
            }

            if (!shouldCacheLocal && MovieMetadataExtractor.SkipFanArtDownload)
            {
                return; //Nothing to do
            }
            MovieInfo movieInfo = new MovieInfo();

            movieInfo.FromMetadata(aspects);

            //Movie fanart
            if (AddToCache(mediaItemId))
            {
                //Actor fanart may be stored in the movie directory, so get the actors now
                IList <Tuple <Guid, string> > actors = null;
                if (MediaItemAspect.TryGetAspect(aspects, VideoAspect.Metadata, out SingleMediaItemAspect videoAspect))
                {
                    var actorNames = videoAspect.GetCollectionAttribute <string>(VideoAspect.ATTR_ACTORS);
                    if (actorNames != null)
                    {
                        RelationshipExtractorUtils.TryGetMappedLinkedIds(PersonAspect.ROLE_ACTOR, aspects, actorNames.ToList(), out actors);
                    }
                }
                if (shouldCacheLocal)
                {
                    await ExtractMovieFolderFanArt(mediaItemLocator, mediaItemId, movieInfo.ToString(), actors).ConfigureAwait(false);
                }
                if (!MovieMetadataExtractor.SkipFanArtDownload)
                {
                    await OnlineMatcherService.Instance.DownloadMovieFanArtAsync(mediaItemId, movieInfo).ConfigureAwait(false);
                }

                //Find central actor information folder
                var          seriesDirectory        = ResourcePathHelper.Combine(mediaItemLocator.NativeResourcePath, "../../");
                ResourcePath centralActorFolderPath = LocalFanartHelper.GetCentralPersonFolder(seriesDirectory, CentralPersonFolderType.MovieActors);
                if (shouldCacheLocal && centralActorFolderPath != null && actors != null)
                {
                    foreach (var actor in actors)
                    {
                        // Check if we already processed this actor
                        if (!AddToCache(actor.Item1))
                        {
                            continue;
                        }

                        // First get the ResourcePath of the central directory
                        var artistFolderPath = ResourcePathHelper.Combine(centralActorFolderPath, $"{LocalFanartHelper.GetSafePersonFolderName(actor.Item2)}/");
                        using (IResourceAccessor accessor = new ResourceLocator(mediaItemLocator.NativeSystemId, artistFolderPath).CreateAccessor())
                        {
                            if (accessor is IFileSystemResourceAccessor directoryAccessor)
                            {
                                FanArtPathCollection paths = new FanArtPathCollection();
                                List <ResourcePath>  potentialFanArtFiles = LocalFanartHelper.GetPotentialFanArtFiles(directoryAccessor);
                                ExtractAllFanArtImages(potentialFanArtFiles, paths);
                                await SaveFolderImagesToCache(mediaItemLocator.NativeSystemId, paths, actor.Item1, actor.Item2).ConfigureAwait(false);
                            }
                        }
                    }
                }
            }

            //Collection fanart
            if (RelationshipExtractorUtils.TryGetLinkedId(MovieCollectionAspect.ROLE_MOVIE_COLLECTION, aspects, out Guid collectionMediaItemId) &&
                AddToCache(collectionMediaItemId))
            {
                MovieCollectionInfo collectionInfo = movieInfo.CloneBasicInstance <MovieCollectionInfo>();
                if (shouldCacheLocal)
                {
                    await ExtractCollectionFolderFanArt(mediaItemLocator, collectionMediaItemId, collectionInfo.ToString()).ConfigureAwait(false);
                }
                if (!MovieMetadataExtractor.SkipFanArtDownload)
                {
                    await OnlineMatcherService.Instance.DownloadMovieFanArtAsync(collectionMediaItemId, collectionInfo).ConfigureAwait(false);
                }
            }
        }
Пример #24
0
        private bool ExtractMovieData(ILocalFsResourceAccessor lfsra, IDictionary <Guid, IList <MediaItemAspect> > extractedAspectData, bool importOnly)
        {
            // Calling EnsureLocalFileSystemAccess not necessary; only string operation
            string[] pathsToTest = new[] { lfsra.LocalFileSystemPath, lfsra.CanonicalLocalResourcePath.ToString() };
            string   title       = null;
            string   sortTitle   = null;

            // VideoAspect must be present to be sure it is actually a video resource.
            if (!extractedAspectData.ContainsKey(VideoStreamAspect.ASPECT_ID) && !extractedAspectData.ContainsKey(SubtitleAspect.ASPECT_ID))
            {
                return(false);
            }

            if (extractedAspectData.ContainsKey(SubtitleAspect.ASPECT_ID) && !importOnly)
            {
                return(false); //Subtitles can only be imported not refreshed
            }
            bool refresh = false;

            if (extractedAspectData.ContainsKey(MovieAspect.ASPECT_ID))
            {
                refresh = true;
            }

            MovieInfo movieInfo = new MovieInfo();

            if (refresh)
            {
                movieInfo.FromMetadata(extractedAspectData);
            }

            if (movieInfo.MovieName.IsEmpty)
            {
                //Try to get title
                if (MediaItemAspect.TryGetAttribute(extractedAspectData, MediaAspect.ATTR_TITLE, out title) &&
                    !string.IsNullOrEmpty(title) && !lfsra.ResourceName.StartsWith(title, StringComparison.InvariantCultureIgnoreCase))
                {
                    movieInfo.MovieName = title;
                    /* Clear the names from unwanted strings */
                    MovieNameMatcher.CleanupTitle(movieInfo);
                }
            }
            if (movieInfo.MovieNameSort.IsEmpty)
            {
                //Try to get sort title
                if (MediaItemAspect.TryGetAttribute(extractedAspectData, MediaAspect.ATTR_SORT_TITLE, out sortTitle) && !string.IsNullOrEmpty(sortTitle))
                {
                    movieInfo.MovieNameSort = sortTitle;
                }
            }

            if (movieInfo.MovieDbId == 0)
            {
                try
                {
                    // Try to use an existing TMDB id for exact mapping
                    string tmdbId;
                    if (MatroskaMatcher.TryMatchTmdbId(lfsra, out tmdbId))
                    {
                        movieInfo.MovieDbId = Convert.ToInt32(tmdbId);
                    }
                }
                catch (Exception ex)
                {
                    ServiceRegistration.Get <ILogger>().Debug("MoviesMetadataExtractor: Exception reading TMDB ID for '{0}'", ex, lfsra.CanonicalLocalResourcePath);
                }
            }

            if (string.IsNullOrEmpty(movieInfo.ImdbId))
            {
                try
                {
                    // Try to use an existing IMDB id for exact mapping
                    string imdbId = null;
                    if (pathsToTest.Any(path => MatroskaMatcher.TryMatchImdbId(lfsra, out imdbId)))
                    {
                        movieInfo.ImdbId = imdbId;
                    }
                    else if (pathsToTest.Any(path => ImdbIdMatcher.TryMatchImdbId(path, out imdbId)))
                    {
                        movieInfo.ImdbId = imdbId;
                    }
                }
                catch (Exception ex)
                {
                    ServiceRegistration.Get <ILogger>().Debug("MoviesMetadataExtractor: Exception reading IMDB ID for '{0}'", ex, lfsra.CanonicalLocalResourcePath);
                }
            }

            if (!movieInfo.IsBaseInfoPresent)
            {
                // Also test the full path year. This is useful if the path contains the real name and year.
                foreach (string path in pathsToTest)
                {
                    if (MovieNameMatcher.MatchTitleYear(path, movieInfo))
                    {
                        break;
                    }
                }
                //Fall back to MediaAspect.ATTR_TITLE
                if (movieInfo.MovieName.IsEmpty && !string.IsNullOrEmpty(title))
                {
                    movieInfo.MovieName = title;
                }

                /* Clear the names from unwanted strings */
                MovieNameMatcher.CleanupTitle(movieInfo);
            }

            if (!movieInfo.ReleaseDate.HasValue && !movieInfo.HasExternalId)
            {
                // 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.ReleaseDate = recordingDate;
                }
            }

            // Allow the online lookup to choose best matching language for metadata
            if (movieInfo.Languages.Count == 0)
            {
                IList <MultipleMediaItemAspect> audioAspects;
                if (MediaItemAspect.TryGetAspects(extractedAspectData, VideoAudioStreamAspect.Metadata, out audioAspects))
                {
                    foreach (MultipleMediaItemAspect aspect in audioAspects)
                    {
                        string language = (string)aspect.GetAttributeValue(VideoAudioStreamAspect.ATTR_AUDIOLANGUAGE);
                        if (!string.IsNullOrEmpty(language) && !movieInfo.Languages.Contains(language))
                        {
                            movieInfo.Languages.Add(language);
                        }
                    }
                }
            }

            if (importOnly)
            {
                try
                {
                    MatroskaMatcher.ExtractFromTags(lfsra, movieInfo);
                    MP4Matcher.ExtractFromTags(lfsra, movieInfo);
                }
                catch (Exception ex)
                {
                    ServiceRegistration.Get <ILogger>().Debug("MoviesMetadataExtractor: Exception reading tags for '{0}'", ex, lfsra.CanonicalLocalResourcePath);
                }
            }

            if (SkipOnlineSearches && !SkipFanArtDownload)
            {
                MovieInfo tempInfo = movieInfo.Clone();
                OnlineMatcherService.Instance.FindAndUpdateMovie(tempInfo, importOnly);
                movieInfo.CopyIdsFrom(tempInfo);
                movieInfo.HasChanged = tempInfo.HasChanged;
            }
            else if (!SkipOnlineSearches)
            {
                OnlineMatcherService.Instance.FindAndUpdateMovie(movieInfo, importOnly);
            }

            //Send it to the videos section
            if (!SkipOnlineSearches && !movieInfo.HasExternalId)
            {
                return(false);
            }

            if (importOnly)
            {
                //Create custom collection (overrides online collection)
                MovieCollectionInfo collectionInfo = movieInfo.CloneBasicInstance <MovieCollectionInfo>();
                string collectionName;
                if (string.IsNullOrEmpty(collectionInfo.NameId) && CollectionFolderHasFanArt(lfsra, out collectionName))
                {
                    collectionInfo = new MovieCollectionInfo();
                    collectionInfo.CollectionName = collectionName;
                    if (!collectionInfo.CollectionName.IsEmpty)
                    {
                        movieInfo.CollectionName = collectionInfo.CollectionName;
                        movieInfo.CopyIdsFrom(collectionInfo); //Reset ID's
                        movieInfo.HasChanged = true;
                    }
                }
            }
            movieInfo.AssignNameId();

            if (refresh)
            {
                if ((IncludeActorDetails && !BaseInfo.HasRelationship(extractedAspectData, PersonAspect.ROLE_ACTOR) && movieInfo.Actors.Count > 0) ||
                    (IncludeCharacterDetails && !BaseInfo.HasRelationship(extractedAspectData, CharacterAspect.ROLE_CHARACTER) && movieInfo.Characters.Count > 0) ||
                    (IncludeDirectorDetails && !BaseInfo.HasRelationship(extractedAspectData, PersonAspect.ROLE_DIRECTOR) && movieInfo.Directors.Count > 0) ||
                    (IncludeWriterDetails && !BaseInfo.HasRelationship(extractedAspectData, PersonAspect.ROLE_WRITER) && movieInfo.Writers.Count > 0) ||
                    (IncludeProductionCompanyDetails && !BaseInfo.HasRelationship(extractedAspectData, CompanyAspect.ROLE_COMPANY) && movieInfo.ProductionCompanies.Count > 0))
                {
                    movieInfo.HasChanged = true;
                }
            }

            if (!movieInfo.HasChanged && !importOnly)
            {
                return(false);
            }

            movieInfo.SetMetadata(extractedAspectData);

            return(movieInfo.IsBaseInfoPresent);
        }
Пример #25
0
        private void ExtractFanArt(Guid mediaItemId, IDictionary <Guid, IList <MediaItemAspect> > aspects, Guid?collectionMediaItemId, IDictionary <Guid, string> actorMediaItems)
        {
            if (aspects.ContainsKey(MovieAspect.ASPECT_ID))
            {
                if (BaseInfo.IsVirtualResource(aspects))
                {
                    return;
                }

                MovieInfo movieInfo = new MovieInfo();
                movieInfo.FromMetadata(aspects);
                bool forceFanart = !movieInfo.IsRefreshed;
                MovieCollectionInfo collectionInfo = movieInfo.CloneBasicInstance <MovieCollectionInfo>();
                ExtractLocalImages(aspects, mediaItemId, collectionMediaItemId, movieInfo.ToString(), collectionInfo.ToString(), actorMediaItems);
                if (!MovieMetadataExtractor.SkipFanArtDownload)
                {
                    OnlineMatcherService.Instance.DownloadMovieFanArt(mediaItemId, movieInfo, forceFanart);
                }

                //Take advantage of the movie language being known and download collection too
                if (collectionMediaItemId.HasValue && !_checkCache.Contains(collectionMediaItemId.Value))
                {
                    if (!MovieMetadataExtractor.SkipFanArtDownload)
                    {
                        OnlineMatcherService.Instance.DownloadMovieFanArt(collectionMediaItemId.Value, collectionInfo, forceFanart);
                    }
                    _checkCache.Add(collectionMediaItemId.Value);
                }
            }
            else if (aspects.ContainsKey(PersonAspect.ASPECT_ID))
            {
                PersonInfo personInfo = new PersonInfo();
                personInfo.FromMetadata(aspects);
                if (personInfo.Occupation == PersonAspect.OCCUPATION_ACTOR || personInfo.Occupation == PersonAspect.OCCUPATION_DIRECTOR ||
                    personInfo.Occupation == PersonAspect.OCCUPATION_WRITER)
                {
                    if (!MovieMetadataExtractor.SkipFanArtDownload)
                    {
                        OnlineMatcherService.Instance.DownloadMovieFanArt(mediaItemId, personInfo, !personInfo.IsRefreshed);
                    }
                }
            }
            else if (aspects.ContainsKey(CharacterAspect.ASPECT_ID))
            {
                CharacterInfo characterInfo = new CharacterInfo();
                characterInfo.FromMetadata(aspects);
                if (!MovieMetadataExtractor.SkipFanArtDownload)
                {
                    OnlineMatcherService.Instance.DownloadMovieFanArt(mediaItemId, characterInfo, !characterInfo.IsRefreshed);
                }
            }
            else if (aspects.ContainsKey(CompanyAspect.ASPECT_ID))
            {
                CompanyInfo companyInfo = new CompanyInfo();
                companyInfo.FromMetadata(aspects);
                if (companyInfo.Type == CompanyAspect.COMPANY_PRODUCTION)
                {
                    if (!MovieMetadataExtractor.SkipFanArtDownload)
                    {
                        OnlineMatcherService.Instance.DownloadMovieFanArt(mediaItemId, companyInfo, !companyInfo.IsRefreshed);
                    }
                }
            }
        }
Пример #26
0
        private async Task <bool> ExtractMovieData(ILocalFsResourceAccessor lfsra, IDictionary <Guid, IList <MediaItemAspect> > extractedAspectData)
        {
            // VideoAspect must be present to be sure it is actually a video resource.
            if (!extractedAspectData.ContainsKey(VideoAspect.ASPECT_ID) && !extractedAspectData.ContainsKey(SubtitleAspect.ASPECT_ID))
            {
                return(false);
            }

            // Calling EnsureLocalFileSystemAccess not necessary; only string operation
            string[] pathsToTest = new[] { lfsra.LocalFileSystemPath, lfsra.CanonicalLocalResourcePath.ToString() };
            string   title       = null;
            string   sortTitle   = null;
            bool     isReimport  = extractedAspectData.ContainsKey(ReimportAspect.ASPECT_ID);

            MovieInfo movieInfo = new MovieInfo();

            if (extractedAspectData.ContainsKey(MovieAspect.ASPECT_ID))
            {
                movieInfo.FromMetadata(extractedAspectData);
            }

            if (movieInfo.MovieName.IsEmpty)
            {
                //Try to get title
                if (MediaItemAspect.TryGetAttribute(extractedAspectData, MediaAspect.ATTR_TITLE, out title) &&
                    !string.IsNullOrEmpty(title) && !lfsra.ResourceName.StartsWith(title, StringComparison.InvariantCultureIgnoreCase))
                {
                    //The title may still contain tags and other noise, try and parse it for a title and year.
                    MovieNameMatcher.MatchTitleYear(title, movieInfo);
                }
            }
            if (movieInfo.MovieNameSort.IsEmpty)
            {
                //Try to get sort title
                if (MediaItemAspect.TryGetAttribute(extractedAspectData, MediaAspect.ATTR_SORT_TITLE, out sortTitle) && !string.IsNullOrEmpty(sortTitle))
                {
                    movieInfo.MovieNameSort = sortTitle;
                }
            }

            if (!isReimport) //Ignore tags or file based information for reimport because they might be the cause of the wrong import
            {
                if (movieInfo.MovieDbId == 0)
                {
                    try
                    {
                        // Try to use an existing TMDB id for exact mapping
                        string tmdbId = await MatroskaMatcher.TryMatchTmdbIdAsync(lfsra).ConfigureAwait(false);

                        if (!string.IsNullOrEmpty(tmdbId))
                        {
                            movieInfo.MovieDbId = Convert.ToInt32(tmdbId);
                        }
                    }
                    catch (Exception ex)
                    {
                        ServiceRegistration.Get <ILogger>().Debug("MoviesMetadataExtractor: Exception reading TMDB ID for '{0}'", ex, lfsra.CanonicalLocalResourcePath);
                    }
                }

                if (string.IsNullOrEmpty(movieInfo.ImdbId))
                {
                    try
                    {
                        // Try to use an existing IMDB id for exact mapping
                        string imdbId = await MatroskaMatcher.TryMatchImdbIdAsync(lfsra).ConfigureAwait(false);

                        if (!string.IsNullOrEmpty(imdbId))
                        {
                            movieInfo.ImdbId = imdbId;
                        }
                        else if (pathsToTest.Any(path => ImdbIdMatcher.TryMatchImdbId(path, out imdbId)))
                        {
                            movieInfo.ImdbId = imdbId;
                        }
                    }
                    catch (Exception ex)
                    {
                        ServiceRegistration.Get <ILogger>().Debug("MoviesMetadataExtractor: Exception reading IMDB ID for '{0}'", ex, lfsra.CanonicalLocalResourcePath);
                    }
                }

                if (!movieInfo.IsBaseInfoPresent || !movieInfo.ReleaseDate.HasValue)
                {
                    // Also test the full path year. This is useful if the path contains the real name and year.
                    foreach (string path in pathsToTest)
                    {
                        if (MovieNameMatcher.MatchTitleYear(path, movieInfo))
                        {
                            break;
                        }
                    }
                    //Fall back to MediaAspect.ATTR_TITLE
                    if (movieInfo.MovieName.IsEmpty && !string.IsNullOrEmpty(title))
                    {
                        movieInfo.MovieName = title;
                    }

                    /* Clear the names from unwanted strings */
                    MovieNameMatcher.CleanupTitle(movieInfo);
                }

                if (!movieInfo.ReleaseDate.HasValue && !movieInfo.HasExternalId)
                {
                    // 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.ReleaseDate = recordingDate;
                    }
                }

                try
                {
                    await MatroskaMatcher.ExtractFromTagsAsync(lfsra, movieInfo).ConfigureAwait(false);

                    MP4Matcher.ExtractFromTags(lfsra, movieInfo);
                }
                catch (Exception ex)
                {
                    ServiceRegistration.Get <ILogger>().Debug("MoviesMetadataExtractor: Exception reading tags for '{0}'", ex, lfsra.CanonicalLocalResourcePath);
                }
            }

            // Allow the online lookup to choose best matching language for metadata
            if (movieInfo.Languages.Count == 0)
            {
                IList <MultipleMediaItemAspect> audioAspects;
                if (MediaItemAspect.TryGetAspects(extractedAspectData, VideoAudioStreamAspect.Metadata, out audioAspects))
                {
                    foreach (MultipleMediaItemAspect aspect in audioAspects)
                    {
                        string language = (string)aspect.GetAttributeValue(VideoAudioStreamAspect.ATTR_AUDIOLANGUAGE);
                        if (!string.IsNullOrEmpty(language) && !movieInfo.Languages.Contains(language))
                        {
                            movieInfo.Languages.Add(language);
                        }
                    }
                }
            }

            if (SkipOnlineSearches && !SkipFanArtDownload)
            {
                MovieInfo tempInfo = movieInfo.Clone();
                if (await OnlineMatcherService.Instance.FindAndUpdateMovieAsync(tempInfo).ConfigureAwait(false))
                {
                    movieInfo.CopyIdsFrom(tempInfo);
                    movieInfo.HasChanged = tempInfo.HasChanged;
                }
            }
            else if (!SkipOnlineSearches)
            {
                await OnlineMatcherService.Instance.FindAndUpdateMovieAsync(movieInfo).ConfigureAwait(false);
            }

            //Asign genre ids
            if (movieInfo.Genres.Count > 0)
            {
                IGenreConverter converter = ServiceRegistration.Get <IGenreConverter>();
                foreach (var genre in movieInfo.Genres)
                {
                    if (!genre.Id.HasValue && converter.GetGenreId(genre.Name, GenreCategory.Movie, null, out int genreId))
                    {
                        genre.Id             = genreId;
                        movieInfo.HasChanged = true;
                    }
                }
            }

            //Send it to the videos section
            if (!SkipOnlineSearches && !movieInfo.HasExternalId)
            {
                return(false);
            }

            //Create custom collection (overrides online collection)
            MovieCollectionInfo collectionInfo = movieInfo.CloneBasicInstance <MovieCollectionInfo>();
            string collectionName;

            if (string.IsNullOrEmpty(collectionInfo.NameId) && CollectionFolderHasFanArt(lfsra, out collectionName))
            {
                collectionInfo = new MovieCollectionInfo();
                collectionInfo.CollectionName = collectionName;
                if (!collectionInfo.CollectionName.IsEmpty)
                {
                    movieInfo.CollectionName = collectionInfo.CollectionName;
                    movieInfo.CopyIdsFrom(collectionInfo); //Reset ID's
                    movieInfo.HasChanged = true;
                }
            }

            if (movieInfo.MovieNameSort.IsEmpty)
            {
                if (!movieInfo.CollectionName.IsEmpty && movieInfo.ReleaseDate.HasValue)
                {
                    movieInfo.MovieNameSort = $"{movieInfo.CollectionName.Text} {movieInfo.ReleaseDate.Value.Year}-{movieInfo.ReleaseDate.Value.Month.ToString("00")}";
                }
                else if (!movieInfo.MovieName.IsEmpty)
                {
                    movieInfo.MovieNameSort = BaseInfo.GetSortTitle(movieInfo.MovieName.Text);
                }
                else
                {
                    movieInfo.MovieNameSort = BaseInfo.GetSortTitle(title);
                }
            }
            movieInfo.SetMetadata(extractedAspectData);

            return(movieInfo.IsBaseInfoPresent);
        }