AudioBookFilePathParser.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Text.RegularExpressions;
  6. using Emby.Naming.Common;
  7. namespace Emby.Naming.AudioBook
  8. {
  9. public class AudioBookFilePathParser
  10. {
  11. private readonly NamingOptions _options;
  12. public AudioBookFilePathParser(NamingOptions options)
  13. {
  14. _options = options;
  15. }
  16. public AudioBookFilePathParserResult Parse(string path)
  17. {
  18. if (path == null)
  19. {
  20. throw new ArgumentNullException(nameof(path));
  21. }
  22. var result = new AudioBookFilePathParserResult();
  23. var fileName = Path.GetFileNameWithoutExtension(path);
  24. foreach (var expression in _options.AudioBookPartsExpressions)
  25. {
  26. var match = new Regex(expression, RegexOptions.IgnoreCase).Match(fileName);
  27. if (match.Success)
  28. {
  29. if (!result.ChapterNumber.HasValue)
  30. {
  31. var value = match.Groups["chapter"];
  32. if (value.Success)
  33. {
  34. if (int.TryParse(value.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
  35. {
  36. result.ChapterNumber = intValue;
  37. }
  38. }
  39. }
  40. if (!result.PartNumber.HasValue)
  41. {
  42. var value = match.Groups["part"];
  43. if (value.Success)
  44. {
  45. if (int.TryParse(value.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
  46. {
  47. result.ChapterNumber = intValue;
  48. }
  49. }
  50. }
  51. }
  52. }
  53. /*var matches = _iRegexProvider.GetRegex("\\d+", RegexOptions.IgnoreCase).Matches(fileName);
  54. if (matches.Count > 0)
  55. {
  56. if (!result.ChapterNumber.HasValue)
  57. {
  58. result.ChapterNumber = int.Parse(matches[0].Groups[0].Value);
  59. }
  60. if (matches.Count > 1)
  61. {
  62. result.PartNumber = int.Parse(matches[matches.Count - 1].Groups[0].Value);
  63. }
  64. }*/
  65. result.Success = result.PartNumber.HasValue || result.ChapterNumber.HasValue;
  66. return result;
  67. }
  68. }
  69. }