DefaultIntroProvider.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Security;
  3. using MediaBrowser.Controller.Channels;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Entities.Movies;
  6. using MediaBrowser.Controller.Entities.TV;
  7. using MediaBrowser.Controller.Library;
  8. using MediaBrowser.Controller.Localization;
  9. using MediaBrowser.Model.Channels;
  10. using MediaBrowser.Model.Configuration;
  11. using MediaBrowser.Model.Entities;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. using CommonIO;
  19. using MediaBrowser.Common.IO;
  20. namespace MediaBrowser.Server.Implementations.Intros
  21. {
  22. public class DefaultIntroProvider : IIntroProvider
  23. {
  24. private readonly ISecurityManager _security;
  25. private readonly IChannelManager _channelManager;
  26. private readonly ILocalizationManager _localization;
  27. private readonly IConfigurationManager _serverConfig;
  28. private readonly ILibraryManager _libraryManager;
  29. private readonly IFileSystem _fileSystem;
  30. public DefaultIntroProvider(ISecurityManager security, IChannelManager channelManager, ILocalizationManager localization, IConfigurationManager serverConfig, ILibraryManager libraryManager, IFileSystem fileSystem)
  31. {
  32. _security = security;
  33. _channelManager = channelManager;
  34. _localization = localization;
  35. _serverConfig = serverConfig;
  36. _libraryManager = libraryManager;
  37. _fileSystem = fileSystem;
  38. }
  39. public async Task<IEnumerable<IntroInfo>> GetIntros(BaseItem item, User user)
  40. {
  41. var config = GetOptions();
  42. if (item is Movie)
  43. {
  44. if (!config.EnableIntrosForMovies)
  45. {
  46. return new List<IntroInfo>();
  47. }
  48. }
  49. else if (item is Episode)
  50. {
  51. if (!config.EnableIntrosForEpisodes)
  52. {
  53. return new List<IntroInfo>();
  54. }
  55. }
  56. else
  57. {
  58. return new List<IntroInfo>();
  59. }
  60. var ratingLevel = string.IsNullOrWhiteSpace(item.OfficialRating)
  61. ? null
  62. : _localization.GetRatingLevel(item.OfficialRating);
  63. var random = new Random(Environment.TickCount + Guid.NewGuid().GetHashCode());
  64. var candidates = new List<ItemWithTrailer>();
  65. var itemPeople = _libraryManager.GetPeople(item);
  66. var allPeople = _libraryManager.GetPeople(new InternalPeopleQuery
  67. {
  68. AppearsInItemId = item.Id
  69. });
  70. if (config.EnableIntrosFromMoviesInLibrary)
  71. {
  72. var inputItems = _libraryManager.GetItems(new InternalItemsQuery(user)
  73. {
  74. IncludeItemTypes = new[] { typeof(Movie).Name }
  75. }, new string[]{});
  76. var itemsWithTrailers = inputItems
  77. .Where(i =>
  78. {
  79. var hasTrailers = i as IHasTrailers;
  80. if (hasTrailers != null && hasTrailers.LocalTrailerIds.Count > 0)
  81. {
  82. if (i is Movie)
  83. {
  84. return !IsDuplicate(item, i);
  85. }
  86. }
  87. return false;
  88. });
  89. candidates.AddRange(itemsWithTrailers.Select(i => new ItemWithTrailer
  90. {
  91. Item = i,
  92. Type = ItemWithTrailerType.ItemWithTrailer,
  93. User = user,
  94. WatchingItem = item,
  95. WatchingItemPeople = itemPeople,
  96. AllPeople = allPeople,
  97. Random = random,
  98. LibraryManager = _libraryManager
  99. }));
  100. }
  101. var trailerTypes = new List<TrailerType>();
  102. if (config.EnableIntrosFromUpcomingTrailers)
  103. {
  104. trailerTypes.Add(TrailerType.ComingSoonToTheaters);
  105. }
  106. if (config.EnableIntrosFromUpcomingDvdMovies)
  107. {
  108. trailerTypes.Add(TrailerType.ComingSoonToDvd);
  109. }
  110. if (config.EnableIntrosFromUpcomingStreamingMovies)
  111. {
  112. trailerTypes.Add(TrailerType.ComingSoonToStreaming);
  113. }
  114. if (config.EnableIntrosFromSimilarMovies)
  115. {
  116. trailerTypes.Add(TrailerType.Archive);
  117. }
  118. if (trailerTypes.Count > 0 && IsSupporter)
  119. {
  120. var channelTrailers = await _channelManager.GetAllMediaInternal(new AllChannelMediaQuery
  121. {
  122. ContentTypes = new[] { ChannelMediaContentType.MovieExtra },
  123. ExtraTypes = new[] { ExtraType.Trailer },
  124. UserId = user.Id.ToString("N"),
  125. TrailerTypes = trailerTypes.ToArray()
  126. }, CancellationToken.None);
  127. candidates.AddRange(channelTrailers.Items.Select(i => new ItemWithTrailer
  128. {
  129. Item = i,
  130. Type = ItemWithTrailerType.ChannelTrailer,
  131. User = user,
  132. WatchingItem = item,
  133. WatchingItemPeople = itemPeople,
  134. AllPeople = allPeople,
  135. Random = random,
  136. LibraryManager = _libraryManager
  137. }));
  138. }
  139. return GetResult(item, candidates, config, ratingLevel);
  140. }
  141. private IEnumerable<IntroInfo> GetResult(BaseItem item, IEnumerable<ItemWithTrailer> candidates, CinemaModeConfiguration config, int? ratingLevel)
  142. {
  143. var customIntros = !string.IsNullOrWhiteSpace(config.CustomIntroPath) ?
  144. GetCustomIntros(config) :
  145. new List<IntroInfo>();
  146. var trailerLimit = config.TrailerLimit;
  147. // Avoid implicitly captured closure
  148. return candidates.Where(i =>
  149. {
  150. if (config.EnableIntrosParentalControl && !FilterByParentalRating(ratingLevel, i.Item))
  151. {
  152. return false;
  153. }
  154. if (!config.EnableIntrosForWatchedContent && i.IsPlayed)
  155. {
  156. return false;
  157. }
  158. return !IsDuplicate(item, i.Item);
  159. })
  160. .OrderByDescending(i => i.Score)
  161. .ThenBy(i => Guid.NewGuid())
  162. .ThenByDescending(i => (i.IsPlayed ? 0 : 1))
  163. .Select(i => i.IntroInfo)
  164. .Take(trailerLimit)
  165. .Concat(customIntros.Take(1));
  166. }
  167. private bool IsDuplicate(BaseItem playingContent, BaseItem test)
  168. {
  169. var id = playingContent.GetProviderId(MetadataProviders.Imdb);
  170. if (!string.IsNullOrWhiteSpace(id) && string.Equals(id, test.GetProviderId(MetadataProviders.Imdb), StringComparison.OrdinalIgnoreCase))
  171. {
  172. return true;
  173. }
  174. id = playingContent.GetProviderId(MetadataProviders.Tmdb);
  175. if (!string.IsNullOrWhiteSpace(id) && string.Equals(id, test.GetProviderId(MetadataProviders.Tmdb), StringComparison.OrdinalIgnoreCase))
  176. {
  177. return true;
  178. }
  179. return false;
  180. }
  181. private CinemaModeConfiguration GetOptions()
  182. {
  183. return _serverConfig.GetConfiguration<CinemaModeConfiguration>("cinemamode");
  184. }
  185. private List<IntroInfo> GetCustomIntros(CinemaModeConfiguration options)
  186. {
  187. try
  188. {
  189. return GetCustomIntroFiles(options, true, false)
  190. .OrderBy(i => Guid.NewGuid())
  191. .Select(i => new IntroInfo
  192. {
  193. Path = i
  194. }).ToList();
  195. }
  196. catch (IOException)
  197. {
  198. return new List<IntroInfo>();
  199. }
  200. }
  201. private IEnumerable<string> GetCustomIntroFiles(CinemaModeConfiguration options, bool enableCustomIntros, bool enableMediaInfoIntros)
  202. {
  203. var list = new List<string>();
  204. if (enableCustomIntros && !string.IsNullOrWhiteSpace(options.CustomIntroPath))
  205. {
  206. list.AddRange(_fileSystem.GetFilePaths(options.CustomIntroPath, true)
  207. .Where(_libraryManager.IsVideoFile));
  208. }
  209. if (enableMediaInfoIntros && !string.IsNullOrWhiteSpace(options.MediaInfoIntroPath))
  210. {
  211. list.AddRange(_fileSystem.GetFilePaths(options.MediaInfoIntroPath, true)
  212. .Where(_libraryManager.IsVideoFile));
  213. }
  214. return list.Distinct(StringComparer.OrdinalIgnoreCase);
  215. }
  216. private bool FilterByParentalRating(int? ratingLevel, BaseItem item)
  217. {
  218. // Only content rated same or lower
  219. if (ratingLevel.HasValue)
  220. {
  221. var level = string.IsNullOrWhiteSpace(item.OfficialRating)
  222. ? (int?)null
  223. : _localization.GetRatingLevel(item.OfficialRating);
  224. return level.HasValue && level.Value <= ratingLevel.Value;
  225. }
  226. return true;
  227. }
  228. internal static int GetSimiliarityScore(BaseItem item1, List<PersonInfo> item1People, List<PersonInfo> allPeople, BaseItem item2, Random random, ILibraryManager libraryManager)
  229. {
  230. var points = 0;
  231. if (!string.IsNullOrEmpty(item1.OfficialRating) && string.Equals(item1.OfficialRating, item2.OfficialRating, StringComparison.OrdinalIgnoreCase))
  232. {
  233. points += 10;
  234. }
  235. // Find common genres
  236. points += item1.Genres.Where(i => item2.Genres.Contains(i, StringComparer.OrdinalIgnoreCase)).Sum(i => 10);
  237. // Find common tags
  238. points += GetTags(item1).Where(i => GetTags(item2).Contains(i, StringComparer.OrdinalIgnoreCase)).Sum(i => 10);
  239. // Find common keywords
  240. points += GetKeywords(item1).Where(i => GetKeywords(item2).Contains(i, StringComparer.OrdinalIgnoreCase)).Sum(i => 10);
  241. // Find common studios
  242. points += item1.Studios.Where(i => item2.Studios.Contains(i, StringComparer.OrdinalIgnoreCase)).Sum(i => 5);
  243. var item2PeopleNames = allPeople.Where(i => i.ItemId == item2.Id)
  244. .Select(i => i.Name)
  245. .Where(i => !string.IsNullOrWhiteSpace(i))
  246. .DistinctNames()
  247. .ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
  248. points += item1People.Where(i => item2PeopleNames.ContainsKey(i.Name)).Sum(i =>
  249. {
  250. if (string.Equals(i.Type, PersonType.Director, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.Director, StringComparison.OrdinalIgnoreCase))
  251. {
  252. return 5;
  253. }
  254. if (string.Equals(i.Type, PersonType.Actor, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.Actor, StringComparison.OrdinalIgnoreCase))
  255. {
  256. return 3;
  257. }
  258. if (string.Equals(i.Type, PersonType.Composer, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.Composer, StringComparison.OrdinalIgnoreCase))
  259. {
  260. return 3;
  261. }
  262. if (string.Equals(i.Type, PersonType.GuestStar, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.GuestStar, StringComparison.OrdinalIgnoreCase))
  263. {
  264. return 3;
  265. }
  266. if (string.Equals(i.Type, PersonType.Writer, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.Writer, StringComparison.OrdinalIgnoreCase))
  267. {
  268. return 2;
  269. }
  270. return 1;
  271. });
  272. // Add some randomization so that you're not always seeing the same ones for a given movie
  273. points += random.Next(0, 50);
  274. return points;
  275. }
  276. private static IEnumerable<string> GetTags(BaseItem item)
  277. {
  278. var hasTags = item as IHasTags;
  279. if (hasTags != null)
  280. {
  281. return hasTags.Tags;
  282. }
  283. return new List<string>();
  284. }
  285. private static IEnumerable<string> GetKeywords(BaseItem item)
  286. {
  287. var hasTags = item as IHasKeywords;
  288. if (hasTags != null)
  289. {
  290. return hasTags.Keywords;
  291. }
  292. return new List<string>();
  293. }
  294. public IEnumerable<string> GetAllIntroFiles()
  295. {
  296. return GetCustomIntroFiles(GetOptions(), true, true);
  297. }
  298. private bool IsSupporter
  299. {
  300. get { return _security.IsMBSupporter; }
  301. }
  302. public string Name
  303. {
  304. get { return "Default"; }
  305. }
  306. internal class ItemWithTrailer
  307. {
  308. internal BaseItem Item;
  309. internal ItemWithTrailerType Type;
  310. internal User User;
  311. internal BaseItem WatchingItem;
  312. internal List<PersonInfo> WatchingItemPeople;
  313. internal List<PersonInfo> AllPeople;
  314. internal Random Random;
  315. internal ILibraryManager LibraryManager;
  316. private bool? _isPlayed;
  317. public bool IsPlayed
  318. {
  319. get
  320. {
  321. if (!_isPlayed.HasValue)
  322. {
  323. _isPlayed = Item.IsPlayed(User);
  324. }
  325. return _isPlayed.Value;
  326. }
  327. }
  328. private int? _score;
  329. public int Score
  330. {
  331. get
  332. {
  333. if (!_score.HasValue)
  334. {
  335. _score = GetSimiliarityScore(WatchingItem, WatchingItemPeople, AllPeople, Item, Random, LibraryManager);
  336. }
  337. return _score.Value;
  338. }
  339. }
  340. public IntroInfo IntroInfo
  341. {
  342. get
  343. {
  344. var id = Item.Id;
  345. if (Type == ItemWithTrailerType.ItemWithTrailer)
  346. {
  347. var hasTrailers = Item as IHasTrailers;
  348. if (hasTrailers != null)
  349. {
  350. id = hasTrailers.LocalTrailerIds.FirstOrDefault();
  351. }
  352. }
  353. return new IntroInfo
  354. {
  355. ItemId = id
  356. };
  357. }
  358. }
  359. }
  360. internal enum ItemWithTrailerType
  361. {
  362. LibraryTrailer,
  363. ChannelTrailer,
  364. ItemWithTrailer
  365. }
  366. }
  367. public class CinemaModeConfigurationFactory : IConfigurationFactory
  368. {
  369. public IEnumerable<ConfigurationStore> GetConfigurations()
  370. {
  371. return new[]
  372. {
  373. new ConfigurationStore
  374. {
  375. ConfigurationType = typeof(CinemaModeConfiguration),
  376. Key = "cinemamode"
  377. }
  378. };
  379. }
  380. }
  381. }