diff options
| author | Luke Pulverenti <luke.pulverenti@gmail.com> | 2016-06-07 02:14:13 -0400 |
|---|---|---|
| committer | Luke Pulverenti <luke.pulverenti@gmail.com> | 2016-06-07 02:14:13 -0400 |
| commit | 7be8a519a35702ec24c9c69ef27766a354fd0960 (patch) | |
| tree | 2e4fdad00e614389abc159490b6b18687a93d51d | |
| parent | c1b68356c4c7175f5d1241fca3ba61af1b407ce2 (diff) | |
add xml tv download
| -rw-r--r-- | MediaBrowser.Server.Implementations/LiveTv/Listings/XmlTvListingsProvider.cs | 40 |
1 files changed, 35 insertions, 5 deletions
diff --git a/MediaBrowser.Server.Implementations/LiveTv/Listings/XmlTvListingsProvider.cs b/MediaBrowser.Server.Implementations/LiveTv/Listings/XmlTvListingsProvider.cs index e0ef67dcd..328dd9979 100644 --- a/MediaBrowser.Server.Implementations/LiveTv/Listings/XmlTvListingsProvider.cs +++ b/MediaBrowser.Server.Implementations/LiveTv/Listings/XmlTvListingsProvider.cs @@ -3,12 +3,14 @@ using MediaBrowser.Model.Dto; using MediaBrowser.Model.LiveTv; using System; using System.Collections.Generic; +using System.Globalization; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; using Emby.XmlTv.Classes; +using MediaBrowser.Common.Net; using MediaBrowser.Controller.Configuration; namespace MediaBrowser.Server.Implementations.LiveTv.Listings @@ -16,10 +18,12 @@ namespace MediaBrowser.Server.Implementations.LiveTv.Listings public class XmlTvListingsProvider : IListingsProvider { private readonly IServerConfigurationManager _config; + private readonly IHttpClient _httpClient; - public XmlTvListingsProvider(IServerConfigurationManager config) + public XmlTvListingsProvider(IServerConfigurationManager config, IHttpClient httpClient) { _config = config; + _httpClient = httpClient; } public string Name @@ -37,13 +41,39 @@ namespace MediaBrowser.Server.Implementations.LiveTv.Listings return _config.Configuration.PreferredMetadataLanguage; } + private async Task<string> GetXml(string path, CancellationToken cancellationToken) + { + if (!path.StartsWith("http", StringComparison.OrdinalIgnoreCase)) + { + return path; + } + + var cacheFilename = DateTime.UtcNow.DayOfYear.ToString(CultureInfo.InvariantCulture) + "_" + DateTime.UtcNow.Hour.ToString(CultureInfo.InvariantCulture) + ".xml"; + var cacheFile = Path.Combine(_config.ApplicationPaths.CachePath, "xmltv", cacheFilename); + if (File.Exists(cacheFile)) + { + return cacheFile; + } + + var tempFile = await _httpClient.GetTempFile(new HttpRequestOptions + { + CancellationToken = cancellationToken, + Url = path + + }).ConfigureAwait(false); + File.Copy(tempFile, cacheFile, true); + + return cacheFile; + } + // TODO: Should this method be async? - public Task<IEnumerable<ProgramInfo>> GetProgramsAsync(ListingsProviderInfo info, string channelNumber, string channelName, DateTime startDateUtc, DateTime endDateUtc, CancellationToken cancellationToken) + public async Task<IEnumerable<ProgramInfo>> GetProgramsAsync(ListingsProviderInfo info, string channelNumber, string channelName, DateTime startDateUtc, DateTime endDateUtc, CancellationToken cancellationToken) { - var reader = new XmlTvReader(info.Path, GetLanguage(), null); + var path = await GetXml(info.Path, cancellationToken).ConfigureAwait(false); + var reader = new XmlTvReader(path, GetLanguage(), null); var results = reader.GetProgrammes(channelNumber, startDateUtc, endDateUtc, cancellationToken); - return Task.FromResult(results.Select(p => new ProgramInfo() + return results.Select(p => new ProgramInfo() { ChannelId = p.ChannelId, EndDate = p.EndDate, @@ -68,7 +98,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv.Listings HasImage = p.Icon != null && !String.IsNullOrEmpty(p.Icon.Source), OfficialRating = p.Rating != null && !String.IsNullOrEmpty(p.Rating.Value) ? p.Rating.Value : null, CommunityRating = p.StarRating.HasValue ? p.StarRating.Value : (float?)null - })); + }); } public Task AddMetadata(ListingsProviderInfo info, List<ChannelInfo> channels, CancellationToken cancellationToken) |
