TVUtils.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using MediaBrowser.Controller.IO;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. using MediaBrowser.Controller.Resolvers;
  7. namespace MediaBrowser.Controller.Library
  8. {
  9. /// <summary>
  10. /// Class TVUtils
  11. /// </summary>
  12. public static class TVUtils
  13. {
  14. /// <summary>
  15. /// The TVDB API key
  16. /// </summary>
  17. public static readonly string TVDBApiKey = "B89CE93890E9419B";
  18. /// <summary>
  19. /// The banner URL
  20. /// </summary>
  21. public static readonly string BannerUrl = "http://www.thetvdb.com/banners/";
  22. /// <summary>
  23. /// A season folder must contain one of these somewhere in the name
  24. /// </summary>
  25. private static readonly string[] SeasonFolderNames = new[]
  26. {
  27. "season",
  28. "sæson",
  29. "temporada",
  30. "saison",
  31. "staffel"
  32. };
  33. /// <summary>
  34. /// Used to detect paths that represent episodes, need to make sure they don't also
  35. /// match movie titles like "2001 A Space..."
  36. /// Currently we limit the numbers here to 2 digits to try and avoid this
  37. /// </summary>
  38. private static readonly Regex[] EpisodeExpressions = new[]
  39. {
  40. new Regex(
  41. @".*\\[s|S]?(?<seasonnumber>\d{1,2})[x|X](?<epnumber>\d{1,3})[^\\]*$",
  42. RegexOptions.Compiled),
  43. // 01x02 blah.avi S01x01 balh.avi
  44. new Regex(
  45. @".*\\[s|S](?<seasonnumber>\d{1,2})x?[e|E](?<epnumber>\d{1,3})[^\\]*$",
  46. RegexOptions.Compiled),
  47. // S01E02 blah.avi, S01xE01 blah.avi
  48. new Regex(
  49. @".*\\(?<seriesname>[^\\]*)[s|S]?(?<seasonnumber>\d{1,2})[x|X](?<epnumber>\d{1,3})[^\\]*$",
  50. RegexOptions.Compiled),
  51. // 01x02 blah.avi S01x01 balh.avi
  52. new Regex(
  53. @".*\\(?<seriesname>[^\\]*)[s|S](?<seasonnumber>\d{1,2})[x|X|\.]?[e|E](?<epnumber>\d{1,3})[^\\]*$",
  54. RegexOptions.Compiled)
  55. // S01E02 blah.avi, S01xE01 blah.avi
  56. };
  57. /// <summary>
  58. /// To avoid the following matching movies they are only valid when contained in a folder which has been matched as a being season
  59. /// </summary>
  60. private static readonly Regex[] EpisodeExpressionsInASeasonFolder = new[]
  61. {
  62. new Regex(
  63. @".*\\(?<epnumber>\d{1,2})\s?-\s?[^\\]*$",
  64. RegexOptions.Compiled),
  65. // 01 - blah.avi, 01-blah.avi
  66. new Regex(
  67. @".*\\(?<epnumber>\d{1,2})[^\d\\]*[^\\]*$",
  68. RegexOptions.Compiled),
  69. // 01.avi, 01.blah.avi "01 - 22 blah.avi"
  70. new Regex(
  71. @".*\\(?<seasonnumber>\d)(?<epnumber>\d{1,2})[^\d\\]+[^\\]*$",
  72. RegexOptions.Compiled),
  73. // 01.avi, 01.blah.avi
  74. new Regex(
  75. @".*\\\D*\d+(?<epnumber>\d{2})",
  76. RegexOptions.Compiled)
  77. // hell0 - 101 - hello.avi
  78. };
  79. /// <summary>
  80. /// Gets the season number from path.
  81. /// </summary>
  82. /// <param name="path">The path.</param>
  83. /// <returns>System.Nullable{System.Int32}.</returns>
  84. public static int? GetSeasonNumberFromPath(string path)
  85. {
  86. // Look for one of the season folder names
  87. foreach (var name in SeasonFolderNames)
  88. {
  89. int index = path.IndexOf(name, StringComparison.OrdinalIgnoreCase);
  90. if (index != -1)
  91. {
  92. return GetSeasonNumberFromPathSubstring(path.Substring(index + name.Length));
  93. }
  94. }
  95. return null;
  96. }
  97. /// <summary>
  98. /// Extracts the season number from the second half of the Season folder name (everything after "Season", or "Staffel")
  99. /// </summary>
  100. /// <param name="path">The path.</param>
  101. /// <returns>System.Nullable{System.Int32}.</returns>
  102. private static int? GetSeasonNumberFromPathSubstring(string path)
  103. {
  104. int numericStart = -1;
  105. int length = 0;
  106. // Find out where the numbers start, and then keep going until they end
  107. for (int i = 0; i < path.Length; i++)
  108. {
  109. if (char.IsNumber(path, i))
  110. {
  111. if (numericStart == -1)
  112. {
  113. numericStart = i;
  114. }
  115. length++;
  116. }
  117. else if (numericStart != -1)
  118. {
  119. break;
  120. }
  121. }
  122. if (numericStart == -1)
  123. {
  124. return null;
  125. }
  126. return int.Parse(path.Substring(numericStart, length));
  127. }
  128. /// <summary>
  129. /// Determines whether [is season folder] [the specified path].
  130. /// </summary>
  131. /// <param name="path">The path.</param>
  132. /// <returns><c>true</c> if [is season folder] [the specified path]; otherwise, <c>false</c>.</returns>
  133. public static bool IsSeasonFolder(string path)
  134. {
  135. return GetSeasonNumberFromPath(path) != null;
  136. }
  137. /// <summary>
  138. /// Determines whether [is series folder] [the specified path].
  139. /// </summary>
  140. /// <param name="path">The path.</param>
  141. /// <param name="fileSystemChildren">The file system children.</param>
  142. /// <returns><c>true</c> if [is series folder] [the specified path]; otherwise, <c>false</c>.</returns>
  143. public static bool IsSeriesFolder(string path, IEnumerable<WIN32_FIND_DATA> fileSystemChildren)
  144. {
  145. // A folder with more than 3 non-season folders in will not becounted as a series
  146. var nonSeriesFolders = 0;
  147. foreach (var child in fileSystemChildren)
  148. {
  149. if (child.IsHidden || child.IsSystemFile)
  150. {
  151. continue;
  152. }
  153. if (child.IsDirectory)
  154. {
  155. if (IsSeasonFolder(child.Path))
  156. {
  157. return true;
  158. }
  159. nonSeriesFolders++;
  160. if (nonSeriesFolders >= 3)
  161. {
  162. return false;
  163. }
  164. }
  165. else
  166. {
  167. if (EntityResolutionHelper.IsVideoFile(child.Path) &&
  168. !string.IsNullOrEmpty(EpisodeNumberFromFile(child.Path, false)))
  169. {
  170. return true;
  171. }
  172. }
  173. }
  174. return false;
  175. }
  176. /// <summary>
  177. /// Episodes the number from file.
  178. /// </summary>
  179. /// <param name="fullPath">The full path.</param>
  180. /// <param name="isInSeason">if set to <c>true</c> [is in season].</param>
  181. /// <returns>System.String.</returns>
  182. public static string EpisodeNumberFromFile(string fullPath, bool isInSeason)
  183. {
  184. string fl = fullPath.ToLower();
  185. foreach (var r in EpisodeExpressions)
  186. {
  187. Match m = r.Match(fl);
  188. if (m.Success)
  189. return m.Groups["epnumber"].Value;
  190. }
  191. if (isInSeason)
  192. {
  193. var match = EpisodeExpressionsInASeasonFolder.Select(r => r.Match(fl))
  194. .FirstOrDefault(m => m.Success);
  195. if (match != null)
  196. {
  197. return match.Value;
  198. }
  199. }
  200. return null;
  201. }
  202. /// <summary>
  203. /// Seasons the number from episode file.
  204. /// </summary>
  205. /// <param name="fullPath">The full path.</param>
  206. /// <returns>System.String.</returns>
  207. public static string SeasonNumberFromEpisodeFile(string fullPath)
  208. {
  209. string fl = fullPath.ToLower();
  210. foreach (var r in EpisodeExpressions)
  211. {
  212. Match m = r.Match(fl);
  213. if (m.Success)
  214. {
  215. Group g = m.Groups["seasonnumber"];
  216. if (g != null)
  217. return g.Value;
  218. return null;
  219. }
  220. }
  221. return null;
  222. }
  223. /// <summary>
  224. /// Gets the air days.
  225. /// </summary>
  226. /// <param name="day">The day.</param>
  227. /// <returns>List{DayOfWeek}.</returns>
  228. public static List<DayOfWeek> GetAirDays(string day)
  229. {
  230. if (!string.IsNullOrWhiteSpace(day))
  231. {
  232. if (day.Equals("Daily", StringComparison.OrdinalIgnoreCase))
  233. {
  234. return new List<DayOfWeek>
  235. {
  236. DayOfWeek.Sunday,
  237. DayOfWeek.Monday,
  238. DayOfWeek.Tuesday,
  239. DayOfWeek.Wednesday,
  240. DayOfWeek.Thursday,
  241. DayOfWeek.Friday,
  242. DayOfWeek.Saturday
  243. };
  244. }
  245. DayOfWeek value;
  246. if (Enum.TryParse(day, true, out value))
  247. {
  248. return new List<DayOfWeek>
  249. {
  250. value
  251. };
  252. }
  253. return new List<DayOfWeek>();
  254. }
  255. return null;
  256. }
  257. }
  258. }