using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.Json; using System.Threading; using System.Threading.Tasks; using Jellyfin.Extensions.Json; using MediaBrowser.Common.Configuration; using Microsoft.Extensions.Logging; namespace Jellyfin.LiveTv.Listings; /// /// Caches one station's schedule for one day, keyed by the md5 Schedules Direct reports for it. /// /// /// The API requires clients to ask /schedules/md5 what changed and re-download only that. A day /// whose md5 still matches needs no /schedules, /programs or /metadata/programs request at all, /// which is what keeps a guide refresh inside the account's daily quotas. /// public class SchedulesDirectScheduleCache { private readonly ILogger _logger; private readonly string _cacheRoot; private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options; /// /// Initializes a new instance of the class. /// /// The logger. /// The application paths. public SchedulesDirectScheduleCache(ILogger logger, IApplicationPaths appPaths) { _logger = logger; _cacheRoot = Path.Combine(appPaths.CachePath, "schedulesdirect", "schedules"); } /// /// Reads the cached day if it was stored under the same md5. /// /// The station id. /// The date, formatted yyyy-MM-dd. /// The md5 Schedules Direct currently reports, or null if unknown. /// The cancellation token. /// The cached entry, or null when it is missing or stale. public async Task GetAsync(string stationId, string date, string? md5, CancellationToken cancellationToken) { if (string.IsNullOrEmpty(md5)) { return null; } var cached = await ReadAsync(stationId, date, cancellationToken).ConfigureAwait(false); return string.Equals(cached?.Md5, md5, StringComparison.Ordinal) ? cached : null; } /// /// Reads the cached day whatever md5 it was stored under, to stand in for a day that could not /// be downloaded. /// /// The station id. /// The date, formatted yyyy-MM-dd. /// The cancellation token. /// The cached entry, or null when there is none. public Task GetLatestAsync(string stationId, string date, CancellationToken cancellationToken) => ReadAsync(stationId, date, cancellationToken); /// /// Stores a freshly downloaded day. /// /// The station id. /// The date, formatted yyyy-MM-dd. /// The day to store. /// The cancellation token. /// A task representing the write. public async Task SetAsync(string stationId, string date, SchedulesDirectCachedDay day, CancellationToken cancellationToken) { if (string.IsNullOrEmpty(day.Md5)) { // Without an md5 there is nothing to validate the entry against later. return; } var path = GetPath(stationId, date); try { Directory.CreateDirectory(Path.GetDirectoryName(path)!); var stream = File.Create(path); await using (stream.ConfigureAwait(false)) { await JsonSerializer.SerializeAsync(stream, day, _jsonOptions, cancellationToken).ConfigureAwait(false); } } catch (Exception ex) when (ex is IOException or JsonException or UnauthorizedAccessException) { _logger.LogWarning(ex, "Failed to cache Schedules Direct schedule {Path}", path); } } /// /// Deletes cached days that are no longer requested, so the cache cannot grow without bound. /// /// The station id. /// The dates to keep. public void Prune(string stationId, IEnumerable keepDates) { var directory = Path.Combine(_cacheRoot, GetSafeName(stationId)); if (!Directory.Exists(directory)) { return; } var keep = new HashSet(keepDates.Select(GetSafeName), StringComparer.Ordinal); try { foreach (var file in Directory.EnumerateFiles(directory, "*.json")) { if (!keep.Contains(Path.GetFileNameWithoutExtension(file))) { File.Delete(file); } } } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) { _logger.LogDebug(ex, "Failed to prune Schedules Direct cache in {Directory}", directory); } } /// /// Deletes entries that have not been touched for a while, so that stations which have left /// the lineup do not keep their cache forever. /// /// The age past which an untouched entry is deleted. public void PruneStale(TimeSpan maxAge) { if (!Directory.Exists(_cacheRoot)) { return; } var cutoff = DateTime.UtcNow - maxAge; try { foreach (var directory in Directory.EnumerateDirectories(_cacheRoot)) { foreach (var file in Directory.EnumerateFiles(directory, "*.json")) { if (File.GetLastWriteTimeUtc(file) < cutoff) { File.Delete(file); } } if (!Directory.EnumerateFileSystemEntries(directory).Any()) { Directory.Delete(directory); } } } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) { _logger.LogDebug(ex, "Failed to prune stale Schedules Direct cache entries in {Directory}", _cacheRoot); } } private async Task ReadAsync(string stationId, string date, CancellationToken cancellationToken) { var path = GetPath(stationId, date); if (!File.Exists(path)) { return null; } try { var stream = File.OpenRead(path); await using (stream.ConfigureAwait(false)) { return await JsonSerializer.DeserializeAsync(stream, _jsonOptions, cancellationToken).ConfigureAwait(false); } } catch (Exception ex) when (ex is IOException or JsonException or UnauthorizedAccessException) { _logger.LogDebug(ex, "Discarding unreadable Schedules Direct cache entry {Path}", path); return null; } } private static string GetSafeName(string value) => string.Create(value.Length, value, (span, source) => { for (var i = 0; i < source.Length; i++) { span[i] = char.IsAsciiLetterOrDigit(source[i]) ? source[i] : '_'; } }); private string GetPath(string stationId, string date) => Path.Combine(_cacheRoot, GetSafeName(stationId), GetSafeName(date) + ".json"); }