AudioBookFilePathParser.cs 2.6 KB

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