SeriesResolver.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System.IO;
  2. using System.Text.RegularExpressions;
  3. using Emby.Naming.Common;
  4. namespace Emby.Naming.TV
  5. {
  6. /// <summary>
  7. /// Used to resolve information about series from path.
  8. /// </summary>
  9. public static class SeriesResolver
  10. {
  11. /// <summary>
  12. /// Regex that matches strings of at least 2 characters separated by a dot or underscore.
  13. /// Used for removing separators between words, i.e turns "The_show" into "The show" while
  14. /// preserving namings like "S.H.O.W".
  15. /// </summary>
  16. private static readonly Regex _seriesNameRegex = new Regex(@"((?<a>[^\._]{2,})[\._]*)|([\._](?<b>[^\._]{2,}))", RegexOptions.Compiled);
  17. /// <summary>
  18. /// Resolve information about series from path.
  19. /// </summary>
  20. /// <param name="options"><see cref="NamingOptions"/> object passed to <see cref="SeriesPathParser"/>.</param>
  21. /// <param name="path">Path to series.</param>
  22. /// <returns>SeriesInfo.</returns>
  23. public static SeriesInfo Resolve(NamingOptions options, string path)
  24. {
  25. string seriesName = Path.GetFileName(path);
  26. SeriesPathParserResult result = SeriesPathParser.Parse(options, path);
  27. if (result.Success)
  28. {
  29. if (!string.IsNullOrEmpty(result.SeriesName))
  30. {
  31. seriesName = result.SeriesName;
  32. }
  33. }
  34. if (!string.IsNullOrEmpty(seriesName))
  35. {
  36. seriesName = _seriesNameRegex.Replace(seriesName, "${a} ${b}").Trim();
  37. }
  38. return new SeriesInfo(path)
  39. {
  40. Name = seriesName
  41. };
  42. }
  43. }
  44. }