| | 1 | | #nullable disable |
| | 2 | |
|
| | 3 | | #pragma warning disable CS1591 |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Globalization; |
| | 8 | | using System.IO; |
| | 9 | | using System.Linq; |
| | 10 | | using System.Threading; |
| | 11 | | using System.Threading.Tasks; |
| | 12 | | using Jellyfin.Data.Enums; |
| | 13 | | using Jellyfin.Data.Events; |
| | 14 | | using Jellyfin.Extensions; |
| | 15 | | using Jellyfin.LiveTv.Configuration; |
| | 16 | | using Jellyfin.LiveTv.Timers; |
| | 17 | | using MediaBrowser.Common.Extensions; |
| | 18 | | using MediaBrowser.Controller.Configuration; |
| | 19 | | using MediaBrowser.Controller.Dto; |
| | 20 | | using MediaBrowser.Controller.Entities; |
| | 21 | | using MediaBrowser.Controller.Library; |
| | 22 | | using MediaBrowser.Controller.LiveTv; |
| | 23 | | using MediaBrowser.Model.Dto; |
| | 24 | | using MediaBrowser.Model.LiveTv; |
| | 25 | | using Microsoft.Extensions.Logging; |
| | 26 | |
|
| | 27 | | namespace Jellyfin.LiveTv |
| | 28 | | { |
| | 29 | | public sealed class DefaultLiveTvService : ILiveTvService, ISupportsDirectStreamProvider, ISupportsNewTimerIds |
| | 30 | | { |
| | 31 | | public const string ServiceName = "Emby"; |
| | 32 | |
|
| | 33 | | private readonly ILogger<DefaultLiveTvService> _logger; |
| | 34 | | private readonly IServerConfigurationManager _config; |
| | 35 | | private readonly ITunerHostManager _tunerHostManager; |
| | 36 | | private readonly IListingsManager _listingsManager; |
| | 37 | | private readonly IRecordingsManager _recordingsManager; |
| | 38 | | private readonly ILibraryManager _libraryManager; |
| | 39 | | private readonly LiveTvDtoService _tvDtoService; |
| | 40 | | private readonly TimerManager _timerManager; |
| | 41 | | private readonly SeriesTimerManager _seriesTimerManager; |
| | 42 | |
|
| | 43 | | public DefaultLiveTvService( |
| | 44 | | ILogger<DefaultLiveTvService> logger, |
| | 45 | | IServerConfigurationManager config, |
| | 46 | | ITunerHostManager tunerHostManager, |
| | 47 | | IListingsManager listingsManager, |
| | 48 | | IRecordingsManager recordingsManager, |
| | 49 | | ILibraryManager libraryManager, |
| | 50 | | LiveTvDtoService tvDtoService, |
| | 51 | | TimerManager timerManager, |
| | 52 | | SeriesTimerManager seriesTimerManager) |
| | 53 | | { |
| 22 | 54 | | _logger = logger; |
| 22 | 55 | | _config = config; |
| 22 | 56 | | _libraryManager = libraryManager; |
| 22 | 57 | | _tunerHostManager = tunerHostManager; |
| 22 | 58 | | _listingsManager = listingsManager; |
| 22 | 59 | | _recordingsManager = recordingsManager; |
| 22 | 60 | | _tvDtoService = tvDtoService; |
| 22 | 61 | | _timerManager = timerManager; |
| 22 | 62 | | _seriesTimerManager = seriesTimerManager; |
| | 63 | |
|
| 22 | 64 | | _timerManager.TimerFired += OnTimerManagerTimerFired; |
| 22 | 65 | | } |
| | 66 | |
|
| | 67 | | public event EventHandler<GenericEventArgs<TimerInfo>> TimerCreated; |
| | 68 | |
|
| | 69 | | public event EventHandler<GenericEventArgs<string>> TimerCancelled; |
| | 70 | |
|
| | 71 | | /// <inheritdoc /> |
| 0 | 72 | | public string Name => ServiceName; |
| | 73 | |
|
| | 74 | | /// <inheritdoc /> |
| 0 | 75 | | public string HomePageUrl => "https://github.com/jellyfin/jellyfin"; |
| | 76 | |
|
| | 77 | | public async Task RefreshSeriesTimers(CancellationToken cancellationToken) |
| | 78 | | { |
| | 79 | | var seriesTimers = await GetSeriesTimersAsync(cancellationToken).ConfigureAwait(false); |
| | 80 | |
|
| | 81 | | foreach (var timer in seriesTimers) |
| | 82 | | { |
| | 83 | | UpdateTimersForSeriesTimer(timer, false, true); |
| | 84 | | } |
| | 85 | | } |
| | 86 | |
|
| | 87 | | public async Task RefreshTimers(CancellationToken cancellationToken) |
| | 88 | | { |
| | 89 | | var timers = await GetTimersAsync(cancellationToken).ConfigureAwait(false); |
| | 90 | |
|
| | 91 | | var tempChannelCache = new Dictionary<Guid, LiveTvChannel>(); |
| | 92 | |
|
| | 93 | | foreach (var timer in timers) |
| | 94 | | { |
| | 95 | | if (DateTime.UtcNow > timer.EndDate && _recordingsManager.GetActiveRecordingPath(timer.Id) is null) |
| | 96 | | { |
| | 97 | | _timerManager.Delete(timer); |
| | 98 | | continue; |
| | 99 | | } |
| | 100 | |
|
| | 101 | | if (string.IsNullOrWhiteSpace(timer.ProgramId) || string.IsNullOrWhiteSpace(timer.ChannelId)) |
| | 102 | | { |
| | 103 | | continue; |
| | 104 | | } |
| | 105 | |
|
| | 106 | | var program = GetProgramInfoFromCache(timer); |
| | 107 | | if (program is null) |
| | 108 | | { |
| | 109 | | _timerManager.Delete(timer); |
| | 110 | | continue; |
| | 111 | | } |
| | 112 | |
|
| | 113 | | CopyProgramInfoToTimerInfo(program, timer, tempChannelCache); |
| | 114 | | _timerManager.Update(timer); |
| | 115 | | } |
| | 116 | | } |
| | 117 | |
|
| | 118 | | private async Task<IEnumerable<ChannelInfo>> GetChannelsAsync(bool enableCache, CancellationToken cancellationTo |
| | 119 | | { |
| | 120 | | var channels = new List<ChannelInfo>(); |
| | 121 | |
|
| | 122 | | foreach (var hostInstance in _tunerHostManager.TunerHosts) |
| | 123 | | { |
| | 124 | | try |
| | 125 | | { |
| | 126 | | var tunerChannels = await hostInstance.GetChannels(enableCache, cancellationToken).ConfigureAwait(fa |
| | 127 | |
|
| | 128 | | channels.AddRange(tunerChannels); |
| | 129 | | } |
| | 130 | | catch (Exception ex) |
| | 131 | | { |
| | 132 | | _logger.LogError(ex, "Error getting channels"); |
| | 133 | | } |
| | 134 | | } |
| | 135 | |
|
| | 136 | | await _listingsManager.AddProviderMetadata(channels, enableCache, cancellationToken).ConfigureAwait(false); |
| | 137 | |
|
| | 138 | | return channels; |
| | 139 | | } |
| | 140 | |
|
| | 141 | | public Task<IEnumerable<ChannelInfo>> GetChannelsAsync(CancellationToken cancellationToken) |
| | 142 | | { |
| 0 | 143 | | return GetChannelsAsync(false, cancellationToken); |
| | 144 | | } |
| | 145 | |
|
| | 146 | | public Task CancelSeriesTimerAsync(string timerId, CancellationToken cancellationToken) |
| | 147 | | { |
| 0 | 148 | | var timers = _timerManager |
| 0 | 149 | | .GetAll() |
| 0 | 150 | | .Where(i => string.Equals(i.SeriesTimerId, timerId, StringComparison.OrdinalIgnoreCase)) |
| 0 | 151 | | .ToList(); |
| | 152 | |
|
| 0 | 153 | | foreach (var timer in timers) |
| | 154 | | { |
| 0 | 155 | | CancelTimerInternal(timer.Id, true, true); |
| | 156 | | } |
| | 157 | |
|
| 0 | 158 | | var remove = _seriesTimerManager.GetAll().FirstOrDefault(r => string.Equals(r.Id, timerId, StringComparison. |
| 0 | 159 | | if (remove is not null) |
| | 160 | | { |
| 0 | 161 | | _seriesTimerManager.Delete(remove); |
| | 162 | | } |
| | 163 | |
|
| 0 | 164 | | return Task.CompletedTask; |
| | 165 | | } |
| | 166 | |
|
| | 167 | | private void CancelTimerInternal(string timerId, bool isSeriesCancelled, bool isManualCancellation) |
| | 168 | | { |
| 0 | 169 | | var timer = _timerManager.GetTimer(timerId); |
| 0 | 170 | | if (timer is not null) |
| | 171 | | { |
| 0 | 172 | | var statusChanging = timer.Status != RecordingStatus.Cancelled; |
| 0 | 173 | | timer.Status = RecordingStatus.Cancelled; |
| | 174 | |
|
| 0 | 175 | | if (isManualCancellation) |
| | 176 | | { |
| 0 | 177 | | timer.IsManual = true; |
| | 178 | | } |
| | 179 | |
|
| 0 | 180 | | if (string.IsNullOrWhiteSpace(timer.SeriesTimerId) || isSeriesCancelled) |
| | 181 | | { |
| 0 | 182 | | _timerManager.Delete(timer); |
| | 183 | | } |
| | 184 | | else |
| | 185 | | { |
| 0 | 186 | | _timerManager.AddOrUpdate(timer, false); |
| | 187 | | } |
| | 188 | |
|
| 0 | 189 | | if (statusChanging && TimerCancelled is not null) |
| | 190 | | { |
| 0 | 191 | | TimerCancelled(this, new GenericEventArgs<string>(timerId)); |
| | 192 | | } |
| | 193 | | } |
| | 194 | |
|
| 0 | 195 | | _recordingsManager.CancelRecording(timerId, timer); |
| 0 | 196 | | } |
| | 197 | |
|
| | 198 | | public Task CancelTimerAsync(string timerId, CancellationToken cancellationToken) |
| | 199 | | { |
| 0 | 200 | | CancelTimerInternal(timerId, false, true); |
| 0 | 201 | | return Task.CompletedTask; |
| | 202 | | } |
| | 203 | |
|
| | 204 | | public Task CreateSeriesTimerAsync(SeriesTimerInfo info, CancellationToken cancellationToken) |
| | 205 | | { |
| 0 | 206 | | throw new NotImplementedException(); |
| | 207 | | } |
| | 208 | |
|
| | 209 | | public Task CreateTimerAsync(TimerInfo info, CancellationToken cancellationToken) |
| | 210 | | { |
| 0 | 211 | | throw new NotImplementedException(); |
| | 212 | | } |
| | 213 | |
|
| | 214 | | public Task<string> CreateTimer(TimerInfo info, CancellationToken cancellationToken) |
| | 215 | | { |
| 0 | 216 | | var existingTimer = string.IsNullOrWhiteSpace(info.ProgramId) ? |
| 0 | 217 | | null : |
| 0 | 218 | | _timerManager.GetTimerByProgramId(info.ProgramId); |
| | 219 | |
|
| 0 | 220 | | if (existingTimer is not null) |
| | 221 | | { |
| 0 | 222 | | if (existingTimer.Status == RecordingStatus.Cancelled |
| 0 | 223 | | || existingTimer.Status == RecordingStatus.Completed) |
| | 224 | | { |
| 0 | 225 | | existingTimer.Status = RecordingStatus.New; |
| 0 | 226 | | existingTimer.IsManual = true; |
| 0 | 227 | | _timerManager.Update(existingTimer); |
| 0 | 228 | | return Task.FromResult(existingTimer.Id); |
| | 229 | | } |
| | 230 | |
|
| 0 | 231 | | throw new ArgumentException("A scheduled recording already exists for this program."); |
| | 232 | | } |
| | 233 | |
|
| 0 | 234 | | info.Id = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture); |
| | 235 | |
|
| 0 | 236 | | LiveTvProgram programInfo = null; |
| | 237 | |
|
| 0 | 238 | | if (!string.IsNullOrWhiteSpace(info.ProgramId)) |
| | 239 | | { |
| 0 | 240 | | programInfo = GetProgramInfoFromCache(info); |
| | 241 | | } |
| | 242 | |
|
| 0 | 243 | | if (programInfo is null) |
| | 244 | | { |
| 0 | 245 | | _logger.LogInformation("Unable to find program with Id {0}. Will search using start date", info.ProgramI |
| 0 | 246 | | programInfo = GetProgramInfoFromCache(info.ChannelId, info.StartDate); |
| | 247 | | } |
| | 248 | |
|
| 0 | 249 | | if (programInfo is not null) |
| | 250 | | { |
| 0 | 251 | | CopyProgramInfoToTimerInfo(programInfo, info); |
| | 252 | | } |
| | 253 | |
|
| 0 | 254 | | info.IsManual = true; |
| 0 | 255 | | _timerManager.Add(info); |
| | 256 | |
|
| 0 | 257 | | TimerCreated?.Invoke(this, new GenericEventArgs<TimerInfo>(info)); |
| | 258 | |
|
| 0 | 259 | | return Task.FromResult(info.Id); |
| | 260 | | } |
| | 261 | |
|
| | 262 | | public async Task<string> CreateSeriesTimer(SeriesTimerInfo info, CancellationToken cancellationToken) |
| | 263 | | { |
| | 264 | | info.Id = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture); |
| | 265 | |
|
| | 266 | | // populate info.seriesID |
| | 267 | | var program = GetProgramInfoFromCache(info.ProgramId); |
| | 268 | |
|
| | 269 | | if (program is not null) |
| | 270 | | { |
| | 271 | | info.SeriesId = program.ExternalSeriesId; |
| | 272 | | } |
| | 273 | | else |
| | 274 | | { |
| | 275 | | throw new InvalidOperationException("SeriesId for program not found"); |
| | 276 | | } |
| | 277 | |
|
| | 278 | | // If any timers have already been manually created, make sure they don't get cancelled |
| | 279 | | var existingTimers = (await GetTimersAsync(CancellationToken.None).ConfigureAwait(false)) |
| | 280 | | .Where(i => |
| | 281 | | { |
| | 282 | | if (string.Equals(i.ProgramId, info.ProgramId, StringComparison.OrdinalIgnoreCase) && !string.IsNull |
| | 283 | | { |
| | 284 | | return true; |
| | 285 | | } |
| | 286 | |
|
| | 287 | | if (string.Equals(i.SeriesId, info.SeriesId, StringComparison.OrdinalIgnoreCase) && !string.IsNullOr |
| | 288 | | { |
| | 289 | | return true; |
| | 290 | | } |
| | 291 | |
|
| | 292 | | return false; |
| | 293 | | }) |
| | 294 | | .ToList(); |
| | 295 | |
|
| | 296 | | _seriesTimerManager.Add(info); |
| | 297 | |
|
| | 298 | | foreach (var timer in existingTimers) |
| | 299 | | { |
| | 300 | | timer.SeriesTimerId = info.Id; |
| | 301 | | timer.IsManual = true; |
| | 302 | |
|
| | 303 | | _timerManager.AddOrUpdate(timer, false); |
| | 304 | | } |
| | 305 | |
|
| | 306 | | UpdateTimersForSeriesTimer(info, true, false); |
| | 307 | |
|
| | 308 | | return info.Id; |
| | 309 | | } |
| | 310 | |
|
| | 311 | | public Task UpdateSeriesTimerAsync(SeriesTimerInfo info, CancellationToken cancellationToken) |
| | 312 | | { |
| 0 | 313 | | var instance = _seriesTimerManager.GetAll().FirstOrDefault(i => string.Equals(i.Id, info.Id, StringCompariso |
| | 314 | |
|
| 0 | 315 | | if (instance is not null) |
| | 316 | | { |
| 0 | 317 | | instance.ChannelId = info.ChannelId; |
| 0 | 318 | | instance.Days = info.Days; |
| 0 | 319 | | instance.EndDate = info.EndDate; |
| 0 | 320 | | instance.IsPostPaddingRequired = info.IsPostPaddingRequired; |
| 0 | 321 | | instance.IsPrePaddingRequired = info.IsPrePaddingRequired; |
| 0 | 322 | | instance.PostPaddingSeconds = info.PostPaddingSeconds; |
| 0 | 323 | | instance.PrePaddingSeconds = info.PrePaddingSeconds; |
| 0 | 324 | | instance.Priority = info.Priority; |
| 0 | 325 | | instance.RecordAnyChannel = info.RecordAnyChannel; |
| 0 | 326 | | instance.RecordAnyTime = info.RecordAnyTime; |
| 0 | 327 | | instance.RecordNewOnly = info.RecordNewOnly; |
| 0 | 328 | | instance.SkipEpisodesInLibrary = info.SkipEpisodesInLibrary; |
| 0 | 329 | | instance.KeepUpTo = info.KeepUpTo; |
| 0 | 330 | | instance.KeepUntil = info.KeepUntil; |
| 0 | 331 | | instance.StartDate = info.StartDate; |
| | 332 | |
|
| 0 | 333 | | _seriesTimerManager.Update(instance); |
| | 334 | |
|
| 0 | 335 | | UpdateTimersForSeriesTimer(instance, true, true); |
| | 336 | | } |
| | 337 | |
|
| 0 | 338 | | return Task.CompletedTask; |
| | 339 | | } |
| | 340 | |
|
| | 341 | | public Task UpdateTimerAsync(TimerInfo updatedTimer, CancellationToken cancellationToken) |
| | 342 | | { |
| 0 | 343 | | var existingTimer = _timerManager.GetTimer(updatedTimer.Id); |
| | 344 | |
|
| 0 | 345 | | if (existingTimer is null) |
| | 346 | | { |
| 0 | 347 | | throw new ResourceNotFoundException(); |
| | 348 | | } |
| | 349 | |
|
| | 350 | | // Only update if not currently active |
| 0 | 351 | | if (_recordingsManager.GetActiveRecordingPath(updatedTimer.Id) is null) |
| | 352 | | { |
| 0 | 353 | | existingTimer.PrePaddingSeconds = updatedTimer.PrePaddingSeconds; |
| 0 | 354 | | existingTimer.PostPaddingSeconds = updatedTimer.PostPaddingSeconds; |
| 0 | 355 | | existingTimer.IsPostPaddingRequired = updatedTimer.IsPostPaddingRequired; |
| 0 | 356 | | existingTimer.IsPrePaddingRequired = updatedTimer.IsPrePaddingRequired; |
| | 357 | |
|
| 0 | 358 | | _timerManager.Update(existingTimer); |
| | 359 | | } |
| | 360 | |
|
| 0 | 361 | | return Task.CompletedTask; |
| | 362 | | } |
| | 363 | |
|
| | 364 | | private static void UpdateExistingTimerWithNewMetadata(TimerInfo existingTimer, TimerInfo updatedTimer) |
| | 365 | | { |
| | 366 | | // Update the program info but retain the status |
| 0 | 367 | | existingTimer.ChannelId = updatedTimer.ChannelId; |
| 0 | 368 | | existingTimer.CommunityRating = updatedTimer.CommunityRating; |
| 0 | 369 | | existingTimer.EndDate = updatedTimer.EndDate; |
| 0 | 370 | | existingTimer.EpisodeNumber = updatedTimer.EpisodeNumber; |
| 0 | 371 | | existingTimer.EpisodeTitle = updatedTimer.EpisodeTitle; |
| 0 | 372 | | existingTimer.Genres = updatedTimer.Genres; |
| 0 | 373 | | existingTimer.IsMovie = updatedTimer.IsMovie; |
| 0 | 374 | | existingTimer.IsSeries = updatedTimer.IsSeries; |
| 0 | 375 | | existingTimer.Tags = updatedTimer.Tags; |
| 0 | 376 | | existingTimer.IsProgramSeries = updatedTimer.IsProgramSeries; |
| 0 | 377 | | existingTimer.IsRepeat = updatedTimer.IsRepeat; |
| 0 | 378 | | existingTimer.Name = updatedTimer.Name; |
| 0 | 379 | | existingTimer.OfficialRating = updatedTimer.OfficialRating; |
| 0 | 380 | | existingTimer.OriginalAirDate = updatedTimer.OriginalAirDate; |
| 0 | 381 | | existingTimer.Overview = updatedTimer.Overview; |
| 0 | 382 | | existingTimer.ProductionYear = updatedTimer.ProductionYear; |
| 0 | 383 | | existingTimer.ProgramId = updatedTimer.ProgramId; |
| 0 | 384 | | existingTimer.SeasonNumber = updatedTimer.SeasonNumber; |
| 0 | 385 | | existingTimer.StartDate = updatedTimer.StartDate; |
| 0 | 386 | | existingTimer.ShowId = updatedTimer.ShowId; |
| 0 | 387 | | existingTimer.ProviderIds = updatedTimer.ProviderIds; |
| 0 | 388 | | existingTimer.SeriesProviderIds = updatedTimer.SeriesProviderIds; |
| 0 | 389 | | } |
| | 390 | |
|
| | 391 | | public Task<IEnumerable<TimerInfo>> GetTimersAsync(CancellationToken cancellationToken) |
| | 392 | | { |
| 0 | 393 | | var excludeStatues = new List<RecordingStatus> |
| 0 | 394 | | { |
| 0 | 395 | | RecordingStatus.Completed |
| 0 | 396 | | }; |
| | 397 | |
|
| 0 | 398 | | var timers = _timerManager.GetAll() |
| 0 | 399 | | .Where(i => !excludeStatues.Contains(i.Status)); |
| | 400 | |
|
| 0 | 401 | | return Task.FromResult(timers); |
| | 402 | | } |
| | 403 | |
|
| | 404 | | public Task<SeriesTimerInfo> GetNewTimerDefaultsAsync(CancellationToken cancellationToken, ProgramInfo program = |
| | 405 | | { |
| 0 | 406 | | var config = _config.GetLiveTvConfiguration(); |
| | 407 | |
|
| 0 | 408 | | var defaults = new SeriesTimerInfo() |
| 0 | 409 | | { |
| 0 | 410 | | PostPaddingSeconds = Math.Max(config.PostPaddingSeconds, 0), |
| 0 | 411 | | PrePaddingSeconds = Math.Max(config.PrePaddingSeconds, 0), |
| 0 | 412 | | RecordAnyChannel = false, |
| 0 | 413 | | RecordAnyTime = true, |
| 0 | 414 | | RecordNewOnly = true, |
| 0 | 415 | |
|
| 0 | 416 | | Days = new List<DayOfWeek> |
| 0 | 417 | | { |
| 0 | 418 | | DayOfWeek.Sunday, |
| 0 | 419 | | DayOfWeek.Monday, |
| 0 | 420 | | DayOfWeek.Tuesday, |
| 0 | 421 | | DayOfWeek.Wednesday, |
| 0 | 422 | | DayOfWeek.Thursday, |
| 0 | 423 | | DayOfWeek.Friday, |
| 0 | 424 | | DayOfWeek.Saturday |
| 0 | 425 | | } |
| 0 | 426 | | }; |
| | 427 | |
|
| 0 | 428 | | if (program is not null) |
| | 429 | | { |
| 0 | 430 | | defaults.SeriesId = program.SeriesId; |
| 0 | 431 | | defaults.ProgramId = program.Id; |
| 0 | 432 | | defaults.RecordNewOnly = !program.IsRepeat; |
| 0 | 433 | | defaults.Name = program.Name; |
| | 434 | | } |
| | 435 | |
|
| 0 | 436 | | defaults.SkipEpisodesInLibrary = defaults.RecordNewOnly; |
| 0 | 437 | | defaults.KeepUntil = KeepUntil.UntilDeleted; |
| | 438 | |
|
| 0 | 439 | | return Task.FromResult(defaults); |
| | 440 | | } |
| | 441 | |
|
| | 442 | | public Task<IEnumerable<SeriesTimerInfo>> GetSeriesTimersAsync(CancellationToken cancellationToken) |
| | 443 | | { |
| 0 | 444 | | return Task.FromResult((IEnumerable<SeriesTimerInfo>)_seriesTimerManager.GetAll()); |
| | 445 | | } |
| | 446 | |
|
| | 447 | | public async Task<IEnumerable<ProgramInfo>> GetProgramsAsync(string channelId, DateTime startDateUtc, DateTime e |
| | 448 | | { |
| | 449 | | var channels = await GetChannelsAsync(true, cancellationToken).ConfigureAwait(false); |
| | 450 | | var channel = channels.First(i => string.Equals(i.Id, channelId, StringComparison.OrdinalIgnoreCase)); |
| | 451 | |
|
| | 452 | | return await _listingsManager.GetProgramsAsync(channel, startDateUtc, endDateUtc, cancellationToken) |
| | 453 | | .ConfigureAwait(false); |
| | 454 | | } |
| | 455 | |
|
| | 456 | | public Task<MediaSourceInfo> GetChannelStream(string channelId, string streamId, CancellationToken cancellationT |
| | 457 | | { |
| 0 | 458 | | throw new NotImplementedException(); |
| | 459 | | } |
| | 460 | |
|
| | 461 | | public async Task<ILiveStream> GetChannelStreamWithDirectStreamProvider(string channelId, string streamId, List< |
| | 462 | | { |
| | 463 | | _logger.LogInformation("Streaming Channel {Id}", channelId); |
| | 464 | |
|
| | 465 | | var result = string.IsNullOrEmpty(streamId) ? |
| | 466 | | null : |
| | 467 | | currentLiveStreams.FirstOrDefault(i => string.Equals(i.OriginalStreamId, streamId, StringComparison.Ordi |
| | 468 | |
|
| | 469 | | if (result is not null && result.EnableStreamSharing) |
| | 470 | | { |
| | 471 | | result.ConsumerCount++; |
| | 472 | |
|
| | 473 | | _logger.LogInformation("Live stream {0} consumer count is now {1}", streamId, result.ConsumerCount); |
| | 474 | |
|
| | 475 | | return result; |
| | 476 | | } |
| | 477 | |
|
| | 478 | | foreach (var hostInstance in _tunerHostManager.TunerHosts) |
| | 479 | | { |
| | 480 | | try |
| | 481 | | { |
| | 482 | | result = await hostInstance.GetChannelStream(channelId, streamId, currentLiveStreams, cancellationTo |
| | 483 | |
|
| | 484 | | var openedMediaSource = result.MediaSource; |
| | 485 | |
|
| | 486 | | result.OriginalStreamId = streamId; |
| | 487 | |
|
| | 488 | | _logger.LogInformation("Returning mediasource streamId {0}, mediaSource.Id {1}, mediaSource.LiveStre |
| | 489 | |
|
| | 490 | | return result; |
| | 491 | | } |
| | 492 | | catch (FileNotFoundException) |
| | 493 | | { |
| | 494 | | } |
| | 495 | | catch (OperationCanceledException) |
| | 496 | | { |
| | 497 | | } |
| | 498 | | } |
| | 499 | |
|
| | 500 | | throw new ResourceNotFoundException("Tuner not found."); |
| | 501 | | } |
| | 502 | |
|
| | 503 | | public async Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(string channelId, CancellationToken cancel |
| | 504 | | { |
| | 505 | | if (string.IsNullOrWhiteSpace(channelId)) |
| | 506 | | { |
| | 507 | | throw new ArgumentNullException(nameof(channelId)); |
| | 508 | | } |
| | 509 | |
|
| | 510 | | foreach (var hostInstance in _tunerHostManager.TunerHosts) |
| | 511 | | { |
| | 512 | | try |
| | 513 | | { |
| | 514 | | var sources = await hostInstance.GetChannelStreamMediaSources(channelId, cancellationToken).Configur |
| | 515 | |
|
| | 516 | | if (sources.Count > 0) |
| | 517 | | { |
| | 518 | | return sources; |
| | 519 | | } |
| | 520 | | } |
| | 521 | | catch (NotImplementedException) |
| | 522 | | { |
| | 523 | | } |
| | 524 | | } |
| | 525 | |
|
| | 526 | | throw new NotImplementedException(); |
| | 527 | | } |
| | 528 | |
|
| | 529 | | public Task CloseLiveStream(string id, CancellationToken cancellationToken) |
| | 530 | | { |
| 0 | 531 | | return Task.CompletedTask; |
| | 532 | | } |
| | 533 | |
|
| | 534 | | public Task ResetTuner(string id, CancellationToken cancellationToken) |
| | 535 | | { |
| 0 | 536 | | return Task.CompletedTask; |
| | 537 | | } |
| | 538 | |
|
| | 539 | | private async void OnTimerManagerTimerFired(object sender, GenericEventArgs<TimerInfo> e) |
| | 540 | | { |
| | 541 | | var timer = e.Argument; |
| | 542 | |
|
| | 543 | | _logger.LogInformation("Recording timer fired for {0}.", timer.Name); |
| | 544 | |
|
| | 545 | | try |
| | 546 | | { |
| | 547 | | var recordingEndDate = timer.EndDate.AddSeconds(timer.PostPaddingSeconds); |
| | 548 | | if (recordingEndDate <= DateTime.UtcNow) |
| | 549 | | { |
| | 550 | | _logger.LogWarning("Recording timer fired for updatedTimer {0}, Id: {1}, but the program has already |
| | 551 | | _timerManager.Delete(timer); |
| | 552 | | return; |
| | 553 | | } |
| | 554 | |
|
| | 555 | | var activeRecordingInfo = new ActiveRecordingInfo |
| | 556 | | { |
| | 557 | | CancellationTokenSource = new CancellationTokenSource(), |
| | 558 | | Timer = timer, |
| | 559 | | Id = timer.Id |
| | 560 | | }; |
| | 561 | |
|
| | 562 | | if (_recordingsManager.GetActiveRecordingPath(timer.Id) is not null) |
| | 563 | | { |
| | 564 | | _logger.LogInformation("Skipping RecordStream because it's already in progress."); |
| | 565 | | return; |
| | 566 | | } |
| | 567 | |
|
| | 568 | | LiveTvProgram programInfo = null; |
| | 569 | | if (!string.IsNullOrWhiteSpace(timer.ProgramId)) |
| | 570 | | { |
| | 571 | | programInfo = GetProgramInfoFromCache(timer); |
| | 572 | | } |
| | 573 | |
|
| | 574 | | if (programInfo is null) |
| | 575 | | { |
| | 576 | | _logger.LogInformation("Unable to find program with Id {0}. Will search using start date", timer.Pro |
| | 577 | | programInfo = GetProgramInfoFromCache(timer.ChannelId, timer.StartDate); |
| | 578 | | } |
| | 579 | |
|
| | 580 | | if (programInfo is not null) |
| | 581 | | { |
| | 582 | | CopyProgramInfoToTimerInfo(programInfo, timer); |
| | 583 | | } |
| | 584 | |
|
| | 585 | | await _recordingsManager.RecordStream(activeRecordingInfo, GetLiveTvChannel(timer), recordingEndDate) |
| | 586 | | .ConfigureAwait(false); |
| | 587 | | } |
| | 588 | | catch (OperationCanceledException) |
| | 589 | | { |
| | 590 | | } |
| | 591 | | catch (Exception ex) |
| | 592 | | { |
| | 593 | | _logger.LogError(ex, "Error recording stream"); |
| | 594 | | } |
| | 595 | | } |
| | 596 | |
|
| | 597 | | private BaseItem GetLiveTvChannel(TimerInfo timer) |
| | 598 | | { |
| 0 | 599 | | var internalChannelId = _tvDtoService.GetInternalChannelId(Name, timer.ChannelId); |
| 0 | 600 | | return _libraryManager.GetItemById(internalChannelId); |
| | 601 | | } |
| | 602 | |
|
| | 603 | | private LiveTvProgram GetProgramInfoFromCache(string programId) |
| | 604 | | { |
| 0 | 605 | | var query = new InternalItemsQuery |
| 0 | 606 | | { |
| 0 | 607 | | ItemIds = [_tvDtoService.GetInternalProgramId(programId)], |
| 0 | 608 | | Limit = 1, |
| 0 | 609 | | DtoOptions = new DtoOptions() |
| 0 | 610 | | }; |
| | 611 | |
|
| 0 | 612 | | return _libraryManager.GetItemList(query).Cast<LiveTvProgram>().FirstOrDefault(); |
| | 613 | | } |
| | 614 | |
|
| | 615 | | private LiveTvProgram GetProgramInfoFromCache(TimerInfo timer) |
| | 616 | | { |
| 0 | 617 | | return GetProgramInfoFromCache(timer.ProgramId); |
| | 618 | | } |
| | 619 | |
|
| | 620 | | private LiveTvProgram GetProgramInfoFromCache(string channelId, DateTime startDateUtc) |
| | 621 | | { |
| 0 | 622 | | var query = new InternalItemsQuery |
| 0 | 623 | | { |
| 0 | 624 | | IncludeItemTypes = new[] { BaseItemKind.LiveTvProgram }, |
| 0 | 625 | | Limit = 1, |
| 0 | 626 | | DtoOptions = new DtoOptions(true) |
| 0 | 627 | | { |
| 0 | 628 | | EnableImages = false |
| 0 | 629 | | }, |
| 0 | 630 | | MinStartDate = startDateUtc.AddMinutes(-3), |
| 0 | 631 | | MaxStartDate = startDateUtc.AddMinutes(3), |
| 0 | 632 | | OrderBy = new[] { (ItemSortBy.StartDate, SortOrder.Ascending) } |
| 0 | 633 | | }; |
| | 634 | |
|
| 0 | 635 | | if (!string.IsNullOrWhiteSpace(channelId)) |
| | 636 | | { |
| 0 | 637 | | query.ChannelIds = [_tvDtoService.GetInternalChannelId(Name, channelId)]; |
| | 638 | | } |
| | 639 | |
|
| 0 | 640 | | return _libraryManager.GetItemList(query).Cast<LiveTvProgram>().FirstOrDefault(); |
| | 641 | | } |
| | 642 | |
|
| | 643 | | private bool ShouldCancelTimerForSeriesTimer(SeriesTimerInfo seriesTimer, TimerInfo timer) |
| | 644 | | { |
| 0 | 645 | | if (timer.IsManual) |
| | 646 | | { |
| 0 | 647 | | return false; |
| | 648 | | } |
| | 649 | |
|
| 0 | 650 | | if (!seriesTimer.RecordAnyTime |
| 0 | 651 | | && Math.Abs(seriesTimer.StartDate.TimeOfDay.Ticks - timer.StartDate.TimeOfDay.Ticks) >= TimeSpan.FromMin |
| | 652 | | { |
| 0 | 653 | | return true; |
| | 654 | | } |
| | 655 | |
|
| 0 | 656 | | if (seriesTimer.RecordNewOnly && timer.IsRepeat) |
| | 657 | | { |
| 0 | 658 | | return true; |
| | 659 | | } |
| | 660 | |
|
| 0 | 661 | | if (!seriesTimer.RecordAnyChannel |
| 0 | 662 | | && !string.Equals(timer.ChannelId, seriesTimer.ChannelId, StringComparison.OrdinalIgnoreCase)) |
| | 663 | | { |
| 0 | 664 | | return true; |
| | 665 | | } |
| | 666 | |
|
| 0 | 667 | | return seriesTimer.SkipEpisodesInLibrary && IsProgramAlreadyInLibrary(timer); |
| | 668 | | } |
| | 669 | |
|
| | 670 | | private void HandleDuplicateShowIds(List<TimerInfo> timers) |
| | 671 | | { |
| | 672 | | // sort showings by HD channels first, then by startDate, record earliest showing possible |
| 0 | 673 | | foreach (var timer in timers.OrderByDescending(t => GetLiveTvChannel(t).IsHD).ThenBy(t => t.StartDate).Skip( |
| | 674 | | { |
| 0 | 675 | | timer.Status = RecordingStatus.Cancelled; |
| 0 | 676 | | _timerManager.Update(timer); |
| | 677 | | } |
| 0 | 678 | | } |
| | 679 | |
|
| | 680 | | private void SearchForDuplicateShowIds(IEnumerable<TimerInfo> timers) |
| | 681 | | { |
| 0 | 682 | | var groups = timers.ToLookup(i => i.ShowId ?? string.Empty).ToList(); |
| | 683 | |
|
| 0 | 684 | | foreach (var group in groups) |
| | 685 | | { |
| 0 | 686 | | if (string.IsNullOrWhiteSpace(group.Key)) |
| | 687 | | { |
| | 688 | | continue; |
| | 689 | | } |
| | 690 | |
|
| 0 | 691 | | var groupTimers = group.ToList(); |
| | 692 | |
|
| 0 | 693 | | if (groupTimers.Count < 2) |
| | 694 | | { |
| | 695 | | continue; |
| | 696 | | } |
| | 697 | |
|
| | 698 | | // Skip ShowId without SubKey from duplicate removal actions - https://github.com/jellyfin/jellyfin/issu |
| 0 | 699 | | if (group.Key.EndsWith("0000", StringComparison.Ordinal)) |
| | 700 | | { |
| | 701 | | continue; |
| | 702 | | } |
| | 703 | |
|
| 0 | 704 | | HandleDuplicateShowIds(groupTimers); |
| | 705 | | } |
| 0 | 706 | | } |
| | 707 | |
|
| | 708 | | private void UpdateTimersForSeriesTimer(SeriesTimerInfo seriesTimer, bool updateTimerSettings, bool deleteInvali |
| | 709 | | { |
| 0 | 710 | | var allTimers = GetTimersForSeries(seriesTimer).ToList(); |
| | 711 | |
|
| 0 | 712 | | var enabledTimersForSeries = new List<TimerInfo>(); |
| 0 | 713 | | foreach (var timer in allTimers) |
| | 714 | | { |
| 0 | 715 | | var existingTimer = _timerManager.GetTimer(timer.Id) |
| 0 | 716 | | ?? (string.IsNullOrWhiteSpace(timer.ProgramId) |
| 0 | 717 | | ? null |
| 0 | 718 | | : _timerManager.GetTimerByProgramId(timer.ProgramId)); |
| | 719 | |
|
| 0 | 720 | | if (existingTimer is null) |
| | 721 | | { |
| 0 | 722 | | if (ShouldCancelTimerForSeriesTimer(seriesTimer, timer)) |
| | 723 | | { |
| 0 | 724 | | timer.Status = RecordingStatus.Cancelled; |
| | 725 | | } |
| | 726 | | else |
| | 727 | | { |
| 0 | 728 | | enabledTimersForSeries.Add(timer); |
| | 729 | | } |
| | 730 | |
|
| 0 | 731 | | _timerManager.Add(timer); |
| | 732 | |
|
| 0 | 733 | | TimerCreated?.Invoke(this, new GenericEventArgs<TimerInfo>(timer)); |
| | 734 | | } |
| | 735 | |
|
| | 736 | | // Only update if not currently active - test both new timer and existing in case Id's are different |
| | 737 | | // Id's could be different if the timer was created manually prior to series timer creation |
| 0 | 738 | | else if (_recordingsManager.GetActiveRecordingPath(timer.Id) is null |
| 0 | 739 | | && _recordingsManager.GetActiveRecordingPath(existingTimer.Id) is null) |
| | 740 | | { |
| 0 | 741 | | UpdateExistingTimerWithNewMetadata(existingTimer, timer); |
| | 742 | |
|
| | 743 | | // Needed by ShouldCancelTimerForSeriesTimer |
| 0 | 744 | | timer.IsManual = existingTimer.IsManual; |
| | 745 | |
|
| 0 | 746 | | if (ShouldCancelTimerForSeriesTimer(seriesTimer, timer)) |
| | 747 | | { |
| 0 | 748 | | existingTimer.Status = RecordingStatus.Cancelled; |
| | 749 | | } |
| 0 | 750 | | else if (!existingTimer.IsManual) |
| | 751 | | { |
| 0 | 752 | | existingTimer.Status = RecordingStatus.New; |
| | 753 | | } |
| | 754 | |
|
| 0 | 755 | | if (existingTimer.Status != RecordingStatus.Cancelled) |
| | 756 | | { |
| 0 | 757 | | enabledTimersForSeries.Add(existingTimer); |
| | 758 | | } |
| | 759 | |
|
| 0 | 760 | | if (updateTimerSettings) |
| | 761 | | { |
| 0 | 762 | | existingTimer.KeepUntil = seriesTimer.KeepUntil; |
| 0 | 763 | | existingTimer.IsPostPaddingRequired = seriesTimer.IsPostPaddingRequired; |
| 0 | 764 | | existingTimer.IsPrePaddingRequired = seriesTimer.IsPrePaddingRequired; |
| 0 | 765 | | existingTimer.PostPaddingSeconds = seriesTimer.PostPaddingSeconds; |
| 0 | 766 | | existingTimer.PrePaddingSeconds = seriesTimer.PrePaddingSeconds; |
| 0 | 767 | | existingTimer.Priority = seriesTimer.Priority; |
| 0 | 768 | | existingTimer.SeriesTimerId = seriesTimer.Id; |
| | 769 | | } |
| | 770 | |
|
| 0 | 771 | | existingTimer.SeriesTimerId = seriesTimer.Id; |
| 0 | 772 | | _timerManager.Update(existingTimer); |
| | 773 | | } |
| | 774 | | } |
| | 775 | |
|
| 0 | 776 | | SearchForDuplicateShowIds(enabledTimersForSeries); |
| | 777 | |
|
| 0 | 778 | | if (deleteInvalidTimers) |
| | 779 | | { |
| 0 | 780 | | var allTimerIds = allTimers |
| 0 | 781 | | .Select(i => i.Id) |
| 0 | 782 | | .ToList(); |
| | 783 | |
|
| 0 | 784 | | var deleteStatuses = new[] |
| 0 | 785 | | { |
| 0 | 786 | | RecordingStatus.New |
| 0 | 787 | | }; |
| | 788 | |
|
| 0 | 789 | | var deletes = _timerManager.GetAll() |
| 0 | 790 | | .Where(i => string.Equals(i.SeriesTimerId, seriesTimer.Id, StringComparison.OrdinalIgnoreCase)) |
| 0 | 791 | | .Where(i => !allTimerIds.Contains(i.Id, StringComparison.OrdinalIgnoreCase) && i.StartDate > DateTim |
| 0 | 792 | | .Where(i => deleteStatuses.Contains(i.Status)) |
| 0 | 793 | | .ToList(); |
| | 794 | |
|
| 0 | 795 | | foreach (var timer in deletes) |
| | 796 | | { |
| 0 | 797 | | CancelTimerInternal(timer.Id, false, false); |
| | 798 | | } |
| | 799 | | } |
| 0 | 800 | | } |
| | 801 | |
|
| | 802 | | private IEnumerable<TimerInfo> GetTimersForSeries(SeriesTimerInfo seriesTimer) |
| | 803 | | { |
| 0 | 804 | | ArgumentNullException.ThrowIfNull(seriesTimer); |
| | 805 | |
|
| 0 | 806 | | var query = new InternalItemsQuery |
| 0 | 807 | | { |
| 0 | 808 | | IncludeItemTypes = new[] { BaseItemKind.LiveTvProgram }, |
| 0 | 809 | | ExternalSeriesId = seriesTimer.SeriesId, |
| 0 | 810 | | DtoOptions = new DtoOptions(true) |
| 0 | 811 | | { |
| 0 | 812 | | EnableImages = false |
| 0 | 813 | | }, |
| 0 | 814 | | MinEndDate = DateTime.UtcNow |
| 0 | 815 | | }; |
| | 816 | |
|
| 0 | 817 | | if (string.IsNullOrEmpty(seriesTimer.SeriesId)) |
| | 818 | | { |
| 0 | 819 | | query.Name = seriesTimer.Name; |
| | 820 | | } |
| | 821 | |
|
| 0 | 822 | | if (!seriesTimer.RecordAnyChannel) |
| | 823 | | { |
| 0 | 824 | | query.ChannelIds = [_tvDtoService.GetInternalChannelId(Name, seriesTimer.ChannelId)]; |
| | 825 | | } |
| | 826 | |
|
| 0 | 827 | | var tempChannelCache = new Dictionary<Guid, LiveTvChannel>(); |
| | 828 | |
|
| 0 | 829 | | return _libraryManager.GetItemList(query).Cast<LiveTvProgram>().Select(i => CreateTimer(i, seriesTimer, temp |
| | 830 | | } |
| | 831 | |
|
| | 832 | | private TimerInfo CreateTimer(LiveTvProgram parent, SeriesTimerInfo seriesTimer, Dictionary<Guid, LiveTvChannel> |
| | 833 | | { |
| 0 | 834 | | string channelId = seriesTimer.RecordAnyChannel ? null : seriesTimer.ChannelId; |
| | 835 | |
|
| 0 | 836 | | if (string.IsNullOrWhiteSpace(channelId) && !parent.ChannelId.IsEmpty()) |
| | 837 | | { |
| 0 | 838 | | if (!tempChannelCache.TryGetValue(parent.ChannelId, out LiveTvChannel channel)) |
| | 839 | | { |
| 0 | 840 | | channel = _libraryManager.GetItemList( |
| 0 | 841 | | new InternalItemsQuery |
| 0 | 842 | | { |
| 0 | 843 | | IncludeItemTypes = new[] { BaseItemKind.LiveTvChannel }, |
| 0 | 844 | | ItemIds = new[] { parent.ChannelId }, |
| 0 | 845 | | DtoOptions = new DtoOptions() |
| 0 | 846 | | }).FirstOrDefault() as LiveTvChannel; |
| | 847 | |
|
| 0 | 848 | | if (channel is not null && !string.IsNullOrWhiteSpace(channel.ExternalId)) |
| | 849 | | { |
| 0 | 850 | | tempChannelCache[parent.ChannelId] = channel; |
| | 851 | | } |
| | 852 | | } |
| | 853 | |
|
| 0 | 854 | | if (channel is not null || tempChannelCache.TryGetValue(parent.ChannelId, out channel)) |
| | 855 | | { |
| 0 | 856 | | channelId = channel.ExternalId; |
| | 857 | | } |
| | 858 | | } |
| | 859 | |
|
| 0 | 860 | | var timer = new TimerInfo |
| 0 | 861 | | { |
| 0 | 862 | | ChannelId = channelId, |
| 0 | 863 | | Id = (seriesTimer.Id + parent.ExternalId).GetMD5().ToString("N", CultureInfo.InvariantCulture), |
| 0 | 864 | | StartDate = parent.StartDate, |
| 0 | 865 | | EndDate = parent.EndDate.Value, |
| 0 | 866 | | ProgramId = parent.ExternalId, |
| 0 | 867 | | PrePaddingSeconds = seriesTimer.PrePaddingSeconds, |
| 0 | 868 | | PostPaddingSeconds = seriesTimer.PostPaddingSeconds, |
| 0 | 869 | | IsPostPaddingRequired = seriesTimer.IsPostPaddingRequired, |
| 0 | 870 | | IsPrePaddingRequired = seriesTimer.IsPrePaddingRequired, |
| 0 | 871 | | KeepUntil = seriesTimer.KeepUntil, |
| 0 | 872 | | Priority = seriesTimer.Priority, |
| 0 | 873 | | Name = parent.Name, |
| 0 | 874 | | Overview = parent.Overview, |
| 0 | 875 | | SeriesId = parent.ExternalSeriesId, |
| 0 | 876 | | SeriesTimerId = seriesTimer.Id, |
| 0 | 877 | | ShowId = parent.ShowId |
| 0 | 878 | | }; |
| | 879 | |
|
| 0 | 880 | | CopyProgramInfoToTimerInfo(parent, timer, tempChannelCache); |
| | 881 | |
|
| 0 | 882 | | return timer; |
| | 883 | | } |
| | 884 | |
|
| | 885 | | private void CopyProgramInfoToTimerInfo(LiveTvProgram programInfo, TimerInfo timerInfo) |
| | 886 | | { |
| 0 | 887 | | var tempChannelCache = new Dictionary<Guid, LiveTvChannel>(); |
| 0 | 888 | | CopyProgramInfoToTimerInfo(programInfo, timerInfo, tempChannelCache); |
| 0 | 889 | | } |
| | 890 | |
|
| | 891 | | private void CopyProgramInfoToTimerInfo(LiveTvProgram programInfo, TimerInfo timerInfo, Dictionary<Guid, LiveTvC |
| | 892 | | { |
| 0 | 893 | | string channelId = null; |
| | 894 | |
|
| 0 | 895 | | if (!programInfo.ChannelId.IsEmpty()) |
| | 896 | | { |
| 0 | 897 | | if (!tempChannelCache.TryGetValue(programInfo.ChannelId, out LiveTvChannel channel)) |
| | 898 | | { |
| 0 | 899 | | channel = _libraryManager.GetItemList( |
| 0 | 900 | | new InternalItemsQuery |
| 0 | 901 | | { |
| 0 | 902 | | IncludeItemTypes = new[] { BaseItemKind.LiveTvChannel }, |
| 0 | 903 | | ItemIds = new[] { programInfo.ChannelId }, |
| 0 | 904 | | DtoOptions = new DtoOptions() |
| 0 | 905 | | }).FirstOrDefault() as LiveTvChannel; |
| | 906 | |
|
| 0 | 907 | | if (channel is not null && !string.IsNullOrWhiteSpace(channel.ExternalId)) |
| | 908 | | { |
| 0 | 909 | | tempChannelCache[programInfo.ChannelId] = channel; |
| | 910 | | } |
| | 911 | | } |
| | 912 | |
|
| 0 | 913 | | if (channel is not null || tempChannelCache.TryGetValue(programInfo.ChannelId, out channel)) |
| | 914 | | { |
| 0 | 915 | | channelId = channel.ExternalId; |
| | 916 | | } |
| | 917 | | } |
| | 918 | |
|
| 0 | 919 | | timerInfo.Name = programInfo.Name; |
| 0 | 920 | | timerInfo.StartDate = programInfo.StartDate; |
| 0 | 921 | | timerInfo.EndDate = programInfo.EndDate.Value; |
| | 922 | |
|
| 0 | 923 | | if (!string.IsNullOrWhiteSpace(channelId)) |
| | 924 | | { |
| 0 | 925 | | timerInfo.ChannelId = channelId; |
| | 926 | | } |
| | 927 | |
|
| 0 | 928 | | timerInfo.SeasonNumber = programInfo.ParentIndexNumber; |
| 0 | 929 | | timerInfo.EpisodeNumber = programInfo.IndexNumber; |
| 0 | 930 | | timerInfo.IsMovie = programInfo.IsMovie; |
| 0 | 931 | | timerInfo.ProductionYear = programInfo.ProductionYear; |
| 0 | 932 | | timerInfo.EpisodeTitle = programInfo.EpisodeTitle; |
| 0 | 933 | | timerInfo.OriginalAirDate = programInfo.PremiereDate; |
| 0 | 934 | | timerInfo.IsProgramSeries = programInfo.IsSeries; |
| | 935 | |
|
| 0 | 936 | | timerInfo.IsSeries = programInfo.IsSeries; |
| | 937 | |
|
| 0 | 938 | | timerInfo.CommunityRating = programInfo.CommunityRating; |
| 0 | 939 | | timerInfo.Overview = programInfo.Overview; |
| 0 | 940 | | timerInfo.OfficialRating = programInfo.OfficialRating; |
| 0 | 941 | | timerInfo.IsRepeat = programInfo.IsRepeat; |
| 0 | 942 | | timerInfo.SeriesId = programInfo.ExternalSeriesId; |
| 0 | 943 | | timerInfo.ProviderIds = programInfo.ProviderIds; |
| 0 | 944 | | timerInfo.Tags = programInfo.Tags; |
| | 945 | |
|
| 0 | 946 | | var seriesProviderIds = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| | 947 | |
|
| 0 | 948 | | foreach (var providerId in timerInfo.ProviderIds) |
| | 949 | | { |
| | 950 | | const string Search = "Series"; |
| 0 | 951 | | if (providerId.Key.StartsWith(Search, StringComparison.OrdinalIgnoreCase)) |
| | 952 | | { |
| 0 | 953 | | seriesProviderIds[providerId.Key.Substring(Search.Length)] = providerId.Value; |
| | 954 | | } |
| | 955 | | } |
| | 956 | |
|
| 0 | 957 | | timerInfo.SeriesProviderIds = seriesProviderIds; |
| 0 | 958 | | } |
| | 959 | |
|
| | 960 | | private bool IsProgramAlreadyInLibrary(TimerInfo program) |
| | 961 | | { |
| 0 | 962 | | if ((program.EpisodeNumber.HasValue && program.SeasonNumber.HasValue) || !string.IsNullOrWhiteSpace(program. |
| | 963 | | { |
| 0 | 964 | | var seriesIds = _libraryManager.GetItemIds( |
| 0 | 965 | | new InternalItemsQuery |
| 0 | 966 | | { |
| 0 | 967 | | IncludeItemTypes = new[] { BaseItemKind.Series }, |
| 0 | 968 | | Name = program.Name |
| 0 | 969 | | }).ToArray(); |
| | 970 | |
|
| 0 | 971 | | if (seriesIds.Length == 0) |
| | 972 | | { |
| 0 | 973 | | return false; |
| | 974 | | } |
| | 975 | |
|
| 0 | 976 | | if (program.EpisodeNumber.HasValue && program.SeasonNumber.HasValue) |
| | 977 | | { |
| 0 | 978 | | var result = _libraryManager.GetItemIds(new InternalItemsQuery |
| 0 | 979 | | { |
| 0 | 980 | | IncludeItemTypes = new[] { BaseItemKind.Episode }, |
| 0 | 981 | | ParentIndexNumber = program.SeasonNumber.Value, |
| 0 | 982 | | IndexNumber = program.EpisodeNumber.Value, |
| 0 | 983 | | AncestorIds = seriesIds, |
| 0 | 984 | | IsVirtualItem = false, |
| 0 | 985 | | Limit = 1 |
| 0 | 986 | | }); |
| | 987 | |
|
| 0 | 988 | | if (result.Count > 0) |
| | 989 | | { |
| 0 | 990 | | return true; |
| | 991 | | } |
| | 992 | | } |
| | 993 | | } |
| | 994 | |
|
| 0 | 995 | | return false; |
| | 996 | | } |
| | 997 | | } |
| | 998 | | } |