TVUtils.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. for (int i = 0; i < fileSystemChildren.Length; i++)
  92. {
  93. var child = fileSystemChildren[i];
  94. if (child.IsDirectory)
  95. {
  96. if (IsSeasonFolder(child.Path))
  97. {
  98. return true;
  99. }
  100. }
  101. else
  102. {
  103. if (!string.IsNullOrEmpty(EpisodeNumberFromFile(child.Path, false)))
  104. {
  105. return true;
  106. }
  107. }
  108. }
  109. return false;
  110. }
  111. public static string EpisodeNumberFromFile(string fullPath, bool isInSeason)
  112. {
  113. string fl = fullPath.ToLower();
  114. foreach (Regex r in episodeExpressions)
  115. {
  116. Match m = r.Match(fl);
  117. if (m.Success)
  118. return m.Groups["epnumber"].Value;
  119. }
  120. if (isInSeason)
  121. {
  122. foreach (Regex r in episodeExpressionsInASeasonFolder)
  123. {
  124. Match m = r.Match(fl);
  125. if (m.Success)
  126. return m.Groups["epnumber"].Value;
  127. }
  128. }
  129. return null;
  130. }
  131. }
  132. }