| | 1 | | using System.Globalization; |
| | 2 | | using System.Text.RegularExpressions; |
| | 3 | | using Emby.Naming.Common; |
| | 4 | |
|
| | 5 | | namespace Emby.Naming.AudioBook |
| | 6 | | { |
| | 7 | | /// <summary> |
| | 8 | | /// Helper class to retrieve name and year from audiobook previously retrieved name. |
| | 9 | | /// </summary> |
| | 10 | | public class AudioBookNameParser |
| | 11 | | { |
| | 12 | | private readonly NamingOptions _options; |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// Initializes a new instance of the <see cref="AudioBookNameParser"/> class. |
| | 16 | | /// </summary> |
| | 17 | | /// <param name="options">Naming options containing AudioBookNamesExpressions.</param> |
| | 18 | | public AudioBookNameParser(NamingOptions options) |
| | 19 | | { |
| 35 | 20 | | _options = options; |
| 35 | 21 | | } |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Parse name and year from previously determined name of audiobook. |
| | 25 | | /// </summary> |
| | 26 | | /// <param name="name">Name of the audiobook.</param> |
| | 27 | | /// <returns>Returns <see cref="AudioBookNameParserResult"/> object.</returns> |
| | 28 | | public AudioBookNameParserResult Parse(string name) |
| | 29 | | { |
| 35 | 30 | | AudioBookNameParserResult result = default; |
| 210 | 31 | | foreach (var expression in _options.AudioBookNamesExpressions) |
| | 32 | | { |
| 70 | 33 | | var match = Regex.Match(name, expression, RegexOptions.IgnoreCase); |
| 70 | 34 | | if (match.Success) |
| | 35 | | { |
| 39 | 36 | | if (result.Name is null) |
| | 37 | | { |
| 34 | 38 | | var value = match.Groups["name"]; |
| 34 | 39 | | if (value.Success) |
| | 40 | | { |
| 34 | 41 | | result.Name = value.Value; |
| | 42 | | } |
| | 43 | | } |
| | 44 | |
|
| 39 | 45 | | if (!result.Year.HasValue) |
| | 46 | | { |
| 34 | 47 | | var value = match.Groups["year"]; |
| 34 | 48 | | if (value.Success) |
| | 49 | | { |
| 5 | 50 | | if (int.TryParse(value.ValueSpan, NumberStyles.Integer, CultureInfo.InvariantCulture, out va |
| | 51 | | { |
| 5 | 52 | | result.Year = intValue; |
| | 53 | | } |
| | 54 | | } |
| | 55 | | } |
| | 56 | | } |
| | 57 | | } |
| | 58 | |
|
| 35 | 59 | | if (string.IsNullOrEmpty(result.Name)) |
| | 60 | | { |
| 1 | 61 | | result.Name = name; |
| | 62 | | } |
| | 63 | |
|
| 35 | 64 | | return result; |
| | 65 | | } |
| | 66 | | } |
| | 67 | | } |