2019-12-13 19:11:37 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2020-11-01 16:10:48 +00:00
|
|
|
using System;
|
2019-01-13 19:54:44 +00:00
|
|
|
using System.Collections.Generic;
|
2020-11-01 16:10:48 +00:00
|
|
|
using System.IO;
|
2018-09-12 17:26:21 +00:00
|
|
|
using System.Linq;
|
|
|
|
using Emby.Naming.Common;
|
|
|
|
using Emby.Naming.Video;
|
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
|
|
|
|
namespace Emby.Naming.AudioBook
|
|
|
|
{
|
|
|
|
public class AudioBookListResolver
|
|
|
|
{
|
|
|
|
private readonly NamingOptions _options;
|
|
|
|
|
|
|
|
public AudioBookListResolver(NamingOptions options)
|
|
|
|
{
|
|
|
|
_options = options;
|
|
|
|
}
|
|
|
|
|
2019-05-10 18:37:42 +00:00
|
|
|
public IEnumerable<AudioBookInfo> Resolve(IEnumerable<FileSystemMetadata> files)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
var audioBookResolver = new AudioBookResolver(_options);
|
|
|
|
|
2020-11-01 16:10:48 +00:00
|
|
|
// File with empty fullname will be sorted out here
|
2018-09-12 17:26:21 +00:00
|
|
|
var audiobookFileInfos = files
|
2020-11-01 12:42:56 +00:00
|
|
|
.Select(i => audioBookResolver.Resolve(i.FullName))
|
2020-11-01 09:47:31 +00:00
|
|
|
.OfType<AudioBookFileInfo>()
|
2018-09-12 17:26:21 +00:00
|
|
|
.ToList();
|
|
|
|
|
|
|
|
var stackResult = new StackResolver(_options)
|
2020-11-01 12:42:56 +00:00
|
|
|
.ResolveAudioBooks(audiobookFileInfos);
|
2018-09-12 17:26:21 +00:00
|
|
|
|
2020-01-22 21:18:56 +00:00
|
|
|
foreach (var stack in stackResult)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2020-11-01 16:10:48 +00:00
|
|
|
var stackFiles = stack.Files
|
|
|
|
.Select(i => audioBookResolver.Resolve(i))
|
|
|
|
.OfType<AudioBookFileInfo>()
|
|
|
|
.ToList();
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
stackFiles.Sort();
|
2020-11-01 16:10:48 +00:00
|
|
|
|
2020-11-03 15:24:04 +00:00
|
|
|
var nameParserResult = new AudioBookNameParser(_options).Parse(stack.Name);
|
2020-11-02 19:03:12 +00:00
|
|
|
|
2020-11-03 15:24:04 +00: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 17:26:21 +00:00
|
|
|
|
2020-01-22 21:18:56 +00:00
|
|
|
yield return info;
|
|
|
|
}
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
2020-11-03 15:24:04 +00: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 });
|
|
|
|
|
|
|
|
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 == "audiobook" ||
|
|
|
|
name.Contains(nameParserResult.Name, StringComparison.OrdinalIgnoreCase) ||
|
|
|
|
name.Contains(nameParserResult.Name.Replace(" ", "."), 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) == name);
|
|
|
|
main ??= files.FirstOrDefault(x => Path.GetFileNameWithoutExtension(x.Path) == "audiobook");
|
|
|
|
main ??= files.OrderBy(x => x.Container)
|
|
|
|
.ThenBy(x => x.Path)
|
|
|
|
.First();
|
|
|
|
|
|
|
|
return main;
|
|
|
|
}
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
}
|