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

302 lines
12 KiB
C#
Raw Normal View History

#nullable disable
#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
2019-07-08 12:50:15 +00:00
using System.Net.Http;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
2021-09-19 18:53:31 +00:00
using Jellyfin.Extensions;
using Jellyfin.XmlTv;
using Jellyfin.XmlTv.Entities;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
2018-12-30 17:21:49 +00:00
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.IO;
2018-12-30 17:21:49 +00:00
using MediaBrowser.Model.LiveTv;
2018-12-30 21:49:09 +00:00
using Microsoft.Extensions.Logging;
namespace Emby.Server.Implementations.LiveTv.Listings
{
public class XmlTvListingsProvider : IListingsProvider
{
private static readonly TimeSpan _maxCacheAge = TimeSpan.FromHours(1);
private readonly IServerConfigurationManager _config;
private readonly IHttpClientFactory _httpClientFactory;
2020-06-06 00:15:56 +00:00
private readonly ILogger<XmlTvListingsProvider> _logger;
private readonly IFileSystem _fileSystem;
private readonly IZipClient _zipClient;
2019-07-08 13:14:51 +00:00
public XmlTvListingsProvider(
IServerConfigurationManager config,
IHttpClientFactory httpClientFactory,
ILogger<XmlTvListingsProvider> logger,
2019-07-08 13:14:51 +00:00
IFileSystem fileSystem,
IZipClient zipClient)
{
_config = config;
_httpClientFactory = httpClientFactory;
_logger = logger;
_fileSystem = fileSystem;
_zipClient = zipClient;
}
public string Name => "XmlTV";
public string Type => "xmltv";
private string GetLanguage(ListingsProviderInfo info)
{
if (!string.IsNullOrWhiteSpace(info.PreferredLanguage))
{
return info.PreferredLanguage;
}
return _config.Configuration.PreferredMetadataLanguage;
}
2021-09-12 18:30:53 +00:00
private async Task<string> GetXml(ListingsProviderInfo info, CancellationToken cancellationToken)
{
2021-09-12 18:30:53 +00:00
_logger.LogInformation("xmltv path: {Path}", info.Path);
2021-09-12 18:30:53 +00:00
if (!info.Path.StartsWith("http", StringComparison.OrdinalIgnoreCase))
{
2021-09-12 18:30:53 +00:00
return UnzipIfNeeded(info.Path, info.Path);
}
string cacheFilename = info.Id + ".xml";
2018-12-30 19:21:48 +00:00
string cacheFile = Path.Combine(_config.ApplicationPaths.CachePath, "xmltv", cacheFilename);
if (File.Exists(cacheFile) && File.GetLastWriteTimeUtc(cacheFile) >= DateTime.UtcNow.Subtract(_maxCacheAge))
{
2021-09-12 18:30:53 +00:00
return UnzipIfNeeded(info.Path, cacheFile);
}
File.Delete(cacheFile);
2021-09-12 18:30:53 +00:00
_logger.LogInformation("Downloading xmltv listings from {Path}", info.Path);
Directory.CreateDirectory(Path.GetDirectoryName(cacheFile));
2021-09-12 18:30:53 +00:00
using var response = await _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(info.Path, cancellationToken).ConfigureAwait(false);
2020-11-17 18:43:00 +00:00
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
2021-09-25 17:44:40 +00:00
await using (var fileStream = new FileStream(cacheFile, FileMode.CreateNew, FileAccess.Write, FileShare.None, IODefaults.CopyToBufferSize, FileOptions.Asynchronous))
2019-07-08 12:50:15 +00:00
{
await stream.CopyToAsync(fileStream, cancellationToken).ConfigureAwait(false);
2019-07-08 12:50:15 +00:00
}
2021-09-12 18:30:53 +00:00
return UnzipIfNeeded(info.Path, cacheFile);
}
2021-09-19 18:53:31 +00:00
private string UnzipIfNeeded(ReadOnlySpan<char> originalUrl, string file)
{
2021-09-19 18:53:31 +00:00
ReadOnlySpan<char> ext = Path.GetExtension(originalUrl.LeftPart('?'));
2021-09-19 18:53:31 +00:00
if (ext.Equals(".gz", StringComparison.OrdinalIgnoreCase))
{
try
{
2018-12-30 19:21:48 +00:00
string tempFolder = ExtractGz(file);
return FindXmlFile(tempFolder);
}
catch (Exception ex)
{
2019-07-08 13:14:51 +00:00
_logger.LogError(ex, "Error extracting from gz file {File}", file);
}
try
{
2018-12-30 19:21:48 +00:00
string tempFolder = ExtractFirstFileFromGz(file);
return FindXmlFile(tempFolder);
}
catch (Exception ex)
{
2019-07-08 13:14:51 +00:00
_logger.LogError(ex, "Error extracting from zip file {File}", file);
}
}
return file;
}
private string ExtractFirstFileFromGz(string file)
{
2019-01-26 21:31:59 +00:00
using (var stream = File.OpenRead(file))
{
string tempFolder = GetTempFolderPath(stream);
Directory.CreateDirectory(tempFolder);
_zipClient.ExtractFirstFileFromGz(stream, tempFolder, "data.xml");
return tempFolder;
}
}
private string ExtractGz(string file)
{
2019-01-26 21:31:59 +00:00
using (var stream = File.OpenRead(file))
{
string tempFolder = GetTempFolderPath(stream);
Directory.CreateDirectory(tempFolder);
_zipClient.ExtractAllFromGz(stream, tempFolder, true);
return tempFolder;
}
}
private string GetTempFolderPath(Stream stream)
{
#pragma warning disable CA5351
using var md5 = MD5.Create();
#pragma warning restore CA5351
var checksum = Convert.ToHexString(md5.ComputeHash(stream));
stream.Position = 0;
return Path.Combine(_config.ApplicationPaths.TempDirectory, checksum);
}
private string FindXmlFile(string directory)
{
return _fileSystem.GetFiles(directory, true)
.Where(i => string.Equals(i.Extension, ".xml", StringComparison.OrdinalIgnoreCase))
.Select(i => i.FullName)
.FirstOrDefault();
}
public async Task<IEnumerable<ProgramInfo>> GetProgramsAsync(ListingsProviderInfo info, string channelId, DateTime startDateUtc, DateTime endDateUtc, CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(channelId))
{
throw new ArgumentNullException(nameof(channelId));
}
2019-07-08 12:50:15 +00:00
_logger.LogDebug("Getting xmltv programs for channel {Id}", channelId);
2021-09-12 18:30:53 +00:00
string path = await GetXml(info, cancellationToken).ConfigureAwait(false);
2019-07-08 12:50:15 +00:00
_logger.LogDebug("Opening XmlTvReader for {Path}", path);
var reader = new XmlTvReader(path, GetLanguage(info));
2018-12-30 19:21:48 +00:00
return reader.GetProgrammes(channelId, startDateUtc, endDateUtc, cancellationToken)
.Select(p => GetProgramInfo(p, info));
}
private static ProgramInfo GetProgramInfo(XmlTvProgram program, ListingsProviderInfo info)
{
2018-12-30 19:21:48 +00:00
string episodeTitle = program.Episode?.Title;
var programInfo = new ProgramInfo
{
2018-12-30 19:21:48 +00:00
ChannelId = program.ChannelId,
EndDate = program.EndDate.UtcDateTime,
EpisodeNumber = program.Episode?.Episode,
EpisodeTitle = episodeTitle,
2018-12-30 19:21:48 +00:00
Genres = program.Categories,
StartDate = program.StartDate.UtcDateTime,
Name = program.Title,
Overview = program.Description,
ProductionYear = program.CopyrightDate?.Year,
SeasonNumber = program.Episode?.Series,
IsSeries = program.Episode != null,
IsRepeat = program.IsPreviouslyShown && !program.IsNew,
IsPremiere = program.Premiere != null,
2021-12-20 12:31:07 +00:00
IsKids = program.Categories.Any(c => info.KidsCategories.Contains(c, StringComparison.OrdinalIgnoreCase)),
IsMovie = program.Categories.Any(c => info.MovieCategories.Contains(c, StringComparison.OrdinalIgnoreCase)),
IsNews = program.Categories.Any(c => info.NewsCategories.Contains(c, StringComparison.OrdinalIgnoreCase)),
IsSports = program.Categories.Any(c => info.SportsCategories.Contains(c, StringComparison.OrdinalIgnoreCase)),
ImageUrl = program.Icon != null && !string.IsNullOrEmpty(program.Icon.Source) ? program.Icon.Source : null,
HasImage = program.Icon != null && !string.IsNullOrEmpty(program.Icon.Source),
OfficialRating = program.Rating != null && !string.IsNullOrEmpty(program.Rating.Value) ? program.Rating.Value : null,
2018-12-30 19:21:48 +00:00
CommunityRating = program.StarRating,
SeriesId = program.Episode == null ? null : program.Title.GetMD5().ToString("N", CultureInfo.InvariantCulture)
};
2018-12-30 19:21:48 +00:00
if (string.IsNullOrWhiteSpace(program.ProgramId))
{
2018-12-30 19:21:48 +00:00
string uniqueString = (program.Title ?? string.Empty) + (episodeTitle ?? string.Empty) /*+ (p.IceTvEpisodeNumber ?? string.Empty)*/;
if (programInfo.SeasonNumber.HasValue)
{
uniqueString = "-" + programInfo.SeasonNumber.Value.ToString(CultureInfo.InvariantCulture);
}
2020-06-15 21:43:52 +00:00
if (programInfo.EpisodeNumber.HasValue)
{
uniqueString = "-" + programInfo.EpisodeNumber.Value.ToString(CultureInfo.InvariantCulture);
}
programInfo.ShowId = uniqueString.GetMD5().ToString("N", CultureInfo.InvariantCulture);
// If we don't have valid episode info, assume it's a unique program, otherwise recordings might be skipped
2018-12-30 19:21:48 +00:00
if (programInfo.IsSeries
&& !programInfo.IsRepeat
&& (programInfo.EpisodeNumber ?? 0) == 0)
{
2020-08-07 17:26:28 +00:00
programInfo.ShowId += programInfo.StartDate.Ticks.ToString(CultureInfo.InvariantCulture);
}
}
2018-12-30 19:21:48 +00:00
else
{
programInfo.ShowId = program.ProgramId;
}
// Construct an id from the channel and start date
2020-08-07 17:26:28 +00:00
programInfo.Id = string.Format(CultureInfo.InvariantCulture, "{0}_{1:O}", program.ChannelId, program.StartDate);
if (programInfo.IsMovie)
{
programInfo.IsSeries = false;
programInfo.EpisodeNumber = null;
programInfo.EpisodeTitle = null;
}
return programInfo;
}
public Task Validate(ListingsProviderInfo info, bool validateLogin, bool validateListings)
{
// 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);
}
2018-12-30 17:21:49 +00:00
return Task.CompletedTask;
}
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
2021-09-12 18:30:53 +00:00
string path = await GetXml(info, CancellationToken.None).ConfigureAwait(false);
2019-07-08 12:50:15 +00:00
_logger.LogDebug("Opening XmlTvReader for {Path}", path);
var reader = new XmlTvReader(path, GetLanguage(info));
IEnumerable<XmlTvChannel> results = reader.GetChannels();
// Should this method be async?
return results.Select(c => new NameIdPair() { Id = c.Id, Name = c.DisplayName }).ToList();
}
public async Task<List<ChannelInfo>> GetChannels(ListingsProviderInfo info, CancellationToken cancellationToken)
{
// In theory this should never be called because there is always only one lineup
2021-09-12 18:30:53 +00:00
string path = await GetXml(info, cancellationToken).ConfigureAwait(false);
2019-07-08 12:50:15 +00:00
_logger.LogDebug("Opening XmlTvReader for {Path}", path);
var reader = new XmlTvReader(path, GetLanguage(info));
2019-01-13 20:37:13 +00:00
var results = reader.GetChannels();
// Should this method be async?
return results.Select(c => new ChannelInfo
{
Id = c.Id,
Name = c.DisplayName,
ImageUrl = c.Icon != null && !string.IsNullOrEmpty(c.Icon.Source) ? c.Icon.Source : null,
Number = string.IsNullOrWhiteSpace(c.Number) ? c.Id : c.Number
}).ToList();
}
}
}