| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using Jellyfin.Data.Enums; |
| | | 7 | | using Jellyfin.Extensions; |
| | | 8 | | using Jellyfin.LiveTv; |
| | | 9 | | using Jellyfin.LiveTv.Configuration; |
| | | 10 | | using MediaBrowser.Common.Configuration; |
| | | 11 | | using MediaBrowser.Controller.Dto; |
| | | 12 | | using MediaBrowser.Controller.Entities; |
| | | 13 | | using MediaBrowser.Controller.Library; |
| | | 14 | | using MediaBrowser.Controller.LiveTv; |
| | | 15 | | using MediaBrowser.Controller.Persistence; |
| | | 16 | | using MediaBrowser.Controller.Providers; |
| | | 17 | | using MediaBrowser.Model.Entities; |
| | | 18 | | using MediaBrowser.Model.IO; |
| | | 19 | | using MediaBrowser.Model.LiveTv; |
| | | 20 | | using Microsoft.Extensions.Logging; |
| | | 21 | | |
| | | 22 | | namespace Jellyfin.LiveTv.Guide; |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | public class GuideManager : IGuideManager |
| | | 26 | | { |
| | | 27 | | private const int MaxGuideDays = 14; |
| | | 28 | | private const string EtagKey = "ProgramEtag"; |
| | | 29 | | private const string ExternalServiceTag = "ExternalServiceId"; |
| | | 30 | | |
| | 0 | 31 | | private static readonly ParallelOptions _cacheParallelOptions = new() { MaxDegreeOfParallelism = Math.Min(Environmen |
| | | 32 | | |
| | | 33 | | private readonly ILogger<GuideManager> _logger; |
| | | 34 | | private readonly IConfigurationManager _config; |
| | | 35 | | private readonly IFileSystem _fileSystem; |
| | | 36 | | private readonly IItemRepository _itemRepo; |
| | | 37 | | private readonly ILibraryManager _libraryManager; |
| | | 38 | | private readonly ILiveTvManager _liveTvManager; |
| | | 39 | | private readonly ITunerHostManager _tunerHostManager; |
| | | 40 | | private readonly IRecordingsManager _recordingsManager; |
| | | 41 | | private readonly ISchedulesDirectService _schedulesDirectService; |
| | | 42 | | private readonly LiveTvDtoService _tvDtoService; |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Amount of days images are pre-cached from external sources. |
| | | 46 | | /// </summary> |
| | | 47 | | public const int MaxCacheDays = 2; |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Initializes a new instance of the <see cref="GuideManager"/> class. |
| | | 51 | | /// </summary> |
| | | 52 | | /// <param name="logger">The <see cref="ILogger{TCategoryName}"/>.</param> |
| | | 53 | | /// <param name="config">The <see cref="IConfigurationManager"/>.</param> |
| | | 54 | | /// <param name="fileSystem">The <see cref="IFileSystem"/>.</param> |
| | | 55 | | /// <param name="itemRepo">The <see cref="IItemRepository"/>.</param> |
| | | 56 | | /// <param name="libraryManager">The <see cref="ILibraryManager"/>.</param> |
| | | 57 | | /// <param name="liveTvManager">The <see cref="ILiveTvManager"/>.</param> |
| | | 58 | | /// <param name="tunerHostManager">The <see cref="ITunerHostManager"/>.</param> |
| | | 59 | | /// <param name="recordingsManager">The <see cref="IRecordingsManager"/>.</param> |
| | | 60 | | /// <param name="schedulesDirectService">The <see cref="ISchedulesDirectService"/>.</param> |
| | | 61 | | /// <param name="tvDtoService">The <see cref="LiveTvDtoService"/>.</param> |
| | | 62 | | public GuideManager( |
| | | 63 | | ILogger<GuideManager> logger, |
| | | 64 | | IConfigurationManager config, |
| | | 65 | | IFileSystem fileSystem, |
| | | 66 | | IItemRepository itemRepo, |
| | | 67 | | ILibraryManager libraryManager, |
| | | 68 | | ILiveTvManager liveTvManager, |
| | | 69 | | ITunerHostManager tunerHostManager, |
| | | 70 | | IRecordingsManager recordingsManager, |
| | | 71 | | ISchedulesDirectService schedulesDirectService, |
| | | 72 | | LiveTvDtoService tvDtoService) |
| | | 73 | | { |
| | 22 | 74 | | _logger = logger; |
| | 22 | 75 | | _config = config; |
| | 22 | 76 | | _fileSystem = fileSystem; |
| | 22 | 77 | | _itemRepo = itemRepo; |
| | 22 | 78 | | _libraryManager = libraryManager; |
| | 22 | 79 | | _liveTvManager = liveTvManager; |
| | 22 | 80 | | _tunerHostManager = tunerHostManager; |
| | 22 | 81 | | _recordingsManager = recordingsManager; |
| | 22 | 82 | | _schedulesDirectService = schedulesDirectService; |
| | 22 | 83 | | _tvDtoService = tvDtoService; |
| | 22 | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <inheritdoc /> |
| | | 87 | | public GuideInfo GetGuideInfo() |
| | | 88 | | { |
| | 0 | 89 | | var startDate = DateTime.UtcNow; |
| | 0 | 90 | | var endDate = startDate.AddDays(GetGuideDays()); |
| | | 91 | | |
| | 0 | 92 | | return new GuideInfo |
| | 0 | 93 | | { |
| | 0 | 94 | | StartDate = startDate, |
| | 0 | 95 | | EndDate = endDate |
| | 0 | 96 | | }; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | /// <inheritdoc /> |
| | | 100 | | public async Task RefreshGuide(IProgress<double> progress, CancellationToken cancellationToken) |
| | | 101 | | { |
| | 1 | 102 | | ArgumentNullException.ThrowIfNull(progress); |
| | | 103 | | |
| | 1 | 104 | | await _recordingsManager.CreateRecordingFolders().ConfigureAwait(false); |
| | | 105 | | |
| | 1 | 106 | | await _tunerHostManager.ScanForTunerDeviceChanges(cancellationToken).ConfigureAwait(false); |
| | | 107 | | |
| | 1 | 108 | | var numComplete = 0; |
| | 1 | 109 | | double progressPerService = _liveTvManager.Services.Count == 0 |
| | 1 | 110 | | ? 0 |
| | 1 | 111 | | : 1.0 / _liveTvManager.Services.Count; |
| | | 112 | | |
| | 1 | 113 | | var newChannelIdList = new List<Guid>(); |
| | 1 | 114 | | var newProgramIdList = new List<Guid>(); |
| | | 115 | | |
| | 1 | 116 | | var cleanDatabase = true; |
| | | 117 | | |
| | 3 | 118 | | foreach (var service in _liveTvManager.Services) |
| | | 119 | | { |
| | 1 | 120 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 121 | | |
| | 0 | 122 | | _logger.LogDebug("Refreshing guide from {Name}", service.Name); |
| | | 123 | | |
| | | 124 | | try |
| | | 125 | | { |
| | 0 | 126 | | var innerProgress = new Progress<double>(p => progress.Report(p * progressPerService)); |
| | | 127 | | |
| | 0 | 128 | | var idList = await RefreshChannelsInternal(service, innerProgress, cancellationToken).ConfigureAwait(fal |
| | | 129 | | |
| | 0 | 130 | | newChannelIdList.AddRange(idList.Item1); |
| | 0 | 131 | | newProgramIdList.AddRange(idList.Item2); |
| | 0 | 132 | | } |
| | 0 | 133 | | catch (OperationCanceledException) |
| | | 134 | | { |
| | 0 | 135 | | throw; |
| | | 136 | | } |
| | 0 | 137 | | catch (Exception ex) |
| | | 138 | | { |
| | 0 | 139 | | cleanDatabase = false; |
| | 0 | 140 | | _logger.LogError(ex, "Error refreshing channels for service"); |
| | 0 | 141 | | } |
| | | 142 | | |
| | 0 | 143 | | numComplete++; |
| | 0 | 144 | | double percent = numComplete; |
| | 0 | 145 | | percent /= _liveTvManager.Services.Count; |
| | | 146 | | |
| | 0 | 147 | | progress.Report(100 * percent); |
| | | 148 | | } |
| | | 149 | | |
| | 0 | 150 | | if (cleanDatabase) |
| | | 151 | | { |
| | 0 | 152 | | CleanDatabase(newChannelIdList.ToArray(), [BaseItemKind.LiveTvChannel], progress, cancellationToken); |
| | 0 | 153 | | CleanDatabase(newProgramIdList.ToArray(), [BaseItemKind.LiveTvProgram], progress, cancellationToken); |
| | | 154 | | } |
| | | 155 | | |
| | 0 | 156 | | var coreService = _liveTvManager.Services.OfType<DefaultLiveTvService>().FirstOrDefault(); |
| | 0 | 157 | | if (coreService is not null) |
| | | 158 | | { |
| | 0 | 159 | | await coreService.RefreshSeriesTimers(cancellationToken).ConfigureAwait(false); |
| | 0 | 160 | | await coreService.RefreshTimers(cancellationToken).ConfigureAwait(false); |
| | | 161 | | } |
| | | 162 | | |
| | 0 | 163 | | progress.Report(100); |
| | 0 | 164 | | } |
| | | 165 | | |
| | | 166 | | private double GetGuideDays() |
| | | 167 | | { |
| | 0 | 168 | | var config = _config.GetLiveTvConfiguration(); |
| | | 169 | | |
| | 0 | 170 | | return config.GuideDays.HasValue |
| | 0 | 171 | | ? Math.Clamp(config.GuideDays.Value, 1, MaxGuideDays) |
| | 0 | 172 | | : 7; |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | private async Task<Tuple<List<Guid>, List<Guid>>> RefreshChannelsInternal(ILiveTvService service, IProgress<double> |
| | | 176 | | { |
| | 0 | 177 | | progress.Report(10); |
| | | 178 | | |
| | 0 | 179 | | var allChannelsList = (await service.GetChannelsAsync(cancellationToken).ConfigureAwait(false)) |
| | 0 | 180 | | .Select(i => new Tuple<string, ChannelInfo>(service.Name, i)) |
| | 0 | 181 | | .ToList(); |
| | | 182 | | |
| | 0 | 183 | | var list = new List<LiveTvChannel>(); |
| | | 184 | | |
| | 0 | 185 | | var numComplete = 0; |
| | 0 | 186 | | var parentFolder = _liveTvManager.GetInternalLiveTvFolder(cancellationToken); |
| | | 187 | | |
| | 0 | 188 | | foreach (var channelInfo in allChannelsList) |
| | | 189 | | { |
| | 0 | 190 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 191 | | |
| | | 192 | | try |
| | | 193 | | { |
| | 0 | 194 | | var item = await GetChannel(channelInfo.Item2, channelInfo.Item1, parentFolder, cancellationToken).Confi |
| | | 195 | | |
| | 0 | 196 | | list.Add(item); |
| | 0 | 197 | | } |
| | 0 | 198 | | catch (OperationCanceledException) |
| | | 199 | | { |
| | 0 | 200 | | throw; |
| | | 201 | | } |
| | 0 | 202 | | catch (Exception ex) |
| | | 203 | | { |
| | 0 | 204 | | _logger.LogError(ex, "Error getting channel information for {Name}", channelInfo.Item2.Name); |
| | 0 | 205 | | } |
| | | 206 | | |
| | 0 | 207 | | numComplete++; |
| | 0 | 208 | | double percent = numComplete; |
| | 0 | 209 | | percent /= allChannelsList.Count; |
| | | 210 | | |
| | 0 | 211 | | progress.Report((5 * percent) + 10); |
| | 0 | 212 | | } |
| | | 213 | | |
| | 0 | 214 | | progress.Report(15); |
| | | 215 | | |
| | 0 | 216 | | numComplete = 0; |
| | 0 | 217 | | var programIds = new List<Guid>(); |
| | 0 | 218 | | var channels = new List<Guid>(); |
| | | 219 | | |
| | 0 | 220 | | var guideDays = GetGuideDays(); |
| | | 221 | | |
| | 0 | 222 | | _logger.LogInformation("Refreshing guide with {Days} days of guide data", guideDays); |
| | | 223 | | |
| | 0 | 224 | | var maxCacheDate = DateTime.UtcNow.AddDays(MaxCacheDays); |
| | 0 | 225 | | foreach (var currentChannel in list) |
| | | 226 | | { |
| | 0 | 227 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 228 | | channels.Add(currentChannel.Id); |
| | | 229 | | |
| | | 230 | | try |
| | | 231 | | { |
| | 0 | 232 | | var start = DateTime.UtcNow.AddHours(-1); |
| | 0 | 233 | | var end = start.AddDays(guideDays); |
| | | 234 | | |
| | 0 | 235 | | var isMovie = false; |
| | 0 | 236 | | var isSports = false; |
| | 0 | 237 | | var isNews = false; |
| | 0 | 238 | | var isKids = false; |
| | 0 | 239 | | var isSeries = false; |
| | | 240 | | |
| | 0 | 241 | | var channelPrograms = (await service.GetProgramsAsync(currentChannel.ExternalId, start, end, cancellatio |
| | | 242 | | |
| | 0 | 243 | | var existingPrograms = _libraryManager.GetItemList(new InternalItemsQuery |
| | 0 | 244 | | { |
| | 0 | 245 | | IncludeItemTypes = [BaseItemKind.LiveTvProgram], |
| | 0 | 246 | | ChannelIds = [currentChannel.Id], |
| | 0 | 247 | | DtoOptions = new DtoOptions(true) |
| | 0 | 248 | | }).Cast<LiveTvProgram>().ToDictionary(i => i.Id); |
| | | 249 | | |
| | 0 | 250 | | var newPrograms = new List<LiveTvProgram>(); |
| | 0 | 251 | | var updatedPrograms = new List<LiveTvProgram>(); |
| | | 252 | | |
| | 0 | 253 | | foreach (var program in channelPrograms) |
| | | 254 | | { |
| | 0 | 255 | | var (programItem, isNew, isUpdated) = GetProgram(program, existingPrograms, currentChannel); |
| | 0 | 256 | | var id = programItem.Id; |
| | 0 | 257 | | if (isNew) |
| | | 258 | | { |
| | 0 | 259 | | newPrograms.Add(programItem); |
| | | 260 | | } |
| | 0 | 261 | | else if (isUpdated) |
| | | 262 | | { |
| | 0 | 263 | | updatedPrograms.Add(programItem); |
| | | 264 | | } |
| | | 265 | | |
| | 0 | 266 | | programIds.Add(programItem.Id); |
| | | 267 | | |
| | 0 | 268 | | isMovie |= program.IsMovie; |
| | 0 | 269 | | isSeries |= program.IsSeries; |
| | 0 | 270 | | isSports |= program.IsSports; |
| | 0 | 271 | | isNews |= program.IsNews; |
| | 0 | 272 | | isKids |= program.IsKids; |
| | | 273 | | } |
| | | 274 | | |
| | 0 | 275 | | _logger.LogDebug( |
| | 0 | 276 | | "Channel {Name} has {NewCount} new programs and {UpdatedCount} updated programs", |
| | 0 | 277 | | currentChannel.Name, |
| | 0 | 278 | | newPrograms.Count, |
| | 0 | 279 | | updatedPrograms.Count); |
| | | 280 | | |
| | 0 | 281 | | if (newPrograms.Count > 0) |
| | | 282 | | { |
| | 0 | 283 | | _libraryManager.CreateItems(newPrograms, currentChannel, cancellationToken); |
| | | 284 | | |
| | 0 | 285 | | await PreCacheImages(newPrograms, maxCacheDate).ConfigureAwait(false); |
| | | 286 | | } |
| | | 287 | | |
| | 0 | 288 | | if (updatedPrograms.Count > 0) |
| | | 289 | | { |
| | 0 | 290 | | await _libraryManager.UpdateItemsAsync( |
| | 0 | 291 | | updatedPrograms, |
| | 0 | 292 | | currentChannel, |
| | 0 | 293 | | ItemUpdateType.MetadataImport, |
| | 0 | 294 | | cancellationToken).ConfigureAwait(false); |
| | | 295 | | |
| | 0 | 296 | | await PreCacheImages(updatedPrograms, maxCacheDate).ConfigureAwait(false); |
| | | 297 | | } |
| | | 298 | | |
| | 0 | 299 | | currentChannel.IsMovie = isMovie; |
| | 0 | 300 | | currentChannel.IsNews = isNews; |
| | 0 | 301 | | currentChannel.IsSports = isSports; |
| | 0 | 302 | | currentChannel.IsSeries = isSeries; |
| | | 303 | | |
| | 0 | 304 | | if (isKids) |
| | | 305 | | { |
| | 0 | 306 | | currentChannel.AddTag("Kids"); |
| | | 307 | | } |
| | | 308 | | |
| | 0 | 309 | | await currentChannel.UpdateToRepositoryAsync(ItemUpdateType.MetadataImport, cancellationToken).Configure |
| | 0 | 310 | | await currentChannel.RefreshMetadata( |
| | 0 | 311 | | new MetadataRefreshOptions(new DirectoryService(_fileSystem)) |
| | 0 | 312 | | { |
| | 0 | 313 | | ForceSave = true |
| | 0 | 314 | | }, |
| | 0 | 315 | | cancellationToken).ConfigureAwait(false); |
| | 0 | 316 | | } |
| | 0 | 317 | | catch (OperationCanceledException) |
| | | 318 | | { |
| | 0 | 319 | | throw; |
| | | 320 | | } |
| | 0 | 321 | | catch (Exception ex) |
| | | 322 | | { |
| | 0 | 323 | | _logger.LogError(ex, "Error getting programs for channel {Name}", currentChannel.Name); |
| | 0 | 324 | | } |
| | | 325 | | |
| | 0 | 326 | | numComplete++; |
| | 0 | 327 | | double percent = numComplete / (double)allChannelsList.Count; |
| | | 328 | | |
| | 0 | 329 | | progress.Report((85 * percent) + 15); |
| | 0 | 330 | | } |
| | | 331 | | |
| | 0 | 332 | | progress.Report(100); |
| | 0 | 333 | | return new Tuple<List<Guid>, List<Guid>>(channels, programIds); |
| | 0 | 334 | | } |
| | | 335 | | |
| | | 336 | | private void CleanDatabase(Guid[] currentIdList, BaseItemKind[] validTypes, IProgress<double> progress, Cancellation |
| | | 337 | | { |
| | 0 | 338 | | var list = _itemRepo.GetItemIdsList(new InternalItemsQuery |
| | 0 | 339 | | { |
| | 0 | 340 | | IncludeItemTypes = validTypes, |
| | 0 | 341 | | DtoOptions = new DtoOptions(false) |
| | 0 | 342 | | }); |
| | | 343 | | |
| | 0 | 344 | | var numComplete = 0; |
| | | 345 | | |
| | 0 | 346 | | foreach (var itemId in list) |
| | | 347 | | { |
| | 0 | 348 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 349 | | |
| | 0 | 350 | | if (itemId.IsEmpty()) |
| | | 351 | | { |
| | | 352 | | // Somehow some invalid data got into the db. It probably predates the boundary checking |
| | | 353 | | continue; |
| | | 354 | | } |
| | | 355 | | |
| | 0 | 356 | | if (!currentIdList.Contains(itemId)) |
| | | 357 | | { |
| | 0 | 358 | | var item = _libraryManager.GetItemById(itemId); |
| | | 359 | | |
| | 0 | 360 | | if (item is not null) |
| | | 361 | | { |
| | 0 | 362 | | _libraryManager.DeleteItem( |
| | 0 | 363 | | item, |
| | 0 | 364 | | new DeleteOptions |
| | 0 | 365 | | { |
| | 0 | 366 | | DeleteFileLocation = false, |
| | 0 | 367 | | DeleteFromExternalProvider = false |
| | 0 | 368 | | }, |
| | 0 | 369 | | false); |
| | | 370 | | } |
| | | 371 | | } |
| | | 372 | | |
| | 0 | 373 | | numComplete++; |
| | 0 | 374 | | double percent = numComplete / (double)list.Count; |
| | | 375 | | |
| | 0 | 376 | | progress.Report(100 * percent); |
| | | 377 | | } |
| | 0 | 378 | | } |
| | | 379 | | |
| | | 380 | | private async Task<LiveTvChannel> GetChannel( |
| | | 381 | | ChannelInfo channelInfo, |
| | | 382 | | string serviceName, |
| | | 383 | | BaseItem parentFolder, |
| | | 384 | | CancellationToken cancellationToken) |
| | | 385 | | { |
| | 0 | 386 | | var parentFolderId = parentFolder.Id; |
| | 0 | 387 | | var isNew = false; |
| | 0 | 388 | | var forceUpdate = false; |
| | | 389 | | |
| | 0 | 390 | | var id = _tvDtoService.GetInternalChannelId(serviceName, channelInfo.Id); |
| | | 391 | | |
| | 0 | 392 | | if (_libraryManager.GetItemById(id) is not LiveTvChannel item) |
| | | 393 | | { |
| | 0 | 394 | | item = new LiveTvChannel |
| | 0 | 395 | | { |
| | 0 | 396 | | Name = channelInfo.Name, |
| | 0 | 397 | | Id = id, |
| | 0 | 398 | | DateCreated = DateTime.UtcNow |
| | 0 | 399 | | }; |
| | | 400 | | |
| | 0 | 401 | | isNew = true; |
| | | 402 | | } |
| | | 403 | | |
| | 0 | 404 | | if (channelInfo.Tags is not null) |
| | | 405 | | { |
| | 0 | 406 | | if (!channelInfo.Tags.SequenceEqual(item.Tags, StringComparer.OrdinalIgnoreCase)) |
| | | 407 | | { |
| | 0 | 408 | | isNew = true; |
| | | 409 | | } |
| | | 410 | | |
| | 0 | 411 | | item.Tags = channelInfo.Tags; |
| | | 412 | | } |
| | | 413 | | |
| | 0 | 414 | | if (!item.ParentId.Equals(parentFolderId)) |
| | | 415 | | { |
| | 0 | 416 | | isNew = true; |
| | | 417 | | } |
| | | 418 | | |
| | 0 | 419 | | item.ParentId = parentFolderId; |
| | | 420 | | |
| | 0 | 421 | | item.ChannelType = channelInfo.ChannelType; |
| | 0 | 422 | | item.ServiceName = serviceName; |
| | | 423 | | |
| | 0 | 424 | | if (!string.Equals(item.GetProviderId(ExternalServiceTag), serviceName, StringComparison.OrdinalIgnoreCase)) |
| | | 425 | | { |
| | 0 | 426 | | forceUpdate = true; |
| | | 427 | | } |
| | | 428 | | |
| | 0 | 429 | | item.SetProviderId(ExternalServiceTag, serviceName); |
| | | 430 | | |
| | 0 | 431 | | if (!string.Equals(channelInfo.Id, item.ExternalId, StringComparison.Ordinal)) |
| | | 432 | | { |
| | 0 | 433 | | forceUpdate = true; |
| | | 434 | | } |
| | | 435 | | |
| | 0 | 436 | | item.ExternalId = channelInfo.Id; |
| | | 437 | | |
| | 0 | 438 | | if (!string.Equals(channelInfo.Number, item.Number, StringComparison.Ordinal)) |
| | | 439 | | { |
| | 0 | 440 | | forceUpdate = true; |
| | | 441 | | } |
| | | 442 | | |
| | 0 | 443 | | item.Number = channelInfo.Number; |
| | | 444 | | |
| | 0 | 445 | | if (!string.Equals(channelInfo.Name, item.Name, StringComparison.Ordinal)) |
| | | 446 | | { |
| | 0 | 447 | | forceUpdate = true; |
| | | 448 | | } |
| | | 449 | | |
| | 0 | 450 | | item.Name = channelInfo.Name; |
| | | 451 | | |
| | 0 | 452 | | if (LiveTvChannelImageHelper.UpdateChannelImageIfNeeded(item, channelInfo.ImagePath, channelInfo.ImageUrl)) |
| | | 453 | | { |
| | 0 | 454 | | forceUpdate = true; |
| | | 455 | | } |
| | | 456 | | |
| | 0 | 457 | | if (isNew) |
| | | 458 | | { |
| | 0 | 459 | | _libraryManager.CreateItem(item, parentFolder); |
| | | 460 | | } |
| | 0 | 461 | | else if (forceUpdate) |
| | | 462 | | { |
| | 0 | 463 | | await _libraryManager.UpdateItemAsync(item, parentFolder, ItemUpdateType.MetadataImport, cancellationToken). |
| | | 464 | | } |
| | | 465 | | |
| | 0 | 466 | | return item; |
| | 0 | 467 | | } |
| | | 468 | | |
| | | 469 | | private (LiveTvProgram Item, bool IsNew, bool IsUpdated) GetProgram( |
| | | 470 | | ProgramInfo info, |
| | | 471 | | Dictionary<Guid, LiveTvProgram> allExistingPrograms, |
| | | 472 | | LiveTvChannel channel) |
| | | 473 | | { |
| | 0 | 474 | | var id = _tvDtoService.GetInternalProgramId(info.Id); |
| | | 475 | | |
| | 0 | 476 | | var isNew = false; |
| | 0 | 477 | | var forceUpdate = false; |
| | | 478 | | |
| | 0 | 479 | | if (!allExistingPrograms.TryGetValue(id, out var item)) |
| | | 480 | | { |
| | 0 | 481 | | isNew = true; |
| | 0 | 482 | | item = new LiveTvProgram |
| | 0 | 483 | | { |
| | 0 | 484 | | Name = info.Name, |
| | 0 | 485 | | Id = id, |
| | 0 | 486 | | DateCreated = DateTime.UtcNow, |
| | 0 | 487 | | DateModified = DateTime.UtcNow |
| | 0 | 488 | | }; |
| | | 489 | | |
| | 0 | 490 | | item.TrySetProviderId(EtagKey, info.Etag); |
| | | 491 | | } |
| | | 492 | | |
| | 0 | 493 | | if (!string.Equals(info.ShowId, item.ShowId, StringComparison.OrdinalIgnoreCase)) |
| | | 494 | | { |
| | 0 | 495 | | item.ShowId = info.ShowId; |
| | 0 | 496 | | forceUpdate = true; |
| | | 497 | | } |
| | | 498 | | |
| | 0 | 499 | | var channelId = channel.Id; |
| | 0 | 500 | | if (!item.ParentId.Equals(channelId)) |
| | | 501 | | { |
| | 0 | 502 | | item.ParentId = channel.Id; |
| | 0 | 503 | | forceUpdate = true; |
| | | 504 | | } |
| | | 505 | | |
| | 0 | 506 | | item.Audio = info.Audio; |
| | 0 | 507 | | item.ChannelId = channelId; |
| | 0 | 508 | | item.CommunityRating = info.CommunityRating; |
| | 0 | 509 | | item.EpisodeTitle = info.EpisodeTitle; |
| | 0 | 510 | | item.ExternalId = info.Id; |
| | | 511 | | |
| | 0 | 512 | | var seriesId = info.SeriesId; |
| | 0 | 513 | | if (!string.IsNullOrWhiteSpace(seriesId) && !string.Equals(item.ExternalSeriesId, seriesId, StringComparison.Ord |
| | | 514 | | { |
| | 0 | 515 | | item.ExternalSeriesId = seriesId; |
| | 0 | 516 | | forceUpdate = true; |
| | | 517 | | } |
| | | 518 | | |
| | 0 | 519 | | var isSeries = info.IsSeries || !string.IsNullOrEmpty(info.EpisodeTitle); |
| | 0 | 520 | | if (isSeries || !string.IsNullOrEmpty(info.EpisodeTitle)) |
| | | 521 | | { |
| | 0 | 522 | | item.SeriesName = info.Name; |
| | | 523 | | } |
| | | 524 | | |
| | 0 | 525 | | var tags = new List<string>(); |
| | 0 | 526 | | if (info.IsLive) |
| | | 527 | | { |
| | 0 | 528 | | tags.Add("Live"); |
| | | 529 | | } |
| | | 530 | | |
| | 0 | 531 | | if (info.IsPremiere) |
| | | 532 | | { |
| | 0 | 533 | | tags.Add("Premiere"); |
| | | 534 | | } |
| | | 535 | | |
| | 0 | 536 | | if (info.IsNews) |
| | | 537 | | { |
| | 0 | 538 | | tags.Add("News"); |
| | | 539 | | } |
| | | 540 | | |
| | 0 | 541 | | if (info.IsSports) |
| | | 542 | | { |
| | 0 | 543 | | tags.Add("Sports"); |
| | | 544 | | } |
| | | 545 | | |
| | 0 | 546 | | if (info.IsKids) |
| | | 547 | | { |
| | 0 | 548 | | tags.Add("Kids"); |
| | | 549 | | } |
| | | 550 | | |
| | 0 | 551 | | if (info.IsRepeat) |
| | | 552 | | { |
| | 0 | 553 | | tags.Add("Repeat"); |
| | | 554 | | } |
| | | 555 | | |
| | 0 | 556 | | if (info.IsMovie) |
| | | 557 | | { |
| | 0 | 558 | | tags.Add("Movie"); |
| | | 559 | | } |
| | | 560 | | |
| | 0 | 561 | | if (isSeries) |
| | | 562 | | { |
| | 0 | 563 | | tags.Add("Series"); |
| | | 564 | | } |
| | | 565 | | |
| | 0 | 566 | | item.Tags = tags.ToArray(); |
| | 0 | 567 | | item.Genres = info.Genres.ToArray(); |
| | | 568 | | |
| | 0 | 569 | | if (info.IsHD ?? false) |
| | | 570 | | { |
| | 0 | 571 | | item.Width = 1280; |
| | 0 | 572 | | item.Height = 720; |
| | | 573 | | } |
| | | 574 | | |
| | 0 | 575 | | item.IsMovie = info.IsMovie; |
| | 0 | 576 | | item.IsRepeat = info.IsRepeat; |
| | 0 | 577 | | if (item.IsSeries != isSeries) |
| | | 578 | | { |
| | 0 | 579 | | item.IsSeries = isSeries; |
| | 0 | 580 | | forceUpdate = true; |
| | | 581 | | } |
| | | 582 | | |
| | 0 | 583 | | item.Name = info.Name; |
| | 0 | 584 | | item.OfficialRating = info.OfficialRating; |
| | 0 | 585 | | item.Overview = info.Overview; |
| | 0 | 586 | | item.RunTimeTicks = (info.EndDate - info.StartDate).Ticks; |
| | 0 | 587 | | foreach (var providerId in info.SeriesProviderIds) |
| | | 588 | | { |
| | 0 | 589 | | info.ProviderIds["Series" + providerId.Key] = providerId.Value; |
| | | 590 | | } |
| | | 591 | | |
| | 0 | 592 | | item.ProviderIds = info.ProviderIds; |
| | 0 | 593 | | if (item.StartDate != info.StartDate) |
| | | 594 | | { |
| | 0 | 595 | | item.StartDate = info.StartDate; |
| | 0 | 596 | | forceUpdate = true; |
| | | 597 | | } |
| | | 598 | | |
| | 0 | 599 | | if (item.EndDate != info.EndDate) |
| | | 600 | | { |
| | 0 | 601 | | item.EndDate = info.EndDate; |
| | 0 | 602 | | forceUpdate = true; |
| | | 603 | | } |
| | | 604 | | |
| | 0 | 605 | | item.ProductionYear = info.ProductionYear; |
| | 0 | 606 | | if (!isSeries || info.IsRepeat) |
| | | 607 | | { |
| | 0 | 608 | | item.PremiereDate = info.OriginalAirDate; |
| | | 609 | | } |
| | | 610 | | |
| | 0 | 611 | | item.IndexNumber = info.EpisodeNumber; |
| | 0 | 612 | | item.ParentIndexNumber = info.SeasonNumber; |
| | | 613 | | |
| | 0 | 614 | | forceUpdate |= UpdateImages(item, info); |
| | | 615 | | |
| | 0 | 616 | | if (isNew) |
| | | 617 | | { |
| | 0 | 618 | | item.OnMetadataChanged(); |
| | | 619 | | |
| | 0 | 620 | | return (item, true, false); |
| | | 621 | | } |
| | | 622 | | |
| | 0 | 623 | | var isUpdated = forceUpdate; |
| | 0 | 624 | | var etag = info.Etag; |
| | 0 | 625 | | if (string.IsNullOrWhiteSpace(etag)) |
| | | 626 | | { |
| | 0 | 627 | | isUpdated = true; |
| | | 628 | | } |
| | 0 | 629 | | else if (!string.Equals(etag, item.GetProviderId(EtagKey), StringComparison.OrdinalIgnoreCase)) |
| | | 630 | | { |
| | 0 | 631 | | item.SetProviderId(EtagKey, etag); |
| | 0 | 632 | | isUpdated = true; |
| | | 633 | | } |
| | | 634 | | |
| | 0 | 635 | | if (isUpdated) |
| | | 636 | | { |
| | 0 | 637 | | item.OnMetadataChanged(); |
| | | 638 | | |
| | 0 | 639 | | return (item, false, true); |
| | | 640 | | } |
| | | 641 | | |
| | 0 | 642 | | return (item, false, false); |
| | | 643 | | } |
| | | 644 | | |
| | | 645 | | private static bool UpdateImages(BaseItem item, ProgramInfo info) |
| | | 646 | | { |
| | 0 | 647 | | var updated = false; |
| | | 648 | | |
| | | 649 | | // Primary |
| | 0 | 650 | | updated |= UpdateImage(ImageType.Primary, item, info); |
| | | 651 | | |
| | | 652 | | // Thumbnail |
| | 0 | 653 | | updated |= UpdateImage(ImageType.Thumb, item, info); |
| | | 654 | | |
| | | 655 | | // Logo |
| | 0 | 656 | | updated |= UpdateImage(ImageType.Logo, item, info); |
| | | 657 | | |
| | | 658 | | // Backdrop |
| | 0 | 659 | | updated |= UpdateImage(ImageType.Backdrop, item, info); |
| | | 660 | | |
| | 0 | 661 | | return updated; |
| | | 662 | | } |
| | | 663 | | |
| | | 664 | | private static bool UpdateImage(ImageType imageType, BaseItem item, ProgramInfo info) |
| | | 665 | | { |
| | 0 | 666 | | var image = item.GetImages(imageType).FirstOrDefault(); |
| | 0 | 667 | | var currentImagePath = image?.Path; |
| | 0 | 668 | | var newImagePath = imageType switch |
| | 0 | 669 | | { |
| | 0 | 670 | | ImageType.Primary => info.ImagePath, |
| | 0 | 671 | | _ => null |
| | 0 | 672 | | }; |
| | 0 | 673 | | var newImageUrl = imageType switch |
| | 0 | 674 | | { |
| | 0 | 675 | | ImageType.Backdrop => info.BackdropImageUrl, |
| | 0 | 676 | | ImageType.Logo => info.LogoImageUrl, |
| | 0 | 677 | | ImageType.Primary => info.ImageUrl, |
| | 0 | 678 | | ImageType.Thumb => info.ThumbImageUrl, |
| | 0 | 679 | | _ => null |
| | 0 | 680 | | }; |
| | | 681 | | |
| | 0 | 682 | | var sameImage = (currentImagePath?.Equals(newImageUrl, StringComparison.OrdinalIgnoreCase) ?? false) |
| | 0 | 683 | | || (currentImagePath?.Equals(newImagePath, StringComparison.OrdinalIgnoreCase) ?? false) |
| | 0 | 684 | | if (sameImage) |
| | | 685 | | { |
| | 0 | 686 | | return false; |
| | | 687 | | } |
| | | 688 | | |
| | 0 | 689 | | if (!string.IsNullOrWhiteSpace(newImagePath)) |
| | | 690 | | { |
| | 0 | 691 | | item.SetImage( |
| | 0 | 692 | | new ItemImageInfo |
| | 0 | 693 | | { |
| | 0 | 694 | | Path = newImagePath, |
| | 0 | 695 | | Type = imageType |
| | 0 | 696 | | }, |
| | 0 | 697 | | 0); |
| | | 698 | | |
| | 0 | 699 | | return true; |
| | | 700 | | } |
| | | 701 | | |
| | 0 | 702 | | if (!string.IsNullOrWhiteSpace(newImageUrl)) |
| | | 703 | | { |
| | 0 | 704 | | item.SetImage( |
| | 0 | 705 | | new ItemImageInfo |
| | 0 | 706 | | { |
| | 0 | 707 | | Path = newImageUrl, |
| | 0 | 708 | | Type = imageType |
| | 0 | 709 | | }, |
| | 0 | 710 | | 0); |
| | | 711 | | |
| | 0 | 712 | | return true; |
| | | 713 | | } |
| | | 714 | | |
| | 0 | 715 | | item.RemoveImage(image); |
| | | 716 | | |
| | 0 | 717 | | return false; |
| | | 718 | | } |
| | | 719 | | |
| | | 720 | | private async Task PreCacheImages(IReadOnlyList<BaseItem> programs, DateTime maxCacheDate) |
| | | 721 | | { |
| | 0 | 722 | | var sdLimitActive = _schedulesDirectService.IsImageDailyLimitActive(); |
| | | 723 | | |
| | 0 | 724 | | await Parallel.ForEachAsync( |
| | 0 | 725 | | programs |
| | 0 | 726 | | .Where(p => p.EndDate.HasValue && p.EndDate.Value < maxCacheDate) |
| | 0 | 727 | | .Where(p => !sdLimitActive || !p.ImageInfos.All( |
| | 0 | 728 | | img => img.IsLocalFile || img.Path.Contains("schedulesdirect", StringComparison.OrdinalIgnoreCase))) |
| | 0 | 729 | | .DistinctBy(p => p.Id), |
| | 0 | 730 | | _cacheParallelOptions, |
| | 0 | 731 | | async (program, cancellationToken) => |
| | 0 | 732 | | { |
| | 0 | 733 | | // Re-check: limit may have been set by a parallel task since the LINQ filter ran. |
| | 0 | 734 | | if (_schedulesDirectService.IsImageDailyLimitActive() |
| | 0 | 735 | | && program.ImageInfos.All( |
| | 0 | 736 | | img => img.IsLocalFile || img.Path.Contains("schedulesdirect", StringComparison.OrdinalIgnoreCas |
| | 0 | 737 | | { |
| | 0 | 738 | | return; |
| | 0 | 739 | | } |
| | 0 | 740 | | |
| | 0 | 741 | | for (var i = 0; i < program.ImageInfos.Length; i++) |
| | 0 | 742 | | { |
| | 0 | 743 | | if (cancellationToken.IsCancellationRequested) |
| | 0 | 744 | | { |
| | 0 | 745 | | return; |
| | 0 | 746 | | } |
| | 0 | 747 | | |
| | 0 | 748 | | var imageInfo = program.ImageInfos[i]; |
| | 0 | 749 | | if (imageInfo.IsLocalFile) |
| | 0 | 750 | | { |
| | 0 | 751 | | continue; |
| | 0 | 752 | | } |
| | 0 | 753 | | |
| | 0 | 754 | | // Skip SD downloads once the daily limit has been hit. |
| | 0 | 755 | | if (imageInfo.Path.Contains("schedulesdirect", StringComparison.OrdinalIgnoreCase) |
| | 0 | 756 | | && _schedulesDirectService.IsImageDailyLimitActive()) |
| | 0 | 757 | | { |
| | 0 | 758 | | continue; |
| | 0 | 759 | | } |
| | 0 | 760 | | |
| | 0 | 761 | | _logger.LogDebug("Caching image locally: {Url}", imageInfo.Path); |
| | 0 | 762 | | try |
| | 0 | 763 | | { |
| | 0 | 764 | | program.ImageInfos[i] = await _libraryManager.ConvertImageToLocal( |
| | 0 | 765 | | program, |
| | 0 | 766 | | imageInfo, |
| | 0 | 767 | | imageIndex: 0, |
| | 0 | 768 | | removeOnFailure: false) |
| | 0 | 769 | | .ConfigureAwait(false); |
| | 0 | 770 | | } |
| | 0 | 771 | | catch (Exception ex) |
| | 0 | 772 | | { |
| | 0 | 773 | | _logger.LogWarning(ex, "Unable to pre-cache {Url}", imageInfo.Path); |
| | 0 | 774 | | } |
| | 0 | 775 | | } |
| | 0 | 776 | | }).ConfigureAwait(false); |
| | 0 | 777 | | } |
| | | 778 | | } |