SeriesPathParser.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using Emby.Naming.Common;
  2. namespace Emby.Naming.TV
  3. {
  4. /// <summary>
  5. /// Used to parse information about series from paths containing more information that only the series name.
  6. /// Uses the same regular expressions as the EpisodePathParser but have different success criteria.
  7. /// </summary>
  8. public static class SeriesPathParser
  9. {
  10. /// <summary>
  11. /// Parses information about series from path.
  12. /// </summary>
  13. /// <param name="options"><see cref="NamingOptions"/> object containing EpisodeExpressions and MultipleEpisodeExpressions.</param>
  14. /// <param name="path">Path.</param>
  15. /// <returns>Returns <see cref="SeriesPathParserResult"/> object.</returns>
  16. public static SeriesPathParserResult Parse(NamingOptions options, string path)
  17. {
  18. SeriesPathParserResult? result = null;
  19. foreach (var expression in options.EpisodeExpressions)
  20. {
  21. var currentResult = Parse(path, expression);
  22. if (currentResult.Success)
  23. {
  24. result = currentResult;
  25. break;
  26. }
  27. }
  28. if (result is not null)
  29. {
  30. if (!string.IsNullOrEmpty(result.SeriesName))
  31. {
  32. result.SeriesName = result.SeriesName.Trim(' ', '_', '.', '-');
  33. }
  34. }
  35. return result ?? new SeriesPathParserResult();
  36. }
  37. private static SeriesPathParserResult Parse(string name, EpisodeExpression expression)
  38. {
  39. var result = new SeriesPathParserResult();
  40. var match = expression.Regex.Match(name);
  41. if (match.Success && match.Groups.Count >= 3)
  42. {
  43. if (expression.IsNamed)
  44. {
  45. result.SeriesName = match.Groups["seriesname"].Value;
  46. result.Success = !string.IsNullOrEmpty(result.SeriesName) && !match.Groups["seasonnumber"].ValueSpan.IsEmpty;
  47. }
  48. }
  49. return result;
  50. }
  51. }
  52. }