SeriesPathParser.cs 2.1 KB

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