jellyfin-server/Emby.Naming/AudioBook/AudioBookFilePathParser.cs

61 lines
2.0 KiB
C#
Raw Normal View History

2020-09-20 12:02:41 +00:00
#nullable enable
#pragma warning disable CS1591
using System.Globalization;
2018-09-12 17:26:21 +00:00
using System.IO;
using System.Text.RegularExpressions;
using Emby.Naming.Common;
namespace Emby.Naming.AudioBook
{
public class AudioBookFilePathParser
{
private readonly NamingOptions _options;
public AudioBookFilePathParser(NamingOptions options)
{
_options = options;
}
2019-05-10 18:37:42 +00:00
public AudioBookFilePathParserResult Parse(string path)
2018-09-12 17:26:21 +00:00
{
2020-09-20 12:02:41 +00:00
AudioBookFilePathParserResult result = default;
2018-09-12 17:26:21 +00:00
var fileName = Path.GetFileNameWithoutExtension(path);
foreach (var expression in _options.AudioBookPartsExpressions)
{
var match = new Regex(expression, RegexOptions.IgnoreCase).Match(fileName);
if (match.Success)
{
if (!result.ChapterNumber.HasValue)
{
var value = match.Groups["chapter"];
if (value.Success)
{
if (int.TryParse(value.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
2018-09-12 17:26:21 +00:00
{
result.ChapterNumber = intValue;
}
}
}
2019-05-10 18:37:42 +00:00
2018-09-12 17:26:21 +00:00
if (!result.PartNumber.HasValue)
{
var value = match.Groups["part"];
if (value.Success)
{
if (int.TryParse(value.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
2018-09-12 17:26:21 +00:00
{
result.PartNumber = intValue;
2018-09-12 17:26:21 +00:00
}
}
}
}
}
result.Success = result.ChapterNumber.HasValue || result.PartNumber.HasValue;
2018-09-12 17:26:21 +00:00
return result;
}
}
}