EpisodePathParser.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using Emby.Naming.Common;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Text.RegularExpressions;
  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 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. int num;
  112. if (int.TryParse(match.Groups["seasonnumber"].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out num))
  113. {
  114. result.SeasonNumber = num;
  115. }
  116. if (int.TryParse(match.Groups["epnumber"].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out num))
  117. {
  118. result.EpisodeNumber = num;
  119. }
  120. Group endingNumberGroup = match.Groups["endingepnumber"];
  121. if (endingNumberGroup.Success)
  122. {
  123. // Will only set EndingEpsiodeNumber if the captured number is not followed by additional numbers
  124. // or a 'p' or 'i' as what you would get with a pixel resolution specification.
  125. // It avoids erroneous parsing of something like "series-s09e14-1080p.mkv" as a multi-episode from E14 to E108
  126. int nextIndex = endingNumberGroup.Index + endingNumberGroup.Length;
  127. if (nextIndex >= name.Length || "0123456789iIpP".IndexOf(name[nextIndex]) == -1)
  128. {
  129. if (int.TryParse(endingNumberGroup.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out num))
  130. {
  131. result.EndingEpsiodeNumber = num;
  132. }
  133. }
  134. }
  135. result.SeriesName = match.Groups["seriesname"].Value;
  136. result.Success = result.EpisodeNumber.HasValue;
  137. }
  138. else
  139. {
  140. int num;
  141. if (int.TryParse(match.Groups[1].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out num))
  142. {
  143. result.SeasonNumber = num;
  144. }
  145. if (int.TryParse(match.Groups[2].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out num))
  146. {
  147. result.EpisodeNumber = num;
  148. }
  149. result.Success = result.EpisodeNumber.HasValue;
  150. }
  151. // Invalidate match when the season is 200 through 1927 or above 2500
  152. // because it is an error unless the TV show is intentionally using false season numbers.
  153. // It avoids erroneous parsing of something like "Series Special (1920x1080).mkv" as being season 1920 episode 1080.
  154. if (result.SeasonNumber >= 200 && result.SeasonNumber < 1928 || result.SeasonNumber > 2500)
  155. result.Success = false;
  156. result.IsByDate = expression.IsByDate;
  157. }
  158. return result;
  159. }
  160. private void FillAdditional(string path, EpisodePathParserResult info)
  161. {
  162. var expressions = _options.MultipleEpisodeExpressions.ToList();
  163. if (string.IsNullOrEmpty(info.SeriesName))
  164. {
  165. expressions.InsertRange(0, _options.EpisodeExpressions.Where(i => i.IsNamed));
  166. }
  167. FillAdditional(path, info, expressions);
  168. }
  169. private void FillAdditional(string path, EpisodePathParserResult info, IEnumerable<EpisodeExpression> expressions)
  170. {
  171. var results = expressions
  172. .Where(i => i.IsNamed)
  173. .Select(i => Parse(path, i))
  174. .Where(i => i.Success);
  175. foreach (var result in results)
  176. {
  177. if (string.IsNullOrEmpty(info.SeriesName))
  178. {
  179. info.SeriesName = result.SeriesName;
  180. }
  181. if (!info.EndingEpsiodeNumber.HasValue && info.EpisodeNumber.HasValue)
  182. {
  183. info.EndingEpsiodeNumber = result.EndingEpsiodeNumber;
  184. }
  185. if (!string.IsNullOrEmpty(info.SeriesName))
  186. {
  187. if (!info.EpisodeNumber.HasValue || info.EndingEpsiodeNumber.HasValue)
  188. {
  189. break;
  190. }
  191. }
  192. }
  193. }
  194. }
  195. }