jellyfin/Emby.Naming/Video/CleanDateTimeParser.cs

58 lines
1.6 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
#pragma warning disable SA1600
2020-01-11 21:16:36 +01:00
#nullable enable
2018-09-12 19:26:21 +02:00
using System.Globalization;
using System.Text.RegularExpressions;
2019-01-13 20:17:29 +01:00
using Emby.Naming.Common;
2018-09-12 19:26:21 +02:00
namespace Emby.Naming.Video
{
/// <summary>
2019-10-25 12:47:20 +02:00
/// <see href="http://kodi.wiki/view/Advancedsettings.xml#video" />.
2018-09-12 19:26:21 +02:00
/// </summary>
public class CleanDateTimeParser
{
private readonly NamingOptions _options;
public CleanDateTimeParser(NamingOptions options)
{
_options = options;
}
public CleanDateTimeResult Clean(string name)
{
2020-01-11 21:16:36 +01:00
var regexes = _options.CleanDateTimeRegexes;
var len = regexes.Length;
2020-01-11 21:18:30 +01:00
CleanDateTimeResult result = new CleanDateTimeResult(name);
2020-01-11 21:16:36 +01:00
for (int i = 0; i < len; i++)
{
if (TryClean(name, regexes[i], ref result))
{
return result;
}
}
2018-09-12 19:26:21 +02:00
2020-01-11 21:16:36 +01:00
return result;
}
private static bool TryClean(string name, Regex expression, ref CleanDateTimeResult result)
{
2018-09-12 19:26:21 +02:00
var match = expression.Match(name);
2019-05-10 20:37:42 +02:00
if (match.Success
2020-01-11 20:25:06 +01:00
&& match.Groups.Count == 5
2019-05-10 20:37:42 +02:00
&& match.Groups[1].Success
&& match.Groups[2].Success
&& int.TryParse(match.Groups[2].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var year))
2018-09-12 19:26:21 +02:00
{
2020-01-11 21:16:36 +01:00
result = new CleanDateTimeResult(match.Groups[1].Value.TrimEnd(), year);
return true;
2018-09-12 19:26:21 +02:00
}
2020-01-11 21:16:36 +01:00
return false;
2018-09-12 19:26:21 +02:00
}
}
}