2020-11-02 19:03:12 +00:00
|
|
|
using System.Globalization;
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
using Emby.Naming.Common;
|
|
|
|
|
|
|
|
namespace Emby.Naming.AudioBook
|
|
|
|
{
|
2020-11-10 16:11:48 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Helper class to retrieve name and year from audiobook previously retrieved name.
|
|
|
|
/// </summary>
|
2020-11-02 19:03:12 +00:00
|
|
|
public class AudioBookNameParser
|
|
|
|
{
|
|
|
|
private readonly NamingOptions _options;
|
|
|
|
|
2020-11-10 16:11:48 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="AudioBookNameParser"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="options">Naming options containing AudioBookNamesExpressions.</param>
|
2020-11-02 19:03:12 +00:00
|
|
|
public AudioBookNameParser(NamingOptions options)
|
|
|
|
{
|
|
|
|
_options = options;
|
|
|
|
}
|
|
|
|
|
2020-11-10 16:11:48 +00:00
|
|
|
/// <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>
|
2020-11-02 19:03:12 +00:00
|
|
|
public AudioBookNameParserResult Parse(string name)
|
|
|
|
{
|
|
|
|
AudioBookNameParserResult result = default;
|
|
|
|
foreach (var expression in _options.AudioBookNamesExpressions)
|
|
|
|
{
|
2023-02-20 15:07:51 +00:00
|
|
|
var match = Regex.Match(name, expression, RegexOptions.IgnoreCase);
|
2020-11-02 19:03:12 +00:00
|
|
|
if (match.Success)
|
|
|
|
{
|
2022-12-05 14:00:20 +00:00
|
|
|
if (result.Name is null)
|
2020-11-02 19:03:12 +00:00
|
|
|
{
|
|
|
|
var value = match.Groups["name"];
|
|
|
|
if (value.Success)
|
|
|
|
{
|
|
|
|
result.Name = value.Value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result.Year.HasValue)
|
|
|
|
{
|
|
|
|
var value = match.Groups["year"];
|
|
|
|
if (value.Success)
|
|
|
|
{
|
2023-02-17 14:00:06 +00:00
|
|
|
if (int.TryParse(value.ValueSpan, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
|
2020-11-02 19:03:12 +00:00
|
|
|
{
|
|
|
|
result.Year = intValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(result.Name))
|
|
|
|
{
|
|
|
|
result.Name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|