TVUtils.cs 17 KB

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