| | | 1 | | #pragma warning disable CS1591 |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Globalization; |
| | | 6 | | using System.IO; |
| | | 7 | | using System.IO.Compression; |
| | | 8 | | using System.Linq; |
| | | 9 | | using System.Net.Http; |
| | | 10 | | using System.Threading; |
| | | 11 | | using System.Threading.Tasks; |
| | | 12 | | using Jellyfin.Extensions; |
| | | 13 | | using Jellyfin.XmlTv; |
| | | 14 | | using Jellyfin.XmlTv.Entities; |
| | | 15 | | using MediaBrowser.Common.Extensions; |
| | | 16 | | using MediaBrowser.Common.Net; |
| | | 17 | | using MediaBrowser.Controller.Configuration; |
| | | 18 | | using MediaBrowser.Controller.LiveTv; |
| | | 19 | | using MediaBrowser.Model.Dto; |
| | | 20 | | using MediaBrowser.Model.IO; |
| | | 21 | | using MediaBrowser.Model.LiveTv; |
| | | 22 | | using Microsoft.Extensions.Logging; |
| | | 23 | | |
| | | 24 | | namespace Jellyfin.LiveTv.Listings |
| | | 25 | | { |
| | | 26 | | public class XmlTvListingsProvider : IListingsProvider |
| | | 27 | | { |
| | 0 | 28 | | private static readonly TimeSpan _maxCacheAge = TimeSpan.FromHours(1); |
| | | 29 | | |
| | | 30 | | private readonly IServerConfigurationManager _config; |
| | | 31 | | private readonly IHttpClientFactory _httpClientFactory; |
| | | 32 | | private readonly ILogger<XmlTvListingsProvider> _logger; |
| | | 33 | | |
| | | 34 | | public XmlTvListingsProvider( |
| | | 35 | | IServerConfigurationManager config, |
| | | 36 | | IHttpClientFactory httpClientFactory, |
| | | 37 | | ILogger<XmlTvListingsProvider> logger) |
| | | 38 | | { |
| | 25 | 39 | | _config = config; |
| | 25 | 40 | | _httpClientFactory = httpClientFactory; |
| | 25 | 41 | | _logger = logger; |
| | 25 | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | public string Name => "XmlTV"; |
| | | 45 | | |
| | 0 | 46 | | public string Type => "xmltv"; |
| | | 47 | | |
| | | 48 | | private string GetLanguage(ListingsProviderInfo info) |
| | | 49 | | { |
| | 4 | 50 | | if (!string.IsNullOrWhiteSpace(info.PreferredLanguage)) |
| | | 51 | | { |
| | 0 | 52 | | return info.PreferredLanguage; |
| | | 53 | | } |
| | | 54 | | |
| | 4 | 55 | | return _config.Configuration.PreferredMetadataLanguage; |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | private async Task<string> GetXml(ListingsProviderInfo info, CancellationToken cancellationToken) |
| | | 59 | | { |
| | 4 | 60 | | _logger.LogInformation("xmltv path: {Path}", info.Path); |
| | | 61 | | |
| | 4 | 62 | | string cacheFilename = info.Id + ".xml"; |
| | 4 | 63 | | string cacheDir = Path.Join(_config.ApplicationPaths.CachePath, "xmltv"); |
| | 4 | 64 | | string cacheFile = Path.Join(cacheDir, cacheFilename); |
| | | 65 | | |
| | 4 | 66 | | if (File.Exists(cacheFile)) |
| | | 67 | | { |
| | 0 | 68 | | if (File.GetLastWriteTimeUtc(cacheFile) >= DateTime.UtcNow.Subtract(_maxCacheAge)) |
| | | 69 | | { |
| | 0 | 70 | | return cacheFile; |
| | | 71 | | } |
| | | 72 | | |
| | 0 | 73 | | File.Delete(cacheFile); |
| | | 74 | | } |
| | | 75 | | else |
| | | 76 | | { |
| | 4 | 77 | | Directory.CreateDirectory(cacheDir); |
| | | 78 | | } |
| | | 79 | | |
| | 4 | 80 | | if (info.Path.StartsWith("http", StringComparison.OrdinalIgnoreCase)) |
| | | 81 | | { |
| | 2 | 82 | | _logger.LogInformation("Downloading xmltv listings from {Path}", info.Path); |
| | | 83 | | |
| | 2 | 84 | | using var response = await _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(info.Path, canc |
| | 2 | 85 | | var redirectedUrl = response.RequestMessage?.RequestUri?.ToString() ?? info.Path; |
| | 2 | 86 | | var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false); |
| | 2 | 87 | | await using (stream.ConfigureAwait(false)) |
| | | 88 | | { |
| | 2 | 89 | | return await UnzipIfNeededAndCopy(redirectedUrl, stream, cacheFile, cancellationToken).ConfigureAwai |
| | | 90 | | } |
| | 0 | 91 | | } |
| | | 92 | | else |
| | | 93 | | { |
| | 2 | 94 | | var stream = AsyncFile.OpenRead(info.Path); |
| | 2 | 95 | | await using (stream.ConfigureAwait(false)) |
| | | 96 | | { |
| | 2 | 97 | | return await UnzipIfNeededAndCopy(info.Path, stream, cacheFile, cancellationToken).ConfigureAwait(fa |
| | | 98 | | } |
| | | 99 | | } |
| | 4 | 100 | | } |
| | | 101 | | |
| | | 102 | | private async Task<string> UnzipIfNeededAndCopy(string originalUrl, Stream stream, string file, CancellationToke |
| | | 103 | | { |
| | 4 | 104 | | var fileStream = new FileStream( |
| | 4 | 105 | | file, |
| | 4 | 106 | | FileMode.CreateNew, |
| | 4 | 107 | | FileAccess.Write, |
| | 4 | 108 | | FileShare.None, |
| | 4 | 109 | | IODefaults.FileStreamBufferSize, |
| | 4 | 110 | | FileOptions.Asynchronous); |
| | | 111 | | |
| | 4 | 112 | | await using (fileStream.ConfigureAwait(false)) |
| | | 113 | | { |
| | 4 | 114 | | if (Path.GetExtension(originalUrl.AsSpan().LeftPart('?')).Equals(".gz", StringComparison.OrdinalIgnoreCa |
| | 4 | 115 | | Path.GetExtension(originalUrl.AsSpan().LeftPart('?')).Equals(".gzip", StringComparison.OrdinalIgnore |
| | | 116 | | { |
| | | 117 | | try |
| | | 118 | | { |
| | 0 | 119 | | using var reader = new GZipStream(stream, CompressionMode.Decompress); |
| | 0 | 120 | | await reader.CopyToAsync(fileStream, cancellationToken).ConfigureAwait(false); |
| | 0 | 121 | | } |
| | 0 | 122 | | catch (Exception ex) |
| | | 123 | | { |
| | 0 | 124 | | _logger.LogError(ex, "Error extracting from gz file {File}", originalUrl); |
| | 0 | 125 | | } |
| | | 126 | | } |
| | | 127 | | else |
| | | 128 | | { |
| | 4 | 129 | | await stream.CopyToAsync(fileStream, cancellationToken).ConfigureAwait(false); |
| | | 130 | | } |
| | | 131 | | |
| | 4 | 132 | | return file; |
| | | 133 | | } |
| | 4 | 134 | | } |
| | | 135 | | |
| | | 136 | | public async Task<IEnumerable<ProgramInfo>> GetProgramsAsync(ListingsProviderInfo info, string channelId, DateTi |
| | | 137 | | { |
| | 4 | 138 | | if (string.IsNullOrWhiteSpace(channelId)) |
| | | 139 | | { |
| | 0 | 140 | | throw new ArgumentNullException(nameof(channelId)); |
| | | 141 | | } |
| | | 142 | | |
| | 4 | 143 | | _logger.LogDebug("Getting xmltv programs for channel {Id}", channelId); |
| | | 144 | | |
| | 4 | 145 | | string path = await GetXml(info, cancellationToken).ConfigureAwait(false); |
| | 4 | 146 | | _logger.LogDebug("Opening XmlTvReader for {Path}", path); |
| | 4 | 147 | | var reader = new XmlTvReader(path, GetLanguage(info)); |
| | | 148 | | |
| | 4 | 149 | | return reader.GetProgrammes(channelId, startDateUtc, endDateUtc, cancellationToken) |
| | 4 | 150 | | .Select(p => GetProgramInfo(p, info)); |
| | 4 | 151 | | } |
| | | 152 | | |
| | | 153 | | private static ProgramInfo GetProgramInfo(XmlTvProgram program, ListingsProviderInfo info) |
| | | 154 | | { |
| | 4 | 155 | | string? episodeTitle = program.Episode?.Title; |
| | 4 | 156 | | var programCategories = program.Categories.Where(c => !string.IsNullOrWhiteSpace(c)).ToList(); |
| | 4 | 157 | | var imageUrl = program.Icons.FirstOrDefault()?.Source; |
| | 4 | 158 | | var rating = program.Ratings.FirstOrDefault()?.Value; |
| | 4 | 159 | | var starRating = program.StarRatings?.FirstOrDefault()?.StarRating; |
| | | 160 | | |
| | 4 | 161 | | var programInfo = new ProgramInfo |
| | 4 | 162 | | { |
| | 4 | 163 | | ChannelId = program.ChannelId, |
| | 4 | 164 | | EndDate = program.EndDate.UtcDateTime, |
| | 4 | 165 | | EpisodeNumber = program.Episode?.Episode, |
| | 4 | 166 | | EpisodeTitle = episodeTitle, |
| | 4 | 167 | | Genres = programCategories, |
| | 4 | 168 | | StartDate = program.StartDate.UtcDateTime, |
| | 4 | 169 | | Name = program.Title, |
| | 4 | 170 | | Overview = program.Description, |
| | 4 | 171 | | ProductionYear = program.CopyrightDate?.Year, |
| | 4 | 172 | | SeasonNumber = program.Episode?.Series, |
| | 4 | 173 | | IsSeries = program.Episode?.Episode is not null, |
| | 4 | 174 | | IsRepeat = program.IsPreviouslyShown && !program.IsNew, |
| | 4 | 175 | | IsPremiere = program.Premiere is not null, |
| | 4 | 176 | | IsLive = program.IsLive, |
| | 4 | 177 | | IsKids = programCategories.Any(c => info.KidsCategories.Contains(c, StringComparison.OrdinalIgnoreCase)) |
| | 4 | 178 | | IsMovie = programCategories.Any(c => info.MovieCategories.Contains(c, StringComparison.OrdinalIgnoreCase |
| | 4 | 179 | | IsNews = programCategories.Any(c => info.NewsCategories.Contains(c, StringComparison.OrdinalIgnoreCase)) |
| | 4 | 180 | | IsSports = programCategories.Any(c => info.SportsCategories.Contains(c, StringComparison.OrdinalIgnoreCa |
| | 4 | 181 | | ImageUrl = string.IsNullOrEmpty(imageUrl) ? null : imageUrl, |
| | 4 | 182 | | HasImage = !string.IsNullOrEmpty(imageUrl), |
| | 4 | 183 | | OfficialRating = string.IsNullOrEmpty(rating) ? null : rating, |
| | 4 | 184 | | CommunityRating = starRating is null ? null : (float)starRating.Value, |
| | 4 | 185 | | SeriesId = program.Episode?.Episode is null ? null : program.Title?.GetMD5().ToString("N", CultureInfo.I |
| | 4 | 186 | | }; |
| | | 187 | | |
| | 4 | 188 | | if (string.IsNullOrWhiteSpace(program.ProgramId)) |
| | | 189 | | { |
| | 4 | 190 | | string uniqueString = (program.Title ?? string.Empty) + (episodeTitle ?? string.Empty); |
| | | 191 | | |
| | 4 | 192 | | if (programInfo.SeasonNumber.HasValue) |
| | | 193 | | { |
| | 0 | 194 | | uniqueString = "-" + programInfo.SeasonNumber.Value.ToString(CultureInfo.InvariantCulture); |
| | | 195 | | } |
| | | 196 | | |
| | 4 | 197 | | if (programInfo.EpisodeNumber.HasValue) |
| | | 198 | | { |
| | 0 | 199 | | uniqueString = "-" + programInfo.EpisodeNumber.Value.ToString(CultureInfo.InvariantCulture); |
| | | 200 | | } |
| | | 201 | | |
| | 4 | 202 | | programInfo.ShowId = uniqueString.GetMD5().ToString("N", CultureInfo.InvariantCulture); |
| | | 203 | | |
| | | 204 | | // If we don't have valid episode info, assume it's a unique program, otherwise recordings might be skip |
| | 4 | 205 | | if (programInfo.IsSeries |
| | 4 | 206 | | && !programInfo.IsRepeat |
| | 4 | 207 | | && (programInfo.EpisodeNumber ?? 0) == 0) |
| | | 208 | | { |
| | 0 | 209 | | programInfo.ShowId += programInfo.StartDate.Ticks.ToString(CultureInfo.InvariantCulture); |
| | | 210 | | } |
| | | 211 | | } |
| | | 212 | | else |
| | | 213 | | { |
| | 0 | 214 | | programInfo.ShowId = program.ProgramId; |
| | | 215 | | } |
| | | 216 | | |
| | | 217 | | // Construct an id from the channel and start date |
| | 4 | 218 | | programInfo.Id = string.Format(CultureInfo.InvariantCulture, "{0}_{1:O}", program.ChannelId, program.StartDa |
| | | 219 | | |
| | 4 | 220 | | if (programInfo.IsMovie) |
| | | 221 | | { |
| | 0 | 222 | | programInfo.IsSeries = false; |
| | 0 | 223 | | programInfo.EpisodeNumber = null; |
| | 0 | 224 | | programInfo.EpisodeTitle = null; |
| | | 225 | | } |
| | | 226 | | |
| | 4 | 227 | | return programInfo; |
| | | 228 | | } |
| | | 229 | | |
| | | 230 | | public Task Validate(ListingsProviderInfo info, bool validateLogin, bool validateListings) |
| | | 231 | | { |
| | | 232 | | // Assume all urls are valid. check files for existence |
| | 0 | 233 | | if (!info.Path.StartsWith("http", StringComparison.OrdinalIgnoreCase) && !File.Exists(info.Path)) |
| | | 234 | | { |
| | 0 | 235 | | throw new FileNotFoundException("Could not find the XmlTv file specified:", info.Path); |
| | | 236 | | } |
| | | 237 | | |
| | 0 | 238 | | return Task.CompletedTask; |
| | | 239 | | } |
| | | 240 | | |
| | | 241 | | public async Task<List<NameIdPair>> GetLineups(ListingsProviderInfo info, string country, string location) |
| | | 242 | | { |
| | | 243 | | // In theory this should never be called because there is always only one lineup |
| | 0 | 244 | | string path = await GetXml(info, CancellationToken.None).ConfigureAwait(false); |
| | 0 | 245 | | _logger.LogDebug("Opening XmlTvReader for {Path}", path); |
| | 0 | 246 | | var reader = new XmlTvReader(path, GetLanguage(info)); |
| | 0 | 247 | | IEnumerable<XmlTvChannel> results = reader.GetChannels(); |
| | | 248 | | |
| | | 249 | | // Should this method be async? |
| | 0 | 250 | | return results.Select(c => new NameIdPair() { Id = c.Id, Name = c.DisplayName }).ToList(); |
| | 0 | 251 | | } |
| | | 252 | | |
| | | 253 | | public async Task<List<ChannelInfo>> GetChannels(ListingsProviderInfo info, CancellationToken cancellationToken) |
| | | 254 | | { |
| | | 255 | | // In theory this should never be called because there is always only one lineup |
| | 0 | 256 | | string path = await GetXml(info, cancellationToken).ConfigureAwait(false); |
| | 0 | 257 | | _logger.LogDebug("Opening XmlTvReader for {Path}", path); |
| | 0 | 258 | | var reader = new XmlTvReader(path, GetLanguage(info)); |
| | 0 | 259 | | var results = reader.GetChannels(); |
| | | 260 | | |
| | | 261 | | // Should this method be async? |
| | 0 | 262 | | return results.Select(c => new ChannelInfo |
| | 0 | 263 | | { |
| | 0 | 264 | | Id = c.Id, |
| | 0 | 265 | | Name = c.DisplayName, |
| | 0 | 266 | | ImageUrl = string.IsNullOrEmpty(c.Icons.FirstOrDefault()?.Source) ? null : c.Icons.FirstOrDefault()!.Sou |
| | 0 | 267 | | Number = string.IsNullOrWhiteSpace(c.Number) ? c.Id : c.Number |
| | 0 | 268 | | }).ToList(); |
| | 0 | 269 | | } |
| | | 270 | | } |
| | | 271 | | } |