CleanDateTimeParser.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System.Collections.Generic;
  2. using System.Globalization;
  3. using System.Text.RegularExpressions;
  4. namespace Emby.Naming.Video
  5. {
  6. /// <summary>
  7. /// <see href="http://kodi.wiki/view/Advancedsettings.xml#video" />.
  8. /// </summary>
  9. public static class CleanDateTimeParser
  10. {
  11. /// <summary>
  12. /// Attempts to clean the name.
  13. /// </summary>
  14. /// <param name="name">Name of video.</param>
  15. /// <param name="cleanDateTimeRegexes">Optional list of regexes to clean the name.</param>
  16. /// <returns>Returns <see cref="CleanDateTimeResult"/> object.</returns>
  17. public static CleanDateTimeResult Clean(string name, IReadOnlyList<Regex> cleanDateTimeRegexes)
  18. {
  19. CleanDateTimeResult result = new CleanDateTimeResult(name);
  20. if (string.IsNullOrEmpty(name))
  21. {
  22. return result;
  23. }
  24. var len = cleanDateTimeRegexes.Count;
  25. for (int i = 0; i < len; i++)
  26. {
  27. if (TryClean(name, cleanDateTimeRegexes[i], ref result))
  28. {
  29. return result;
  30. }
  31. }
  32. return result;
  33. }
  34. private static bool TryClean(string name, Regex expression, ref CleanDateTimeResult result)
  35. {
  36. var match = expression.Match(name);
  37. if (match.Success
  38. && match.Groups.Count == 5
  39. && match.Groups[1].Success
  40. && match.Groups[2].Success
  41. && int.TryParse(match.Groups[2].ValueSpan, NumberStyles.Integer, CultureInfo.InvariantCulture, out var year))
  42. {
  43. result = new CleanDateTimeResult(match.Groups[1].Value.TrimEnd(), year);
  44. return true;
  45. }
  46. return false;
  47. }
  48. }
  49. }