| | 1 | | using System.Globalization; |
| | 2 | | using System.IO; |
| | 3 | | using System.Text.RegularExpressions; |
| | 4 | | using Emby.Naming.Common; |
| | 5 | |
|
| | 6 | | namespace Emby.Naming.AudioBook |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Parser class to extract part and/or chapter number from audiobook filename. |
| | 10 | | /// </summary> |
| | 11 | | public class AudioBookFilePathParser |
| | 12 | | { |
| | 13 | | private readonly NamingOptions _options; |
| | 14 | |
|
| | 15 | | /// <summary> |
| | 16 | | /// Initializes a new instance of the <see cref="AudioBookFilePathParser"/> class. |
| | 17 | | /// </summary> |
| | 18 | | /// <param name="options">Naming options containing AudioBookPartsExpressions.</param> |
| | 19 | | public AudioBookFilePathParser(NamingOptions options) |
| | 20 | | { |
| 117 | 21 | | _options = options; |
| 117 | 22 | | } |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Based on regex determines if filename includes part/chapter number. |
| | 26 | | /// </summary> |
| | 27 | | /// <param name="path">Path to audiobook file.</param> |
| | 28 | | /// <returns>Returns <see cref="AudioBookFilePathParser"/> object.</returns> |
| | 29 | | public AudioBookFilePathParserResult Parse(string path) |
| | 30 | | { |
| 117 | 31 | | AudioBookFilePathParserResult result = default; |
| 117 | 32 | | var fileName = Path.GetFileNameWithoutExtension(path); |
| 1638 | 33 | | foreach (var expression in _options.AudioBookPartsExpressions) |
| | 34 | | { |
| 702 | 35 | | var match = Regex.Match(fileName, expression, RegexOptions.IgnoreCase); |
| 702 | 36 | | if (match.Success) |
| | 37 | | { |
| 103 | 38 | | if (!result.ChapterNumber.HasValue) |
| | 39 | | { |
| 78 | 40 | | var value = match.Groups["chapter"]; |
| 78 | 41 | | if (value.Success) |
| | 42 | | { |
| 46 | 43 | | if (int.TryParse(value.ValueSpan, NumberStyles.Integer, CultureInfo.InvariantCulture, out va |
| | 44 | | { |
| 46 | 45 | | result.ChapterNumber = intValue; |
| | 46 | | } |
| | 47 | | } |
| | 48 | | } |
| | 49 | |
|
| 103 | 50 | | if (!result.PartNumber.HasValue) |
| | 51 | | { |
| 83 | 52 | | var value = match.Groups["part"]; |
| 83 | 53 | | if (value.Success) |
| | 54 | | { |
| 37 | 55 | | if (int.TryParse(value.ValueSpan, NumberStyles.Integer, CultureInfo.InvariantCulture, out va |
| | 56 | | { |
| 37 | 57 | | result.PartNumber = intValue; |
| | 58 | | } |
| | 59 | | } |
| | 60 | | } |
| | 61 | | } |
| | 62 | | } |
| | 63 | |
|
| 117 | 64 | | return result; |
| | 65 | | } |
| | 66 | | } |
| | 67 | | } |