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