TVUtils.cs 16 KB

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