AudioBookNameParser.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #nullable enable
  2. #pragma warning disable CS1591
  3. using System.Globalization;
  4. using System.Text.RegularExpressions;
  5. using Emby.Naming.Common;
  6. namespace Emby.Naming.AudioBook
  7. {
  8. public class AudioBookNameParser
  9. {
  10. private readonly NamingOptions _options;
  11. public AudioBookNameParser(NamingOptions options)
  12. {
  13. _options = options;
  14. }
  15. public AudioBookNameParserResult Parse(string name)
  16. {
  17. AudioBookNameParserResult result = default;
  18. foreach (var expression in _options.AudioBookNamesExpressions)
  19. {
  20. var match = new Regex(expression, RegexOptions.IgnoreCase).Match(name);
  21. if (match.Success)
  22. {
  23. if (result.Name == null)
  24. {
  25. var value = match.Groups["name"];
  26. if (value.Success)
  27. {
  28. result.Name = value.Value;
  29. }
  30. }
  31. if (!result.Year.HasValue)
  32. {
  33. var value = match.Groups["year"];
  34. if (value.Success)
  35. {
  36. if (int.TryParse(value.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
  37. {
  38. result.Year = intValue;
  39. }
  40. }
  41. }
  42. }
  43. }
  44. if (string.IsNullOrEmpty(result.Name))
  45. {
  46. result.Name = name;
  47. }
  48. return result;
  49. }
  50. }
  51. }