AudioBookFilePathParser.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #nullable enable
  2. #pragma warning disable CS1591
  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. AudioBookFilePathParserResult result = default;
  19. var fileName = Path.GetFileNameWithoutExtension(path);
  20. foreach (var expression in _options.AudioBookPartsExpressions)
  21. {
  22. var match = new Regex(expression, RegexOptions.IgnoreCase).Match(fileName);
  23. if (match.Success)
  24. {
  25. if (!result.ChapterNumber.HasValue)
  26. {
  27. var value = match.Groups["chapter"];
  28. if (value.Success)
  29. {
  30. if (int.TryParse(value.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
  31. {
  32. result.ChapterNumber = intValue;
  33. }
  34. }
  35. }
  36. if (!result.PartNumber.HasValue)
  37. {
  38. var value = match.Groups["part"];
  39. if (value.Success)
  40. {
  41. if (int.TryParse(value.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
  42. {
  43. result.PartNumber = intValue;
  44. }
  45. }
  46. }
  47. }
  48. }
  49. result.Success = result.ChapterNumber.HasValue || result.PartNumber.HasValue;
  50. return result;
  51. }
  52. }
  53. }