jellyfin/MediaBrowser.Server.Implementations/LiveTv/Listings/XmlTvListingsProvider.cs

176 lines
7.5 KiB
C#
Raw Normal View History

using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.LiveTv;
using System;
using System.Collections.Generic;
2016-06-07 08:14:13 +02:00
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Emby.XmlTv.Classes;
2016-06-15 05:12:32 +02:00
using MediaBrowser.Common.Extensions;
2016-06-07 08:14:13 +02:00
using MediaBrowser.Common.Net;
2016-06-05 23:27:14 +02:00
using MediaBrowser.Controller.Configuration;
2016-06-14 08:40:21 +02:00
using MediaBrowser.Model.Logging;
namespace MediaBrowser.Server.Implementations.LiveTv.Listings
{
public class XmlTvListingsProvider : IListingsProvider
{
2016-06-05 23:27:14 +02:00
private readonly IServerConfigurationManager _config;
2016-06-07 08:14:13 +02:00
private readonly IHttpClient _httpClient;
2016-06-14 08:40:21 +02:00
private readonly ILogger _logger;
2016-06-14 08:40:21 +02:00
public XmlTvListingsProvider(IServerConfigurationManager config, IHttpClient httpClient, ILogger logger)
2016-06-05 23:27:14 +02:00
{
_config = config;
2016-06-07 08:14:13 +02:00
_httpClient = httpClient;
2016-06-14 08:40:21 +02:00
_logger = logger;
2016-06-05 23:27:14 +02:00
}
public string Name
{
get { return "XmlTV"; }
}
public string Type
{
get { return "xmltv"; }
}
2016-06-05 23:27:14 +02:00
private string GetLanguage()
{
return _config.Configuration.PreferredMetadataLanguage;
}
2016-06-07 08:14:13 +02:00
private async Task<string> GetXml(string path, CancellationToken cancellationToken)
{
2016-06-14 21:21:26 +02:00
_logger.Info("xmltv path: {0}", path);
2016-06-07 08:14:13 +02:00
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;
}
2016-06-14 08:40:21 +02:00
_logger.Info("Downloading xmltv listings from {0}", path);
2016-06-07 08:14:13 +02:00
var tempFile = await _httpClient.GetTempFile(new HttpRequestOptions
{
CancellationToken = cancellationToken,
2016-06-14 06:06:57 +02:00
Url = path,
Progress = new Progress<Double>()
2016-06-07 08:14:13 +02:00
}).ConfigureAwait(false);
2016-06-15 05:12:32 +02:00
Directory.CreateDirectory(Path.GetDirectoryName(cacheFile));
2016-06-07 08:14:13 +02:00
File.Copy(tempFile, cacheFile, true);
return cacheFile;
}
public async Task<IEnumerable<ProgramInfo>> GetProgramsAsync(ListingsProviderInfo info, string channelNumber, string channelName, DateTime startDateUtc, DateTime endDateUtc, CancellationToken cancellationToken)
{
2016-06-07 08:14:13 +02:00
var path = await GetXml(info.Path, cancellationToken).ConfigureAwait(false);
var reader = new XmlTvReader(path, GetLanguage(), null);
var results = reader.GetProgrammes(channelNumber, startDateUtc, endDateUtc, cancellationToken);
2016-06-07 08:14:13 +02:00
return results.Select(p => new ProgramInfo()
{
ChannelId = p.ChannelId,
EndDate = p.EndDate,
EpisodeNumber = p.Episode == null ? null : p.Episode.Episode,
EpisodeTitle = p.Episode == null ? null : p.Episode.Title,
Genres = p.Categories,
Id = String.Format("{0}_{1:O}", p.ChannelId, p.StartDate), // Construct an id from the channel and start date,
StartDate = p.StartDate,
Name = p.Title,
Overview = p.Description,
ShortOverview = p.Description,
ProductionYear = !p.CopyrightDate.HasValue ? (int?)null : p.CopyrightDate.Value.Year,
SeasonNumber = p.Episode == null ? null : p.Episode.Series,
2016-06-15 12:30:02 +02:00
IsSeries = p.Episode != null,
IsRepeat = p.IsRepeat,
IsPremiere = p.Premiere != null,
IsKids = p.Categories.Any(c => info.KidsCategories.Contains(c, StringComparer.InvariantCultureIgnoreCase)),
IsMovie = p.Categories.Any(c => info.MovieCategories.Contains(c, StringComparer.InvariantCultureIgnoreCase)),
IsNews = p.Categories.Any(c => info.NewsCategories.Contains(c, StringComparer.InvariantCultureIgnoreCase)),
IsSports = p.Categories.Any(c => info.SportsCategories.Contains(c, StringComparer.InvariantCultureIgnoreCase)),
ImageUrl = p.Icon != null && !String.IsNullOrEmpty(p.Icon.Source) ? p.Icon.Source : null,
HasImage = p.Icon != null && !String.IsNullOrEmpty(p.Icon.Source),
OfficialRating = p.Rating != null && !String.IsNullOrEmpty(p.Rating.Value) ? p.Rating.Value : null,
2016-06-15 05:12:32 +02:00
CommunityRating = p.StarRating.HasValue ? p.StarRating.Value : (float?)null,
2016-06-15 12:30:02 +02:00
SeriesId = p.Episode != null ? p.Title.GetMD5().ToString("N") : null
2016-06-07 08:14:13 +02:00
});
}
2016-06-14 08:40:21 +02:00
public async Task AddMetadata(ListingsProviderInfo info, List<ChannelInfo> channels, CancellationToken cancellationToken)
{
// Add the channel image url
2016-06-14 08:40:21 +02:00
var path = await GetXml(info.Path, cancellationToken).ConfigureAwait(false);
var reader = new XmlTvReader(path, GetLanguage(), null);
var results = reader.GetChannels().ToList();
2016-06-08 06:57:03 +02:00
if (channels != null)
2016-06-07 07:42:26 +02:00
{
channels.ForEach(c =>
{
2016-06-08 06:57:03 +02:00
var channelNumber = info.GetMappedChannel(c.Number);
var match = results.FirstOrDefault(r => string.Equals(r.Id, channelNumber, StringComparison.OrdinalIgnoreCase));
if (match != null && match.Icon != null && !String.IsNullOrEmpty(match.Icon.Source))
{
c.ImageUrl = match.Icon.Source;
}
});
2016-06-07 07:42:26 +02:00
}
}
2016-06-07 07:42:26 +02:00
public Task Validate(ListingsProviderInfo info, bool validateLogin, bool validateListings)
{
2016-06-07 07:42:26 +02:00
// Assume all urls are valid. check files for existence
if (!info.Path.StartsWith("http", StringComparison.OrdinalIgnoreCase) && !File.Exists(info.Path))
{
throw new FileNotFoundException("Could not find the XmlTv file specified:", info.Path);
}
2016-06-07 07:42:26 +02:00
return Task.FromResult(true);
}
2016-06-14 08:40:21 +02:00
public async Task<List<NameIdPair>> GetLineups(ListingsProviderInfo info, string country, string location)
{
// In theory this should never be called because there is always only one lineup
2016-06-14 08:40:21 +02:00
var path = await GetXml(info.Path, CancellationToken.None).ConfigureAwait(false);
var reader = new XmlTvReader(path, GetLanguage(), null);
var results = reader.GetChannels();
// Should this method be async?
2016-06-14 08:40:21 +02:00
return results.Select(c => new NameIdPair() { Id = c.Id, Name = c.DisplayName }).ToList();
}
2016-06-08 07:24:25 +02:00
public async Task<List<ChannelInfo>> GetChannels(ListingsProviderInfo info, CancellationToken cancellationToken)
{
// In theory this should never be called because there is always only one lineup
2016-06-14 08:40:21 +02:00
var path = await GetXml(info.Path, cancellationToken).ConfigureAwait(false);
var reader = new XmlTvReader(path, GetLanguage(), null);
var results = reader.GetChannels();
// Should this method be async?
return results.Select(c => new ChannelInfo()
{
Id = c.Id,
Name = c.DisplayName,
2016-06-14 21:21:26 +02:00
ImageUrl = c.Icon != null && !String.IsNullOrEmpty(c.Icon.Source) ? c.Icon.Source : null,
Number = c.Id
}).ToList();
2016-06-08 07:24:25 +02:00
}
}
}