diff options
Diffstat (limited to 'MediaBrowser.Server.Implementations/LiveTv/TunerHosts/M3UTunerHost.cs')
| -rw-r--r-- | MediaBrowser.Server.Implementations/LiveTv/TunerHosts/M3UTunerHost.cs | 97 |
1 files changed, 55 insertions, 42 deletions
diff --git a/MediaBrowser.Server.Implementations/LiveTv/TunerHosts/M3UTunerHost.cs b/MediaBrowser.Server.Implementations/LiveTv/TunerHosts/M3UTunerHost.cs index 3783e4b08..a26ed7a2a 100644 --- a/MediaBrowser.Server.Implementations/LiveTv/TunerHosts/M3UTunerHost.cs +++ b/MediaBrowser.Server.Implementations/LiveTv/TunerHosts/M3UTunerHost.cs @@ -12,14 +12,22 @@ using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; +using MediaBrowser.Common.IO; +using MediaBrowser.Common.Net; +using MediaBrowser.Model.Serialization; namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts { public class M3UTunerHost : BaseTunerHost, ITunerHost { - public M3UTunerHost(IConfigurationManager config, ILogger logger) - : base(config, logger) + private readonly IFileSystem _fileSystem; + private IHttpClient _httpClient; + + public M3UTunerHost(IConfigurationManager config, ILogger logger, IFileSystem fileSystem, IHttpClient httpClient, IJsonSerializer jsonSerializer) + : base(config, logger, jsonSerializer) { + _fileSystem = fileSystem; + _httpClient = httpClient; } public override string Type @@ -41,47 +49,48 @@ namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts string line; // Read the file and display it line by line. - var file = new StreamReader(url); - var channels = new List<M3UChannel>(); + using (var file = new StreamReader(await GetListingsStream(info, cancellationToken).ConfigureAwait(false))) + { + var channels = new List<M3UChannel>(); - string channnelName = null; - string channelNumber = null; + string channnelName = null; + string channelNumber = null; - while ((line = file.ReadLine()) != null) - { - line = line.Trim(); - if (string.IsNullOrWhiteSpace(line)) + while ((line = file.ReadLine()) != null) { - continue; - } + line = line.Trim(); + if (string.IsNullOrWhiteSpace(line)) + { + continue; + } - if (line.StartsWith("#EXTM3U", StringComparison.OrdinalIgnoreCase)) - { - continue; - } + if (line.StartsWith("#EXTM3U", StringComparison.OrdinalIgnoreCase)) + { + continue; + } - if (line.StartsWith("#EXTINF:", StringComparison.OrdinalIgnoreCase)) - { - var parts = line.Split(new[] { ':' }, 2).Last().Split(new[] { ',' }, 2); - channelNumber = parts[0]; - channnelName = parts[1]; - } - else if (!string.IsNullOrWhiteSpace(channelNumber)) - { - channels.Add(new M3UChannel + if (line.StartsWith("#EXTINF:", StringComparison.OrdinalIgnoreCase)) { - Name = channnelName, - Number = channelNumber, - Id = ChannelIdPrefix + urlHash + channelNumber, - Path = line - }); - - channelNumber = null; - channnelName = null; + var parts = line.Split(new[] { ':' }, 2).Last().Split(new[] { ',' }, 2); + channelNumber = parts[0]; + channnelName = parts[1]; + } + else if (!string.IsNullOrWhiteSpace(channelNumber)) + { + channels.Add(new M3UChannel + { + Name = channnelName, + Number = channelNumber, + Id = ChannelIdPrefix + urlHash + channelNumber, + Path = line + }); + + channelNumber = null; + channnelName = null; + } } + return channels; } - file.Close(); - return channels; } public Task<List<LiveTvTunerInfo>> GetTunerInfos(CancellationToken cancellationToken) @@ -119,9 +128,9 @@ namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts public async Task Validate(TunerHostInfo info) { - if (!File.Exists(info.Url)) + using (var stream = await GetListingsStream(info, CancellationToken.None).ConfigureAwait(false)) { - throw new FileNotFoundException(); + } } @@ -130,6 +139,15 @@ namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts return channelId.StartsWith(ChannelIdPrefix, StringComparison.OrdinalIgnoreCase); } + private Task<Stream> GetListingsStream(TunerHostInfo info, CancellationToken cancellationToken) + { + if (info.Url.StartsWith("http", StringComparison.OrdinalIgnoreCase)) + { + return _httpClient.Get(info.Url, cancellationToken); + } + return Task.FromResult(_fileSystem.OpenRead(info.Url)); + } + protected override async Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(TunerHostInfo info, string channelId, CancellationToken cancellationToken) { var urlHash = info.Url.GetMD5().ToString("N"); @@ -190,10 +208,5 @@ namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts } return new List<MediaSourceInfo> { }; } - - protected override Task<bool> IsAvailableInternal(TunerHostInfo tuner, string channelId, CancellationToken cancellationToken) - { - return Task.FromResult(true); - } } } |
