TVUtils.cs 12 KB

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