2020-11-01 09:10:48 -07:00
|
|
|
using System;
|
2019-01-13 12:54:44 -07:00
|
|
|
using System.Collections.Generic;
|
2020-11-01 09:10:48 -07:00
|
|
|
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);
|
|
|
|
|
2020-11-12 04:54:55 -07:00
|
|
|
// File with empty fullname will be sorted out here.
|
2018-09-12 10:26:21 -07:00
|
|
|
var audiobookFileInfos = files
|
2020-11-01 05:42:56 -07:00
|
|
|
.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)
|
2020-11-01 05:42:56 -07:00
|
|
|
.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
|
|
|
{
|
2020-11-01 09:10:48 -07:00
|
|
|
var stackFiles = stack.Files
|
|
|
|
.Select(i => audioBookResolver.Resolve(i))
|
|
|
|
.OfType<AudioBookFileInfo>()
|
|
|
|
.ToList();
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
stackFiles.Sort();
|
2020-11-01 09:10:48 -07:00
|
|
|
|
2020-11-03 08:24:04 -07:00
|
|
|
var nameParserResult = new AudioBookNameParser(_options).Parse(stack.Name);
|
2020-11-02 12:03:12 -07:00
|
|
|
|
2020-11-03 08:24:04 -07:00
|
|
|
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
|
|
|
}
|
2020-11-03 08:24:04 -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(' ', '.');
|
2020-11-03 08:24:04 -07:00
|
|
|
|
|
|
|
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);
|
2020-11-12 05:16:33 -07:00
|
|
|
if (name.Equals("audiobook") ||
|
2020-11-03 08:24:04 -07:00
|
|
|
name.Contains(nameParserResult.Name, StringComparison.OrdinalIgnoreCase) ||
|
2020-11-12 05:16:33 -07:00
|
|
|
name.Contains(nameWithReplacedDots, StringComparison.OrdinalIgnoreCase))
|
2020-11-03 08:24:04 -07:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2020-11-12 08:51:52 -07:00
|
|
|
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));
|
2020-11-03 08:24:04 -07:00
|
|
|
main ??= files.OrderBy(x => x.Container)
|
|
|
|
.ThenBy(x => x.Path)
|
|
|
|
.First();
|
|
|
|
|
|
|
|
return main;
|
|
|
|
}
|
2018-09-12 10:26:21 -07:00
|
|
|
}
|
|
|
|
}
|