| | | 1 | | using System; |
| | | 2 | | using System.Collections.Concurrent; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Globalization; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Linq; |
| | | 7 | | using System.Threading; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using Jellyfin.LiveTv.Configuration; |
| | | 10 | | using Jellyfin.LiveTv.Guide; |
| | | 11 | | using MediaBrowser.Common.Configuration; |
| | | 12 | | using MediaBrowser.Common.Extensions; |
| | | 13 | | using MediaBrowser.Controller.LiveTv; |
| | | 14 | | using MediaBrowser.Model.Dto; |
| | | 15 | | using MediaBrowser.Model.LiveTv; |
| | | 16 | | using MediaBrowser.Model.Tasks; |
| | | 17 | | using Microsoft.Extensions.Logging; |
| | | 18 | | |
| | | 19 | | namespace Jellyfin.LiveTv.Listings; |
| | | 20 | | |
| | | 21 | | /// <inheritdoc /> |
| | | 22 | | public class ListingsManager : IListingsManager |
| | | 23 | | { |
| | | 24 | | private readonly ILogger<ListingsManager> _logger; |
| | | 25 | | private readonly IConfigurationManager _config; |
| | | 26 | | private readonly ITaskManager _taskManager; |
| | | 27 | | private readonly ITunerHostManager _tunerHostManager; |
| | | 28 | | private readonly IListingsProvider[] _listingsProviders; |
| | | 29 | | |
| | 22 | 30 | | private readonly ConcurrentDictionary<string, EpgChannelData> _epgChannels = new(StringComparer.OrdinalIgnoreCase); |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Initializes a new instance of the <see cref="ListingsManager"/> class. |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="logger">The <see cref="ILogger{TCategoryName}"/>.</param> |
| | | 36 | | /// <param name="config">The <see cref="IConfigurationManager"/>.</param> |
| | | 37 | | /// <param name="taskManager">The <see cref="ITaskManager"/>.</param> |
| | | 38 | | /// <param name="tunerHostManager">The <see cref="ITunerHostManager"/>.</param> |
| | | 39 | | /// <param name="listingsProviders">The <see cref="IListingsProvider"/>.</param> |
| | | 40 | | public ListingsManager( |
| | | 41 | | ILogger<ListingsManager> logger, |
| | | 42 | | IConfigurationManager config, |
| | | 43 | | ITaskManager taskManager, |
| | | 44 | | ITunerHostManager tunerHostManager, |
| | | 45 | | IEnumerable<IListingsProvider> listingsProviders) |
| | | 46 | | { |
| | 22 | 47 | | _logger = logger; |
| | 22 | 48 | | _config = config; |
| | 22 | 49 | | _taskManager = taskManager; |
| | 22 | 50 | | _tunerHostManager = tunerHostManager; |
| | 22 | 51 | | _listingsProviders = listingsProviders.ToArray(); |
| | 22 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <inheritdoc /> |
| | | 55 | | public async Task<ListingsProviderInfo> SaveListingProvider(ListingsProviderInfo info, bool validateLogin, bool vali |
| | | 56 | | { |
| | 0 | 57 | | ArgumentNullException.ThrowIfNull(info); |
| | | 58 | | |
| | 0 | 59 | | var provider = GetProvider(info.Type); |
| | 0 | 60 | | await provider.Validate(info, validateLogin, validateListings).ConfigureAwait(false); |
| | | 61 | | |
| | 0 | 62 | | var config = _config.GetLiveTvConfiguration(); |
| | | 63 | | |
| | 0 | 64 | | var list = config.ListingProviders; |
| | 0 | 65 | | int index = Array.FindIndex(list, i => string.Equals(i.Id, info.Id, StringComparison.OrdinalIgnoreCase)); |
| | | 66 | | |
| | 0 | 67 | | if (index == -1 || string.IsNullOrWhiteSpace(info.Id)) |
| | | 68 | | { |
| | 0 | 69 | | info.Id = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture); |
| | 0 | 70 | | config.ListingProviders = [..list, info]; |
| | | 71 | | } |
| | | 72 | | else |
| | | 73 | | { |
| | 0 | 74 | | config.ListingProviders[index] = info; |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | _config.SaveConfiguration("livetv", config); |
| | | 78 | | |
| | 0 | 79 | | InvalidateListingsProviderCache(info.Id); |
| | | 80 | | |
| | 0 | 81 | | _taskManager.CancelIfRunningAndQueue<RefreshGuideScheduledTask>(); |
| | | 82 | | |
| | 0 | 83 | | return info; |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <inheritdoc /> |
| | | 87 | | public void DeleteListingsProvider(string? id) |
| | | 88 | | { |
| | 1 | 89 | | var config = _config.GetLiveTvConfiguration(); |
| | | 90 | | |
| | 1 | 91 | | config.ListingProviders = config.ListingProviders.Where(i => !string.Equals(id, i.Id, StringComparison.OrdinalIg |
| | | 92 | | |
| | 1 | 93 | | _config.SaveConfiguration("livetv", config); |
| | | 94 | | |
| | 1 | 95 | | if (!string.IsNullOrEmpty(id)) |
| | | 96 | | { |
| | 1 | 97 | | InvalidateListingsProviderCache(id); |
| | | 98 | | } |
| | | 99 | | |
| | 1 | 100 | | _taskManager.CancelIfRunningAndQueue<RefreshGuideScheduledTask>(); |
| | 1 | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <inheritdoc /> |
| | | 104 | | public Task<List<NameIdPair>> GetLineups(string? providerType, string? providerId, string? country, string? location |
| | | 105 | | { |
| | 0 | 106 | | if (string.IsNullOrWhiteSpace(providerId)) |
| | | 107 | | { |
| | 0 | 108 | | return GetProvider(providerType).GetLineups(null, country, location); |
| | | 109 | | } |
| | | 110 | | |
| | 0 | 111 | | var info = _config.GetLiveTvConfiguration().ListingProviders |
| | 0 | 112 | | .FirstOrDefault(i => string.Equals(i.Id, providerId, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 113 | | ?? throw new ResourceNotFoundException(); |
| | | 114 | | |
| | 0 | 115 | | return GetProvider(info.Type).GetLineups(info, country, location); |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | /// <inheritdoc /> |
| | | 119 | | public async Task<IEnumerable<ProgramInfo>> GetProgramsAsync( |
| | | 120 | | ChannelInfo channel, |
| | | 121 | | DateTime startDateUtc, |
| | | 122 | | DateTime endDateUtc, |
| | | 123 | | CancellationToken cancellationToken) |
| | | 124 | | { |
| | 0 | 125 | | ArgumentNullException.ThrowIfNull(channel); |
| | | 126 | | |
| | 0 | 127 | | foreach (var (provider, providerInfo) in GetListingProviders()) |
| | | 128 | | { |
| | 0 | 129 | | if (!IsListingProviderEnabledForTuner(providerInfo, channel.TunerHostId)) |
| | | 130 | | { |
| | 0 | 131 | | _logger.LogDebug( |
| | 0 | 132 | | "Skipping getting programs for channel {0}-{1} from {2}-{3}, because it's not enabled for this tuner |
| | 0 | 133 | | channel.Number, |
| | 0 | 134 | | channel.Name, |
| | 0 | 135 | | provider.Name, |
| | 0 | 136 | | providerInfo.ListingsId ?? string.Empty); |
| | 0 | 137 | | continue; |
| | | 138 | | } |
| | | 139 | | |
| | 0 | 140 | | _logger.LogDebug( |
| | 0 | 141 | | "Getting programs for channel {0}-{1} from {2}-{3}", |
| | 0 | 142 | | channel.Number, |
| | 0 | 143 | | channel.Name, |
| | 0 | 144 | | provider.Name, |
| | 0 | 145 | | providerInfo.ListingsId ?? string.Empty); |
| | | 146 | | |
| | 0 | 147 | | var epgChannels = await GetEpgChannels(provider, providerInfo, true, cancellationToken).ConfigureAwait(false |
| | | 148 | | |
| | 0 | 149 | | var epgChannel = GetEpgChannelFromTunerChannel(providerInfo.ChannelMappings, channel, epgChannels); |
| | 0 | 150 | | if (epgChannel is null) |
| | | 151 | | { |
| | 0 | 152 | | _logger.LogDebug("EPG channel not found for tuner channel {0}-{1} from {2}-{3}", channel.Number, channel |
| | 0 | 153 | | continue; |
| | | 154 | | } |
| | | 155 | | |
| | 0 | 156 | | var programs = (await provider |
| | 0 | 157 | | .GetProgramsAsync(providerInfo, epgChannel.Id, startDateUtc, endDateUtc, cancellationToken).ConfigureAwa |
| | 0 | 158 | | .ToList(); |
| | | 159 | | |
| | | 160 | | // Replace the value that came from the provider with a normalized value |
| | 0 | 161 | | foreach (var program in programs) |
| | | 162 | | { |
| | 0 | 163 | | program.ChannelId = channel.Id; |
| | 0 | 164 | | program.Id += "_" + channel.Id; |
| | | 165 | | } |
| | | 166 | | |
| | 0 | 167 | | if (programs.Count > 0) |
| | | 168 | | { |
| | 0 | 169 | | return programs; |
| | | 170 | | } |
| | 0 | 171 | | } |
| | | 172 | | |
| | 0 | 173 | | return Enumerable.Empty<ProgramInfo>(); |
| | 0 | 174 | | } |
| | | 175 | | |
| | | 176 | | /// <inheritdoc /> |
| | | 177 | | public async Task AddProviderMetadata(IList<ChannelInfo> channels, bool enableCache, CancellationToken cancellationT |
| | | 178 | | { |
| | 0 | 179 | | ArgumentNullException.ThrowIfNull(channels); |
| | | 180 | | |
| | 0 | 181 | | foreach (var (provider, providerInfo) in GetListingProviders()) |
| | | 182 | | { |
| | 0 | 183 | | var enabledChannels = channels |
| | 0 | 184 | | .Where(i => IsListingProviderEnabledForTuner(providerInfo, i.TunerHostId)) |
| | 0 | 185 | | .ToList(); |
| | | 186 | | |
| | 0 | 187 | | if (enabledChannels.Count == 0) |
| | | 188 | | { |
| | | 189 | | continue; |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | try |
| | | 193 | | { |
| | 0 | 194 | | await AddMetadata(provider, providerInfo, enabledChannels, enableCache, cancellationToken).ConfigureAwai |
| | 0 | 195 | | } |
| | 0 | 196 | | catch (NotSupportedException) |
| | | 197 | | { |
| | 0 | 198 | | } |
| | 0 | 199 | | catch (Exception ex) |
| | | 200 | | { |
| | 0 | 201 | | _logger.LogError(ex, "Error adding metadata"); |
| | 0 | 202 | | } |
| | | 203 | | } |
| | 0 | 204 | | } |
| | | 205 | | |
| | | 206 | | /// <inheritdoc /> |
| | | 207 | | public async Task<ChannelMappingOptionsDto> GetChannelMappingOptions(string? providerId) |
| | | 208 | | { |
| | 0 | 209 | | var listingsProviderInfo = _config.GetLiveTvConfiguration().ListingProviders |
| | 0 | 210 | | .First(info => string.Equals(providerId, info.Id, StringComparison.OrdinalIgnoreCase)); |
| | | 211 | | |
| | 0 | 212 | | var provider = GetProvider(listingsProviderInfo.Type); |
| | | 213 | | |
| | 0 | 214 | | var tunerChannels = await GetChannelsForListingsProvider(listingsProviderInfo, CancellationToken.None) |
| | 0 | 215 | | .ConfigureAwait(false); |
| | | 216 | | |
| | 0 | 217 | | var providerChannels = await provider.GetChannels(listingsProviderInfo, default) |
| | 0 | 218 | | .ConfigureAwait(false); |
| | | 219 | | |
| | 0 | 220 | | var mappings = listingsProviderInfo.ChannelMappings; |
| | | 221 | | |
| | 0 | 222 | | return new ChannelMappingOptionsDto |
| | 0 | 223 | | { |
| | 0 | 224 | | TunerChannels = tunerChannels.Select(i => GetTunerChannelMapping(i, mappings, providerChannels)).ToList(), |
| | 0 | 225 | | ProviderChannels = providerChannels.Select(i => new NameIdPair |
| | 0 | 226 | | { |
| | 0 | 227 | | Name = i.Name, |
| | 0 | 228 | | Id = i.Id |
| | 0 | 229 | | }).ToList(), |
| | 0 | 230 | | Mappings = mappings, |
| | 0 | 231 | | ProviderName = provider.Name |
| | 0 | 232 | | }; |
| | 0 | 233 | | } |
| | | 234 | | |
| | | 235 | | /// <inheritdoc /> |
| | | 236 | | public async Task<TunerChannelMapping> SetChannelMapping(string providerId, string tunerChannelNumber, string provid |
| | | 237 | | { |
| | 0 | 238 | | var config = _config.GetLiveTvConfiguration(); |
| | | 239 | | |
| | 0 | 240 | | var listingsProviderInfo = config.ListingProviders |
| | 0 | 241 | | .First(info => string.Equals(providerId, info.Id, StringComparison.OrdinalIgnoreCase)); |
| | | 242 | | |
| | 0 | 243 | | var channelMappingExists = listingsProviderInfo.ChannelMappings |
| | 0 | 244 | | .Any(pair => string.Equals(pair.Name, tunerChannelNumber, StringComparison.OrdinalIgnoreCase) |
| | 0 | 245 | | && string.Equals(pair.Value, providerChannelNumber, StringComparison.OrdinalIgnoreCase)); |
| | | 246 | | |
| | 0 | 247 | | listingsProviderInfo.ChannelMappings = listingsProviderInfo.ChannelMappings |
| | 0 | 248 | | .Where(pair => !string.Equals(pair.Name, tunerChannelNumber, StringComparison.OrdinalIgnoreCase)).ToArray(); |
| | | 249 | | |
| | 0 | 250 | | if (!string.Equals(tunerChannelNumber, providerChannelNumber, StringComparison.OrdinalIgnoreCase) |
| | 0 | 251 | | && !channelMappingExists) |
| | | 252 | | { |
| | 0 | 253 | | var newItem = new NameValuePair |
| | 0 | 254 | | { |
| | 0 | 255 | | Name = tunerChannelNumber, |
| | 0 | 256 | | Value = providerChannelNumber |
| | 0 | 257 | | }; |
| | 0 | 258 | | listingsProviderInfo.ChannelMappings = [..listingsProviderInfo.ChannelMappings, newItem]; |
| | | 259 | | } |
| | | 260 | | |
| | 0 | 261 | | _config.SaveConfiguration("livetv", config); |
| | | 262 | | |
| | 0 | 263 | | var tunerChannels = await GetChannelsForListingsProvider(listingsProviderInfo, CancellationToken.None) |
| | 0 | 264 | | .ConfigureAwait(false); |
| | | 265 | | |
| | 0 | 266 | | var providerChannels = await GetProvider(listingsProviderInfo.Type).GetChannels(listingsProviderInfo, default) |
| | 0 | 267 | | .ConfigureAwait(false); |
| | | 268 | | |
| | 0 | 269 | | var tunerChannelMappings = tunerChannels |
| | 0 | 270 | | .Select(i => GetTunerChannelMapping(i, listingsProviderInfo.ChannelMappings, providerChannels)).ToList(); |
| | | 271 | | |
| | 0 | 272 | | _taskManager.CancelIfRunningAndQueue<RefreshGuideScheduledTask>(); |
| | | 273 | | |
| | 0 | 274 | | return tunerChannelMappings.First(i => string.Equals(i.Id, tunerChannelNumber, StringComparison.OrdinalIgnoreCas |
| | 0 | 275 | | } |
| | | 276 | | |
| | | 277 | | private List<(IListingsProvider Provider, ListingsProviderInfo ProviderInfo)> GetListingProviders() |
| | 0 | 278 | | => _config.GetLiveTvConfiguration().ListingProviders |
| | 0 | 279 | | .Select(info => ( |
| | 0 | 280 | | Provider: _listingsProviders.FirstOrDefault(l |
| | 0 | 281 | | => string.Equals(l.Type, info.Type, StringComparison.OrdinalIgnoreCase)), |
| | 0 | 282 | | ProviderInfo: info)) |
| | 0 | 283 | | .Where(i => i.Provider is not null) |
| | 0 | 284 | | .ToList()!; // Already filtered out null |
| | | 285 | | |
| | | 286 | | private async Task AddMetadata( |
| | | 287 | | IListingsProvider provider, |
| | | 288 | | ListingsProviderInfo info, |
| | | 289 | | IEnumerable<ChannelInfo> tunerChannels, |
| | | 290 | | bool enableCache, |
| | | 291 | | CancellationToken cancellationToken) |
| | | 292 | | { |
| | 0 | 293 | | var epgChannels = await GetEpgChannels(provider, info, enableCache, cancellationToken).ConfigureAwait(false); |
| | | 294 | | |
| | 0 | 295 | | foreach (var tunerChannel in tunerChannels) |
| | | 296 | | { |
| | 0 | 297 | | var epgChannel = GetEpgChannelFromTunerChannel(info.ChannelMappings, tunerChannel, epgChannels); |
| | 0 | 298 | | if (epgChannel is null) |
| | | 299 | | { |
| | | 300 | | continue; |
| | | 301 | | } |
| | | 302 | | |
| | 0 | 303 | | if (!string.IsNullOrWhiteSpace(epgChannel.ImageUrl)) |
| | | 304 | | { |
| | 0 | 305 | | tunerChannel.ImageUrl = epgChannel.ImageUrl; |
| | | 306 | | } |
| | | 307 | | } |
| | 0 | 308 | | } |
| | | 309 | | |
| | | 310 | | private static bool IsListingProviderEnabledForTuner(ListingsProviderInfo info, string tunerHostId) |
| | | 311 | | { |
| | 0 | 312 | | if (info.EnableAllTuners) |
| | | 313 | | { |
| | 0 | 314 | | return true; |
| | | 315 | | } |
| | | 316 | | |
| | 0 | 317 | | ArgumentException.ThrowIfNullOrWhiteSpace(tunerHostId); |
| | | 318 | | |
| | 0 | 319 | | return info.EnabledTuners.Contains(tunerHostId, StringComparer.OrdinalIgnoreCase); |
| | | 320 | | } |
| | | 321 | | |
| | | 322 | | private static string GetMappedChannel(string channelId, NameValuePair[] mappings) |
| | | 323 | | { |
| | 0 | 324 | | foreach (NameValuePair mapping in mappings) |
| | | 325 | | { |
| | 0 | 326 | | if (string.Equals(mapping.Name, channelId, StringComparison.OrdinalIgnoreCase)) |
| | | 327 | | { |
| | 0 | 328 | | return mapping.Value; |
| | | 329 | | } |
| | | 330 | | } |
| | | 331 | | |
| | 0 | 332 | | return channelId; |
| | | 333 | | } |
| | | 334 | | |
| | | 335 | | private void InvalidateListingsProviderCache(string providerId) |
| | | 336 | | { |
| | | 337 | | // Clear in-memory EPG channel cache for this provider |
| | 1 | 338 | | _epgChannels.TryRemove(providerId, out _); |
| | | 339 | | |
| | | 340 | | // Provider IDs are generated as Guid.NewGuid().ToString("N") |
| | | 341 | | // reject anything else so we never use untrusted input in a path or log entry. |
| | 1 | 342 | | if (!Guid.TryParseExact(providerId, "N", out var providerGuid)) |
| | | 343 | | { |
| | 1 | 344 | | return; |
| | | 345 | | } |
| | | 346 | | |
| | | 347 | | // Delete the cached XMLTV file so a fresh copy is downloaded |
| | 0 | 348 | | var cachePath = _config.CommonApplicationPaths?.CachePath; |
| | 0 | 349 | | if (!string.IsNullOrEmpty(cachePath)) |
| | | 350 | | { |
| | 0 | 351 | | var safeId = providerGuid.ToString("N", CultureInfo.InvariantCulture); |
| | 0 | 352 | | var xmltvCacheFile = Path.Combine(cachePath, "xmltv", safeId + ".xml"); |
| | | 353 | | try |
| | | 354 | | { |
| | 0 | 355 | | File.Delete(xmltvCacheFile); |
| | 0 | 356 | | } |
| | 0 | 357 | | catch (IOException ex) |
| | | 358 | | { |
| | 0 | 359 | | _logger.LogWarning(ex, "Error deleting XMLTV cache file for provider {ProviderId}", safeId); |
| | 0 | 360 | | } |
| | | 361 | | } |
| | 0 | 362 | | } |
| | | 363 | | |
| | | 364 | | private async Task<EpgChannelData> GetEpgChannels( |
| | | 365 | | IListingsProvider provider, |
| | | 366 | | ListingsProviderInfo info, |
| | | 367 | | bool enableCache, |
| | | 368 | | CancellationToken cancellationToken) |
| | | 369 | | { |
| | 0 | 370 | | if (enableCache && _epgChannels.TryGetValue(info.Id, out var result)) |
| | | 371 | | { |
| | 0 | 372 | | return result; |
| | | 373 | | } |
| | | 374 | | |
| | 0 | 375 | | var channels = await provider.GetChannels(info, cancellationToken).ConfigureAwait(false); |
| | 0 | 376 | | foreach (var channel in channels) |
| | | 377 | | { |
| | 0 | 378 | | _logger.LogInformation("Found epg channel in {0} {1} {2} {3}", provider.Name, info.ListingsId, channel.Name, |
| | | 379 | | } |
| | | 380 | | |
| | 0 | 381 | | result = new EpgChannelData(channels); |
| | 0 | 382 | | _epgChannels.AddOrUpdate(info.Id, result, (_, _) => result); |
| | | 383 | | |
| | 0 | 384 | | return result; |
| | 0 | 385 | | } |
| | | 386 | | |
| | | 387 | | private static ChannelInfo? GetEpgChannelFromTunerChannel( |
| | | 388 | | NameValuePair[] mappings, |
| | | 389 | | ChannelInfo tunerChannel, |
| | | 390 | | EpgChannelData epgChannelData) |
| | | 391 | | { |
| | 0 | 392 | | if (!string.IsNullOrWhiteSpace(tunerChannel.Id)) |
| | | 393 | | { |
| | 0 | 394 | | var mappedTunerChannelId = GetMappedChannel(tunerChannel.Id, mappings); |
| | 0 | 395 | | if (string.IsNullOrWhiteSpace(mappedTunerChannelId)) |
| | | 396 | | { |
| | 0 | 397 | | mappedTunerChannelId = tunerChannel.Id; |
| | | 398 | | } |
| | | 399 | | |
| | 0 | 400 | | var channel = epgChannelData.GetChannelById(mappedTunerChannelId); |
| | 0 | 401 | | if (channel is not null) |
| | | 402 | | { |
| | 0 | 403 | | return channel; |
| | | 404 | | } |
| | | 405 | | } |
| | | 406 | | |
| | 0 | 407 | | if (!string.IsNullOrWhiteSpace(tunerChannel.TunerChannelId)) |
| | | 408 | | { |
| | 0 | 409 | | var tunerChannelId = tunerChannel.TunerChannelId; |
| | 0 | 410 | | if (tunerChannelId.Contains(".json.schedulesdirect.org", StringComparison.OrdinalIgnoreCase)) |
| | | 411 | | { |
| | 0 | 412 | | tunerChannelId = tunerChannelId.Replace(".json.schedulesdirect.org", string.Empty, StringComparison.Ordi |
| | | 413 | | } |
| | | 414 | | |
| | 0 | 415 | | var mappedTunerChannelId = GetMappedChannel(tunerChannelId, mappings); |
| | 0 | 416 | | if (string.IsNullOrWhiteSpace(mappedTunerChannelId)) |
| | | 417 | | { |
| | 0 | 418 | | mappedTunerChannelId = tunerChannelId; |
| | | 419 | | } |
| | | 420 | | |
| | 0 | 421 | | var channel = epgChannelData.GetChannelById(mappedTunerChannelId); |
| | 0 | 422 | | if (channel is not null) |
| | | 423 | | { |
| | 0 | 424 | | return channel; |
| | | 425 | | } |
| | | 426 | | } |
| | | 427 | | |
| | 0 | 428 | | if (!string.IsNullOrWhiteSpace(tunerChannel.Number)) |
| | | 429 | | { |
| | 0 | 430 | | var tunerChannelNumber = GetMappedChannel(tunerChannel.Number, mappings); |
| | 0 | 431 | | if (string.IsNullOrWhiteSpace(tunerChannelNumber)) |
| | | 432 | | { |
| | 0 | 433 | | tunerChannelNumber = tunerChannel.Number; |
| | | 434 | | } |
| | | 435 | | |
| | 0 | 436 | | var channel = epgChannelData.GetChannelByNumber(tunerChannelNumber); |
| | 0 | 437 | | if (channel is not null) |
| | | 438 | | { |
| | 0 | 439 | | return channel; |
| | | 440 | | } |
| | | 441 | | } |
| | | 442 | | |
| | 0 | 443 | | if (!string.IsNullOrWhiteSpace(tunerChannel.Name)) |
| | | 444 | | { |
| | 0 | 445 | | var normalizedName = EpgChannelData.NormalizeName(tunerChannel.Name); |
| | | 446 | | |
| | 0 | 447 | | var channel = epgChannelData.GetChannelByName(normalizedName); |
| | 0 | 448 | | if (channel is not null) |
| | | 449 | | { |
| | 0 | 450 | | return channel; |
| | | 451 | | } |
| | | 452 | | } |
| | | 453 | | |
| | 0 | 454 | | return null; |
| | | 455 | | } |
| | | 456 | | |
| | | 457 | | private static TunerChannelMapping GetTunerChannelMapping(ChannelInfo tunerChannel, NameValuePair[] mappings, IList< |
| | | 458 | | { |
| | 0 | 459 | | var result = new TunerChannelMapping |
| | 0 | 460 | | { |
| | 0 | 461 | | Name = tunerChannel.Name, |
| | 0 | 462 | | Id = tunerChannel.Id |
| | 0 | 463 | | }; |
| | | 464 | | |
| | 0 | 465 | | if (!string.IsNullOrWhiteSpace(tunerChannel.Number)) |
| | | 466 | | { |
| | 0 | 467 | | result.Name = tunerChannel.Number + " " + result.Name; |
| | | 468 | | } |
| | | 469 | | |
| | 0 | 470 | | var providerChannel = GetEpgChannelFromTunerChannel(mappings, tunerChannel, new EpgChannelData(providerChannels) |
| | 0 | 471 | | if (providerChannel is not null) |
| | | 472 | | { |
| | 0 | 473 | | result.ProviderChannelName = providerChannel.Name; |
| | 0 | 474 | | result.ProviderChannelId = providerChannel.Id; |
| | | 475 | | } |
| | | 476 | | |
| | 0 | 477 | | return result; |
| | | 478 | | } |
| | | 479 | | |
| | | 480 | | private async Task<List<ChannelInfo>> GetChannelsForListingsProvider(ListingsProviderInfo info, CancellationToken ca |
| | | 481 | | { |
| | 0 | 482 | | var channels = new List<ChannelInfo>(); |
| | 0 | 483 | | foreach (var hostInstance in _tunerHostManager.TunerHosts) |
| | | 484 | | { |
| | | 485 | | try |
| | | 486 | | { |
| | 0 | 487 | | var tunerChannels = await hostInstance.GetChannels(false, cancellationToken).ConfigureAwait(false); |
| | | 488 | | |
| | 0 | 489 | | channels.AddRange(tunerChannels.Where(channel => IsListingProviderEnabledForTuner(info, channel.TunerHos |
| | 0 | 490 | | } |
| | 0 | 491 | | catch (Exception ex) |
| | | 492 | | { |
| | 0 | 493 | | _logger.LogError(ex, "Error getting channels"); |
| | 0 | 494 | | } |
| | | 495 | | } |
| | | 496 | | |
| | 0 | 497 | | return channels; |
| | 0 | 498 | | } |
| | | 499 | | |
| | | 500 | | private IListingsProvider GetProvider(string? providerType) |
| | 0 | 501 | | => _listingsProviders.FirstOrDefault(i => string.Equals(providerType, i.Type, StringComparison.OrdinalIgnoreCase |
| | 0 | 502 | | ?? throw new ResourceNotFoundException($"Couldn't find provider of type {providerType}"); |
| | | 503 | | } |