EpisodePathParser.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. using Emby.Naming.Common;
  7. namespace Emby.Naming.TV
  8. {
  9. public class EpisodePathParser
  10. {
  11. private readonly NamingOptions _options;
  12. public EpisodePathParser(NamingOptions options)
  13. {
  14. _options = options;
  15. }
  16. public EpisodePathParserResult Parse(string path, bool IsDirectory, bool? isNamed = null, bool? isOptimistic = null, bool? supportsAbsoluteNumbers = null, bool fillExtendedInfo = true)
  17. {
  18. // Added to be able to use regex patterns which require a file extension.
  19. // There were no failed tests without this block, but to be safe, we can keep it until
  20. // the regex which require file extensions are modified so that they don't need them.
  21. if (IsDirectory)
  22. path += ".mp4";
  23. EpisodePathParserResult result = null;
  24. foreach (var expression in _options.EpisodeExpressions)
  25. {
  26. if (supportsAbsoluteNumbers.HasValue)
  27. {
  28. if (expression.SupportsAbsoluteEpisodeNumbers != supportsAbsoluteNumbers.Value)
  29. {
  30. continue;
  31. }
  32. }
  33. if (isNamed.HasValue)
  34. {
  35. if (expression.IsNamed != isNamed.Value)
  36. {
  37. continue;
  38. }
  39. }
  40. if (isOptimistic.HasValue)
  41. {
  42. if (expression.IsOptimistic != isOptimistic.Value)
  43. {
  44. continue;
  45. }
  46. }
  47. var currentResult = Parse(path, expression);
  48. if (currentResult.Success)
  49. {
  50. result = currentResult;
  51. break;
  52. }
  53. }
  54. if (result != null && fillExtendedInfo)
  55. {
  56. FillAdditional(path, result);
  57. if (!string.IsNullOrEmpty(result.SeriesName))
  58. {
  59. result.SeriesName = result.SeriesName
  60. .Trim()
  61. .Trim(new[] { '_', '.', '-' })
  62. .Trim();
  63. }
  64. }
  65. return result ?? new EpisodePathParserResult();
  66. }
  67. private static EpisodePathParserResult Parse(string name, EpisodeExpression expression)
  68. {
  69. var result = new EpisodePathParserResult();
  70. // This is a hack to handle wmc naming
  71. if (expression.IsByDate)
  72. {
  73. name = name.Replace('_', '-');
  74. }
  75. var match = expression.Regex.Match(name);
  76. // (Full)(Season)(Episode)(Extension)
  77. if (match.Success && match.Groups.Count >= 3)
  78. {
  79. if (expression.IsByDate)
  80. {
  81. DateTime date;
  82. if (expression.DateTimeFormats.Length > 0)
  83. {
  84. if (DateTime.TryParseExact(match.Groups[0].Value,
  85. expression.DateTimeFormats,
  86. CultureInfo.InvariantCulture,
  87. DateTimeStyles.None,
  88. out date))
  89. {
  90. result.Year = date.Year;
  91. result.Month = date.Month;
  92. result.Day = date.Day;
  93. result.Success = true;
  94. }
  95. }
  96. else
  97. {
  98. if (DateTime.TryParse(match.Groups[0].Value, out date))
  99. {
  100. result.Year = date.Year;
  101. result.Month = date.Month;
  102. result.Day = date.Day;
  103. result.Success = true;
  104. }
  105. }
  106. // TODO: Only consider success if date successfully parsed?
  107. result.Success = true;
  108. }
  109. else if (expression.IsNamed)
  110. {
  111. if (int.TryParse(match.Groups["seasonnumber"].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var num))
  112. {
  113. result.SeasonNumber = num;
  114. }
  115. if (int.TryParse(match.Groups["epnumber"].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out num))
  116. {
  117. result.EpisodeNumber = num;
  118. }
  119. var endingNumberGroup = match.Groups["endingepnumber"];
  120. if (endingNumberGroup.Success)
  121. {
  122. // Will only set EndingEpsiodeNumber if the captured number is not followed by additional numbers
  123. // or a 'p' or 'i' as what you would get with a pixel resolution specification.
  124. // It avoids erroneous parsing of something like "series-s09e14-1080p.mkv" as a multi-episode from E14 to E108
  125. int nextIndex = endingNumberGroup.Index + endingNumberGroup.Length;
  126. if (nextIndex >= name.Length || "0123456789iIpP".IndexOf(name[nextIndex]) == -1)
  127. {
  128. if (int.TryParse(endingNumberGroup.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out num))
  129. {
  130. result.EndingEpsiodeNumber = num;
  131. }
  132. }
  133. }
  134. result.SeriesName = match.Groups["seriesname"].Value;
  135. result.Success = result.EpisodeNumber.HasValue;
  136. }
  137. else
  138. {
  139. if (int.TryParse(match.Groups[1].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var num))
  140. {
  141. result.SeasonNumber = num;
  142. }
  143. if (int.TryParse(match.Groups[2].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out num))
  144. {
  145. result.EpisodeNumber = num;
  146. }
  147. result.Success = result.EpisodeNumber.HasValue;
  148. }
  149. // Invalidate match when the season is 200 through 1927 or above 2500
  150. // because it is an error unless the TV show is intentionally using false season numbers.
  151. // It avoids erroneous parsing of something like "Series Special (1920x1080).mkv" as being season 1920 episode 1080.
  152. if (result.SeasonNumber >= 200 && result.SeasonNumber < 1928 || result.SeasonNumber > 2500)
  153. result.Success = false;
  154. result.IsByDate = expression.IsByDate;
  155. }
  156. return result;
  157. }
  158. private void FillAdditional(string path, EpisodePathParserResult info)
  159. {
  160. var expressions = _options.MultipleEpisodeExpressions.ToList();
  161. if (string.IsNullOrEmpty(info.SeriesName))
  162. {
  163. expressions.InsertRange(0, _options.EpisodeExpressions.Where(i => i.IsNamed));
  164. }
  165. FillAdditional(path, info, expressions);
  166. }
  167. private void FillAdditional(string path, EpisodePathParserResult info, IEnumerable<EpisodeExpression> expressions)
  168. {
  169. var results = expressions
  170. .Where(i => i.IsNamed)
  171. .Select(i => Parse(path, i))
  172. .Where(i => i.Success);
  173. foreach (var result in results)
  174. {
  175. if (string.IsNullOrEmpty(info.SeriesName))
  176. {
  177. info.SeriesName = result.SeriesName;
  178. }
  179. if (!info.EndingEpsiodeNumber.HasValue && info.EpisodeNumber.HasValue)
  180. {
  181. info.EndingEpsiodeNumber = result.EndingEpsiodeNumber;
  182. }
  183. if (!string.IsNullOrEmpty(info.SeriesName))
  184. {
  185. if (!info.EpisodeNumber.HasValue || info.EndingEpsiodeNumber.HasValue)
  186. {
  187. break;
  188. }
  189. }
  190. }
  191. }
  192. }
  193. }