TVUtils.cs 19 KB

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