jellyfin/Emby.Naming/AudioBook/AudioBookListResolver.cs

153 lines
6.1 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
2018-09-12 10:26:21 -07:00
using System.Linq;
using Emby.Naming.Common;
using Emby.Naming.Video;
using MediaBrowser.Model.IO;
namespace Emby.Naming.AudioBook
{
2020-11-10 09:11:48 -07:00
/// <summary>
/// Class used to resolve Name, Year, alternative files and extras from stack of files.
/// </summary>
2018-09-12 10:26:21 -07:00
public class AudioBookListResolver
{
private readonly NamingOptions _options;
2020-11-10 09:11:48 -07:00
/// <summary>
/// Initializes a new instance of the <see cref="AudioBookListResolver"/> class.
/// </summary>
/// <param name="options">Naming options passed along to <see cref="AudioBookResolver"/> and <see cref="AudioBookNameParser"/>.</param>
2018-09-12 10:26:21 -07:00
public AudioBookListResolver(NamingOptions options)
{
_options = options;
}
2020-11-10 09:11:48 -07:00
/// <summary>
/// Resolves Name, Year and differentiate alternative files and extras from regular audiobook files.
/// </summary>
/// <param name="files">List of files related to audiobook.</param>
/// <returns>Returns IEnumerable of <see cref="AudioBookInfo"/>.</returns>
2019-05-10 11:37:42 -07:00
public IEnumerable<AudioBookInfo> Resolve(IEnumerable<FileSystemMetadata> files)
2018-09-12 10:26:21 -07:00
{
var audioBookResolver = new AudioBookResolver(_options);
// File with empty fullname will be sorted out here.
2018-09-12 10:26:21 -07:00
var audiobookFileInfos = files
.Select(i => audioBookResolver.Resolve(i.FullName))
2020-11-01 02:47:31 -07:00
.OfType<AudioBookFileInfo>()
2018-09-12 10:26:21 -07:00
.ToList();
var stackResult = new StackResolver(_options)
.ResolveAudioBooks(audiobookFileInfos);
2018-09-12 10:26:21 -07:00
2020-01-22 14:18:56 -07:00
foreach (var stack in stackResult)
2018-09-12 10:26:21 -07:00
{
var stackFiles = stack.Files
.Select(i => audioBookResolver.Resolve(i))
.OfType<AudioBookFileInfo>()
.ToList();
2018-09-12 10:26:21 -07:00
stackFiles.Sort();
var nameParserResult = new AudioBookNameParser(_options).Parse(stack.Name);
FindExtraAndAlternativeFiles(ref stackFiles, out var extras, out var alternativeVersions, nameParserResult);
var info = new AudioBookInfo(
nameParserResult.Name,
nameParserResult.Year,
stackFiles,
extras,
alternativeVersions);
2018-09-12 10:26:21 -07:00
2020-01-22 14:18:56 -07:00
yield return info;
}
2018-09-12 10:26:21 -07:00
}
private void FindExtraAndAlternativeFiles(ref List<AudioBookFileInfo> stackFiles, out List<AudioBookFileInfo> extras, out List<AudioBookFileInfo> alternativeVersions, AudioBookNameParserResult nameParserResult)
{
extras = new List<AudioBookFileInfo>();
alternativeVersions = new List<AudioBookFileInfo>();
var haveChaptersOrPages = stackFiles.Any(x => x.ChapterNumber != null || x.PartNumber != null);
var groupedBy = stackFiles.GroupBy(file => new { file.ChapterNumber, file.PartNumber });
2021-02-12 16:39:18 -07:00
var nameWithReplacedDots = nameParserResult.Name.Replace(' ', '.');
foreach (var group in groupedBy)
{
if (group.Key.ChapterNumber == null && group.Key.PartNumber == null)
{
if (group.Count() > 1 || haveChaptersOrPages)
{
var ex = new List<AudioBookFileInfo>();
var alt = new List<AudioBookFileInfo>();
foreach (var audioFile in group)
{
var name = Path.GetFileNameWithoutExtension(audioFile.Path);
if (name.Equals("audiobook") ||
name.Contains(nameParserResult.Name, StringComparison.OrdinalIgnoreCase) ||
name.Contains(nameWithReplacedDots, StringComparison.OrdinalIgnoreCase))
{
alt.Add(audioFile);
}
else
{
ex.Add(audioFile);
}
}
if (ex.Count > 0)
{
var extra = ex
.OrderBy(x => x.Container)
.ThenBy(x => x.Path)
.ToList();
stackFiles = stackFiles.Except(extra).ToList();
extras.AddRange(extra);
}
if (alt.Count > 0)
{
var alternatives = alt
.OrderBy(x => x.Container)
.ThenBy(x => x.Path)
.ToList();
var main = FindMainAudioBookFile(alternatives, nameParserResult.Name);
alternatives.Remove(main);
stackFiles = stackFiles.Except(alternatives).ToList();
alternativeVersions.AddRange(alternatives);
}
}
}
else if (group.Count() > 1)
{
var alternatives = group
.OrderBy(x => x.Container)
.ThenBy(x => x.Path)
.Skip(1)
.ToList();
stackFiles = stackFiles.Except(alternatives).ToList();
alternativeVersions.AddRange(alternatives);
}
}
}
private AudioBookFileInfo FindMainAudioBookFile(List<AudioBookFileInfo> files, string name)
{
var main = files.Find(x => Path.GetFileNameWithoutExtension(x.Path).Equals(name, StringComparison.OrdinalIgnoreCase));
main ??= files.FirstOrDefault(x => Path.GetFileNameWithoutExtension(x.Path).Equals("audiobook", StringComparison.OrdinalIgnoreCase));
main ??= files.OrderBy(x => x.Container)
.ThenBy(x => x.Path)
.First();
return main;
}
2018-09-12 10:26:21 -07:00
}
}