jellyfin/MediaBrowser.Server.Implementations/Intros/DefaultIntroProvider.cs

445 lines
15 KiB
C#
Raw Normal View History

2014-09-22 23:56:54 +02:00
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Security;
using MediaBrowser.Controller.Channels;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Localization;
using MediaBrowser.Model.Channels;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Entities;
using System;
using System.Collections.Generic;
2014-09-23 02:04:50 +02:00
using System.IO;
2014-09-22 23:56:54 +02:00
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2015-10-04 06:23:11 +02:00
using CommonIO;
2015-09-24 19:50:49 +02:00
using MediaBrowser.Common.IO;
2014-09-22 23:56:54 +02:00
namespace MediaBrowser.Server.Implementations.Intros
{
public class DefaultIntroProvider : IIntroProvider
{
private readonly ISecurityManager _security;
private readonly IChannelManager _channelManager;
private readonly ILocalizationManager _localization;
private readonly IConfigurationManager _serverConfig;
2014-11-16 21:44:08 +01:00
private readonly ILibraryManager _libraryManager;
2015-09-24 19:50:49 +02:00
private readonly IFileSystem _fileSystem;
2014-09-22 23:56:54 +02:00
2015-09-24 19:50:49 +02:00
public DefaultIntroProvider(ISecurityManager security, IChannelManager channelManager, ILocalizationManager localization, IConfigurationManager serverConfig, ILibraryManager libraryManager, IFileSystem fileSystem)
2014-09-22 23:56:54 +02:00
{
_security = security;
_channelManager = channelManager;
_localization = localization;
_serverConfig = serverConfig;
2014-11-16 21:44:08 +01:00
_libraryManager = libraryManager;
2015-09-24 19:50:49 +02:00
_fileSystem = fileSystem;
2014-09-22 23:56:54 +02:00
}
public async Task<IEnumerable<IntroInfo>> GetIntros(BaseItem item, User user)
{
var config = GetOptions();
if (item is Movie)
{
if (!config.EnableIntrosForMovies)
{
return new List<IntroInfo>();
}
}
else if (item is Episode)
{
if (!config.EnableIntrosForEpisodes)
{
return new List<IntroInfo>();
}
}
else
{
return new List<IntroInfo>();
}
var ratingLevel = string.IsNullOrWhiteSpace(item.OfficialRating)
2014-12-03 04:13:03 +01:00
? null
2014-09-22 23:56:54 +02:00
: _localization.GetRatingLevel(item.OfficialRating);
var random = new Random(Environment.TickCount + Guid.NewGuid().GetHashCode());
var candidates = new List<ItemWithTrailer>();
2015-07-08 18:10:34 +02:00
var itemPeople = _libraryManager.GetPeople(item);
var allPeople = _libraryManager.GetPeople(new InternalPeopleQuery
{
AppearsInItemId = item.Id
});
2014-09-22 23:56:54 +02:00
if (config.EnableIntrosFromMoviesInLibrary)
{
2015-10-29 14:28:05 +01:00
var inputItems = _libraryManager.GetItems(new InternalItemsQuery(user)
2015-10-19 17:33:49 +02:00
{
2015-10-29 14:28:05 +01:00
IncludeItemTypes = new[] { typeof(Movie).Name }
2015-10-19 17:33:49 +02:00
2015-11-11 15:56:31 +01:00
}, new string[]{});
2015-10-19 17:33:49 +02:00
var itemsWithTrailers = inputItems
.Where(i =>
2015-01-25 07:34:50 +01:00
{
var hasTrailers = i as IHasTrailers;
if (hasTrailers != null && hasTrailers.LocalTrailerIds.Count > 0)
{
if (i is Movie)
{
return !IsDuplicate(item, i);
}
}
return false;
});
2014-09-22 23:56:54 +02:00
candidates.AddRange(itemsWithTrailers.Select(i => new ItemWithTrailer
{
Item = i,
Type = ItemWithTrailerType.ItemWithTrailer,
User = user,
WatchingItem = item,
2015-07-08 18:10:34 +02:00
WatchingItemPeople = itemPeople,
AllPeople = allPeople,
2015-06-21 05:35:22 +02:00
Random = random,
LibraryManager = _libraryManager
2014-09-22 23:56:54 +02:00
}));
}
2014-09-30 06:47:30 +02:00
var trailerTypes = new List<TrailerType>();
if (config.EnableIntrosFromUpcomingTrailers)
{
trailerTypes.Add(TrailerType.ComingSoonToTheaters);
}
if (config.EnableIntrosFromUpcomingDvdMovies)
{
trailerTypes.Add(TrailerType.ComingSoonToDvd);
}
if (config.EnableIntrosFromUpcomingStreamingMovies)
{
trailerTypes.Add(TrailerType.ComingSoonToStreaming);
}
2014-10-09 01:31:44 +02:00
if (config.EnableIntrosFromSimilarMovies)
{
trailerTypes.Add(TrailerType.Archive);
}
2014-09-30 06:47:30 +02:00
if (trailerTypes.Count > 0 && IsSupporter)
2014-09-22 23:56:54 +02:00
{
var channelTrailers = await _channelManager.GetAllMediaInternal(new AllChannelMediaQuery
{
2014-09-28 17:27:26 +02:00
ContentTypes = new[] { ChannelMediaContentType.MovieExtra },
ExtraTypes = new[] { ExtraType.Trailer },
2014-09-28 22:49:43 +02:00
UserId = user.Id.ToString("N"),
2014-09-30 06:47:30 +02:00
TrailerTypes = trailerTypes.ToArray()
2014-09-22 23:56:54 +02:00
}, CancellationToken.None);
candidates.AddRange(channelTrailers.Items.Select(i => new ItemWithTrailer
{
Item = i,
Type = ItemWithTrailerType.ChannelTrailer,
User = user,
WatchingItem = item,
2015-07-08 18:10:34 +02:00
WatchingItemPeople = itemPeople,
AllPeople = allPeople,
2015-06-21 05:35:22 +02:00
Random = random,
LibraryManager = _libraryManager
2014-09-22 23:56:54 +02:00
}));
}
2015-01-25 07:34:50 +01:00
return GetResult(item, candidates, config, ratingLevel);
}
private IEnumerable<IntroInfo> GetResult(BaseItem item, IEnumerable<ItemWithTrailer> candidates, CinemaModeConfiguration config, int? ratingLevel)
{
2014-10-02 02:28:16 +02:00
var customIntros = !string.IsNullOrWhiteSpace(config.CustomIntroPath) ?
2015-11-17 18:46:15 +01:00
GetCustomIntros(config) :
2014-09-22 23:56:54 +02:00
new List<IntroInfo>();
2014-10-10 00:22:04 +02:00
var trailerLimit = config.TrailerLimit;
2014-09-22 23:56:54 +02:00
// Avoid implicitly captured closure
return candidates.Where(i =>
{
if (config.EnableIntrosParentalControl && !FilterByParentalRating(ratingLevel, i.Item))
{
return false;
}
if (!config.EnableIntrosForWatchedContent && i.IsPlayed)
{
return false;
}
2014-10-02 03:36:58 +02:00
return !IsDuplicate(item, i.Item);
2014-09-22 23:56:54 +02:00
})
.OrderByDescending(i => i.Score)
.ThenBy(i => Guid.NewGuid())
.ThenByDescending(i => (i.IsPlayed ? 0 : 1))
.Select(i => i.IntroInfo)
.Take(trailerLimit)
.Concat(customIntros.Take(1));
}
2014-10-02 03:36:58 +02:00
private bool IsDuplicate(BaseItem playingContent, BaseItem test)
{
var id = playingContent.GetProviderId(MetadataProviders.Imdb);
if (!string.IsNullOrWhiteSpace(id) && string.Equals(id, test.GetProviderId(MetadataProviders.Imdb), StringComparison.OrdinalIgnoreCase))
{
return true;
}
id = playingContent.GetProviderId(MetadataProviders.Tmdb);
if (!string.IsNullOrWhiteSpace(id) && string.Equals(id, test.GetProviderId(MetadataProviders.Tmdb), StringComparison.OrdinalIgnoreCase))
{
return true;
}
return false;
}
2014-09-22 23:56:54 +02:00
private CinemaModeConfiguration GetOptions()
{
return _serverConfig.GetConfiguration<CinemaModeConfiguration>("cinemamode");
}
2015-11-17 18:46:15 +01:00
private List<IntroInfo> GetCustomIntros(CinemaModeConfiguration options)
2014-09-22 23:56:54 +02:00
{
2014-09-23 02:04:50 +02:00
try
{
2015-11-17 18:46:15 +01:00
return GetCustomIntroFiles(options, true, false)
2014-09-23 02:04:50 +02:00
.OrderBy(i => Guid.NewGuid())
.Select(i => new IntroInfo
{
Path = i
}).ToList();
}
catch (IOException)
{
return new List<IntroInfo>();
}
}
2015-11-17 18:46:15 +01:00
private IEnumerable<string> GetCustomIntroFiles(CinemaModeConfiguration options, bool enableCustomIntros, bool enableMediaInfoIntros)
2014-09-23 02:04:50 +02:00
{
2015-11-17 17:37:16 +01:00
var list = new List<string>();
2015-11-17 18:46:15 +01:00
if (enableCustomIntros && !string.IsNullOrWhiteSpace(options.CustomIntroPath))
2014-09-23 02:04:50 +02:00
{
2015-11-17 17:37:16 +01:00
list.AddRange(_fileSystem.GetFilePaths(options.CustomIntroPath, true)
.Where(_libraryManager.IsVideoFile));
2014-09-23 02:04:50 +02:00
}
2015-11-17 18:46:15 +01:00
if (enableMediaInfoIntros && !string.IsNullOrWhiteSpace(options.MediaInfoIntroPath))
2015-11-17 17:37:16 +01:00
{
2015-11-17 18:41:58 +01:00
list.AddRange(_fileSystem.GetFilePaths(options.MediaInfoIntroPath, true)
2015-11-17 17:37:16 +01:00
.Where(_libraryManager.IsVideoFile));
}
2015-11-17 18:46:15 +01:00
2015-11-17 17:37:16 +01:00
return list.Distinct(StringComparer.OrdinalIgnoreCase);
2014-09-22 23:56:54 +02:00
}
private bool FilterByParentalRating(int? ratingLevel, BaseItem item)
{
// Only content rated same or lower
if (ratingLevel.HasValue)
{
var level = string.IsNullOrWhiteSpace(item.OfficialRating)
? (int?)null
: _localization.GetRatingLevel(item.OfficialRating);
return level.HasValue && level.Value <= ratingLevel.Value;
}
return true;
}
2015-07-08 18:10:34 +02:00
internal static int GetSimiliarityScore(BaseItem item1, List<PersonInfo> item1People, List<PersonInfo> allPeople, BaseItem item2, Random random, ILibraryManager libraryManager)
2014-09-22 23:56:54 +02:00
{
var points = 0;
if (!string.IsNullOrEmpty(item1.OfficialRating) && string.Equals(item1.OfficialRating, item2.OfficialRating, StringComparison.OrdinalIgnoreCase))
{
points += 10;
}
// Find common genres
points += item1.Genres.Where(i => item2.Genres.Contains(i, StringComparer.OrdinalIgnoreCase)).Sum(i => 10);
// Find common tags
points += GetTags(item1).Where(i => GetTags(item2).Contains(i, StringComparer.OrdinalIgnoreCase)).Sum(i => 10);
// Find common keywords
points += GetKeywords(item1).Where(i => GetKeywords(item2).Contains(i, StringComparer.OrdinalIgnoreCase)).Sum(i => 10);
// Find common studios
points += item1.Studios.Where(i => item2.Studios.Contains(i, StringComparer.OrdinalIgnoreCase)).Sum(i => 5);
2015-07-08 18:10:34 +02:00
var item2PeopleNames = allPeople.Where(i => i.ItemId == item2.Id)
.Select(i => i.Name)
.Where(i => !string.IsNullOrWhiteSpace(i))
.DistinctNames()
2014-09-22 23:56:54 +02:00
.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
2015-07-08 18:10:34 +02:00
points += item1People.Where(i => item2PeopleNames.ContainsKey(i.Name)).Sum(i =>
2014-09-22 23:56:54 +02:00
{
if (string.Equals(i.Type, PersonType.Director, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.Director, StringComparison.OrdinalIgnoreCase))
{
return 5;
}
if (string.Equals(i.Type, PersonType.Actor, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.Actor, StringComparison.OrdinalIgnoreCase))
{
return 3;
}
if (string.Equals(i.Type, PersonType.Composer, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.Composer, StringComparison.OrdinalIgnoreCase))
{
return 3;
}
if (string.Equals(i.Type, PersonType.GuestStar, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.GuestStar, StringComparison.OrdinalIgnoreCase))
{
return 3;
}
if (string.Equals(i.Type, PersonType.Writer, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.Writer, StringComparison.OrdinalIgnoreCase))
{
return 2;
}
return 1;
});
// Add some randomization so that you're not always seeing the same ones for a given movie
points += random.Next(0, 50);
return points;
}
private static IEnumerable<string> GetTags(BaseItem item)
{
var hasTags = item as IHasTags;
if (hasTags != null)
{
return hasTags.Tags;
}
return new List<string>();
}
private static IEnumerable<string> GetKeywords(BaseItem item)
{
var hasTags = item as IHasKeywords;
if (hasTags != null)
{
return hasTags.Keywords;
}
return new List<string>();
}
public IEnumerable<string> GetAllIntroFiles()
{
2015-11-17 18:46:15 +01:00
return GetCustomIntroFiles(GetOptions(), true, true);
2014-09-22 23:56:54 +02:00
}
private bool IsSupporter
{
get { return _security.IsMBSupporter; }
}
public string Name
{
get { return "Default"; }
}
internal class ItemWithTrailer
{
internal BaseItem Item;
internal ItemWithTrailerType Type;
internal User User;
internal BaseItem WatchingItem;
2015-07-08 18:10:34 +02:00
internal List<PersonInfo> WatchingItemPeople;
internal List<PersonInfo> AllPeople;
2014-09-22 23:56:54 +02:00
internal Random Random;
2015-06-21 05:35:22 +02:00
internal ILibraryManager LibraryManager;
2014-09-22 23:56:54 +02:00
private bool? _isPlayed;
public bool IsPlayed
{
get
{
if (!_isPlayed.HasValue)
{
_isPlayed = Item.IsPlayed(User);
}
return _isPlayed.Value;
}
}
private int? _score;
public int Score
{
get
{
if (!_score.HasValue)
{
2015-07-08 18:10:34 +02:00
_score = GetSimiliarityScore(WatchingItem, WatchingItemPeople, AllPeople, Item, Random, LibraryManager);
2014-09-22 23:56:54 +02:00
}
return _score.Value;
}
}
public IntroInfo IntroInfo
{
get
{
var id = Item.Id;
if (Type == ItemWithTrailerType.ItemWithTrailer)
{
var hasTrailers = Item as IHasTrailers;
if (hasTrailers != null)
{
id = hasTrailers.LocalTrailerIds.FirstOrDefault();
}
}
return new IntroInfo
{
ItemId = id
};
}
}
}
internal enum ItemWithTrailerType
{
LibraryTrailer,
ChannelTrailer,
ItemWithTrailer
}
}
public class CinemaModeConfigurationFactory : IConfigurationFactory
{
public IEnumerable<ConfigurationStore> GetConfigurations()
{
return new[]
{
new ConfigurationStore
{
ConfigurationType = typeof(CinemaModeConfiguration),
Key = "cinemamode"
}
};
}
}
}