Пример #1
0
        /// <summary>
        /// Resolves alternative versions and extras from list of video files.
        /// </summary>
        /// <param name="files">List of related video files.</param>
        /// <param name="supportMultiVersion">Indication we should consider multi-versions of content.</param>
        /// <returns>Returns enumerable of <see cref="VideoInfo"/> which groups files together when related.</returns>
        public IEnumerable <VideoInfo> Resolve(List <FileSystemMetadata> files, bool supportMultiVersion = true)
        {
            var videoResolver = new VideoResolver(_options);

            var videoInfos = files
                             .Select(i => videoResolver.Resolve(i.FullName, i.IsDirectory))
                             .OfType <VideoFileInfo>()
                             .ToList();

            // Filter out all extras, otherwise they could cause stacks to not be resolved
            // See the unit test TestStackedWithTrailer
            var nonExtras = videoInfos
                            .Where(i => i.ExtraType == null)
                            .Select(i => new FileSystemMetadata {
                FullName = i.Path, IsDirectory = i.IsDirectory
            });

            var stackResult = new StackResolver(_options)
                              .Resolve(nonExtras).ToList();

            var remainingFiles = videoInfos
                                 .Where(i => !stackResult.Any(s => i.Path != null && s.ContainsFile(i.Path, i.IsDirectory)))
                                 .ToList();

            var list = new List <VideoInfo>();

            foreach (var stack in stackResult)
            {
                var info = new VideoInfo(stack.Name)
                {
                    Files = stack.Files.Select(i => videoResolver.Resolve(i, stack.IsDirectoryStack))
                            .OfType <VideoFileInfo>()
                            .ToList()
                };

                info.Year = info.Files[0].Year;

                var extraBaseNames = new List <string> {
                    stack.Name, Path.GetFileNameWithoutExtension(stack.Files[0])
                };

                var extras = GetExtras(remainingFiles, extraBaseNames);

                if (extras.Count > 0)
                {
                    remainingFiles = remainingFiles
                                     .Except(extras)
                                     .ToList();

                    info.Extras = extras;
                }

                list.Add(info);
            }

            var standaloneMedia = remainingFiles
                                  .Where(i => i.ExtraType == null)
                                  .ToList();

            foreach (var media in standaloneMedia)
            {
                var info = new VideoInfo(media.Name)
                {
                    Files = new List <VideoFileInfo> {
                        media
                    }
                };

                info.Year = info.Files[0].Year;

                var extras = GetExtras(remainingFiles, new List <string> {
                    media.FileNameWithoutExtension
                });

                remainingFiles = remainingFiles
                                 .Except(extras.Concat(new[] { media }))
                                 .ToList();

                info.Extras = extras;

                list.Add(info);
            }

            if (supportMultiVersion)
            {
                list = GetVideosGroupedByVersion(list)
                       .ToList();
            }

            // If there's only one resolved video, use the folder name as well to find extras
            if (list.Count == 1)
            {
                var info       = list[0];
                var videoPath  = list[0].Files[0].Path;
                var parentPath = Path.GetDirectoryName(videoPath);

                if (!string.IsNullOrEmpty(parentPath))
                {
                    var folderName = Path.GetFileName(parentPath);
                    if (!string.IsNullOrEmpty(folderName))
                    {
                        var extras = GetExtras(remainingFiles, new List <string> {
                            folderName
                        });

                        remainingFiles = remainingFiles
                                         .Except(extras)
                                         .ToList();

                        extras.AddRange(info.Extras);
                        info.Extras = extras;
                    }
                }

                // Add the extras that are just based on file name as well
                var extrasByFileName = remainingFiles
                                       .Where(i => i.ExtraRule != null && i.ExtraRule.RuleType == ExtraRuleType.Filename)
                                       .ToList();

                remainingFiles = remainingFiles
                                 .Except(extrasByFileName)
                                 .ToList();

                extrasByFileName.AddRange(info.Extras);
                info.Extras = extrasByFileName;
            }

            // If there's only one video, accept all trailers
            // Be lenient because people use all kinds of mishmash conventions with trailers.
            if (list.Count == 1)
            {
                var trailers = remainingFiles
                               .Where(i => i.ExtraType == ExtraType.Trailer)
                               .ToList();

                trailers.AddRange(list[0].Extras);
                list[0].Extras = trailers;

                remainingFiles = remainingFiles
                                 .Except(trailers)
                                 .ToList();
            }

            // Whatever files are left, just add them
            list.AddRange(remainingFiles.Select(i => new VideoInfo(i.Name)
            {
                Files = new List <VideoFileInfo> {
                    i
                },
                Year = i.Year
            }));

            return(list);
        }
Пример #2
0
        /// <summary>
        /// Resolves alternative versions and extras from list of video files.
        /// </summary>
        /// <param name="videoInfos">List of related video files.</param>
        /// <param name="namingOptions">The naming options.</param>
        /// <param name="supportMultiVersion">Indication we should consider multi-versions of content.</param>
        /// <param name="parseName">Whether to parse the name or use the filename.</param>
        /// <returns>Returns enumerable of <see cref="VideoInfo"/> which groups files together when related.</returns>
        public static IReadOnlyList <VideoInfo> Resolve(IReadOnlyList <VideoFileInfo> videoInfos, NamingOptions namingOptions, bool supportMultiVersion = true, bool parseName = true)
        {
            // Filter out all extras, otherwise they could cause stacks to not be resolved
            // See the unit test TestStackedWithTrailer
            var nonExtras = videoInfos
                            .Where(i => i.ExtraType == null)
                            .Select(i => new FileSystemMetadata {
                FullName = i.Path, IsDirectory = i.IsDirectory
            });

            var stackResult = StackResolver.Resolve(nonExtras, namingOptions).ToList();

            var remainingFiles  = new List <VideoFileInfo>();
            var standaloneMedia = new List <VideoFileInfo>();

            for (var i = 0; i < videoInfos.Count; i++)
            {
                var current = videoInfos[i];
                if (stackResult.Any(s => s.ContainsFile(current.Path, current.IsDirectory)))
                {
                    continue;
                }

                if (current.ExtraType == null)
                {
                    standaloneMedia.Add(current);
                }
                else
                {
                    remainingFiles.Add(current);
                }
            }

            var list = new List <VideoInfo>();

            foreach (var stack in stackResult)
            {
                var info = new VideoInfo(stack.Name)
                {
                    Files = stack.Files.Select(i => VideoResolver.Resolve(i, stack.IsDirectoryStack, namingOptions, parseName))
                            .OfType <VideoFileInfo>()
                            .ToList()
                };

                info.Year = info.Files[0].Year;
                list.Add(info);
            }

            foreach (var media in standaloneMedia)
            {
                var info = new VideoInfo(media.Name)
                {
                    Files = new[] { media }
                };

                info.Year = info.Files[0].Year;
                list.Add(info);
            }

            if (supportMultiVersion)
            {
                list = GetVideosGroupedByVersion(list, namingOptions);
            }

            // Whatever files are left, just add them
            list.AddRange(remainingFiles.Select(i => new VideoInfo(i.Name)
            {
                Files     = new[] { i },
                Year      = i.Year,
                ExtraType = i.ExtraType
            }));

            return(list);
        }