diff options
| author | Greenback <jimcartlidge@yahoo.co.uk> | 2020-11-19 18:27:18 +0000 |
|---|---|---|
| committer | Greenback <jimcartlidge@yahoo.co.uk> | 2020-11-19 18:27:18 +0000 |
| commit | a3e47f3e4eed60b227359e8ae59c8a6659a1942c (patch) | |
| tree | e5af2d5c0e658505f397ed48abc4e5870f3857d7 /Emby.Naming/AudioBook/AudioBookNameParser.cs | |
| parent | 60a6627140a83408b8157b9543e62ff48918ef7a (diff) | |
| parent | e71ab2afb1fda7521c61f860654fd94e3bd5e3e8 (diff) | |
Updated to latest Unstable.
Diffstat (limited to 'Emby.Naming/AudioBook/AudioBookNameParser.cs')
| -rw-r--r-- | Emby.Naming/AudioBook/AudioBookNameParser.cs | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/Emby.Naming/AudioBook/AudioBookNameParser.cs b/Emby.Naming/AudioBook/AudioBookNameParser.cs new file mode 100644 index 000000000..120482bc2 --- /dev/null +++ b/Emby.Naming/AudioBook/AudioBookNameParser.cs @@ -0,0 +1,67 @@ +using System.Globalization; +using System.Text.RegularExpressions; +using Emby.Naming.Common; + +namespace Emby.Naming.AudioBook +{ + /// <summary> + /// Helper class to retrieve name and year from audiobook previously retrieved name. + /// </summary> + public class AudioBookNameParser + { + private readonly NamingOptions _options; + + /// <summary> + /// Initializes a new instance of the <see cref="AudioBookNameParser"/> class. + /// </summary> + /// <param name="options">Naming options containing AudioBookNamesExpressions.</param> + public AudioBookNameParser(NamingOptions options) + { + _options = options; + } + + /// <summary> + /// Parse name and year from previously determined name of audiobook. + /// </summary> + /// <param name="name">Name of the audiobook.</param> + /// <returns>Returns <see cref="AudioBookNameParserResult"/> object.</returns> + public AudioBookNameParserResult Parse(string name) + { + AudioBookNameParserResult result = default; + foreach (var expression in _options.AudioBookNamesExpressions) + { + var match = new Regex(expression, RegexOptions.IgnoreCase).Match(name); + if (match.Success) + { + if (result.Name == null) + { + var value = match.Groups["name"]; + if (value.Success) + { + result.Name = value.Value; + } + } + + if (!result.Year.HasValue) + { + var value = match.Groups["year"]; + if (value.Success) + { + if (int.TryParse(value.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue)) + { + result.Year = intValue; + } + } + } + } + } + + if (string.IsNullOrEmpty(result.Name)) + { + result.Name = name; + } + + return result; + } + } +} |
