repo creation with initial code after cloning public repo
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
|
||||
namespace Emby.Naming.AudioBook
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a single video file.
|
||||
/// </summary>
|
||||
public class AudioBookFileInfo : IComparable<AudioBookFileInfo>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AudioBookFileInfo"/> class.
|
||||
/// </summary>
|
||||
/// <param name="path">Path to audiobook file.</param>
|
||||
/// <param name="container">File type.</param>
|
||||
/// <param name="partNumber">Number of part this file represents.</param>
|
||||
/// <param name="chapterNumber">Number of chapter this file represents.</param>
|
||||
public AudioBookFileInfo(string path, string container, int? partNumber = default, int? chapterNumber = default)
|
||||
{
|
||||
Path = path;
|
||||
Container = container;
|
||||
PartNumber = partNumber;
|
||||
ChapterNumber = chapterNumber;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the path.
|
||||
/// </summary>
|
||||
/// <value>The path.</value>
|
||||
public string Path { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the container.
|
||||
/// </summary>
|
||||
/// <value>The container.</value>
|
||||
public string Container { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the part number.
|
||||
/// </summary>
|
||||
/// <value>The part number.</value>
|
||||
public int? PartNumber { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the chapter number.
|
||||
/// </summary>
|
||||
/// <value>The chapter number.</value>
|
||||
public int? ChapterNumber { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public int CompareTo(AudioBookFileInfo? other)
|
||||
{
|
||||
if (ReferenceEquals(this, other))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ReferenceEquals(null, other))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
var chapterNumberComparison = Nullable.Compare(ChapterNumber, other.ChapterNumber);
|
||||
if (chapterNumberComparison != 0)
|
||||
{
|
||||
return chapterNumberComparison;
|
||||
}
|
||||
|
||||
var partNumberComparison = Nullable.Compare(PartNumber, other.PartNumber);
|
||||
if (partNumberComparison != 0)
|
||||
{
|
||||
return partNumberComparison;
|
||||
}
|
||||
|
||||
return string.Compare(Path, other.Path, StringComparison.Ordinal);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text.RegularExpressions;
|
||||
using Emby.Naming.Common;
|
||||
|
||||
namespace Emby.Naming.AudioBook
|
||||
{
|
||||
/// <summary>
|
||||
/// Parser class to extract part and/or chapter number from audiobook filename.
|
||||
/// </summary>
|
||||
public class AudioBookFilePathParser
|
||||
{
|
||||
private readonly NamingOptions _options;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AudioBookFilePathParser"/> class.
|
||||
/// </summary>
|
||||
/// <param name="options">Naming options containing AudioBookPartsExpressions.</param>
|
||||
public AudioBookFilePathParser(NamingOptions options)
|
||||
{
|
||||
_options = options;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Based on regex determines if filename includes part/chapter number.
|
||||
/// </summary>
|
||||
/// <param name="path">Path to audiobook file.</param>
|
||||
/// <returns>Returns <see cref="AudioBookFilePathParser"/> object.</returns>
|
||||
public AudioBookFilePathParserResult Parse(string path)
|
||||
{
|
||||
AudioBookFilePathParserResult result = default;
|
||||
var fileName = Path.GetFileNameWithoutExtension(path);
|
||||
foreach (var expression in _options.AudioBookPartsExpressions)
|
||||
{
|
||||
var match = Regex.Match(fileName, expression, RegexOptions.IgnoreCase);
|
||||
if (match.Success)
|
||||
{
|
||||
if (!result.ChapterNumber.HasValue)
|
||||
{
|
||||
var value = match.Groups["chapter"];
|
||||
if (value.Success)
|
||||
{
|
||||
if (int.TryParse(value.ValueSpan, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
|
||||
{
|
||||
result.ChapterNumber = intValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!result.PartNumber.HasValue)
|
||||
{
|
||||
var value = match.Groups["part"];
|
||||
if (value.Success)
|
||||
{
|
||||
if (int.TryParse(value.ValueSpan, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
|
||||
{
|
||||
result.PartNumber = intValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
namespace Emby.Naming.AudioBook
|
||||
{
|
||||
/// <summary>
|
||||
/// Data object for passing result of audiobook part/chapter extraction.
|
||||
/// </summary>
|
||||
public record struct AudioBookFilePathParserResult
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets optional number of path extracted from audiobook filename.
|
||||
/// </summary>
|
||||
public int? PartNumber { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets optional number of chapter extracted from audiobook filename.
|
||||
/// </summary>
|
||||
public int? ChapterNumber { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Emby.Naming.AudioBook
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a complete video, including all parts and subtitles.
|
||||
/// </summary>
|
||||
public class AudioBookInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AudioBookInfo" /> class.
|
||||
/// </summary>
|
||||
/// <param name="name">Name of audiobook.</param>
|
||||
/// <param name="year">Year of audiobook release.</param>
|
||||
/// <param name="files">List of files composing the actual audiobook.</param>
|
||||
/// <param name="extras">List of extra files.</param>
|
||||
/// <param name="alternateVersions">Alternative version of files.</param>
|
||||
public AudioBookInfo(string name, int? year, IReadOnlyList<AudioBookFileInfo> files, IReadOnlyList<AudioBookFileInfo> extras, IReadOnlyList<AudioBookFileInfo> alternateVersions)
|
||||
{
|
||||
Name = name;
|
||||
Year = year;
|
||||
Files = files;
|
||||
Extras = extras;
|
||||
AlternateVersions = alternateVersions;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the name.
|
||||
/// </summary>
|
||||
/// <value>The name.</value>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the year.
|
||||
/// </summary>
|
||||
public int? Year { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the files.
|
||||
/// </summary>
|
||||
/// <value>The files.</value>
|
||||
public IReadOnlyList<AudioBookFileInfo> Files { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the extras.
|
||||
/// </summary>
|
||||
/// <value>The extras.</value>
|
||||
public IReadOnlyList<AudioBookFileInfo> Extras { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the alternate versions.
|
||||
/// </summary>
|
||||
/// <value>The alternate versions.</value>
|
||||
public IReadOnlyList<AudioBookFileInfo> AlternateVersions { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Emby.Naming.Common;
|
||||
using Emby.Naming.Video;
|
||||
using MediaBrowser.Model.IO;
|
||||
|
||||
namespace Emby.Naming.AudioBook
|
||||
{
|
||||
/// <summary>
|
||||
/// Class used to resolve Name, Year, alternative files and extras from stack of files.
|
||||
/// </summary>
|
||||
public class AudioBookListResolver
|
||||
{
|
||||
private readonly NamingOptions _options;
|
||||
private readonly AudioBookResolver _audioBookResolver;
|
||||
|
||||
/// <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>
|
||||
public AudioBookListResolver(NamingOptions options)
|
||||
{
|
||||
_options = options;
|
||||
_audioBookResolver = new AudioBookResolver(_options);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public IEnumerable<AudioBookInfo> Resolve(IEnumerable<FileSystemMetadata> files)
|
||||
{
|
||||
// File with empty fullname will be sorted out here.
|
||||
var audiobookFileInfos = files
|
||||
.Select(i => _audioBookResolver.Resolve(i.FullName))
|
||||
.OfType<AudioBookFileInfo>();
|
||||
|
||||
var stackResult = StackResolver.ResolveAudioBooks(audiobookFileInfos);
|
||||
|
||||
foreach (var stack in stackResult)
|
||||
{
|
||||
var stackFiles = stack.Files
|
||||
.Select(i => _audioBookResolver.Resolve(i))
|
||||
.OfType<AudioBookFileInfo>()
|
||||
.ToList();
|
||||
|
||||
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);
|
||||
|
||||
yield return info;
|
||||
}
|
||||
}
|
||||
|
||||
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 is not null || x.PartNumber is not null);
|
||||
var groupedBy = stackFiles.GroupBy(file => new { file.ChapterNumber, file.PartNumber });
|
||||
var nameWithReplacedDots = nameParserResult.Name.Replace(' ', '.');
|
||||
|
||||
foreach (var group in groupedBy)
|
||||
{
|
||||
if (group.Key.ChapterNumber is null && group.Key.PartNumber is null)
|
||||
{
|
||||
if (group.Count() > 1 || haveChaptersOrPages)
|
||||
{
|
||||
List<AudioBookFileInfo>? ex = null;
|
||||
List<AudioBookFileInfo>? alt = null;
|
||||
|
||||
foreach (var audioFile in group)
|
||||
{
|
||||
var name = Path.GetFileNameWithoutExtension(audioFile.Path.AsSpan());
|
||||
if (name.Equals("audiobook", StringComparison.OrdinalIgnoreCase)
|
||||
|| name.Contains(nameParserResult.Name, StringComparison.OrdinalIgnoreCase)
|
||||
|| name.Contains(nameWithReplacedDots, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
(alt ??= new()).Add(audioFile);
|
||||
}
|
||||
else
|
||||
{
|
||||
(ex ??= new()).Add(audioFile);
|
||||
}
|
||||
}
|
||||
|
||||
if (ex is not null)
|
||||
{
|
||||
var extra = ex
|
||||
.OrderBy(x => x.Container)
|
||||
.ThenBy(x => x.Path)
|
||||
.ToList();
|
||||
|
||||
stackFiles = stackFiles.Except(extra).ToList();
|
||||
extras.AddRange(extra);
|
||||
}
|
||||
|
||||
if (alt is not null)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
using System.Globalization;
|
||||
using System.Text.RegularExpressions;
|
||||
using Emby.Naming.Common;
|
||||
|
||||
namespace Emby.Naming.AudioBook
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper class to retrieve name and year from audiobook previously retrieved name.
|
||||
/// </summary>
|
||||
public class AudioBookNameParser
|
||||
{
|
||||
private readonly NamingOptions _options;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AudioBookNameParser"/> class.
|
||||
/// </summary>
|
||||
/// <param name="options">Naming options containing AudioBookNamesExpressions.</param>
|
||||
public AudioBookNameParser(NamingOptions options)
|
||||
{
|
||||
_options = options;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse name and year from previously determined name of audiobook.
|
||||
/// </summary>
|
||||
/// <param name="name">Name of the audiobook.</param>
|
||||
/// <returns>Returns <see cref="AudioBookNameParserResult"/> object.</returns>
|
||||
public AudioBookNameParserResult Parse(string name)
|
||||
{
|
||||
AudioBookNameParserResult result = default;
|
||||
foreach (var expression in _options.AudioBookNamesExpressions)
|
||||
{
|
||||
var match = Regex.Match(name, expression, RegexOptions.IgnoreCase);
|
||||
if (match.Success)
|
||||
{
|
||||
if (result.Name is null)
|
||||
{
|
||||
var value = match.Groups["name"];
|
||||
if (value.Success)
|
||||
{
|
||||
result.Name = value.Value;
|
||||
}
|
||||
}
|
||||
|
||||
if (!result.Year.HasValue)
|
||||
{
|
||||
var value = match.Groups["year"];
|
||||
if (value.Success)
|
||||
{
|
||||
if (int.TryParse(value.ValueSpan, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
|
||||
{
|
||||
result.Year = intValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(result.Name))
|
||||
{
|
||||
result.Name = name;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
namespace Emby.Naming.AudioBook
|
||||
{
|
||||
/// <summary>
|
||||
/// Data object used to pass result of name and year parsing.
|
||||
/// </summary>
|
||||
public struct AudioBookNameParserResult
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets name of audiobook.
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets optional year of release.
|
||||
/// </summary>
|
||||
public int? Year { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Emby.Naming.Common;
|
||||
using Jellyfin.Extensions;
|
||||
|
||||
namespace Emby.Naming.AudioBook
|
||||
{
|
||||
/// <summary>
|
||||
/// Resolve specifics (path, container, partNumber, chapterNumber) about audiobook file.
|
||||
/// </summary>
|
||||
public class AudioBookResolver
|
||||
{
|
||||
private readonly NamingOptions _options;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AudioBookResolver"/> class.
|
||||
/// </summary>
|
||||
/// <param name="options"><see cref="NamingOptions"/> containing AudioFileExtensions and also used to pass to AudioBookFilePathParser.</param>
|
||||
public AudioBookResolver(NamingOptions options)
|
||||
{
|
||||
_options = options;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resolve specifics (path, container, partNumber, chapterNumber) about audiobook file.
|
||||
/// </summary>
|
||||
/// <param name="path">Path to audiobook file.</param>
|
||||
/// <returns>Returns <see cref="AudioBookResolver"/> object.</returns>
|
||||
public AudioBookFileInfo? Resolve(string path)
|
||||
{
|
||||
if (path.Length == 0 || Path.GetFileNameWithoutExtension(path).Length == 0)
|
||||
{
|
||||
// Return null to indicate this path will not be used, instead of stopping whole process with exception
|
||||
return null;
|
||||
}
|
||||
|
||||
var extension = Path.GetExtension(path);
|
||||
|
||||
// Check supported extensions
|
||||
if (!_options.AudioFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var container = extension.TrimStart('.');
|
||||
|
||||
var parsingResult = new AudioBookFilePathParser(_options).Parse(path);
|
||||
|
||||
return new AudioBookFileInfo(
|
||||
path,
|
||||
container,
|
||||
chapterNumber: parsingResult.ChapterNumber,
|
||||
partNumber: parsingResult.PartNumber);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user