TVUtils.cs 13 KB

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