AudioBookNameParser.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System.Globalization;
  2. using System.Text.RegularExpressions;
  3. using Emby.Naming.Common;
  4. namespace Emby.Naming.AudioBook
  5. {
  6. /// <summary>
  7. /// Helper class to retrieve name and year from audiobook previously retrieved name.
  8. /// </summary>
  9. public class AudioBookNameParser
  10. {
  11. private readonly NamingOptions _options;
  12. /// <summary>
  13. /// Initializes a new instance of the <see cref="AudioBookNameParser"/> class.
  14. /// </summary>
  15. /// <param name="options">Naming options containing AudioBookNamesExpressions.</param>
  16. public AudioBookNameParser(NamingOptions options)
  17. {
  18. _options = options;
  19. }
  20. /// <summary>
  21. /// Parse name and year from previously determined name of audiobook.
  22. /// </summary>
  23. /// <param name="name">Name of the audiobook.</param>
  24. /// <returns>Returns <see cref="AudioBookNameParserResult"/> object.</returns>
  25. public AudioBookNameParserResult Parse(string name)
  26. {
  27. AudioBookNameParserResult result = default;
  28. foreach (var expression in _options.AudioBookNamesExpressions)
  29. {
  30. var match = Regex.Match(name, expression, RegexOptions.IgnoreCase);
  31. if (match.Success)
  32. {
  33. if (result.Name is null)
  34. {
  35. var value = match.Groups["name"];
  36. if (value.Success)
  37. {
  38. result.Name = value.Value;
  39. }
  40. }
  41. if (!result.Year.HasValue)
  42. {
  43. var value = match.Groups["year"];
  44. if (value.Success)
  45. {
  46. if (int.TryParse(value.ValueSpan, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
  47. {
  48. result.Year = intValue;
  49. }
  50. }
  51. }
  52. }
  53. }
  54. if (string.IsNullOrEmpty(result.Name))
  55. {
  56. result.Name = name;
  57. }
  58. return result;
  59. }
  60. }
  61. }