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

287 lines
12 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;
2017-02-11 22:16:22 +01:00
using System.IO.Compression;
using System.Linq;
2016-06-16 04:37:06 +02:00
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Emby.XmlTv.Classes;
2016-09-18 07:52:27 +02:00
using Emby.XmlTv.Entities;
2016-06-15 05:12:32 +02:00
using MediaBrowser.Common.Extensions;
2016-06-07 08:14:13 +02:00
using MediaBrowser.Common.Net;
2017-06-23 18:04:45 +02:00
using MediaBrowser.Common.Progress;
2016-06-05 23:27:14 +02:00
using MediaBrowser.Controller.Configuration;
2016-11-04 00:35:19 +01:00
using MediaBrowser.Model.IO;
2016-06-14 08:40:21 +02:00
using MediaBrowser.Model.Logging;
2016-11-04 00:35:19 +01:00
namespace Emby.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-11-04 00:35:19 +01:00
private readonly IFileSystem _fileSystem;
2017-02-11 22:16:22 +01:00
private readonly IZipClient _zipClient;
2017-02-11 22:16:22 +01:00
public XmlTvListingsProvider(IServerConfigurationManager config, IHttpClient httpClient, ILogger logger, IFileSystem fileSystem, IZipClient zipClient)
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-11-04 00:35:19 +01:00
_fileSystem = fileSystem;
2017-02-11 22:16:22 +01:00
_zipClient = zipClient;
2016-06-05 23:27:14 +02:00
}
public string Name
{
get { return "XmlTV"; }
}
public string Type
{
get { return "xmltv"; }
}
2017-06-24 20:32:22 +02:00
private string GetLanguage(ListingsProviderInfo info)
2016-06-05 23:27:14 +02:00
{
2017-06-24 20:32:22 +02:00
if (!string.IsNullOrWhiteSpace(info.PreferredLanguage))
{
return info.PreferredLanguage;
}
2016-06-05 23:27:14 +02:00
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;
}
2016-06-16 04:37:06 +02:00
var cacheFilename = DateTime.UtcNow.DayOfYear.ToString(CultureInfo.InvariantCulture) + "-" + DateTime.UtcNow.Hour.ToString(CultureInfo.InvariantCulture) + ".xml";
2016-06-07 08:14:13 +02:00
var cacheFile = Path.Combine(_config.ApplicationPaths.CachePath, "xmltv", cacheFilename);
2016-11-04 00:35:19 +01:00
if (_fileSystem.FileExists(cacheFile))
2016-06-07 08:14:13 +02:00
{
2017-02-11 22:16:22 +01:00
return UnzipIfNeeded(path, cacheFile);
2016-06-07 08:14:13 +02:00
}
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,
2017-06-23 18:04:45 +02:00
Progress = new SimpleProgress<Double>(),
2016-11-03 20:07:48 +01:00
DecompressionMethod = CompressionMethod.Gzip,
2016-06-16 04:37:06 +02:00
// It's going to come back gzipped regardless of this value
// So we need to make sure the decompression method is set to gzip
2016-12-04 22:31:05 +01:00
EnableHttpCompression = true,
UserAgent = "Emby/3.0"
2016-06-07 08:14:13 +02:00
}).ConfigureAwait(false);
2016-06-15 05:12:32 +02:00
2017-05-04 20:14:45 +02:00
_fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(cacheFile));
2016-06-07 08:14:13 +02:00
2016-11-04 00:35:19 +01:00
using (var stream = _fileSystem.OpenRead(tempFile))
2016-06-16 04:37:06 +02:00
{
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
2016-11-04 00:35:19 +01:00
using (var fileStream = _fileSystem.GetFileStream(cacheFile, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read))
2016-06-16 04:37:06 +02:00
{
using (var writer = new StreamWriter(fileStream))
{
while (!reader.EndOfStream)
{
writer.WriteLine(reader.ReadLine());
}
}
}
}
}
_logger.Debug("Returning xmltv path {0}", cacheFile);
2017-02-11 22:16:22 +01:00
return UnzipIfNeeded(path, cacheFile);
}
private string UnzipIfNeeded(string originalUrl, string file)
{
//var ext = Path.GetExtension(originalUrl);
//if (string.Equals(ext, ".gz", StringComparison.OrdinalIgnoreCase))
//{
// using (var stream = _fileSystem.OpenRead(file))
// {
// var tempFolder = Path.Combine(_config.ApplicationPaths.TempDirectory, Guid.NewGuid().ToString());
// _fileSystem.CreateDirectory(tempFolder);
// _zipClient.ExtractAllFromZip(stream, tempFolder, true);
// return _fileSystem.GetFiles(tempFolder, true)
// .Where(i => string.Equals(i.Extension, ".xml", StringComparison.OrdinalIgnoreCase))
// .Select(i => i.FullName)
// .FirstOrDefault();
// }
//}
return file;
2016-06-07 08:14:13 +02:00
}
2017-02-05 00:32:16 +01:00
public async Task<IEnumerable<ProgramInfo>> GetProgramsAsync(ListingsProviderInfo info, string channelId, DateTime startDateUtc, DateTime endDateUtc, CancellationToken cancellationToken)
{
2017-02-05 00:32:16 +01:00
if (string.IsNullOrWhiteSpace(channelId))
{
throw new ArgumentNullException("channelId");
}
if (!await EmbyTV.EmbyTVRegistration.Instance.EnableXmlTv().ConfigureAwait(false))
{
var length = endDateUtc - startDateUtc;
if (length.TotalDays > 1)
{
endDateUtc = startDateUtc.AddDays(1);
}
}
2017-02-11 22:16:22 +01:00
_logger.Debug("Getting xmltv programs for channel {0}", channelId);
2016-06-07 08:14:13 +02:00
var path = await GetXml(info.Path, cancellationToken).ConfigureAwait(false);
2017-06-24 20:32:22 +02:00
var reader = new XmlTvReader(path, GetLanguage(info));
2017-02-05 00:32:16 +01:00
var results = reader.GetProgrammes(channelId, startDateUtc, endDateUtc, cancellationToken);
2016-09-18 07:52:27 +02:00
return results.Select(p => GetProgramInfo(p, info));
}
private ProgramInfo GetProgramInfo(XmlTvProgram p, ListingsProviderInfo info)
{
var episodeTitle = p.Episode == null ? null : p.Episode.Title;
2016-09-18 07:52:27 +02:00
var programInfo = new ProgramInfo
{
ChannelId = p.ChannelId,
2016-06-28 06:47:30 +02:00
EndDate = GetDate(p.EndDate),
EpisodeNumber = p.Episode == null ? null : p.Episode.Episode,
EpisodeTitle = episodeTitle,
Genres = p.Categories,
2016-06-28 06:47:30 +02:00
StartDate = GetDate(p.StartDate),
Name = p.Title,
Overview = 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,
2016-11-28 06:36:35 +01:00
IsRepeat = p.IsPreviouslyShown && !p.IsNew,
IsPremiere = p.Premiere != null,
2016-11-04 00:35:19 +01:00
IsKids = p.Categories.Any(c => info.KidsCategories.Contains(c, StringComparer.OrdinalIgnoreCase)),
IsMovie = p.Categories.Any(c => info.MovieCategories.Contains(c, StringComparer.OrdinalIgnoreCase)),
IsNews = p.Categories.Any(c => info.NewsCategories.Contains(c, StringComparer.OrdinalIgnoreCase)),
IsSports = p.Categories.Any(c => info.SportsCategories.Contains(c, StringComparer.OrdinalIgnoreCase)),
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,
2017-01-28 04:17:26 +01:00
SeriesId = p.Episode != null ? p.Title.GetMD5().ToString("N") : null
2016-09-18 07:52:27 +02:00
};
2017-01-28 04:17:26 +01:00
if (!string.IsNullOrWhiteSpace(p.ProgramId))
{
programInfo.ShowId = p.ProgramId;
}
else
{
2017-02-01 08:47:12 +01:00
var uniqueString = (p.Title ?? string.Empty) + (episodeTitle ?? string.Empty) + (p.IceTvEpisodeNumber ?? string.Empty);
2017-01-31 22:25:42 +01:00
2017-02-01 08:47:12 +01:00
if (programInfo.SeasonNumber.HasValue)
{
uniqueString = "-" + programInfo.SeasonNumber.Value.ToString(CultureInfo.InvariantCulture);
}
2017-01-31 22:25:42 +01:00
if (programInfo.EpisodeNumber.HasValue)
{
uniqueString = "-" + programInfo.EpisodeNumber.Value.ToString(CultureInfo.InvariantCulture);
}
programInfo.ShowId = uniqueString.GetMD5().ToString("N");
// If we don't have valid episode info, assume it's a unique program, otherwise recordings might be skipped
if (programInfo.IsSeries && !programInfo.IsRepeat)
{
if ((programInfo.EpisodeNumber ?? 0) == 0)
{
programInfo.ShowId = programInfo.ShowId + programInfo.StartDate.Ticks.ToString(CultureInfo.InvariantCulture);
}
}
2017-01-28 04:17:26 +01:00
}
// Construct an id from the channel and start date
programInfo.Id = String.Format("{0}_{1:O}", p.ChannelId, p.StartDate);
2016-09-18 07:52:27 +02:00
if (programInfo.IsMovie)
{
programInfo.IsSeries = false;
programInfo.EpisodeNumber = null;
programInfo.EpisodeTitle = null;
}
return programInfo;
}
2016-06-28 06:47:30 +02:00
private DateTime GetDate(DateTime date)
{
if (date.Kind != DateTimeKind.Utc)
{
date = DateTime.SpecifyKind(date, DateTimeKind.Utc);
}
return date;
}
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
2016-11-04 00:35:19 +01:00
if (!info.Path.StartsWith("http", StringComparison.OrdinalIgnoreCase) && !_fileSystem.FileExists(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);
2017-06-24 20:32:22 +02:00
var reader = new XmlTvReader(path, GetLanguage(info));
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);
2017-06-24 20:32:22 +02:00
var reader = new XmlTvReader(path, GetLanguage(info));
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 = string.IsNullOrWhiteSpace(c.Number) ? c.Id : c.Number
2016-06-14 21:21:26 +02:00
}).ToList();
2016-06-08 07:24:25 +02:00
}
}
}