TVUtils.cs 18 KB

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