TVUtils.cs 12 KB

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