diff --git a/MediaBrowser.Api/LiveTv/LiveTvService.cs b/MediaBrowser.Api/LiveTv/LiveTvService.cs index 50e7319b9e..f92c4932e0 100644 --- a/MediaBrowser.Api/LiveTv/LiveTvService.cs +++ b/MediaBrowser.Api/LiveTv/LiveTvService.cs @@ -154,6 +154,23 @@ namespace MediaBrowser.Api.LiveTv public string MaxEndDate { get; set; } } + [Route("/LiveTv/Programs/Recommended", "GET")] + [Api(Description = "Gets available live tv epgs..")] + public class GetRecommendedPrograms : IReturn> + { + [ApiMember(Name = "UserId", Description = "Optional filter by user id.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET,POST")] + public string UserId { get; set; } + + [ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")] + public int? Limit { get; set; } + + [ApiMember(Name = "IsAiring", Description = "Optional. Filter by programs that are currently airing, or not.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")] + public bool? IsAiring { get; set; } + + [ApiMember(Name = "HasAired", Description = "Optional. Filter by programs that have completed airing, or not.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")] + public bool? HasAired { get; set; } + } + [Route("/LiveTv/Programs/{Id}", "GET")] [Api(Description = "Gets a live tv program")] public class GetProgram : IReturn @@ -331,6 +348,21 @@ namespace MediaBrowser.Api.LiveTv return ToOptimizedResult(result); } + public object Get(GetRecommendedPrograms request) + { + var query = new RecommendedProgramQuery + { + UserId = request.UserId, + IsAiring = request.IsAiring, + Limit = request.Limit, + HasAired = request.HasAired + }; + + var result = _liveTvManager.GetRecommendedPrograms(query, CancellationToken.None).Result; + + return ToOptimizedResult(result); + } + public object Post(GetPrograms request) { return Get(request); diff --git a/MediaBrowser.Controller/LiveTv/ILiveTvManager.cs b/MediaBrowser.Controller/LiveTv/ILiveTvManager.cs index 1856182da2..31c336932f 100644 --- a/MediaBrowser.Controller/LiveTv/ILiveTvManager.cs +++ b/MediaBrowser.Controller/LiveTv/ILiveTvManager.cs @@ -240,5 +240,14 @@ namespace MediaBrowser.Controller.LiveTv /// /// GuideInfo. GuideInfo GetGuideInfo(); + + /// + /// Gets the recommended programs. + /// + /// The query. + /// The cancellation token. + /// Task{QueryResult{ProgramInfoDto}}. + Task> GetRecommendedPrograms(RecommendedProgramQuery query, + CancellationToken cancellationToken); } } diff --git a/MediaBrowser.Controller/LiveTv/ILiveTvService.cs b/MediaBrowser.Controller/LiveTv/ILiveTvService.cs index 1e535139ce..81613c1df6 100644 --- a/MediaBrowser.Controller/LiveTv/ILiveTvService.cs +++ b/MediaBrowser.Controller/LiveTv/ILiveTvService.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -37,7 +38,7 @@ namespace MediaBrowser.Controller.LiveTv /// The cancellation token. /// Task. Task CancelSeriesTimerAsync(string timerId, CancellationToken cancellationToken); - + /// /// Deletes the recording asynchronous. /// @@ -77,7 +78,7 @@ namespace MediaBrowser.Controller.LiveTv /// The cancellation token. /// Task. Task UpdateSeriesTimerAsync(SeriesTimerInfo info, CancellationToken cancellationToken); - + /// /// Gets the channel image asynchronous. This only needs to be implemented if an image path or url cannot be supplied to ChannelInfo /// @@ -102,7 +103,7 @@ namespace MediaBrowser.Controller.LiveTv /// The cancellation token. /// Task{ImageResponseInfo}. Task GetProgramImageAsync(string programId, string channelId, CancellationToken cancellationToken); - + /// /// Gets the recordings asynchronous. /// @@ -123,21 +124,23 @@ namespace MediaBrowser.Controller.LiveTv /// The cancellation token. /// Task{TimerInfo}. Task GetNewTimerDefaultsAsync(CancellationToken cancellationToken); - + /// /// Gets the series timers asynchronous. /// /// The cancellation token. /// Task{IEnumerable{SeriesTimerInfo}}. Task> GetSeriesTimersAsync(CancellationToken cancellationToken); - + /// /// Gets the programs asynchronous. /// /// The channel identifier. + /// The start date UTC. + /// The end date UTC. /// The cancellation token. /// Task{IEnumerable{ProgramInfo}}. - Task> GetProgramsAsync(string channelId, CancellationToken cancellationToken); + Task> GetProgramsAsync(string channelId, DateTime startDateUtc, DateTime endDateUtc, CancellationToken cancellationToken); /// /// Gets the recording stream. @@ -162,5 +165,13 @@ namespace MediaBrowser.Controller.LiveTv /// The cancellation token. /// Task. Task CloseLiveStream(string id, CancellationToken cancellationToken); + + /// + /// Records the live stream. + /// + /// The identifier. + /// The cancellation token. + /// Task. + Task RecordLiveStream(string id, CancellationToken cancellationToken); } } diff --git a/MediaBrowser.Controller/LiveTv/LiveTvProgram.cs b/MediaBrowser.Controller/LiveTv/LiveTvProgram.cs index abacc0c18e..aceb32885e 100644 --- a/MediaBrowser.Controller/LiveTv/LiveTvProgram.cs +++ b/MediaBrowser.Controller/LiveTv/LiveTvProgram.cs @@ -1,5 +1,6 @@ using MediaBrowser.Controller.Entities; using MediaBrowser.Model.LiveTv; +using System; namespace MediaBrowser.Controller.LiveTv { @@ -28,6 +29,26 @@ namespace MediaBrowser.Controller.LiveTv } } + public bool IsAiring + { + get + { + var now = DateTime.UtcNow; + + return now >= ProgramInfo.StartDate && now < ProgramInfo.EndDate; + } + } + + public bool HasAired + { + get + { + var now = DateTime.UtcNow; + + return now >= ProgramInfo.EndDate; + } + } + public override string GetClientTypeName() { return "Program"; diff --git a/MediaBrowser.Model/LiveTv/ProgramQuery.cs b/MediaBrowser.Model/LiveTv/ProgramQuery.cs index 36c06d4c08..a2a8249942 100644 --- a/MediaBrowser.Model/LiveTv/ProgramQuery.cs +++ b/MediaBrowser.Model/LiveTv/ProgramQuery.cs @@ -32,4 +32,31 @@ namespace MediaBrowser.Model.LiveTv ChannelIdList = new string[] { }; } } + + public class RecommendedProgramQuery + { + /// + /// Gets or sets the user identifier. + /// + /// The user identifier. + public string UserId { get; set; } + + /// + /// Gets or sets a value indicating whether this instance is airing. + /// + /// true if this instance is airing; otherwise, false. + public bool? IsAiring { get; set; } + + /// + /// Gets or sets a value indicating whether this instance has aired. + /// + /// null if [has aired] contains no value, true if [has aired]; otherwise, false. + public bool? HasAired { get; set; } + + /// + /// The maximum number of items to return + /// + /// The limit. + public int? Limit { get; set; } + } } diff --git a/MediaBrowser.Server.Implementations/LiveTv/LiveTvManager.cs b/MediaBrowser.Server.Implementations/LiveTv/LiveTvManager.cs index 17e17ab70a..91766e0f80 100644 --- a/MediaBrowser.Server.Implementations/LiveTv/LiveTvManager.cs +++ b/MediaBrowser.Server.Implementations/LiveTv/LiveTvManager.cs @@ -32,6 +32,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv private readonly ILogger _logger; private readonly IItemRepository _itemRepo; private readonly IUserManager _userManager; + private readonly IUserDataManager _userDataManager; private readonly ILibraryManager _libraryManager; private readonly IMediaEncoder _mediaEncoder; @@ -54,6 +55,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv _userManager = userManager; _libraryManager = libraryManager; _mediaEncoder = mediaEncoder; + _userDataManager = userDataManager; _tvDtoService = new LiveTvDtoService(dtoService, userDataManager, imageProcessor, logger, _itemRepo); } @@ -428,7 +430,6 @@ namespace MediaBrowser.Server.Implementations.LiveTv } var returnArray = programs - .OrderBy(i => i.ProgramInfo.StartDate) .Select(i => { var channel = GetChannel(i); @@ -450,6 +451,138 @@ namespace MediaBrowser.Server.Implementations.LiveTv return result; } + public async Task> GetRecommendedPrograms(RecommendedProgramQuery query, CancellationToken cancellationToken) + { + IEnumerable programs = _programs.Values; + + var user = _userManager.GetUserById(new Guid(query.UserId)); + + // Avoid implicitly captured closure + var currentUser = user; + programs = programs.Where(i => i.IsParentalAllowed(currentUser)); + + if (query.IsAiring.HasValue) + { + var val = query.IsAiring.Value; + programs = programs.Where(i => i.IsAiring == val); + } + + if (query.HasAired.HasValue) + { + var val = query.HasAired.Value; + programs = programs.Where(i => i.HasAired == val); + } + + var serviceName = ActiveService.Name; + + var programList = programs.ToList(); + + var genres = programList.SelectMany(i => i.Genres) + .Distinct(StringComparer.OrdinalIgnoreCase) + .Select(i => _libraryManager.GetGenre(i)) + .ToDictionary(i => i.Name, StringComparer.OrdinalIgnoreCase); + + programs = programList.OrderByDescending(i => GetRecommendationScore(i.ProgramInfo, user.Id, serviceName, genres)) + .ThenBy(i => i.ProgramInfo.StartDate); + + if (query.Limit.HasValue) + { + programs = programs.Take(query.Limit.Value) + .OrderBy(i => i.ProgramInfo.StartDate); + } + + var returnArray = programs + .Select(i => + { + var channel = GetChannel(i); + + var channelName = channel == null ? null : channel.ChannelInfo.Name; + + return _tvDtoService.GetProgramInfoDto(i, channelName, user); + }) + .ToArray(); + + await AddRecordingInfo(returnArray, cancellationToken).ConfigureAwait(false); + + var result = new QueryResult + { + Items = returnArray, + TotalRecordCount = returnArray.Length + }; + + return result; + } + + private int GetRecommendationScore(ProgramInfo program, Guid userId, string serviceName, Dictionary genres) + { + var score = 0; + + if (program.IsLive) + { + score++; + } + + if (program.IsSeries && !program.IsRepeat) + { + score++; + } + + var internalChannelId = _tvDtoService.GetInternalChannelId(serviceName, program.ChannelId); + var channel = GetInternalChannel(internalChannelId); + + var channelUserdata = _userDataManager.GetUserData(userId, channel.GetUserDataKey()); + + if ((channelUserdata.Likes ?? false)) + { + score += 2; + } + else if (!(channelUserdata.Likes ?? true)) + { + score -= 2; + } + + if (channelUserdata.IsFavorite) + { + score += 3; + } + + score += GetGenreScore(program.Genres, userId, genres); + + return score; + } + + private int GetGenreScore(IEnumerable programGenres, Guid userId, Dictionary genres) + { + return programGenres.Select(i => + { + var score = 0; + + Genre genre; + + if (genres.TryGetValue(i, out genre)) + { + var genreUserdata = _userDataManager.GetUserData(userId, genre.GetUserDataKey()); + + if ((genreUserdata.Likes ?? false)) + { + score++; + } + else if (!(genreUserdata.Likes ?? true)) + { + score--; + } + + if (genreUserdata.IsFavorite) + { + score += 2; + } + } + + return score; + + }).Sum(); + } + private async Task AddRecordingInfo(IEnumerable programs, CancellationToken cancellationToken) { var timers = await ActiveService.GetTimersAsync(cancellationToken).ConfigureAwait(false); @@ -533,7 +666,10 @@ namespace MediaBrowser.Server.Implementations.LiveTv try { - var channelPrograms = await service.GetProgramsAsync(currentChannel.ChannelInfo.Id, cancellationToken).ConfigureAwait(false); + var start = DateTime.UtcNow; + var end = start.AddDays(3); + + var channelPrograms = await service.GetProgramsAsync(currentChannel.ChannelInfo.Id, start, end, cancellationToken).ConfigureAwait(false); var programTasks = channelPrograms.Select(program => GetProgram(program, currentChannel.ChannelInfo.ChannelType, service.Name, cancellationToken)); var programEntities = await Task.WhenAll(programTasks).ConfigureAwait(false); diff --git a/MediaBrowser.WebDashboard/ApiClient.js b/MediaBrowser.WebDashboard/ApiClient.js index fe0e5e541f..b35341ebb0 100644 --- a/MediaBrowser.WebDashboard/ApiClient.js +++ b/MediaBrowser.WebDashboard/ApiClient.js @@ -438,7 +438,7 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout, wi options = options || {}; - if (options.channelIds) { + if (options.channelIds && options.channelIds.length > 1800) { return self.ajax({ type: "POST", @@ -458,6 +458,17 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout, wi } }; + self.getLiveTvRecommendedPrograms = function (options) { + + options = options || {}; + + return self.ajax({ + type: "GET", + url: self.getUrl("LiveTv/Programs/Recommended", options), + dataType: "json" + }); + }; + self.getLiveTvRecordings = function (options) { var url = self.getUrl("LiveTv/Recordings", options || {}); diff --git a/MediaBrowser.WebDashboard/packages.config b/MediaBrowser.WebDashboard/packages.config index 6c512e8bb6..b28f5655a4 100644 --- a/MediaBrowser.WebDashboard/packages.config +++ b/MediaBrowser.WebDashboard/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file diff --git a/Nuget/MediaBrowser.Common.Internal.nuspec b/Nuget/MediaBrowser.Common.Internal.nuspec index 534957de61..9c011b3f41 100644 --- a/Nuget/MediaBrowser.Common.Internal.nuspec +++ b/Nuget/MediaBrowser.Common.Internal.nuspec @@ -2,7 +2,7 @@ MediaBrowser.Common.Internal - 3.0.298 + 3.0.299 MediaBrowser.Common.Internal Luke ebr,Luke,scottisafool @@ -12,7 +12,7 @@ Contains common components shared by Media Browser Theater and Media Browser Server. Not intended for plugin developer consumption. Copyright © Media Browser 2013 - + diff --git a/Nuget/MediaBrowser.Common.nuspec b/Nuget/MediaBrowser.Common.nuspec index e6792e1df4..d0cd3a3069 100644 --- a/Nuget/MediaBrowser.Common.nuspec +++ b/Nuget/MediaBrowser.Common.nuspec @@ -2,7 +2,7 @@ MediaBrowser.Common - 3.0.298 + 3.0.299 MediaBrowser.Common Media Browser Team ebr,Luke,scottisafool diff --git a/Nuget/MediaBrowser.Server.Core.nuspec b/Nuget/MediaBrowser.Server.Core.nuspec index db48a2891f..cc15b8c4d1 100644 --- a/Nuget/MediaBrowser.Server.Core.nuspec +++ b/Nuget/MediaBrowser.Server.Core.nuspec @@ -2,7 +2,7 @@ MediaBrowser.Server.Core - 3.0.298 + 3.0.299 Media Browser.Server.Core Media Browser Team ebr,Luke,scottisafool @@ -12,7 +12,7 @@ Contains core components required to build plugins for Media Browser Server. Copyright © Media Browser 2013 - +