2020-02-06 14:20:23 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2018-12-30 16:42:54 +00:00
|
|
|
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;
|
2018-12-30 16:42:54 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2019-12-14 05:45:29 +00:00
|
|
|
using Jellyfin.XmlTv;
|
|
|
|
using Jellyfin.XmlTv.Entities;
|
2018-12-30 16:42:54 +00:00
|
|
|
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;
|
2018-12-30 16:42:54 +00:00
|
|
|
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;
|
2018-12-30 16:42:54 +00:00
|
|
|
|
2019-02-09 09:10:33 +00:00
|
|
|
namespace Emby.Server.Implementations.LiveTv.Listings
|
2018-12-30 16:42:54 +00:00
|
|
|
{
|
|
|
|
public class XmlTvListingsProvider : IListingsProvider
|
|
|
|
{
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2020-08-31 18:00:09 +00:00
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
2020-06-06 00:15:56 +00:00
|
|
|
private readonly ILogger<XmlTvListingsProvider> _logger;
|
2018-12-30 16:42:54 +00:00
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
private readonly IZipClient _zipClient;
|
|
|
|
|
2019-07-08 13:14:51 +00:00
|
|
|
public XmlTvListingsProvider(
|
|
|
|
IServerConfigurationManager config,
|
2020-08-31 18:00:09 +00:00
|
|
|
IHttpClientFactory httpClientFactory,
|
2020-03-03 22:07:10 +00:00
|
|
|
ILogger<XmlTvListingsProvider> logger,
|
2019-07-08 13:14:51 +00:00
|
|
|
IFileSystem fileSystem,
|
|
|
|
IZipClient zipClient)
|
2018-12-30 16:42:54 +00:00
|
|
|
{
|
|
|
|
_config = config;
|
2020-08-31 18:00:09 +00:00
|
|
|
_httpClientFactory = httpClientFactory;
|
2018-12-30 16:42:54 +00:00
|
|
|
_logger = logger;
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
_zipClient = zipClient;
|
|
|
|
}
|
|
|
|
|
2019-01-06 20:50:43 +00:00
|
|
|
public string Name => "XmlTV";
|
2018-12-30 16:42:54 +00:00
|
|
|
|
2019-01-06 20:50:43 +00:00
|
|
|
public string Type => "xmltv";
|
2018-12-30 16:42:54 +00:00
|
|
|
|
|
|
|
private string GetLanguage(ListingsProviderInfo info)
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrWhiteSpace(info.PreferredLanguage))
|
|
|
|
{
|
|
|
|
return info.PreferredLanguage;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _config.Configuration.PreferredMetadataLanguage;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<string> GetXml(string path, CancellationToken cancellationToken)
|
|
|
|
{
|
2019-07-08 12:50:15 +00:00
|
|
|
_logger.LogInformation("xmltv path: {Path}", path);
|
2018-12-30 16:42:54 +00:00
|
|
|
|
|
|
|
if (!path.StartsWith("http", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return UnzipIfNeeded(path, path);
|
|
|
|
}
|
|
|
|
|
2018-12-30 19:21:48 +00:00
|
|
|
string cacheFilename = DateTime.UtcNow.DayOfYear.ToString(CultureInfo.InvariantCulture) + "-" + DateTime.UtcNow.Hour.ToString(CultureInfo.InvariantCulture) + ".xml";
|
|
|
|
string cacheFile = Path.Combine(_config.ApplicationPaths.CachePath, "xmltv", cacheFilename);
|
2019-01-26 21:59:53 +00:00
|
|
|
if (File.Exists(cacheFile))
|
2018-12-30 16:42:54 +00:00
|
|
|
{
|
|
|
|
return UnzipIfNeeded(path, cacheFile);
|
|
|
|
}
|
|
|
|
|
2019-07-08 12:50:15 +00:00
|
|
|
_logger.LogInformation("Downloading xmltv listings from {Path}", path);
|
2018-12-30 16:42:54 +00:00
|
|
|
|
2019-01-26 21:08:04 +00:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(cacheFile));
|
2018-12-30 16:42:54 +00:00
|
|
|
|
2020-08-31 18:00:09 +00:00
|
|
|
using var response = await _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(path, cancellationToken).ConfigureAwait(false);
|
2020-11-17 18:43:00 +00:00
|
|
|
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
|
2020-08-31 18:00:09 +00:00
|
|
|
await using (var fileStream = new FileStream(cacheFile, FileMode.CreateNew))
|
2019-07-08 12:50:15 +00:00
|
|
|
{
|
2020-08-31 18:00:09 +00:00
|
|
|
await stream.CopyToAsync(fileStream, cancellationToken).ConfigureAwait(false);
|
2019-07-08 12:50:15 +00:00
|
|
|
}
|
2018-12-30 16:42:54 +00:00
|
|
|
|
|
|
|
return UnzipIfNeeded(path, cacheFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
private string UnzipIfNeeded(string originalUrl, string file)
|
|
|
|
{
|
2018-12-30 19:21:48 +00:00
|
|
|
string ext = Path.GetExtension(originalUrl.Split('?')[0]);
|
2018-12-30 16:42:54 +00:00
|
|
|
|
|
|
|
if (string.Equals(ext, ".gz", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
2018-12-30 16:56:47 +00:00
|
|
|
try
|
2018-12-30 16:42:54 +00:00
|
|
|
{
|
2018-12-30 19:21:48 +00:00
|
|
|
string tempFolder = ExtractGz(file);
|
2018-12-30 16:56:47 +00:00
|
|
|
return FindXmlFile(tempFolder);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2019-07-08 13:14:51 +00:00
|
|
|
_logger.LogError(ex, "Error extracting from gz file {File}", file);
|
2018-12-30 16:56:47 +00:00
|
|
|
}
|
2018-12-30 16:42:54 +00:00
|
|
|
|
2018-12-30 16:56:47 +00:00
|
|
|
try
|
|
|
|
{
|
2018-12-30 19:21:48 +00:00
|
|
|
string tempFolder = ExtractFirstFileFromGz(file);
|
2018-12-30 16:56:47 +00:00
|
|
|
return FindXmlFile(tempFolder);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2019-07-08 13:14:51 +00:00
|
|
|
_logger.LogError(ex, "Error extracting from zip file {File}", file);
|
2018-12-30 16:42:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2018-12-30 16:56:47 +00:00
|
|
|
private string ExtractFirstFileFromGz(string file)
|
|
|
|
{
|
2019-01-26 21:31:59 +00:00
|
|
|
using (var stream = File.OpenRead(file))
|
2018-12-30 16:56:47 +00:00
|
|
|
{
|
2018-12-30 19:21:48 +00:00
|
|
|
string tempFolder = Path.Combine(_config.ApplicationPaths.TempDirectory, Guid.NewGuid().ToString());
|
2019-01-26 21:08:04 +00:00
|
|
|
Directory.CreateDirectory(tempFolder);
|
2018-12-30 16:56:47 +00:00
|
|
|
|
|
|
|
_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))
|
2018-12-30 16:56:47 +00:00
|
|
|
{
|
2018-12-30 19:21:48 +00:00
|
|
|
string tempFolder = Path.Combine(_config.ApplicationPaths.TempDirectory, Guid.NewGuid().ToString());
|
2019-01-26 21:08:04 +00:00
|
|
|
Directory.CreateDirectory(tempFolder);
|
2018-12-30 16:56:47 +00:00
|
|
|
|
|
|
|
_zipClient.ExtractAllFromGz(stream, tempFolder, true);
|
|
|
|
|
|
|
|
return tempFolder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private string FindXmlFile(string directory)
|
|
|
|
{
|
|
|
|
return _fileSystem.GetFiles(directory, true)
|
|
|
|
.Where(i => string.Equals(i.Extension, ".xml", StringComparison.OrdinalIgnoreCase))
|
|
|
|
.Select(i => i.FullName)
|
|
|
|
.FirstOrDefault();
|
|
|
|
}
|
|
|
|
|
2018-12-30 16:42:54 +00:00
|
|
|
public async Task<IEnumerable<ProgramInfo>> GetProgramsAsync(ListingsProviderInfo info, string channelId, DateTime startDateUtc, DateTime endDateUtc, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace(channelId))
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(channelId));
|
2018-12-30 16:42:54 +00:00
|
|
|
}
|
|
|
|
|
2019-07-08 12:50:15 +00:00
|
|
|
_logger.LogDebug("Getting xmltv programs for channel {Id}", channelId);
|
2018-12-30 16:42:54 +00:00
|
|
|
|
2018-12-30 19:21:48 +00:00
|
|
|
string path = await GetXml(info.Path, cancellationToken).ConfigureAwait(false);
|
2019-07-08 12:50:15 +00:00
|
|
|
_logger.LogDebug("Opening XmlTvReader for {Path}", path);
|
2018-12-30 16:42:54 +00:00
|
|
|
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));
|
2018-12-30 16:42:54 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 20:50:43 +00:00
|
|
|
private static ProgramInfo GetProgramInfo(XmlTvProgram program, ListingsProviderInfo info)
|
2018-12-30 16:42:54 +00:00
|
|
|
{
|
2018-12-30 19:21:48 +00:00
|
|
|
string episodeTitle = program.Episode?.Title;
|
2018-12-30 16:42:54 +00:00
|
|
|
|
|
|
|
var programInfo = new ProgramInfo
|
|
|
|
{
|
2018-12-30 19:21:48 +00:00
|
|
|
ChannelId = program.ChannelId,
|
|
|
|
EndDate = program.EndDate.UtcDateTime,
|
|
|
|
EpisodeNumber = program.Episode?.Episode,
|
2018-12-30 16:42:54 +00:00
|
|
|
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,
|
|
|
|
IsKids = program.Categories.Any(c => info.KidsCategories.Contains(c, StringComparer.OrdinalIgnoreCase)),
|
|
|
|
IsMovie = program.Categories.Any(c => info.MovieCategories.Contains(c, StringComparer.OrdinalIgnoreCase)),
|
|
|
|
IsNews = program.Categories.Any(c => info.NewsCategories.Contains(c, StringComparer.OrdinalIgnoreCase)),
|
|
|
|
IsSports = program.Categories.Any(c => info.SportsCategories.Contains(c, StringComparer.OrdinalIgnoreCase)),
|
2019-01-06 20:50:43 +00:00
|
|
|
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,
|
2019-02-28 22:22:57 +00:00
|
|
|
SeriesId = program.Episode == null ? null : program.Title.GetMD5().ToString("N", CultureInfo.InvariantCulture)
|
2018-12-30 16:42:54 +00:00
|
|
|
};
|
|
|
|
|
2018-12-30 19:21:48 +00:00
|
|
|
if (string.IsNullOrWhiteSpace(program.ProgramId))
|
2018-12-30 16:42:54 +00:00
|
|
|
{
|
2018-12-30 19:21:48 +00:00
|
|
|
string uniqueString = (program.Title ?? string.Empty) + (episodeTitle ?? string.Empty) /*+ (p.IceTvEpisodeNumber ?? string.Empty)*/;
|
2018-12-30 16:42:54 +00:00
|
|
|
|
|
|
|
if (programInfo.SeasonNumber.HasValue)
|
|
|
|
{
|
|
|
|
uniqueString = "-" + programInfo.SeasonNumber.Value.ToString(CultureInfo.InvariantCulture);
|
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2018-12-30 16:42:54 +00:00
|
|
|
if (programInfo.EpisodeNumber.HasValue)
|
|
|
|
{
|
|
|
|
uniqueString = "-" + programInfo.EpisodeNumber.Value.ToString(CultureInfo.InvariantCulture);
|
|
|
|
}
|
|
|
|
|
2019-02-28 22:22:57 +00:00
|
|
|
programInfo.ShowId = uniqueString.GetMD5().ToString("N", CultureInfo.InvariantCulture);
|
2018-12-30 16:42:54 +00:00
|
|
|
|
|
|
|
// 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)
|
2018-12-30 16:42:54 +00:00
|
|
|
{
|
2020-08-07 17:26:28 +00:00
|
|
|
programInfo.ShowId += programInfo.StartDate.Ticks.ToString(CultureInfo.InvariantCulture);
|
2018-12-30 16:42:54 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-30 19:21:48 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
programInfo.ShowId = program.ProgramId;
|
|
|
|
}
|
2018-12-30 16:42:54 +00:00
|
|
|
|
|
|
|
// 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);
|
2018-12-30 16:42:54 +00:00
|
|
|
|
|
|
|
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
|
2019-01-26 21:59:53 +00:00
|
|
|
if (!info.Path.StartsWith("http", StringComparison.OrdinalIgnoreCase) && !File.Exists(info.Path))
|
2018-12-30 16:42:54 +00:00
|
|
|
{
|
|
|
|
throw new FileNotFoundException("Could not find the XmlTv file specified:", info.Path);
|
|
|
|
}
|
|
|
|
|
2018-12-30 17:21:49 +00:00
|
|
|
return Task.CompletedTask;
|
2018-12-30 16:42:54 +00: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
|
2018-12-30 19:21:48 +00:00
|
|
|
string path = await GetXml(info.Path, CancellationToken.None).ConfigureAwait(false);
|
2019-07-08 12:50:15 +00:00
|
|
|
_logger.LogDebug("Opening XmlTvReader for {Path}", path);
|
2018-12-30 16:42:54 +00:00
|
|
|
var reader = new XmlTvReader(path, GetLanguage(info));
|
2019-01-19 13:30:23 +00:00
|
|
|
IEnumerable<XmlTvChannel> results = reader.GetChannels();
|
2018-12-30 16:42:54 +00:00
|
|
|
|
|
|
|
// 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
|
2018-12-30 19:21:48 +00:00
|
|
|
string path = await GetXml(info.Path, cancellationToken).ConfigureAwait(false);
|
2019-07-08 12:50:15 +00:00
|
|
|
_logger.LogDebug("Opening XmlTvReader for {Path}", path);
|
2018-12-30 16:42:54 +00:00
|
|
|
var reader = new XmlTvReader(path, GetLanguage(info));
|
2019-01-13 20:37:13 +00:00
|
|
|
var results = reader.GetChannels();
|
2018-12-30 16:42:54 +00:00
|
|
|
|
|
|
|
// Should this method be async?
|
|
|
|
return results.Select(c => new ChannelInfo
|
|
|
|
{
|
|
|
|
Id = c.Id,
|
|
|
|
Name = c.DisplayName,
|
2019-01-06 20:50:43 +00:00
|
|
|
ImageUrl = c.Icon != null && !string.IsNullOrEmpty(c.Icon.Source) ? c.Icon.Source : null,
|
2018-12-30 16:42:54 +00:00
|
|
|
Number = string.IsNullOrWhiteSpace(c.Number) ? c.Id : c.Number
|
|
|
|
}).ToList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|