aboutsummaryrefslogtreecommitdiff
path: root/Emby.Naming/Video/CleanDateTimeParser.cs
blob: 8638dddd4282abcff73295b6671aa403cfcf4cdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#pragma warning disable CS1591
#pragma warning disable SA1600
#nullable enable

using System.Globalization;
using System.Text.RegularExpressions;
using Emby.Naming.Common;

namespace Emby.Naming.Video
{
    /// <summary>
    /// <see href="http://kodi.wiki/view/Advancedsettings.xml#video" />.
    /// </summary>
    public class CleanDateTimeParser
    {
        private readonly NamingOptions _options;

        public CleanDateTimeParser(NamingOptions options)
        {
            _options = options;
        }

        public CleanDateTimeResult Clean(string name)
        {
            var regexes = _options.CleanDateTimeRegexes;
            var len = regexes.Length;
            CleanDateTimeResult result = new CleanDateTimeResult(name);
            if (len == 0)
            {
                return result;
            }

            for (int i = 0; i < len; i++)
            {
                if (TryClean(name, regexes[i], ref result))
                {
                    return result;
                }
            }

            return result;
        }

        private static bool TryClean(string name, Regex expression, ref CleanDateTimeResult result)
        {
            var match = expression.Match(name);

            if (match.Success
                && match.Groups.Count == 5
                && match.Groups[1].Success
                && match.Groups[2].Success
                && int.TryParse(match.Groups[2].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var year))
            {
                result = new CleanDateTimeResult(match.Groups[1].Value.TrimEnd(), year);
                return true;
            }

            return false;
        }
    }
}