2019-12-13 14:11:37 -05:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-05-10 14:37:42 -04:00
|
|
|
using System;
|
2019-01-13 14:54:44 -05:00
|
|
|
using System.Globalization;
|
2018-09-12 13:26:21 -04: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 14:37:42 -04:00
|
|
|
public AudioBookFilePathParserResult Parse(string path)
|
2018-09-12 13:26:21 -04:00
|
|
|
{
|
2019-05-10 14:37:42 -04:00
|
|
|
if (path == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(path));
|
|
|
|
}
|
2018-09-12 13:26:21 -04:00
|
|
|
|
|
|
|
var result = new AudioBookFilePathParserResult();
|
|
|
|
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)
|
|
|
|
{
|
2019-01-13 15:46:33 -05:00
|
|
|
if (int.TryParse(value.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
|
2018-09-12 13:26:21 -04:00
|
|
|
{
|
|
|
|
result.ChapterNumber = intValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-10 14:37:42 -04:00
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
if (!result.PartNumber.HasValue)
|
|
|
|
{
|
|
|
|
var value = match.Groups["part"];
|
|
|
|
if (value.Success)
|
|
|
|
{
|
2019-01-13 15:46:33 -05:00
|
|
|
if (int.TryParse(value.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
|
2018-09-12 13:26:21 -04:00
|
|
|
{
|
|
|
|
result.ChapterNumber = intValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*var matches = _iRegexProvider.GetRegex("\\d+", RegexOptions.IgnoreCase).Matches(fileName);
|
|
|
|
if (matches.Count > 0)
|
|
|
|
{
|
|
|
|
if (!result.ChapterNumber.HasValue)
|
|
|
|
{
|
|
|
|
result.ChapterNumber = int.Parse(matches[0].Groups[0].Value);
|
|
|
|
}
|
2020-06-15 17:43:52 -04:00
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
if (matches.Count > 1)
|
|
|
|
{
|
|
|
|
result.PartNumber = int.Parse(matches[matches.Count - 1].Groups[0].Value);
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
result.Success = result.PartNumber.HasValue || result.ChapterNumber.HasValue;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|