TVUtils.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Resolvers;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text.RegularExpressions;
  9. namespace MediaBrowser.Controller.Library
  10. {
  11. /// <summary>
  12. /// Class TVUtils
  13. /// </summary>
  14. public static class TVUtils
  15. {
  16. /// <summary>
  17. /// The TVDB API key
  18. /// </summary>
  19. public static readonly string TvdbApiKey = "B89CE93890E9419B";
  20. /// <summary>
  21. /// The banner URL
  22. /// </summary>
  23. public static readonly string BannerUrl = "http://www.thetvdb.com/banners/";
  24. /// <summary>
  25. /// A season folder must contain one of these somewhere in the name
  26. /// </summary>
  27. private static readonly string[] SeasonFolderNames = new[]
  28. {
  29. "season",
  30. "sæson",
  31. "temporada",
  32. "saison",
  33. "staffel"
  34. };
  35. /// <summary>
  36. /// Used to detect paths that represent episodes, need to make sure they don't also
  37. /// match movie titles like "2001 A Space..."
  38. /// Currently we limit the numbers here to 2 digits to try and avoid this
  39. /// </summary>
  40. private static readonly Regex[] EpisodeExpressions = new[]
  41. {
  42. new Regex(
  43. @".*\\[sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3})[^\\]*$",
  44. RegexOptions.Compiled),
  45. new Regex(
  46. @".*\\[sS](?<seasonnumber>\d{1,4})[x,X]?[eE](?<epnumber>\d{1,3})[^\\]*$",
  47. RegexOptions.Compiled),
  48. new Regex(
  49. @".*\\(?<seriesname>((?![sS]?\d{1,4}[xX]\d{1,3})[^\\])*)?([sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3}))[^\\]*$",
  50. RegexOptions.Compiled),
  51. new Regex(
  52. @".*\\(?<seriesname>[^\\]*)[sS](?<seasonnumber>\d{1,4})[xX\.]?[eE](?<epnumber>\d{1,3})[^\\]*$",
  53. RegexOptions.Compiled)
  54. };
  55. private static readonly Regex[] MultipleEpisodeExpressions = new[]
  56. {
  57. new Regex(
  58. @".*\\[sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3})((-| - )\d{1,4}[eExX](?<endingepnumber>\d{1,3}))+[^\\]*$",
  59. RegexOptions.Compiled),
  60. new Regex(
  61. @".*\\[sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3})((-| - )\d{1,4}[xX][eE](?<endingepnumber>\d{1,3}))+[^\\]*$",
  62. RegexOptions.Compiled),
  63. new Regex(
  64. @".*\\[sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3})((-| - )?[xXeE](?<endingepnumber>\d{1,3}))+[^\\]*$",
  65. RegexOptions.Compiled),
  66. new Regex(
  67. @".*\\[sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3})(-[xE]?[eE]?(?<endingepnumber>\d{1,3}))+[^\\]*$",
  68. RegexOptions.Compiled),
  69. new Regex(
  70. @".*\\(?<seriesname>((?![sS]?\d{1,4}[xX]\d{1,3})[^\\])*)?([sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3}))((-| - )\d{1,4}[xXeE](?<endingepnumber>\d{1,3}))+[^\\]*$",
  71. RegexOptions.Compiled),
  72. new Regex(
  73. @".*\\(?<seriesname>((?![sS]?\d{1,4}[xX]\d{1,3})[^\\])*)?([sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3}))((-| - )\d{1,4}[xX][eE](?<endingepnumber>\d{1,3}))+[^\\]*$",
  74. RegexOptions.Compiled),
  75. new Regex(
  76. @".*\\(?<seriesname>((?![sS]?\d{1,4}[xX]\d{1,3})[^\\])*)?([sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3}))((-| - )?[xXeE](?<endingepnumber>\d{1,3}))+[^\\]*$",
  77. RegexOptions.Compiled),
  78. new Regex(
  79. @".*\\(?<seriesname>((?![sS]?\d{1,4}[xX]\d{1,3})[^\\])*)?([sS]?(?<seasonnumber>\d{1,4})[xX](?<epnumber>\d{1,3}))(-[xX]?[eE]?(?<endingepnumber>\d{1,3}))+[^\\]*$",
  80. RegexOptions.Compiled),
  81. new Regex(
  82. @".*\\(?<seriesname>[^\\]*)[sS](?<seasonnumber>\d{1,4})[xX\.]?[eE](?<epnumber>\d{1,3})((-| - )?[xXeE](?<endingepnumber>\d{1,3}))+[^\\]*$",
  83. RegexOptions.Compiled),
  84. new Regex(
  85. @".*\\(?<seriesname>[^\\]*)[sS](?<seasonnumber>\d{1,4})[xX\.]?[eE](?<epnumber>\d{1,3})(-[xX]?[eE]?(?<endingepnumber>\d{1,3}))+[^\\]*$",
  86. RegexOptions.Compiled)
  87. };
  88. /// <summary>
  89. /// To avoid the following matching movies they are only valid when contained in a folder which has been matched as a being season
  90. /// </summary>
  91. private static readonly Regex[] EpisodeExpressionsInASeasonFolder = new[]
  92. {
  93. new Regex(
  94. @".*\\(?<epnumber>\d{1,2})\s?-\s?[^\\]*$",
  95. RegexOptions.Compiled),
  96. // 01 - blah.avi, 01-blah.avi
  97. new Regex(
  98. @".*\\(?<epnumber>\d{1,2})[^\d\\]*[^\\]*$",
  99. RegexOptions.Compiled),
  100. // 01.avi, 01.blah.avi "01 - 22 blah.avi"
  101. new Regex(
  102. @".*\\(?<seasonnumber>\d)(?<epnumber>\d{1,2})[^\d\\]+[^\\]*$",
  103. RegexOptions.Compiled),
  104. // 01.avi, 01.blah.avi
  105. new Regex(
  106. @".*\\\D*\d+(?<epnumber>\d{2})",
  107. RegexOptions.Compiled)
  108. // hell0 - 101 - hello.avi
  109. };
  110. /// <summary>
  111. /// Gets the season number from path.
  112. /// </summary>
  113. /// <param name="path">The path.</param>
  114. /// <returns>System.Nullable{System.Int32}.</returns>
  115. public static int? GetSeasonNumberFromPath(string path)
  116. {
  117. var filename = Path.GetFileName(path);
  118. // Look for one of the season folder names
  119. foreach (var name in SeasonFolderNames)
  120. {
  121. var index = filename.IndexOf(name, StringComparison.OrdinalIgnoreCase);
  122. if (index != -1)
  123. {
  124. return GetSeasonNumberFromPathSubstring(filename.Substring(index + name.Length));
  125. }
  126. }
  127. return null;
  128. }
  129. /// <summary>
  130. /// Extracts the season number from the second half of the Season folder name (everything after "Season", or "Staffel")
  131. /// </summary>
  132. /// <param name="path">The path.</param>
  133. /// <returns>System.Nullable{System.Int32}.</returns>
  134. private static int? GetSeasonNumberFromPathSubstring(string path)
  135. {
  136. int numericStart = -1;
  137. int length = 0;
  138. // Find out where the numbers start, and then keep going until they end
  139. for (var i = 0; i < path.Length; i++)
  140. {
  141. if (char.IsNumber(path, i))
  142. {
  143. if (numericStart == -1)
  144. {
  145. numericStart = i;
  146. }
  147. length++;
  148. }
  149. else if (numericStart != -1)
  150. {
  151. break;
  152. }
  153. }
  154. if (numericStart == -1)
  155. {
  156. return null;
  157. }
  158. return int.Parse(path.Substring(numericStart, length));
  159. }
  160. /// <summary>
  161. /// Determines whether [is season folder] [the specified path].
  162. /// </summary>
  163. /// <param name="path">The path.</param>
  164. /// <returns><c>true</c> if [is season folder] [the specified path]; otherwise, <c>false</c>.</returns>
  165. private static bool IsSeasonFolder(string path)
  166. {
  167. // It's a season folder if it's named as such and does not contain any audio files, apart from theme.mp3
  168. return GetSeasonNumberFromPath(path) != null && !Directory.EnumerateFiles(path).Any(i => EntityResolutionHelper.IsAudioFile(i) && !string.Equals(Path.GetFileNameWithoutExtension(i), BaseItem.ThemeSongFilename));
  169. }
  170. /// <summary>
  171. /// Determines whether [is series folder] [the specified path].
  172. /// </summary>
  173. /// <param name="path">The path.</param>
  174. /// <param name="fileSystemChildren">The file system children.</param>
  175. /// <returns><c>true</c> if [is series folder] [the specified path]; otherwise, <c>false</c>.</returns>
  176. public static bool IsSeriesFolder(string path, IEnumerable<FileSystemInfo> fileSystemChildren)
  177. {
  178. // A folder with more than 3 non-season folders in will not becounted as a series
  179. var nonSeriesFolders = 0;
  180. foreach (var child in fileSystemChildren)
  181. {
  182. var attributes = child.Attributes;
  183. if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
  184. {
  185. continue;
  186. }
  187. if ((attributes & FileAttributes.System) == FileAttributes.System)
  188. {
  189. continue;
  190. }
  191. if ((attributes & FileAttributes.Directory) == FileAttributes.Directory)
  192. {
  193. if (IsSeasonFolder(child.FullName))
  194. {
  195. return true;
  196. }
  197. nonSeriesFolders++;
  198. if (nonSeriesFolders >= 3)
  199. {
  200. return false;
  201. }
  202. }
  203. else
  204. {
  205. var fullName = child.FullName;
  206. if (EntityResolutionHelper.IsVideoFile(fullName) && GetEpisodeNumberFromFile(fullName, false).HasValue)
  207. {
  208. return true;
  209. }
  210. }
  211. }
  212. return false;
  213. }
  214. /// <summary>
  215. /// Episodes the number from file.
  216. /// </summary>
  217. /// <param name="fullPath">The full path.</param>
  218. /// <param name="isInSeason">if set to <c>true</c> [is in season].</param>
  219. /// <returns>System.String.</returns>
  220. public static int? GetEpisodeNumberFromFile(string fullPath, bool isInSeason)
  221. {
  222. string fl = fullPath.ToLower();
  223. foreach (var r in EpisodeExpressions)
  224. {
  225. Match m = r.Match(fl);
  226. if (m.Success)
  227. return ParseEpisodeNumber(m.Groups["epnumber"].Value);
  228. }
  229. if (isInSeason)
  230. {
  231. var match = EpisodeExpressionsInASeasonFolder.Select(r => r.Match(fl))
  232. .FirstOrDefault(m => m.Success);
  233. if (match != null)
  234. {
  235. return ParseEpisodeNumber(match.Value);
  236. }
  237. }
  238. return null;
  239. }
  240. public static int? GetEndingEpisodeNumberFromFile(string fullPath)
  241. {
  242. var fl = fullPath.ToLower();
  243. foreach (var r in MultipleEpisodeExpressions)
  244. {
  245. var m = r.Match(fl);
  246. if (m.Success && !string.IsNullOrEmpty(m.Groups["endingepnumber"].Value))
  247. return ParseEpisodeNumber(m.Groups["endingepnumber"].Value);
  248. }
  249. return null;
  250. }
  251. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  252. private static int? ParseEpisodeNumber(string val)
  253. {
  254. int num;
  255. if (!string.IsNullOrEmpty(val) && int.TryParse(val, NumberStyles.Integer, UsCulture, out num))
  256. {
  257. return num;
  258. }
  259. return null;
  260. }
  261. /// <summary>
  262. /// Seasons the number from episode file.
  263. /// </summary>
  264. /// <param name="fullPath">The full path.</param>
  265. /// <returns>System.String.</returns>
  266. public static int? GetSeasonNumberFromEpisodeFile(string fullPath)
  267. {
  268. string fl = fullPath.ToLower();
  269. foreach (var r in EpisodeExpressions)
  270. {
  271. Match m = r.Match(fl);
  272. if (m.Success)
  273. {
  274. Group g = m.Groups["seasonnumber"];
  275. if (g != null)
  276. {
  277. var val = g.Value;
  278. if (!string.IsNullOrWhiteSpace(val))
  279. {
  280. int num;
  281. if (int.TryParse(val, NumberStyles.Integer, UsCulture, out num))
  282. {
  283. return num;
  284. }
  285. }
  286. }
  287. return null;
  288. }
  289. }
  290. return null;
  291. }
  292. /// <summary>
  293. /// Gets the air days.
  294. /// </summary>
  295. /// <param name="day">The day.</param>
  296. /// <returns>List{DayOfWeek}.</returns>
  297. public static List<DayOfWeek> GetAirDays(string day)
  298. {
  299. if (!string.IsNullOrWhiteSpace(day))
  300. {
  301. if (day.Equals("Daily", StringComparison.OrdinalIgnoreCase))
  302. {
  303. return new List<DayOfWeek>
  304. {
  305. DayOfWeek.Sunday,
  306. DayOfWeek.Monday,
  307. DayOfWeek.Tuesday,
  308. DayOfWeek.Wednesday,
  309. DayOfWeek.Thursday,
  310. DayOfWeek.Friday,
  311. DayOfWeek.Saturday
  312. };
  313. }
  314. DayOfWeek value;
  315. if (Enum.TryParse(day, true, out value))
  316. {
  317. return new List<DayOfWeek>
  318. {
  319. value
  320. };
  321. }
  322. return new List<DayOfWeek>();
  323. }
  324. return null;
  325. }
  326. }
  327. }