2
0

AudioBookFilePathParser.cs 2.9 KB

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