TVUtils.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using MediaBrowser.Controller.IO;
  4. namespace MediaBrowser.TV
  5. {
  6. public static class TVUtils
  7. {
  8. /// <summary>
  9. /// A season folder must contain one of these somewhere in the name
  10. /// </summary>
  11. private static string[] SeasonFolderNames = new string[] {
  12. "season",
  13. "sæson",
  14. "temporada",
  15. "saison",
  16. "staffel"
  17. };
  18. /// <summary>
  19. /// Used to detect paths that represent episodes, need to make sure they don't also
  20. /// match movie titles like "2001 A Space..."
  21. /// Currently we limit the numbers here to 2 digits to try and avoid this
  22. /// </summary>
  23. /// <remarks>
  24. /// The order here is important, if the order is changed some of the later
  25. /// ones might incorrectly match things that higher ones would have caught.
  26. /// The most restrictive expressions should appear first
  27. /// </remarks>
  28. private static readonly Regex[] episodeExpressions = new Regex[] {
  29. new Regex(@".*\\[s|S]?(?<seasonnumber>\d{1,2})[x|X](?<epnumber>\d{1,3})[^\\]*$", RegexOptions.Compiled), // 01x02 blah.avi S01x01 balh.avi
  30. new Regex(@".*\\[s|S](?<seasonnumber>\d{1,2})x?[e|E](?<epnumber>\d{1,3})[^\\]*$", RegexOptions.Compiled), // S01E02 blah.avi, S01xE01 blah.avi
  31. new Regex(@".*\\(?<seriesname>[^\\]*)[s|S]?(?<seasonnumber>\d{1,2})[x|X](?<epnumber>\d{1,3})[^\\]*$", RegexOptions.Compiled), // 01x02 blah.avi S01x01 balh.avi
  32. new Regex(@".*\\(?<seriesname>[^\\]*)[s|S](?<seasonnumber>\d{1,2})[x|X|\.]?[e|E](?<epnumber>\d{1,3})[^\\]*$", RegexOptions.Compiled) // S01E02 blah.avi, S01xE01 blah.avi
  33. };
  34. /// <summary>
  35. /// To avoid the following matching movies they are only valid when contained in a folder which has been matched as a being season
  36. /// </summary>
  37. private static readonly Regex[] episodeExpressionsInASeasonFolder = new Regex[] {
  38. new Regex(@".*\\(?<epnumber>\d{1,2})\s?-\s?[^\\]*$", RegexOptions.Compiled), // 01 - blah.avi, 01-blah.avi
  39. new Regex(@".*\\(?<epnumber>\d{1,2})[^\d\\]*[^\\]*$", RegexOptions.Compiled), // 01.avi, 01.blah.avi "01 - 22 blah.avi"
  40. new Regex(@".*\\(?<seasonnumber>\d)(?<epnumber>\d{1,2})[^\d\\]+[^\\]*$", RegexOptions.Compiled), // 01.avi, 01.blah.avi
  41. new Regex(@".*\\\D*\d+(?<epnumber>\d{2})", RegexOptions.Compiled) // hell0 - 101 - hello.avi
  42. };
  43. public static int? GetSeasonNumberFromPath(string path)
  44. {
  45. // Look for one of the season folder names
  46. foreach (string name in SeasonFolderNames)
  47. {
  48. int index = path.IndexOf(name, StringComparison.OrdinalIgnoreCase);
  49. if (index != -1)
  50. {
  51. return GetSeasonNumberFromPathSubstring(path.Substring(index + name.Length));
  52. }
  53. }
  54. return null;
  55. }
  56. /// <summary>
  57. /// Extracts the season number from the second half of the Season folder name (everything after "Season", or "Staffel")
  58. /// </summary>
  59. private static int? GetSeasonNumberFromPathSubstring(string path)
  60. {
  61. int numericStart = -1;
  62. int length = 0;
  63. // Find out where the numbers start, and then keep going until they end
  64. for (int i = 0; i < path.Length; i++)
  65. {
  66. if (char.IsNumber(path, i))
  67. {
  68. if (numericStart == -1)
  69. {
  70. numericStart = i;
  71. }
  72. length++;
  73. }
  74. else if (numericStart != -1)
  75. {
  76. break;
  77. }
  78. }
  79. if (numericStart == -1)
  80. {
  81. return null;
  82. }
  83. return int.Parse(path.Substring(numericStart, length));
  84. }
  85. public static bool IsSeasonFolder(string path)
  86. {
  87. return GetSeasonNumberFromPath(path) != null;
  88. }
  89. public static bool IsSeriesFolder(string path, WIN32_FIND_DATA[] fileSystemChildren)
  90. {
  91. // A folder with more than 3 non-season folders in will not becounted as a series
  92. int nonSeriesFolders = 0;
  93. for (int i = 0; i < fileSystemChildren.Length; i++)
  94. {
  95. var child = fileSystemChildren[i];
  96. if (child.IsHidden || child.IsSystemFile)
  97. {
  98. continue;
  99. }
  100. if (child.IsDirectory)
  101. {
  102. if (IsSeasonFolder(child.Path))
  103. {
  104. return true;
  105. }
  106. else
  107. {
  108. nonSeriesFolders++;
  109. if (nonSeriesFolders >= 3)
  110. {
  111. return false;
  112. }
  113. }
  114. }
  115. else
  116. {
  117. if (!string.IsNullOrEmpty(EpisodeNumberFromFile(child.Path, false)))
  118. {
  119. return true;
  120. }
  121. }
  122. }
  123. return false;
  124. }
  125. public static string EpisodeNumberFromFile(string fullPath, bool isInSeason)
  126. {
  127. string fl = fullPath.ToLower();
  128. foreach (Regex r in episodeExpressions)
  129. {
  130. Match m = r.Match(fl);
  131. if (m.Success)
  132. return m.Groups["epnumber"].Value;
  133. }
  134. if (isInSeason)
  135. {
  136. foreach (Regex r in episodeExpressionsInASeasonFolder)
  137. {
  138. Match m = r.Match(fl);
  139. if (m.Success)
  140. return m.Groups["epnumber"].Value;
  141. }
  142. }
  143. return null;
  144. }
  145. }
  146. }