TVUtils.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 =
  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 =
  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 =
  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 =
  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. var numericStart = -1;
  144. var 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) || EntityResolutionHelper.IsVideoPlaceHolder(fullName))
  215. {
  216. if (GetEpisodeNumberFromFile(fullName, false).HasValue)
  217. {
  218. return true;
  219. }
  220. }
  221. }
  222. }
  223. return false;
  224. }
  225. /// <summary>
  226. /// Episodes the number from file.
  227. /// </summary>
  228. /// <param name="fullPath">The full path.</param>
  229. /// <param name="isInSeason">if set to <c>true</c> [is in season].</param>
  230. /// <returns>System.String.</returns>
  231. public static int? GetEpisodeNumberFromFile(string fullPath, bool isInSeason)
  232. {
  233. string fl = fullPath.ToLower();
  234. foreach (var r in EpisodeExpressions)
  235. {
  236. Match m = r.Match(fl);
  237. if (m.Success)
  238. return ParseEpisodeNumber(m.Groups["epnumber"].Value);
  239. }
  240. if (isInSeason)
  241. {
  242. var match = EpisodeExpressionsInASeasonFolder.Select(r => r.Match(fl))
  243. .FirstOrDefault(m => m.Success);
  244. if (match != null)
  245. {
  246. return ParseEpisodeNumber(match.Groups["epnumber"].Value);
  247. }
  248. }
  249. return null;
  250. }
  251. public static int? GetEndingEpisodeNumberFromFile(string fullPath)
  252. {
  253. var fl = fullPath.ToLower();
  254. foreach (var r in MultipleEpisodeExpressions)
  255. {
  256. var m = r.Match(fl);
  257. if (m.Success && !string.IsNullOrEmpty(m.Groups["endingepnumber"].Value))
  258. return ParseEpisodeNumber(m.Groups["endingepnumber"].Value);
  259. }
  260. return null;
  261. }
  262. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  263. private static int? ParseEpisodeNumber(string val)
  264. {
  265. int num;
  266. if (!string.IsNullOrEmpty(val) && int.TryParse(val, NumberStyles.Integer, UsCulture, out num))
  267. {
  268. return num;
  269. }
  270. return null;
  271. }
  272. /// <summary>
  273. /// Seasons the number from episode file.
  274. /// </summary>
  275. /// <param name="fullPath">The full path.</param>
  276. /// <returns>System.String.</returns>
  277. public static int? GetSeasonNumberFromEpisodeFile(string fullPath)
  278. {
  279. string fl = fullPath.ToLower();
  280. foreach (var r in EpisodeExpressions)
  281. {
  282. Match m = r.Match(fl);
  283. if (m.Success)
  284. {
  285. Group g = m.Groups["seasonnumber"];
  286. if (g != null)
  287. {
  288. var val = g.Value;
  289. if (!string.IsNullOrWhiteSpace(val))
  290. {
  291. int num;
  292. if (int.TryParse(val, NumberStyles.Integer, UsCulture, out num))
  293. {
  294. return num;
  295. }
  296. }
  297. }
  298. return null;
  299. }
  300. }
  301. return null;
  302. }
  303. public static string GetSeriesNameFromEpisodeFile(string fullPath)
  304. {
  305. var fl = fullPath.ToLower();
  306. foreach (var r in EpisodeExpressions)
  307. {
  308. var m = r.Match(fl);
  309. if (m.Success)
  310. {
  311. var g = m.Groups["seriesname"];
  312. if (g != null)
  313. {
  314. var val = g.Value;
  315. if (!string.IsNullOrWhiteSpace(val))
  316. {
  317. return val;
  318. }
  319. }
  320. return null;
  321. }
  322. }
  323. return null;
  324. }
  325. /// <summary>
  326. /// Gets the air days.
  327. /// </summary>
  328. /// <param name="day">The day.</param>
  329. /// <returns>List{DayOfWeek}.</returns>
  330. public static List<DayOfWeek> GetAirDays(string day)
  331. {
  332. if (!string.IsNullOrWhiteSpace(day))
  333. {
  334. if (day.Equals("Daily", StringComparison.OrdinalIgnoreCase))
  335. {
  336. return new List<DayOfWeek>
  337. {
  338. DayOfWeek.Sunday,
  339. DayOfWeek.Monday,
  340. DayOfWeek.Tuesday,
  341. DayOfWeek.Wednesday,
  342. DayOfWeek.Thursday,
  343. DayOfWeek.Friday,
  344. DayOfWeek.Saturday
  345. };
  346. }
  347. DayOfWeek value;
  348. if (Enum.TryParse(day, true, out value))
  349. {
  350. return new List<DayOfWeek>
  351. {
  352. value
  353. };
  354. }
  355. return new List<DayOfWeek>();
  356. }
  357. return null;
  358. }
  359. }
  360. }