Exemple #1
0
    protected override async Task <Either <BaseError, MediaItemScanResult <PlexMovie> > > UpdateMetadata(
        MediaItemScanResult <PlexMovie> result,
        MovieMetadata fullMetadata)
    {
        PlexMovie     existing         = result.Item;
        MovieMetadata existingMetadata = existing.MovieMetadata.Head();

        _logger.LogDebug(
            "Refreshing {Attribute} for {Title}",
            "Plex Metadata",
            existingMetadata.Title);

        if (existingMetadata.MetadataKind != MetadataKind.External)
        {
            existingMetadata.MetadataKind = MetadataKind.External;
            await _metadataRepository.MarkAsExternal(existingMetadata);
        }

        if (existingMetadata.ContentRating != fullMetadata.ContentRating)
        {
            existingMetadata.ContentRating = fullMetadata.ContentRating;
            await _metadataRepository.SetContentRating(existingMetadata, fullMetadata.ContentRating);

            result.IsUpdated = true;
        }

        foreach (Genre genre in existingMetadata.Genres
                 .Filter(g => fullMetadata.Genres.All(g2 => g2.Name != g.Name))
                 .ToList())
        {
            existingMetadata.Genres.Remove(genre);
            if (await _metadataRepository.RemoveGenre(genre))
            {
                result.IsUpdated = true;
            }
        }

        foreach (Genre genre in fullMetadata.Genres
                 .Filter(g => existingMetadata.Genres.All(g2 => g2.Name != g.Name))
                 .ToList())
        {
            existingMetadata.Genres.Add(genre);
            if (await _movieRepository.AddGenre(existingMetadata, genre))
            {
                result.IsUpdated = true;
            }
        }

        foreach (Studio studio in existingMetadata.Studios
                 .Filter(s => fullMetadata.Studios.All(s2 => s2.Name != s.Name))
                 .ToList())
        {
            existingMetadata.Studios.Remove(studio);
            if (await _metadataRepository.RemoveStudio(studio))
            {
                result.IsUpdated = true;
            }
        }

        foreach (Studio studio in fullMetadata.Studios
                 .Filter(s => existingMetadata.Studios.All(s2 => s2.Name != s.Name))
                 .ToList())
        {
            existingMetadata.Studios.Add(studio);
            if (await _movieRepository.AddStudio(existingMetadata, studio))
            {
                result.IsUpdated = true;
            }
        }

        foreach (Actor actor in existingMetadata.Actors
                 .Filter(
                     a => fullMetadata.Actors.All(
                         a2 => a2.Name != a.Name || a.Artwork == null && a2.Artwork != null))
                 .ToList())
        {
            existingMetadata.Actors.Remove(actor);
            if (await _metadataRepository.RemoveActor(actor))
            {
                result.IsUpdated = true;
            }
        }

        foreach (Actor actor in fullMetadata.Actors
                 .Filter(a => existingMetadata.Actors.All(a2 => a2.Name != a.Name))
                 .ToList())
        {
            existingMetadata.Actors.Add(actor);
            if (await _movieRepository.AddActor(existingMetadata, actor))
            {
                result.IsUpdated = true;
            }
        }

        foreach (Director director in existingMetadata.Directors
                 .Filter(g => fullMetadata.Directors.All(g2 => g2.Name != g.Name))
                 .ToList())
        {
            existingMetadata.Directors.Remove(director);
            if (await _metadataRepository.RemoveDirector(director))
            {
                result.IsUpdated = true;
            }
        }

        foreach (Director director in fullMetadata.Directors
                 .Filter(g => existingMetadata.Directors.All(g2 => g2.Name != g.Name))
                 .ToList())
        {
            existingMetadata.Directors.Add(director);
            if (await _movieRepository.AddDirector(existingMetadata, director))
            {
                result.IsUpdated = true;
            }
        }

        foreach (Writer writer in existingMetadata.Writers
                 .Filter(g => fullMetadata.Writers.All(g2 => g2.Name != g.Name))
                 .ToList())
        {
            existingMetadata.Writers.Remove(writer);
            if (await _metadataRepository.RemoveWriter(writer))
            {
                result.IsUpdated = true;
            }
        }

        foreach (Writer writer in fullMetadata.Writers
                 .Filter(g => existingMetadata.Writers.All(g2 => g2.Name != g.Name))
                 .ToList())
        {
            existingMetadata.Writers.Add(writer);
            if (await _movieRepository.AddWriter(existingMetadata, writer))
            {
                result.IsUpdated = true;
            }
        }

        foreach (MetadataGuid guid in existingMetadata.Guids
                 .Filter(g => fullMetadata.Guids.All(g2 => g2.Guid != g.Guid))
                 .ToList())
        {
            existingMetadata.Guids.Remove(guid);
            if (await _metadataRepository.RemoveGuid(guid))
            {
                result.IsUpdated = true;
            }
        }

        foreach (MetadataGuid guid in fullMetadata.Guids
                 .Filter(g => existingMetadata.Guids.All(g2 => g2.Guid != g.Guid))
                 .ToList())
        {
            existingMetadata.Guids.Add(guid);
            if (await _metadataRepository.AddGuid(existingMetadata, guid))
            {
                result.IsUpdated = true;
            }
        }

        foreach (Tag tag in existingMetadata.Tags
                 .Filter(g => fullMetadata.Tags.All(g2 => g2.Name != g.Name))
                 .ToList())
        {
            existingMetadata.Tags.Remove(tag);
            if (await _metadataRepository.RemoveTag(tag))
            {
                result.IsUpdated = true;
            }
        }

        foreach (Tag tag in fullMetadata.Tags
                 .Filter(g => existingMetadata.Tags.All(g2 => g2.Name != g.Name))
                 .ToList())
        {
            existingMetadata.Tags.Add(tag);
            if (await _movieRepository.AddTag(existingMetadata, tag))
            {
                result.IsUpdated = true;
            }
        }

        if (fullMetadata.SortTitle != existingMetadata.SortTitle)
        {
            existingMetadata.SortTitle = fullMetadata.SortTitle;
            if (await _movieRepository.UpdateSortTitle(existingMetadata))
            {
                result.IsUpdated = true;
            }
        }

        bool poster = await UpdateArtworkIfNeeded(existingMetadata, fullMetadata, ArtworkKind.Poster);

        bool fanArt = await UpdateArtworkIfNeeded(existingMetadata, fullMetadata, ArtworkKind.FanArt);

        if (poster || fanArt)
        {
            result.IsUpdated = true;
        }

        if (result.IsUpdated)
        {
            await _metadataRepository.MarkAsUpdated(existingMetadata, fullMetadata.DateUpdated);
        }

        return(result);
    }