TVUtils.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. // Look for one of the season folder names
  121. foreach (var name in SeasonFolderNames)
  122. {
  123. var index = filename.IndexOf(name, StringComparison.OrdinalIgnoreCase);
  124. if (index != -1)
  125. {
  126. return GetSeasonNumberFromPathSubstring(filename.Substring(index + name.Length));
  127. }
  128. }
  129. return null;
  130. }
  131. /// <summary>
  132. /// Extracts the season number from the second half of the Season folder name (everything after "Season", or "Staffel")
  133. /// </summary>
  134. /// <param name="path">The path.</param>
  135. /// <returns>System.Nullable{System.Int32}.</returns>
  136. private static int? GetSeasonNumberFromPathSubstring(string path)
  137. {
  138. int numericStart = -1;
  139. int length = 0;
  140. // Find out where the numbers start, and then keep going until they end
  141. for (var i = 0; i < path.Length; i++)
  142. {
  143. if (char.IsNumber(path, i))
  144. {
  145. if (numericStart == -1)
  146. {
  147. numericStart = i;
  148. }
  149. length++;
  150. }
  151. else if (numericStart != -1)
  152. {
  153. break;
  154. }
  155. }
  156. if (numericStart == -1)
  157. {
  158. return null;
  159. }
  160. return int.Parse(path.Substring(numericStart, length));
  161. }
  162. /// <summary>
  163. /// Determines whether [is season folder] [the specified path].
  164. /// </summary>
  165. /// <param name="path">The path.</param>
  166. /// <returns><c>true</c> if [is season folder] [the specified path]; otherwise, <c>false</c>.</returns>
  167. private static bool IsSeasonFolder(string path)
  168. {
  169. // It's a season folder if it's named as such and does not contain any audio files, apart from theme.mp3
  170. return GetSeasonNumberFromPath(path) != null && !Directory.EnumerateFiles(path).Any(i => EntityResolutionHelper.IsAudioFile(i) && !string.Equals(Path.GetFileNameWithoutExtension(i), BaseItem.ThemeSongFilename));
  171. }
  172. /// <summary>
  173. /// Determines whether [is series folder] [the specified path].
  174. /// </summary>
  175. /// <param name="path">The path.</param>
  176. /// <param name="fileSystemChildren">The file system children.</param>
  177. /// <returns><c>true</c> if [is series folder] [the specified path]; otherwise, <c>false</c>.</returns>
  178. public static bool IsSeriesFolder(string path, IEnumerable<FileSystemInfo> fileSystemChildren)
  179. {
  180. // A folder with more than 3 non-season folders in will not becounted as a series
  181. var nonSeriesFolders = 0;
  182. foreach (var child in fileSystemChildren)
  183. {
  184. var attributes = child.Attributes;
  185. if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
  186. {
  187. continue;
  188. }
  189. if ((attributes & FileAttributes.System) == FileAttributes.System)
  190. {
  191. continue;
  192. }
  193. if ((attributes & FileAttributes.Directory) == FileAttributes.Directory)
  194. {
  195. if (IsSeasonFolder(child.FullName))
  196. {
  197. return true;
  198. }
  199. nonSeriesFolders++;
  200. if (nonSeriesFolders >= 3)
  201. {
  202. return false;
  203. }
  204. }
  205. else
  206. {
  207. var fullName = child.FullName;
  208. if (EntityResolutionHelper.IsVideoFile(fullName) && GetEpisodeNumberFromFile(fullName, false).HasValue)
  209. {
  210. return true;
  211. }
  212. }
  213. }
  214. return false;
  215. }
  216. /// <summary>
  217. /// Episodes the number from file.
  218. /// </summary>
  219. /// <param name="fullPath">The full path.</param>
  220. /// <param name="isInSeason">if set to <c>true</c> [is in season].</param>
  221. /// <returns>System.String.</returns>
  222. public static int? GetEpisodeNumberFromFile(string fullPath, bool isInSeason)
  223. {
  224. string fl = fullPath.ToLower();
  225. foreach (var r in EpisodeExpressions)
  226. {
  227. Match m = r.Match(fl);
  228. if (m.Success)
  229. return ParseEpisodeNumber(m.Groups["epnumber"].Value);
  230. }
  231. if (isInSeason)
  232. {
  233. var match = EpisodeExpressionsInASeasonFolder.Select(r => r.Match(fl))
  234. .FirstOrDefault(m => m.Success);
  235. if (match != null)
  236. {
  237. return ParseEpisodeNumber(match.Groups["epnumber"].Value);
  238. }
  239. }
  240. return null;
  241. }
  242. public static int? GetEndingEpisodeNumberFromFile(string fullPath)
  243. {
  244. var fl = fullPath.ToLower();
  245. foreach (var r in MultipleEpisodeExpressions)
  246. {
  247. var m = r.Match(fl);
  248. if (m.Success && !string.IsNullOrEmpty(m.Groups["endingepnumber"].Value))
  249. return ParseEpisodeNumber(m.Groups["endingepnumber"].Value);
  250. }
  251. return null;
  252. }
  253. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  254. private static int? ParseEpisodeNumber(string val)
  255. {
  256. int num;
  257. if (!string.IsNullOrEmpty(val) && int.TryParse(val, NumberStyles.Integer, UsCulture, out num))
  258. {
  259. return num;
  260. }
  261. return null;
  262. }
  263. /// <summary>
  264. /// Seasons the number from episode file.
  265. /// </summary>
  266. /// <param name="fullPath">The full path.</param>
  267. /// <returns>System.String.</returns>
  268. public static int? GetSeasonNumberFromEpisodeFile(string fullPath)
  269. {
  270. string fl = fullPath.ToLower();
  271. foreach (var r in EpisodeExpressions)
  272. {
  273. Match m = r.Match(fl);
  274. if (m.Success)
  275. {
  276. Group g = m.Groups["seasonnumber"];
  277. if (g != null)
  278. {
  279. var val = g.Value;
  280. if (!string.IsNullOrWhiteSpace(val))
  281. {
  282. int num;
  283. if (int.TryParse(val, NumberStyles.Integer, UsCulture, out num))
  284. {
  285. return num;
  286. }
  287. }
  288. }
  289. return null;
  290. }
  291. }
  292. return null;
  293. }
  294. /// <summary>
  295. /// Gets the air days.
  296. /// </summary>
  297. /// <param name="day">The day.</param>
  298. /// <returns>List{DayOfWeek}.</returns>
  299. public static List<DayOfWeek> GetAirDays(string day)
  300. {
  301. if (!string.IsNullOrWhiteSpace(day))
  302. {
  303. if (day.Equals("Daily", StringComparison.OrdinalIgnoreCase))
  304. {
  305. return new List<DayOfWeek>
  306. {
  307. DayOfWeek.Sunday,
  308. DayOfWeek.Monday,
  309. DayOfWeek.Tuesday,
  310. DayOfWeek.Wednesday,
  311. DayOfWeek.Thursday,
  312. DayOfWeek.Friday,
  313. DayOfWeek.Saturday
  314. };
  315. }
  316. DayOfWeek value;
  317. if (Enum.TryParse(day, true, out value))
  318. {
  319. return new List<DayOfWeek>
  320. {
  321. value
  322. };
  323. }
  324. return new List<DayOfWeek>();
  325. }
  326. return null;
  327. }
  328. }
  329. }