AudioBookFilePathParser.cs 2.6 KB

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