TVUtils.cs 18 KB

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