jellyfin/Emby.Naming/Video/CleanDateTimeParser.cs

56 lines
1.8 KiB
C#
Raw Normal View History

2020-01-11 15:31:35 -05:00
using System.Collections.Generic;
2018-09-12 13:26:21 -04:00
using System.Globalization;
using System.Text.RegularExpressions;
namespace Emby.Naming.Video
{
/// <summary>
2019-10-25 06:47:20 -04:00
/// <see href="http://kodi.wiki/view/Advancedsettings.xml#video" />.
2018-09-12 13:26:21 -04:00
/// </summary>
2020-01-11 15:31:35 -05:00
public static class CleanDateTimeParser
2018-09-12 13:26:21 -04:00
{
2020-11-10 11:11:48 -05:00
/// <summary>
/// Attempts to clean the name.
/// </summary>
/// <param name="name">Name of video.</param>
/// <param name="cleanDateTimeRegexes">Optional list of regexes to clean the name.</param>
/// <returns>Returns <see cref="CleanDateTimeResult"/> object.</returns>
2020-01-11 15:31:35 -05:00
public static CleanDateTimeResult Clean(string name, IReadOnlyList<Regex> cleanDateTimeRegexes)
2018-09-12 13:26:21 -04:00
{
2020-01-11 15:18:30 -05:00
CleanDateTimeResult result = new CleanDateTimeResult(name);
2020-11-09 08:10:16 -05:00
if (string.IsNullOrEmpty(name))
{
return result;
}
2020-01-11 15:31:35 -05:00
var len = cleanDateTimeRegexes.Count;
2020-01-11 15:16:36 -05:00
for (int i = 0; i < len; i++)
{
2020-01-11 15:31:35 -05:00
if (TryClean(name, cleanDateTimeRegexes[i], ref result))
2020-01-11 15:16:36 -05:00
{
return result;
}
}
2018-09-12 13:26:21 -04:00
2020-01-11 15:16:36 -05:00
return result;
}
private static bool TryClean(string name, Regex expression, ref CleanDateTimeResult result)
{
2018-09-12 13:26:21 -04:00
var match = expression.Match(name);
2019-05-10 14:37:42 -04:00
if (match.Success
2020-01-11 14:25:06 -05:00
&& match.Groups.Count == 5
2019-05-10 14:37:42 -04:00
&& match.Groups[1].Success
&& match.Groups[2].Success
2023-02-17 09:00:06 -05:00
&& int.TryParse(match.Groups[2].ValueSpan, NumberStyles.Integer, CultureInfo.InvariantCulture, out var year))
2018-09-12 13:26:21 -04:00
{
2020-01-11 15:16:36 -05:00
result = new CleanDateTimeResult(match.Groups[1].Value.TrimEnd(), year);
return true;
2018-09-12 13:26:21 -04:00
}
2020-01-11 15:16:36 -05:00
return false;
2018-09-12 13:26:21 -04:00
}
}
}