mirror of
				https://github.com/jellyfin/jellyfin.git
				synced 2025-11-01 19:17:23 -04:00 
			
		
		
		
	Clean up TunerHost classes
This commit is contained in:
		
							parent
							
								
									43fa983414
								
							
						
					
					
						commit
						5c65abcd94
					
				| @ -1,10 +1,10 @@ | ||||
| #pragma warning disable CS1591 | ||||
| 
 | ||||
| using System; | ||||
| using System.Collections.Concurrent; | ||||
| using System.Collections.Generic; | ||||
| using System.IO; | ||||
| using System.Linq; | ||||
| using System.Text.Json; | ||||
| using System.Threading; | ||||
| using System.Threading.Tasks; | ||||
| using MediaBrowser.Common.Configuration; | ||||
| @ -14,7 +14,7 @@ using MediaBrowser.Controller.LiveTv; | ||||
| using MediaBrowser.Model.Dto; | ||||
| using MediaBrowser.Model.IO; | ||||
| using MediaBrowser.Model.LiveTv; | ||||
| using MediaBrowser.Model.Serialization; | ||||
| using Microsoft.Extensions.Caching.Memory; | ||||
| using Microsoft.Extensions.Logging; | ||||
| 
 | ||||
| namespace Emby.Server.Implementations.LiveTv.TunerHosts | ||||
| @ -23,17 +23,15 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts | ||||
|     { | ||||
|         protected readonly IServerConfigurationManager Config; | ||||
|         protected readonly ILogger<BaseTunerHost> Logger; | ||||
|         protected IJsonSerializer JsonSerializer; | ||||
|         protected readonly IFileSystem FileSystem; | ||||
| 
 | ||||
|         private readonly ConcurrentDictionary<string, ChannelCache> _channelCache = | ||||
|             new ConcurrentDictionary<string, ChannelCache>(StringComparer.OrdinalIgnoreCase); | ||||
|         private readonly IMemoryCache _memoryCache; | ||||
| 
 | ||||
|         protected BaseTunerHost(IServerConfigurationManager config, ILogger<BaseTunerHost> logger, IJsonSerializer jsonSerializer, IFileSystem fileSystem) | ||||
|         protected BaseTunerHost(IServerConfigurationManager config, ILogger<BaseTunerHost> logger, IFileSystem fileSystem, IMemoryCache memoryCache) | ||||
|         { | ||||
|             Config = config; | ||||
|             Logger = logger; | ||||
|             JsonSerializer = jsonSerializer; | ||||
|             _memoryCache = memoryCache; | ||||
|             FileSystem = fileSystem; | ||||
|         } | ||||
| 
 | ||||
| @ -44,23 +42,19 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts | ||||
| 
 | ||||
|         public async Task<List<ChannelInfo>> GetChannels(TunerHostInfo tuner, bool enableCache, CancellationToken cancellationToken) | ||||
|         { | ||||
|             ChannelCache cache = null; | ||||
|             var key = tuner.Id; | ||||
| 
 | ||||
|             if (enableCache && !string.IsNullOrEmpty(key) && _channelCache.TryGetValue(key, out cache)) | ||||
|             if (enableCache && !string.IsNullOrEmpty(key) && _memoryCache.TryGetValue(key, out List<ChannelInfo> cache)) | ||||
|             { | ||||
|                 return cache.Channels.ToList(); | ||||
|                 return cache; | ||||
|             } | ||||
| 
 | ||||
|             var result = await GetChannelsInternal(tuner, cancellationToken).ConfigureAwait(false); | ||||
|             var list = result.ToList(); | ||||
|             var list = await GetChannelsInternal(tuner, cancellationToken).ConfigureAwait(false); | ||||
|             // logger.LogInformation("Channels from {0}: {1}", tuner.Url, JsonSerializer.SerializeToString(list)); | ||||
| 
 | ||||
|             if (!string.IsNullOrEmpty(key) && list.Count > 0) | ||||
|             { | ||||
|                 cache = cache ?? new ChannelCache(); | ||||
|                 cache.Channels = list; | ||||
|                 _channelCache.AddOrUpdate(key, cache, (k, v) => cache); | ||||
|                 _memoryCache.CreateEntry(key).SetValue(list); | ||||
|             } | ||||
| 
 | ||||
|             return list; | ||||
| @ -95,7 +89,8 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts | ||||
|                         try | ||||
|                         { | ||||
|                             Directory.CreateDirectory(Path.GetDirectoryName(channelCacheFile)); | ||||
|                             JsonSerializer.SerializeToFile(channels, channelCacheFile); | ||||
|                             await using var writeStream = File.OpenWrite(channelCacheFile); | ||||
|                             await JsonSerializer.SerializeAsync(writeStream, channels, cancellationToken: cancellationToken).ConfigureAwait(false); | ||||
|                         } | ||||
|                         catch (IOException) | ||||
|                         { | ||||
| @ -110,7 +105,9 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts | ||||
|                     { | ||||
|                         try | ||||
|                         { | ||||
|                             var channels = JsonSerializer.DeserializeFromFile<List<ChannelInfo>>(channelCacheFile); | ||||
|                             await using var readStream = File.OpenRead(channelCacheFile); | ||||
|                             var channels = await JsonSerializer.DeserializeAsync<List<ChannelInfo>>(readStream, cancellationToken: cancellationToken) | ||||
|                                 .ConfigureAwait(false); | ||||
|                             list.AddRange(channels); | ||||
|                         } | ||||
|                         catch (IOException) | ||||
| @ -233,10 +230,5 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts | ||||
|         { | ||||
|             return Config.GetConfiguration<LiveTvOptions>("livetv"); | ||||
|         } | ||||
| 
 | ||||
|         private class ChannelCache | ||||
|         { | ||||
|             public List<ChannelInfo> Channels; | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -7,6 +7,7 @@ using System.IO; | ||||
| using System.Linq; | ||||
| using System.Net; | ||||
| using System.Net.Http; | ||||
| using System.Text.Json; | ||||
| using System.Threading; | ||||
| using System.Threading.Tasks; | ||||
| using MediaBrowser.Common.Configuration; | ||||
| @ -23,7 +24,7 @@ using MediaBrowser.Model.IO; | ||||
| using MediaBrowser.Model.LiveTv; | ||||
| using MediaBrowser.Model.MediaInfo; | ||||
| using MediaBrowser.Model.Net; | ||||
| using MediaBrowser.Model.Serialization; | ||||
| using Microsoft.Extensions.Caching.Memory; | ||||
| using Microsoft.Extensions.Logging; | ||||
| 
 | ||||
| namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun | ||||
| @ -39,14 +40,14 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun | ||||
|         public HdHomerunHost( | ||||
|             IServerConfigurationManager config, | ||||
|             ILogger<HdHomerunHost> logger, | ||||
|             IJsonSerializer jsonSerializer, | ||||
|             IFileSystem fileSystem, | ||||
|             IHttpClient httpClient, | ||||
|             IServerApplicationHost appHost, | ||||
|             ISocketFactory socketFactory, | ||||
|             INetworkManager networkManager, | ||||
|             IStreamHelper streamHelper) | ||||
|             : base(config, logger, jsonSerializer, fileSystem) | ||||
|             IStreamHelper streamHelper, | ||||
|             IMemoryCache memoryCache) | ||||
|             : base(config, logger, fileSystem, memoryCache) | ||||
|         { | ||||
|             _httpClient = httpClient; | ||||
|             _appHost = appHost; | ||||
| @ -75,18 +76,17 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun | ||||
|                 BufferContent = false | ||||
|             }; | ||||
| 
 | ||||
|             using (var response = await _httpClient.SendAsync(options, HttpMethod.Get).ConfigureAwait(false)) | ||||
|             using (var stream = response.Content) | ||||
|             using var response = await _httpClient.SendAsync(options, HttpMethod.Get).ConfigureAwait(false); | ||||
|             await using var stream = response.Content; | ||||
|             var lineup = await JsonSerializer.DeserializeAsync<List<Channels>>(stream, cancellationToken: cancellationToken) | ||||
|                 .ConfigureAwait(false) ?? new List<Channels>(); | ||||
| 
 | ||||
|             if (info.ImportFavoritesOnly) | ||||
|             { | ||||
|                 var lineup = await JsonSerializer.DeserializeFromStreamAsync<List<Channels>>(stream).ConfigureAwait(false) ?? new List<Channels>(); | ||||
| 
 | ||||
|                 if (info.ImportFavoritesOnly) | ||||
|                 { | ||||
|                     lineup = lineup.Where(i => i.Favorite).ToList(); | ||||
|                 } | ||||
| 
 | ||||
|                 return lineup.Where(i => !i.DRM).ToList(); | ||||
|                 lineup = lineup.Where(i => i.Favorite).ToList(); | ||||
|             } | ||||
| 
 | ||||
|             return lineup.Where(i => !i.DRM).ToList(); | ||||
|         } | ||||
| 
 | ||||
|         private class HdHomerunChannelInfo : ChannelInfo | ||||
| @ -132,30 +132,30 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun | ||||
| 
 | ||||
|             try | ||||
|             { | ||||
|                 using (var response = await _httpClient.SendAsync(new HttpRequestOptions() | ||||
|                 using var response = await _httpClient.SendAsync( | ||||
|                     new HttpRequestOptions | ||||
|                 { | ||||
|                     Url = string.Format("{0}/discover.json", GetApiUrl(info)), | ||||
|                     Url = string.Format(CultureInfo.InvariantCulture, "{0}/discover.json", GetApiUrl(info)), | ||||
|                     CancellationToken = cancellationToken, | ||||
|                     BufferContent = false | ||||
|                 }, HttpMethod.Get).ConfigureAwait(false)) | ||||
|                 using (var stream = response.Content) | ||||
|                 }, HttpMethod.Get).ConfigureAwait(false); | ||||
|                 await using var stream = response.Content; | ||||
|                 var discoverResponse = await JsonSerializer.DeserializeAsync<DiscoverResponse>(stream, cancellationToken: cancellationToken) | ||||
|                     .ConfigureAwait(false); | ||||
| 
 | ||||
|                 if (!string.IsNullOrEmpty(cacheKey)) | ||||
|                 { | ||||
|                     var discoverResponse = await JsonSerializer.DeserializeFromStreamAsync<DiscoverResponse>(stream).ConfigureAwait(false); | ||||
| 
 | ||||
|                     if (!string.IsNullOrEmpty(cacheKey)) | ||||
|                     lock (_modelCache) | ||||
|                     { | ||||
|                         lock (_modelCache) | ||||
|                         { | ||||
|                             _modelCache[cacheKey] = discoverResponse; | ||||
|                         } | ||||
|                         _modelCache[cacheKey] = discoverResponse; | ||||
|                     } | ||||
| 
 | ||||
|                     return discoverResponse; | ||||
|                 } | ||||
| 
 | ||||
|                 return discoverResponse; | ||||
|             } | ||||
|             catch (HttpException ex) | ||||
|             { | ||||
|                 if (!throwAllExceptions && ex.StatusCode.HasValue && ex.StatusCode.Value == System.Net.HttpStatusCode.NotFound) | ||||
|                 if (!throwAllExceptions && ex.StatusCode.HasValue && ex.StatusCode.Value == HttpStatusCode.NotFound) | ||||
|                 { | ||||
|                     var defaultValue = "HDHR"; | ||||
|                     var response = new DiscoverResponse | ||||
|  | ||||
| @ -18,7 +18,7 @@ using MediaBrowser.Model.Entities; | ||||
| using MediaBrowser.Model.IO; | ||||
| using MediaBrowser.Model.LiveTv; | ||||
| using MediaBrowser.Model.MediaInfo; | ||||
| using MediaBrowser.Model.Serialization; | ||||
| using Microsoft.Extensions.Caching.Memory; | ||||
| using Microsoft.Extensions.Logging; | ||||
| using Microsoft.Net.Http.Headers; | ||||
| 
 | ||||
| @ -36,13 +36,13 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts | ||||
|             IServerConfigurationManager config, | ||||
|             IMediaSourceManager mediaSourceManager, | ||||
|             ILogger<M3UTunerHost> logger, | ||||
|             IJsonSerializer jsonSerializer, | ||||
|             IFileSystem fileSystem, | ||||
|             IHttpClient httpClient, | ||||
|             IServerApplicationHost appHost, | ||||
|             INetworkManager networkManager, | ||||
|             IStreamHelper streamHelper) | ||||
|             : base(config, logger, jsonSerializer, fileSystem) | ||||
|             IStreamHelper streamHelper, | ||||
|             IMemoryCache memoryCache) | ||||
|             : base(config, logger, fileSystem, memoryCache) | ||||
|         { | ||||
|             _httpClient = httpClient; | ||||
|             _appHost = appHost; | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user