TVUtils.cs 18 KB

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