mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-11-21 15:22:34 +00:00
Merge pull request #18048 from Shadowghost/fix-schedules-direct
Follow the Schedules Direct API contract when refreshing the guide
This commit is contained in:
commit
85fa90cb75
28 changed files with 3146 additions and 353 deletions
|
|
@ -3750,6 +3750,7 @@ namespace Emby.Server.Implementations.Library
|
|||
|
||||
public async Task<ItemImageInfo> ConvertImageToLocal(BaseItem item, ItemImageInfo image, int imageIndex, bool removeOnFailure)
|
||||
{
|
||||
HttpRequestException? lastHttpException = null;
|
||||
foreach (var url in image.Path.Split('|'))
|
||||
{
|
||||
try
|
||||
|
|
@ -3781,6 +3782,7 @@ namespace Emby.Server.Implementations.Library
|
|||
&& (ex.StatusCode.Value == HttpStatusCode.NotFound || ex.StatusCode.Value == HttpStatusCode.Forbidden))
|
||||
{
|
||||
_logger.LogDebug(ex, "Error downloading image {Url}", url);
|
||||
lastHttpException = ex;
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
@ -3795,13 +3797,17 @@ namespace Emby.Server.Implementations.Library
|
|||
await item.UpdateToRepositoryAsync(ItemUpdateType.ImageUpdate, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
throw new InvalidOperationException(string.Format(
|
||||
CultureInfo.InvariantCulture,
|
||||
"Unable to convert any {0} image url in \"{1}\" to a local file for {2} ({3})",
|
||||
image.Type,
|
||||
image.Path,
|
||||
item.Name,
|
||||
item.Id));
|
||||
// Keep the rejection as the inner exception: callers such as the Live TV guide need the
|
||||
// status code to tell a missing image from a provider that is refusing us.
|
||||
throw new InvalidOperationException(
|
||||
string.Format(
|
||||
CultureInfo.InvariantCulture,
|
||||
"Unable to convert any {0} image url in \"{1}\" to a local file for {2} ({3})",
|
||||
image.Type,
|
||||
image.Path,
|
||||
item.Name,
|
||||
item.Id),
|
||||
lastHttpException);
|
||||
}
|
||||
|
||||
public async Task AddVirtualFolder(string name, CollectionTypeOptions? collectionType, LibraryOptions options, bool refreshLibrary)
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ using MediaBrowser.Controller.Configuration;
|
|||
using MediaBrowser.Controller.Drawing;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Controller.LiveTv;
|
||||
using MediaBrowser.Controller.Providers;
|
||||
using MediaBrowser.Model.Branding;
|
||||
using MediaBrowser.Model.Drawing;
|
||||
|
|
@ -50,6 +51,7 @@ public class ImageController : BaseJellyfinApiController
|
|||
private readonly ILogger<ImageController> _logger;
|
||||
private readonly IServerConfigurationManager _serverConfigurationManager;
|
||||
private readonly IApplicationPaths _appPaths;
|
||||
private readonly ISchedulesDirectService _schedulesDirectService;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ImageController"/> class.
|
||||
|
|
@ -62,6 +64,7 @@ public class ImageController : BaseJellyfinApiController
|
|||
/// <param name="logger">Instance of the <see cref="ILogger{ImageController}"/> interface.</param>
|
||||
/// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
|
||||
/// <param name="appPaths">Instance of the <see cref="IApplicationPaths"/> interface.</param>
|
||||
/// <param name="schedulesDirectService">Instance of the <see cref="ISchedulesDirectService"/> interface.</param>
|
||||
public ImageController(
|
||||
IUserManager userManager,
|
||||
ILibraryManager libraryManager,
|
||||
|
|
@ -70,7 +73,8 @@ public class ImageController : BaseJellyfinApiController
|
|||
IFileSystem fileSystem,
|
||||
ILogger<ImageController> logger,
|
||||
IServerConfigurationManager serverConfigurationManager,
|
||||
IApplicationPaths appPaths)
|
||||
IApplicationPaths appPaths,
|
||||
ISchedulesDirectService schedulesDirectService)
|
||||
{
|
||||
_userManager = userManager;
|
||||
_libraryManager = libraryManager;
|
||||
|
|
@ -80,6 +84,7 @@ public class ImageController : BaseJellyfinApiController
|
|||
_logger = logger;
|
||||
_serverConfigurationManager = serverConfigurationManager;
|
||||
_appPaths = appPaths;
|
||||
_schedulesDirectService = schedulesDirectService;
|
||||
}
|
||||
|
||||
private static CryptoStream GetFromBase64Stream(Stream inputStream)
|
||||
|
|
@ -1893,7 +1898,13 @@ public class ImageController : BaseJellyfinApiController
|
|||
|
||||
if (!imageInfo.IsLocalFile && item is not null)
|
||||
{
|
||||
imageInfo = await _libraryManager.ConvertImageToLocal(item, imageInfo, imageIndex ?? 0).ConfigureAwait(false);
|
||||
var localImage = await _schedulesDirectService.ConvertImageToLocal(item, imageInfo, imageIndex ?? 0).ConfigureAwait(false);
|
||||
if (localImage is null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
imageInfo = localImage;
|
||||
}
|
||||
|
||||
var options = new ImageProcessingOptions
|
||||
|
|
|
|||
31
MediaBrowser.Common/Net/HttpResponseBodyException.cs
Normal file
31
MediaBrowser.Common/Net/HttpResponseBodyException.cs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
using System.Net;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace MediaBrowser.Common.Net;
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="HttpRequestException"/> that retains the body of the failed response.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Providers report why they refused a request in the response body, which
|
||||
/// <see cref="HttpResponseMessage.EnsureSuccessStatusCode"/> discards.
|
||||
/// </remarks>
|
||||
public class HttpResponseBodyException : HttpRequestException
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="HttpResponseBodyException"/> class.
|
||||
/// </summary>
|
||||
/// <param name="message">The exception message.</param>
|
||||
/// <param name="statusCode">The status code of the failed response.</param>
|
||||
/// <param name="responseBody">The body of the failed response.</param>
|
||||
public HttpResponseBodyException(string message, HttpStatusCode? statusCode, string? responseBody)
|
||||
: base(message, null, statusCode)
|
||||
{
|
||||
ResponseBody = responseBody;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the body of the failed response.
|
||||
/// </summary>
|
||||
public string? ResponseBody { get; }
|
||||
}
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
|
||||
namespace MediaBrowser.Controller.LiveTv;
|
||||
|
||||
|
|
@ -17,15 +19,36 @@ public interface ISchedulesDirectService
|
|||
Task<Stream> GetAvailableCountries(CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the Schedules Direct daily image download limit is currently active.
|
||||
/// Gets a value indicating whether an image may be downloaded from the given url.
|
||||
/// </summary>
|
||||
/// <returns><c>true</c> if the image limit has been hit and has not yet reset; otherwise <c>false</c>.</returns>
|
||||
bool IsImageDailyLimitActive();
|
||||
/// <param name="imageUrl">The image url.</param>
|
||||
/// <returns><c>false</c> for a Schedules Direct url while the daily image limit is active; otherwise <c>true</c>.</returns>
|
||||
bool CanDownloadImage(string imageUrl);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the Schedules Direct service is available.
|
||||
/// Returns <c>false</c> if a permanent account error has occurred or a transient backoff is active.
|
||||
/// Reports a successful image download, so that an unexplained failure streak is reset.
|
||||
/// </summary>
|
||||
/// <returns><c>true</c> if the service can accept requests; otherwise <c>false</c>.</returns>
|
||||
bool IsServiceAvailable();
|
||||
/// <param name="imageUrl">The image url that was downloaded.</param>
|
||||
void ReportImageDownloadSuccess(string imageUrl);
|
||||
|
||||
/// <summary>
|
||||
/// Reports a failed image download. Schedules Direct answers an exhausted image quota with
|
||||
/// an HTTP 200 JSON error body rather than a failure status, so the body is what decides
|
||||
/// whether image acquisition has to stop.
|
||||
/// </summary>
|
||||
/// <param name="imageUrl">The image url that failed.</param>
|
||||
/// <param name="statusCode">The HTTP status code of the failure, if any.</param>
|
||||
/// <param name="responseBody">The body of the failed response, if it was readable.</param>
|
||||
/// <returns>What the caller must do with the image.</returns>
|
||||
ImageDownloadFailureAction ReportImageDownloadFailure(string imageUrl, HttpStatusCode? statusCode, string? responseBody);
|
||||
|
||||
/// <summary>
|
||||
/// Downloads a remote image unless Schedules Direct has blocked it, and reports the outcome.
|
||||
/// Schedules Direct counts every image request, so downloads outside the guide refresh go through here.
|
||||
/// </summary>
|
||||
/// <param name="item">The item the image belongs to.</param>
|
||||
/// <param name="image">The remote image.</param>
|
||||
/// <param name="imageIndex">The image index.</param>
|
||||
/// <returns>The local image, or <c>null</c> when the download is blocked.</returns>
|
||||
Task<ItemImageInfo?> ConvertImageToLocal(BaseItem item, ItemImageInfo image, int imageIndex);
|
||||
}
|
||||
|
|
|
|||
17
MediaBrowser.Controller/LiveTv/ImageDownloadFailureAction.cs
Normal file
17
MediaBrowser.Controller/LiveTv/ImageDownloadFailureAction.cs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
namespace MediaBrowser.Controller.LiveTv;
|
||||
|
||||
/// <summary>
|
||||
/// What the caller must do with an image after a failed download.
|
||||
/// </summary>
|
||||
public enum ImageDownloadFailureAction
|
||||
{
|
||||
/// <summary>
|
||||
/// Keep the image; the download may succeed on a later attempt.
|
||||
/// </summary>
|
||||
None = 0,
|
||||
|
||||
/// <summary>
|
||||
/// Drop the image. The url is permanently invalid and re-requesting it gets the account blocked.
|
||||
/// </summary>
|
||||
RemoveImage = 1
|
||||
}
|
||||
|
|
@ -212,7 +212,16 @@ namespace MediaBrowser.Providers.Manager
|
|||
var httpClient = _httpClientFactory.CreateClient(NamedClient.Default);
|
||||
using var response = await httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
response.EnsureSuccessStatusCode();
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
// Keep the body: providers report why they refused in it, and a caller that
|
||||
// only sees the status code cannot tell a missing image from an exhausted quota.
|
||||
var errorBody = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
|
||||
throw new HttpResponseBodyException(
|
||||
$"Response status code does not indicate success: {(int)response.StatusCode} ({response.ReasonPhrase}).",
|
||||
response.StatusCode,
|
||||
errorBody);
|
||||
}
|
||||
|
||||
var contentType = response.Content.Headers.ContentType?.MediaType;
|
||||
|
||||
|
|
@ -236,7 +245,17 @@ namespace MediaBrowser.Providers.Manager
|
|||
|
||||
if (!contentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
throw new HttpRequestException($"Request returned '{contentType}' instead of an image type", null, HttpStatusCode.NotFound);
|
||||
// A provider can answer 200 with a JSON error instead of the image; Schedules
|
||||
// Direct reports an exhausted image quota that way, so keep the body.
|
||||
var errorBody = contentType.Contains("json", StringComparison.OrdinalIgnoreCase)
|
||||
|| contentType.StartsWith("text/", StringComparison.OrdinalIgnoreCase)
|
||||
? await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false)
|
||||
: null;
|
||||
|
||||
throw new HttpResponseBodyException(
|
||||
$"Request returned '{contentType}' instead of an image type",
|
||||
HttpStatusCode.NotFound,
|
||||
errorBody);
|
||||
}
|
||||
|
||||
var responseBytes = await response.Content.ReadAsByteArrayAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Data.Enums;
|
||||
|
|
@ -8,6 +9,7 @@ using Jellyfin.Extensions;
|
|||
using Jellyfin.LiveTv.Configuration;
|
||||
using Jellyfin.LiveTv.Listings;
|
||||
using MediaBrowser.Common.Configuration;
|
||||
using MediaBrowser.Common.Net;
|
||||
using MediaBrowser.Controller.Dto;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Library;
|
||||
|
|
@ -747,26 +749,17 @@ public class GuideManager : IGuideManager
|
|||
return false;
|
||||
}
|
||||
|
||||
private async Task PreCacheImages(IReadOnlyList<BaseItem> programs, DateTime maxCacheDate)
|
||||
internal async Task PreCacheImages(IReadOnlyList<BaseItem> programs, DateTime maxCacheDate)
|
||||
{
|
||||
var sdLimitActive = _schedulesDirectService.IsImageDailyLimitActive();
|
||||
|
||||
await Parallel.ForEachAsync(
|
||||
programs
|
||||
.Where(p => p.EndDate.HasValue && p.EndDate.Value < maxCacheDate)
|
||||
.Where(p => !sdLimitActive || !p.ImageInfos.All(
|
||||
img => img.IsLocalFile || img.Path.Contains("schedulesdirect", StringComparison.OrdinalIgnoreCase)))
|
||||
.Where(p => p.ImageInfos.Any(img => !img.IsLocalFile))
|
||||
.DistinctBy(p => p.Id),
|
||||
_cacheParallelOptions,
|
||||
async (program, cancellationToken) =>
|
||||
{
|
||||
// Re-check: limit may have been set by a parallel task since the LINQ filter ran.
|
||||
if (_schedulesDirectService.IsImageDailyLimitActive()
|
||||
&& program.ImageInfos.All(
|
||||
img => img.IsLocalFile || img.Path.Contains("schedulesdirect", StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
return;
|
||||
}
|
||||
List<ItemImageInfo>? deadImages = null;
|
||||
|
||||
for (var i = 0; i < program.ImageInfos.Length; i++)
|
||||
{
|
||||
|
|
@ -781,10 +774,12 @@ public class GuideManager : IGuideManager
|
|||
continue;
|
||||
}
|
||||
|
||||
// Skip SD downloads once the daily limit has been hit.
|
||||
if (imageInfo.Path.Contains("schedulesdirect", StringComparison.OrdinalIgnoreCase)
|
||||
&& _schedulesDirectService.IsImageDailyLimitActive())
|
||||
// Re-checked per image: the limit may have been hit by a parallel task. A blocked
|
||||
// url left on the program would still be fetched when a client asks for it.
|
||||
if (!_schedulesDirectService.CanDownloadImage(imageInfo.Path))
|
||||
{
|
||||
deadImages ??= [];
|
||||
deadImages.Add(imageInfo);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
@ -797,12 +792,35 @@ public class GuideManager : IGuideManager
|
|||
imageIndex: 0,
|
||||
removeOnFailure: false)
|
||||
.ConfigureAwait(false);
|
||||
_schedulesDirectService.ReportImageDownloadSuccess(imageInfo.Path);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// A rejected download never surfaces as an error on its own, so feed the
|
||||
// response back to the provider to stop us hammering an exhausted account.
|
||||
var httpException = (ex as HttpRequestException) ?? ex.InnerException as HttpRequestException;
|
||||
var action = _schedulesDirectService.ReportImageDownloadFailure(
|
||||
imageInfo.Path,
|
||||
httpException?.StatusCode,
|
||||
(httpException as HttpResponseBodyException)?.ResponseBody);
|
||||
|
||||
if (action == ImageDownloadFailureAction.RemoveImage)
|
||||
{
|
||||
deadImages ??= [];
|
||||
deadImages.Add(imageInfo);
|
||||
}
|
||||
|
||||
_logger.LogWarning(ex, "Unable to pre-cache {Url}", imageInfo.Path);
|
||||
}
|
||||
}
|
||||
|
||||
if (deadImages is not null)
|
||||
{
|
||||
// Requesting a url Schedules Direct has already rejected is what gets an
|
||||
// account blocked, so it must not survive to the next refresh.
|
||||
program.RemoveImages(deadImages);
|
||||
await program.UpdateToRepositoryAsync(ItemUpdateType.ImageUpdate, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -124,6 +124,8 @@ public class ListingsManager : IListingsManager
|
|||
{
|
||||
ArgumentNullException.ThrowIfNull(channel);
|
||||
|
||||
Exception? failure = null;
|
||||
|
||||
foreach (var (provider, providerInfo) in GetListingProviders())
|
||||
{
|
||||
if (!IsListingProviderEnabledForTuner(providerInfo, channel.TunerHostId))
|
||||
|
|
@ -144,19 +146,42 @@ public class ListingsManager : IListingsManager
|
|||
provider.Name,
|
||||
providerInfo.ListingsId ?? string.Empty);
|
||||
|
||||
var epgChannels = await GetEpgChannels(provider, providerInfo, true, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var epgChannel = GetEpgChannelFromTunerChannel(providerInfo.ChannelMappings, channel, epgChannels);
|
||||
if (epgChannel is null)
|
||||
List<ProgramInfo> programs;
|
||||
try
|
||||
{
|
||||
_logger.LogDebug("EPG channel not found for tuner channel {0}-{1} from {2}-{3}", channel.Number, channel.Name, provider.Name, providerInfo.ListingsId ?? string.Empty);
|
||||
// Listing the provider's channels can fail as well, and that must not stop the
|
||||
// fallback to the next provider either.
|
||||
var epgChannels = await GetEpgChannels(provider, providerInfo, true, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var epgChannel = GetEpgChannelFromTunerChannel(providerInfo.ChannelMappings, channel, epgChannels);
|
||||
if (epgChannel is null)
|
||||
{
|
||||
_logger.LogDebug("EPG channel not found for tuner channel {0}-{1} from {2}-{3}", channel.Number, channel.Name, provider.Name, providerInfo.ListingsId ?? string.Empty);
|
||||
continue;
|
||||
}
|
||||
|
||||
programs = (await provider
|
||||
.GetProgramsAsync(providerInfo, epgChannel.Id, startDateUtc, endDateUtc, cancellationToken).ConfigureAwait(false))
|
||||
.ToList();
|
||||
}
|
||||
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Another provider may still be able to serve this channel, so the failure is
|
||||
// held back rather than thrown, and only surfaces if nothing else answers.
|
||||
_logger.LogError(
|
||||
ex,
|
||||
"Error getting programs for channel {Number}-{Name} from {Provider}",
|
||||
channel.Number,
|
||||
channel.Name,
|
||||
provider.Name);
|
||||
failure ??= ex;
|
||||
continue;
|
||||
}
|
||||
|
||||
var programs = (await provider
|
||||
.GetProgramsAsync(providerInfo, epgChannel.Id, startDateUtc, endDateUtc, cancellationToken).ConfigureAwait(false))
|
||||
.ToList();
|
||||
|
||||
// Replace the value that came from the provider with a normalized value
|
||||
foreach (var program in programs)
|
||||
{
|
||||
|
|
@ -170,7 +195,16 @@ public class ListingsManager : IListingsManager
|
|||
}
|
||||
}
|
||||
|
||||
return Enumerable.Empty<ProgramInfo>();
|
||||
// Reporting no programs would have the guide refresh delete the ones this channel already
|
||||
// has, so a channel nothing could be fetched for has to fail instead.
|
||||
if (failure is not null)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"No listings provider could supply programs for channel {channel.Number}-{channel.Name}",
|
||||
failure);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
29
src/Jellyfin.LiveTv/Listings/SchedulesDirectCachedDay.cs
Normal file
29
src/Jellyfin.LiveTv/Listings/SchedulesDirectCachedDay.cs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
using System.Collections.Generic;
|
||||
using Jellyfin.LiveTv.Listings.SchedulesDirectDtos;
|
||||
|
||||
namespace Jellyfin.LiveTv.Listings;
|
||||
|
||||
/// <summary>
|
||||
/// One station's schedule for one day, together with the details of its programs.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Artwork is deliberately not cached: the spec states the image uri is ephemeral and must not be
|
||||
/// stored, and artwork changes are not reflected in the schedule md5 this entry is keyed by.
|
||||
/// </remarks>
|
||||
public class SchedulesDirectCachedDay
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the md5 Schedules Direct reported for this day's schedule.
|
||||
/// </summary>
|
||||
public string? Md5 { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the schedule.
|
||||
/// </summary>
|
||||
public DayDto? Schedule { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the details of the programs in the schedule.
|
||||
/// </summary>
|
||||
public IReadOnlyList<ProgramDetailsDto> Programs { get; set; } = [];
|
||||
}
|
||||
|
|
@ -21,6 +21,35 @@ namespace Jellyfin.LiveTv.Listings.SchedulesDirectDtos
|
|||
[JsonPropertyName("programs")]
|
||||
public IReadOnlyList<ProgramDto> Programs { get; set; } = Array.Empty<ProgramDto>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the error code. Set when this entry is a per-station error
|
||||
/// (for example SCHEDULE_RANGE_EXCEEDED) instead of a schedule.
|
||||
/// </summary>
|
||||
[JsonPropertyName("code")]
|
||||
public int? Code { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the error message that goes with <see cref="Code"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Not every error entry carries one; STATIONID_DELETED only sets <see cref="Response"/>.
|
||||
/// </remarks>
|
||||
[JsonPropertyName("message")]
|
||||
public string? Message { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the symbolic name of <see cref="Code"/>, for example STATIONID_DELETED.
|
||||
/// </summary>
|
||||
[JsonPropertyName("response")]
|
||||
public string? Response { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the earliest time a queued schedule (SCHEDULE_QUEUED) may be requested
|
||||
/// again. The spec requires the client to wait at least this long.
|
||||
/// </summary>
|
||||
[JsonPropertyName("retryTime")]
|
||||
public DateTime? RetryTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the metadata schedule.
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -21,6 +21,19 @@ namespace Jellyfin.LiveTv.Listings.SchedulesDirectDtos
|
|||
[JsonPropertyName("programID")]
|
||||
public string? ProgramId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the error code. Set when this entry is a per-program error
|
||||
/// (for example INVALID_PROGRAMID) instead of a program.
|
||||
/// </summary>
|
||||
[JsonPropertyName("code")]
|
||||
public int? Code { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the error message that goes with <see cref="Code"/>.
|
||||
/// </summary>
|
||||
[JsonPropertyName("message")]
|
||||
public string? Message { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the list of titles.
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,34 @@
|
|||
using System;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Jellyfin.LiveTv.Listings.SchedulesDirectDtos;
|
||||
|
||||
/// <summary>
|
||||
/// The md5 and last modified date of one station's schedule for one day.
|
||||
/// </summary>
|
||||
public class ScheduleMd5Dto
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the error code. Non-zero when the day is unavailable.
|
||||
/// </summary>
|
||||
[JsonPropertyName("code")]
|
||||
public int Code { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the message that goes with <see cref="Code"/>.
|
||||
/// </summary>
|
||||
[JsonPropertyName("message")]
|
||||
public string? Message { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the time the schedule was last modified.
|
||||
/// </summary>
|
||||
[JsonPropertyName("lastModified")]
|
||||
public DateTime? LastModified { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the md5 of the schedule. A change means the schedule must be downloaded again.
|
||||
/// </summary>
|
||||
[JsonPropertyName("md5")]
|
||||
public string? Md5 { get; set; }
|
||||
}
|
||||
|
|
@ -1,19 +1,161 @@
|
|||
#pragma warning disable CA1008 // Enums should have zero value
|
||||
|
||||
namespace Jellyfin.LiveTv.Listings.SchedulesDirectDtos;
|
||||
|
||||
/// <summary>
|
||||
/// Schedules Direct API error codes. See https://github.com/SchedulesDirect/JSON-Service/wiki/API-20141201#error-response for details.
|
||||
/// Schedules Direct API error codes. See https://github.com/SchedulesDirect/JSON-Service/wiki/API-20141201#error-codes for details.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A complete mirror of the documented table. Codes the client does not act on are still listed,
|
||||
/// because membership is what lets a response be logged by name instead of by number.
|
||||
/// </remarks>
|
||||
public enum SdErrorCode
|
||||
{
|
||||
/// <summary>
|
||||
/// Schedules Direct unavailable/out of service.
|
||||
/// No error.
|
||||
/// </summary>
|
||||
Ok = 0,
|
||||
|
||||
/// <summary>
|
||||
/// Unable to decode JSON.
|
||||
/// </summary>
|
||||
InvalidJson = 1001,
|
||||
|
||||
/// <summary>
|
||||
/// Did not receive a User-Agent in the request header.
|
||||
/// </summary>
|
||||
UserAgentRequired = 1003,
|
||||
|
||||
/// <summary>
|
||||
/// Token required but not provided in the request header.
|
||||
/// </summary>
|
||||
TokenMissing = 1004,
|
||||
|
||||
/// <summary>
|
||||
/// Did not recognize the submitted client.
|
||||
/// </summary>
|
||||
UnknownClient = 1005,
|
||||
|
||||
/// <summary>
|
||||
/// The maximum number of elements in a single request is 5000.
|
||||
/// </summary>
|
||||
MaxChunkExceeded = 1006,
|
||||
|
||||
/// <summary>
|
||||
/// The request sent to the server was empty.
|
||||
/// </summary>
|
||||
EmptyRequest = 1007,
|
||||
|
||||
/// <summary>
|
||||
/// The request is improperly formatted.
|
||||
/// </summary>
|
||||
IncorrectRequest = 1008,
|
||||
|
||||
/// <summary>
|
||||
/// The maximum number of elements in a single request is 500.
|
||||
/// </summary>
|
||||
MaxChunkExceededMetadata = 1009,
|
||||
|
||||
/// <summary>
|
||||
/// Token is not 32 characters.
|
||||
/// </summary>
|
||||
TokenInvalid = 1010,
|
||||
|
||||
/// <summary>
|
||||
/// The Content-Type header must be application/json.
|
||||
/// </summary>
|
||||
IncorrectContentType = 1011,
|
||||
|
||||
/// <summary>
|
||||
/// Did not receive request.
|
||||
/// </summary>
|
||||
RequiredRequestMissing = 2002,
|
||||
|
||||
/// <summary>
|
||||
/// Searching for lineups requires a 3-letter country parameter.
|
||||
/// </summary>
|
||||
CountryRequired = 2004,
|
||||
|
||||
/// <summary>
|
||||
/// Searching for lineups requires a postal code parameter.
|
||||
/// </summary>
|
||||
PostalCodeRequired = 2005,
|
||||
|
||||
/// <summary>
|
||||
/// The request is missing the personID / nameID in the path.
|
||||
/// </summary>
|
||||
PersonIdRequired = 2020,
|
||||
|
||||
/// <summary>
|
||||
/// The country parameter must be ISO-3166-1 alpha 3.
|
||||
/// </summary>
|
||||
InvalidCountryParameter = 2050,
|
||||
|
||||
/// <summary>
|
||||
/// Unknown request; available data may be requested for COUNTRIES.
|
||||
/// </summary>
|
||||
UnknownRequest = 2054,
|
||||
|
||||
/// <summary>
|
||||
/// Unexpected debug connection from client.
|
||||
/// </summary>
|
||||
InvalidDebugParameter = 2055,
|
||||
|
||||
/// <summary>
|
||||
/// Lineup already in account.
|
||||
/// </summary>
|
||||
DuplicateLineup = 2100,
|
||||
|
||||
/// <summary>
|
||||
/// Lineup not in account; add it before requesting its mapping.
|
||||
/// </summary>
|
||||
LineupNotFound = 2101,
|
||||
|
||||
/// <summary>
|
||||
/// Invalid lineup requested; check the country/postal code combination.
|
||||
/// </summary>
|
||||
UnknownLineup = 2102,
|
||||
|
||||
/// <summary>
|
||||
/// Delete of a lineup that is not in the account.
|
||||
/// </summary>
|
||||
InvalidLineupDelete = 2103,
|
||||
|
||||
/// <summary>
|
||||
/// Lineup must be formatted COUNTRY-LINEUP-DEVICE or COUNTRY-OTA-POSTALCODE.
|
||||
/// </summary>
|
||||
LineupWrongFormat = 2104,
|
||||
|
||||
/// <summary>
|
||||
/// The requested lineup has been deleted from the server.
|
||||
/// </summary>
|
||||
LineupDeleted = 2106,
|
||||
|
||||
/// <summary>
|
||||
/// The requested country is mis-typed or has no valid data.
|
||||
/// </summary>
|
||||
InvalidCountry = 2108,
|
||||
|
||||
/// <summary>
|
||||
/// The requested personID does not exist.
|
||||
/// </summary>
|
||||
InvalidPersonId = 2109,
|
||||
|
||||
/// <summary>
|
||||
/// The requested stationID is not in any of the configured lineups.
|
||||
/// </summary>
|
||||
StationIdNotFound = 2200,
|
||||
|
||||
/// <summary>
|
||||
/// The requested stationID has been marked as deleted and will be purged.
|
||||
/// </summary>
|
||||
StationIdDeleted = 2201,
|
||||
|
||||
/// <summary>
|
||||
/// Server offline for maintenance.
|
||||
/// </summary>
|
||||
ServiceOffline = 3000,
|
||||
|
||||
/// <summary>
|
||||
/// Schedules Direct busy.
|
||||
/// Server is busy processing other requests. Retry.
|
||||
/// </summary>
|
||||
ServiceBusy = 3001,
|
||||
|
||||
|
|
@ -23,72 +165,125 @@ public enum SdErrorCode
|
|||
AccountExpired = 4001,
|
||||
|
||||
/// <summary>
|
||||
/// Invalid password hash.
|
||||
/// Password hash must be a lowercase 40 character sha1_hex of the password.
|
||||
/// </summary>
|
||||
InvalidHash = 4002,
|
||||
|
||||
/// <summary>
|
||||
/// Invalid user or password.
|
||||
/// Invalid username or password.
|
||||
/// </summary>
|
||||
InvalidUser = 4003,
|
||||
|
||||
/// <summary>
|
||||
/// Account temporarily locked due to login failures.
|
||||
/// Too many login failures. Locked for 15 minutes.
|
||||
/// </summary>
|
||||
AccountTempLock = 4004,
|
||||
|
||||
/// <summary>
|
||||
/// Account permanently locked due to abuse.
|
||||
/// Access to the account via JSON has been disabled; the user must contact SD support.
|
||||
/// </summary>
|
||||
AccountLocked = 4005,
|
||||
|
||||
/// <summary>
|
||||
/// Token has expired. Request a new one.
|
||||
/// Token has expired. Request a new token.
|
||||
/// </summary>
|
||||
TokenExpired = 4006,
|
||||
|
||||
/// <summary>
|
||||
/// Application locked out.
|
||||
/// Application not authorized to use the data service.
|
||||
/// </summary>
|
||||
AppLocked = 4007,
|
||||
|
||||
/// <summary>
|
||||
/// Account not active.
|
||||
/// Account inactive.
|
||||
/// </summary>
|
||||
AccountInactive = 4008,
|
||||
|
||||
/// <summary>
|
||||
/// Maximum login attempts exceeded.
|
||||
/// Exceeded the maximum number of logins in 24 hours; clears 24 hours after the first login.
|
||||
/// </summary>
|
||||
MaxLoginAttempts = 4009,
|
||||
|
||||
/// <summary>
|
||||
/// Maximum unique IP attempts reached.
|
||||
/// Exceeded the maximum number of unique IP addresses in 24 hours; the user must contact SD support.
|
||||
/// </summary>
|
||||
MaxIPAttempts = 4010,
|
||||
|
||||
/// <summary>
|
||||
/// Lineup change maximum reached.
|
||||
/// Exceeded the maximum number of lineup changes for today.
|
||||
/// </summary>
|
||||
MaxScheduleRequests = 4100,
|
||||
MaxLineupChanges = 4100,
|
||||
|
||||
/// <summary>
|
||||
/// Requested image not found.
|
||||
/// Exceeded the number of lineups this account may hold.
|
||||
/// </summary>
|
||||
MaxLineups = 4101,
|
||||
|
||||
/// <summary>
|
||||
/// No lineups have been added to this account.
|
||||
/// </summary>
|
||||
NoLineups = 4102,
|
||||
|
||||
/// <summary>
|
||||
/// Could not find the requested image.
|
||||
/// </summary>
|
||||
ImageNotFound = 5000,
|
||||
|
||||
/// <summary>
|
||||
/// Maximum image downloads reached for the day.
|
||||
/// Maximum image downloads reached. The counter resets at 00:00Z.
|
||||
/// </summary>
|
||||
MaxImageDownloads = 5002,
|
||||
|
||||
/// <summary>
|
||||
/// Trial specific maximum image downloads reached for the day.
|
||||
/// Maximum image downloads for a trial user reached. The counter resets at 00:00Z.
|
||||
/// </summary>
|
||||
MaxImageDownloadsTrial = 5003,
|
||||
|
||||
/// <summary>
|
||||
/// Maximum schedule/metadata requests reached for the day.
|
||||
/// Exceeded the maximum number of invalid image URIs in 24 hours. The application is
|
||||
/// requesting URIs which do not exist and the account is about to be blocked.
|
||||
/// </summary>
|
||||
MaxInvalidImages = 5004
|
||||
MaxInvalidImages = 5004,
|
||||
|
||||
/// <summary>
|
||||
/// The requested programID does not exist. Permanent failure, do not ask again.
|
||||
/// </summary>
|
||||
InvalidProgramId = 6000,
|
||||
|
||||
/// <summary>
|
||||
/// The requested programID is queued for generation at the server. Soft failure, retry later.
|
||||
/// </summary>
|
||||
ProgramQueued = 6001,
|
||||
|
||||
/// <summary>
|
||||
/// The requested schedule should be available but was not found.
|
||||
/// </summary>
|
||||
ScheduleNotFound = 7000,
|
||||
|
||||
/// <summary>
|
||||
/// The server cannot determine whether the requested schedule is valid; the user has to open
|
||||
/// a support ticket.
|
||||
/// </summary>
|
||||
InvalidScheduleRequest = 7010,
|
||||
|
||||
/// <summary>
|
||||
/// The requested date is outside the range the server holds for that station.
|
||||
/// </summary>
|
||||
ScheduleRangeExceeded = 7020,
|
||||
|
||||
/// <summary>
|
||||
/// The requested schedule is not in any of the configured lineups.
|
||||
/// </summary>
|
||||
ScheduleNotInLineup = 7030,
|
||||
|
||||
/// <summary>
|
||||
/// The schedule is queued for generation and is not ready yet. The client must wait until the
|
||||
/// retryTime carried by the response before asking for it again.
|
||||
/// </summary>
|
||||
ScheduleQueued = 7100,
|
||||
|
||||
/// <summary>
|
||||
/// Unknown error; the user has to open a support ticket.
|
||||
/// </summary>
|
||||
UnknownError = 9999
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,22 @@
|
|||
using System;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Jellyfin.LiveTv.Listings.SchedulesDirectDtos;
|
||||
|
||||
/// <summary>
|
||||
/// The account block of the /status response.
|
||||
/// </summary>
|
||||
public class StatusAccountDto
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the time the subscription expires.
|
||||
/// </summary>
|
||||
[JsonPropertyName("expires")]
|
||||
public DateTime? Expires { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the maximum number of lineups the account may hold.
|
||||
/// </summary>
|
||||
[JsonPropertyName("maxLineups")]
|
||||
public int? MaxLineups { get; set; }
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Jellyfin.LiveTv.Listings.SchedulesDirectDtos;
|
||||
|
||||
/// <summary>
|
||||
/// The /status response.
|
||||
/// </summary>
|
||||
public class StatusDto
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the account details.
|
||||
/// </summary>
|
||||
[JsonPropertyName("account")]
|
||||
public StatusAccountDto? Account { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the lineups on the account, with the time each was last modified.
|
||||
/// </summary>
|
||||
[JsonPropertyName("lineups")]
|
||||
public IReadOnlyList<StatusLineupDto> Lineups { get; set; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the system status entries. The spec does not define their order, so every
|
||||
/// entry has to be inspected.
|
||||
/// </summary>
|
||||
[JsonPropertyName("systemStatus")]
|
||||
public IReadOnlyList<SystemStatusDto> SystemStatus { get; set; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the response code.
|
||||
/// </summary>
|
||||
[JsonPropertyName("code")]
|
||||
public int Code { get; set; }
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
using System;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Jellyfin.LiveTv.Listings.SchedulesDirectDtos;
|
||||
|
||||
/// <summary>
|
||||
/// One lineup entry of the /status response.
|
||||
/// </summary>
|
||||
public class StatusLineupDto
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the lineup id.
|
||||
/// </summary>
|
||||
[JsonPropertyName("lineup")]
|
||||
public string? Lineup { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the lineup id as reported for deleted lineups, which use a different name.
|
||||
/// </summary>
|
||||
[JsonPropertyName("ID")]
|
||||
public string? Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the time the lineup was last modified. A client only has to download the
|
||||
/// lineup again when this is newer than the copy it holds.
|
||||
/// </summary>
|
||||
[JsonPropertyName("modified")]
|
||||
public DateTime? Modified { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether the lineup has been deleted at the headend.
|
||||
/// </summary>
|
||||
[JsonPropertyName("isDeleted")]
|
||||
public bool IsDeleted { get; set; }
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
using System;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Jellyfin.LiveTv.Listings.SchedulesDirectDtos;
|
||||
|
||||
/// <summary>
|
||||
/// One entry of the system status.
|
||||
/// </summary>
|
||||
public class SystemStatusDto
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the time the status was posted.
|
||||
/// </summary>
|
||||
[JsonPropertyName("date")]
|
||||
public DateTime? Date { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the status. "Offline" means the client must disconnect for an hour.
|
||||
/// </summary>
|
||||
[JsonPropertyName("status")]
|
||||
public string? Status { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the message describing the status.
|
||||
/// </summary>
|
||||
[JsonPropertyName("message")]
|
||||
public string? Message { get; set; }
|
||||
}
|
||||
117
src/Jellyfin.LiveTv/Listings/SchedulesDirectLineupCache.cs
Normal file
117
src/Jellyfin.LiveTv/Listings/SchedulesDirectLineupCache.cs
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
using System;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Extensions.Json;
|
||||
using Jellyfin.LiveTv.Listings.SchedulesDirectDtos;
|
||||
using MediaBrowser.Common.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.LiveTv.Listings;
|
||||
|
||||
/// <summary>
|
||||
/// Caches a lineup's channel map, keyed by the "modified" date Schedules Direct reports for it.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The spec's steady-state flow is to compare the lineup's modified date in /status against the
|
||||
/// copy the client holds and only download the lineup again when the server's is newer.
|
||||
/// </remarks>
|
||||
public class SchedulesDirectLineupCache
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly string _cacheRoot;
|
||||
private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SchedulesDirectLineupCache"/> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">The logger.</param>
|
||||
/// <param name="appPaths">The application paths.</param>
|
||||
public SchedulesDirectLineupCache(ILogger logger, IApplicationPaths appPaths)
|
||||
{
|
||||
_logger = logger;
|
||||
_cacheRoot = Path.Combine(appPaths.CachePath, "schedulesdirect", "lineups");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the cached lineup if it was stored under the same modified date.
|
||||
/// </summary>
|
||||
/// <param name="lineupId">The lineup id.</param>
|
||||
/// <param name="modified">The modified date Schedules Direct currently reports.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>The cached lineup, or <c>null</c> when it is missing or stale.</returns>
|
||||
public async Task<ChannelDto?> GetAsync(string lineupId, DateTime modified, CancellationToken cancellationToken)
|
||||
{
|
||||
var path = GetPath(lineupId, modified);
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var stream = File.OpenRead(path);
|
||||
await using (stream.ConfigureAwait(false))
|
||||
{
|
||||
return await JsonSerializer.DeserializeAsync<ChannelDto>(stream, _jsonOptions, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or JsonException or UnauthorizedAccessException)
|
||||
{
|
||||
_logger.LogDebug(ex, "Discarding unreadable Schedules Direct lineup cache entry {Path}", path);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stores a freshly downloaded lineup and drops the copies it supersedes.
|
||||
/// </summary>
|
||||
/// <param name="lineupId">The lineup id.</param>
|
||||
/// <param name="modified">The modified date Schedules Direct reports for it.</param>
|
||||
/// <param name="lineup">The lineup to store.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>A task representing the write.</returns>
|
||||
public async Task SetAsync(string lineupId, DateTime modified, ChannelDto lineup, CancellationToken cancellationToken)
|
||||
{
|
||||
var path = GetPath(lineupId, modified);
|
||||
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
|
||||
var stream = File.Create(path);
|
||||
await using (stream.ConfigureAwait(false))
|
||||
{
|
||||
await JsonSerializer.SerializeAsync(stream, lineup, _jsonOptions, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
foreach (var stale in Directory.EnumerateFiles(Path.GetDirectoryName(path)!, "*.json"))
|
||||
{
|
||||
if (!string.Equals(stale, path, StringComparison.Ordinal))
|
||||
{
|
||||
File.Delete(stale);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or JsonException or UnauthorizedAccessException)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to cache Schedules Direct lineup {Path}", path);
|
||||
}
|
||||
}
|
||||
|
||||
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 lineupId, DateTime modified)
|
||||
=> Path.Combine(
|
||||
_cacheRoot,
|
||||
GetSafeName(lineupId),
|
||||
modified.ToUniversalTime().ToString("yyyyMMddHHmmss", CultureInfo.InvariantCulture) + ".json");
|
||||
}
|
||||
205
src/Jellyfin.LiveTv/Listings/SchedulesDirectScheduleCache.cs
Normal file
205
src/Jellyfin.LiveTv/Listings/SchedulesDirectScheduleCache.cs
Normal file
|
|
@ -0,0 +1,205 @@
|
|||
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;
|
||||
|
||||
/// <summary>
|
||||
/// Caches one station's schedule for one day, keyed by the md5 Schedules Direct reports for it.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 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.
|
||||
/// </remarks>
|
||||
public class SchedulesDirectScheduleCache
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly string _cacheRoot;
|
||||
private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SchedulesDirectScheduleCache"/> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">The logger.</param>
|
||||
/// <param name="appPaths">The application paths.</param>
|
||||
public SchedulesDirectScheduleCache(ILogger logger, IApplicationPaths appPaths)
|
||||
{
|
||||
_logger = logger;
|
||||
_cacheRoot = Path.Combine(appPaths.CachePath, "schedulesdirect", "schedules");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the cached day if it was stored under the same md5.
|
||||
/// </summary>
|
||||
/// <param name="stationId">The station id.</param>
|
||||
/// <param name="date">The date, formatted yyyy-MM-dd.</param>
|
||||
/// <param name="md5">The md5 Schedules Direct currently reports, or null if unknown.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>The cached entry, or <c>null</c> when it is missing or stale.</returns>
|
||||
public async Task<SchedulesDirectCachedDay?> 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the cached day whatever md5 it was stored under, to stand in for a day that could not
|
||||
/// be downloaded.
|
||||
/// </summary>
|
||||
/// <param name="stationId">The station id.</param>
|
||||
/// <param name="date">The date, formatted yyyy-MM-dd.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>The cached entry, or <c>null</c> when there is none.</returns>
|
||||
public Task<SchedulesDirectCachedDay?> GetLatestAsync(string stationId, string date, CancellationToken cancellationToken)
|
||||
=> ReadAsync(stationId, date, cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Stores a freshly downloaded day.
|
||||
/// </summary>
|
||||
/// <param name="stationId">The station id.</param>
|
||||
/// <param name="date">The date, formatted yyyy-MM-dd.</param>
|
||||
/// <param name="day">The day to store.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>A task representing the write.</returns>
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes cached days that are no longer requested, so the cache cannot grow without bound.
|
||||
/// </summary>
|
||||
/// <param name="stationId">The station id.</param>
|
||||
/// <param name="keepDates">The dates to keep.</param>
|
||||
public void Prune(string stationId, IEnumerable<string> keepDates)
|
||||
{
|
||||
var directory = Path.Combine(_cacheRoot, GetSafeName(stationId));
|
||||
if (!Directory.Exists(directory))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var keep = new HashSet<string>(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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes entries that have not been touched for a while, so that stations which have left
|
||||
/// the lineup do not keep their cache forever.
|
||||
/// </summary>
|
||||
/// <param name="maxAge">The age past which an untouched entry is deleted.</param>
|
||||
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<SchedulesDirectCachedDay?> 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<SchedulesDirectCachedDay>(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");
|
||||
}
|
||||
|
|
@ -30,6 +30,7 @@ public class RecordingsMetadataManager
|
|||
private readonly ILogger<RecordingsMetadataManager> _logger;
|
||||
private readonly IConfigurationManager _config;
|
||||
private readonly ILibraryManager _libraryManager;
|
||||
private readonly ISchedulesDirectService _schedulesDirectService;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RecordingsMetadataManager"/> class.
|
||||
|
|
@ -37,14 +38,17 @@ public class RecordingsMetadataManager
|
|||
/// <param name="logger">The <see cref="ILogger"/>.</param>
|
||||
/// <param name="config">The <see cref="IConfigurationManager"/>.</param>
|
||||
/// <param name="libraryManager">The <see cref="ILibraryManager"/>.</param>
|
||||
/// <param name="schedulesDirectService">The <see cref="ISchedulesDirectService"/>.</param>
|
||||
public RecordingsMetadataManager(
|
||||
ILogger<RecordingsMetadataManager> logger,
|
||||
IConfigurationManager config,
|
||||
ILibraryManager libraryManager)
|
||||
ILibraryManager libraryManager,
|
||||
ISchedulesDirectService schedulesDirectService)
|
||||
{
|
||||
_logger = logger;
|
||||
_config = config;
|
||||
_libraryManager = libraryManager;
|
||||
_schedulesDirectService = schedulesDirectService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -469,7 +473,14 @@ public class RecordingsMetadataManager
|
|||
{
|
||||
if (!image.IsLocalFile)
|
||||
{
|
||||
image = await _libraryManager.ConvertImageToLocal(program, image, 0).ConfigureAwait(false);
|
||||
var localImage = await _schedulesDirectService.ConvertImageToLocal(program, image, 0).ConfigureAwait(false);
|
||||
if (localImage is null)
|
||||
{
|
||||
_logger.LogDebug("Not saving recording image {Url}: Schedules Direct has blocked it", image.Path);
|
||||
return;
|
||||
}
|
||||
|
||||
image = localImage;
|
||||
}
|
||||
|
||||
var imageSaveFilenameWithoutExtension = image.Type switch
|
||||
|
|
|
|||
95
tests/Jellyfin.LiveTv.Tests/Guide/GuideManagerTests.cs
Normal file
95
tests/Jellyfin.LiveTv.Tests/Guide/GuideManagerTests.cs
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
using System;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.LiveTv.Guide;
|
||||
using MediaBrowser.Common.Configuration;
|
||||
using MediaBrowser.Common.Net;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Controller.LiveTv;
|
||||
using MediaBrowser.Controller.Persistence;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using MediaBrowser.Model.IO;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Jellyfin.LiveTv.Tests.Guide;
|
||||
|
||||
public sealed class GuideManagerTests : IDisposable
|
||||
{
|
||||
private const string ImageUrl = "https://json.schedulesdirect.org/20141201/image/assets/p1_b.jpg?token=abc";
|
||||
private const string ImageNotFoundBody = "{\"response\":\"IMAGE_NOT_FOUND\",\"code\":5000,\"message\":\"test\"}";
|
||||
|
||||
private readonly ILibraryManager? _previousLibraryManager = BaseItem.LibraryManager;
|
||||
private readonly Mock<ILibraryManager> _libraryManager = new();
|
||||
private readonly Mock<ISchedulesDirectService> _schedulesDirect = new();
|
||||
|
||||
public GuideManagerTests()
|
||||
{
|
||||
// UpdateToRepositoryAsync goes through the static.
|
||||
BaseItem.LibraryManager = _libraryManager.Object;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
BaseItem.LibraryManager = _previousLibraryManager!;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PreCacheImages_ImageNotFound_ReportsTheBodyAndRemovesTheImage()
|
||||
{
|
||||
var program = CreateProgram();
|
||||
_schedulesDirect.Setup(s => s.CanDownloadImage(ImageUrl)).Returns(true);
|
||||
_schedulesDirect.Setup(s => s.ReportImageDownloadFailure(ImageUrl, HttpStatusCode.NotFound, ImageNotFoundBody))
|
||||
.Returns(ImageDownloadFailureAction.RemoveImage);
|
||||
|
||||
// ConvertImageToLocal wraps the rejection, which is the only place the SD error body survives.
|
||||
_libraryManager.Setup(l => l.ConvertImageToLocal(program, It.IsAny<ItemImageInfo>(), 0, false))
|
||||
.ThrowsAsync(new InvalidOperationException(
|
||||
"test",
|
||||
new HttpResponseBodyException("test", HttpStatusCode.NotFound, ImageNotFoundBody)));
|
||||
|
||||
await CreateGuideManager().PreCacheImages([program], DateTime.UtcNow.AddDays(GuideManager.MaxCacheDays));
|
||||
|
||||
_schedulesDirect.Verify(s => s.ReportImageDownloadFailure(ImageUrl, HttpStatusCode.NotFound, ImageNotFoundBody), Times.Once);
|
||||
Assert.Empty(program.ImageInfos);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PreCacheImages_ImageBlocked_RemovesItWithoutDownloading()
|
||||
{
|
||||
// A blocked url left on the program would be fetched from SD when a client displays it.
|
||||
var program = CreateProgram();
|
||||
_schedulesDirect.Setup(s => s.CanDownloadImage(ImageUrl)).Returns(false);
|
||||
|
||||
await CreateGuideManager().PreCacheImages([program], DateTime.UtcNow.AddDays(GuideManager.MaxCacheDays));
|
||||
|
||||
_libraryManager.Verify(
|
||||
l => l.ConvertImageToLocal(It.IsAny<BaseItem>(), It.IsAny<ItemImageInfo>(), It.IsAny<int>(), It.IsAny<bool>()),
|
||||
Times.Never);
|
||||
Assert.Empty(program.ImageInfos);
|
||||
}
|
||||
|
||||
private static LiveTvProgram CreateProgram()
|
||||
=> new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = "Test",
|
||||
EndDate = DateTime.UtcNow.AddHours(1),
|
||||
ImageInfos = [new ItemImageInfo { Path = ImageUrl, Type = ImageType.Primary }]
|
||||
};
|
||||
|
||||
private GuideManager CreateGuideManager()
|
||||
=> new(
|
||||
NullLogger<GuideManager>.Instance,
|
||||
Mock.Of<IConfigurationManager>(),
|
||||
Mock.Of<IFileSystem>(),
|
||||
Mock.Of<IItemRepository>(),
|
||||
_libraryManager.Object,
|
||||
Mock.Of<ILiveTvManager>(),
|
||||
Mock.Of<ITunerHostManager>(),
|
||||
Mock.Of<IRecordingsManager>(),
|
||||
_schedulesDirect.Object,
|
||||
null!);
|
||||
}
|
||||
|
|
@ -1,4 +1,8 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.LiveTv.Configuration;
|
||||
using Jellyfin.LiveTv.Listings;
|
||||
using MediaBrowser.Common.Configuration;
|
||||
|
|
@ -47,4 +51,44 @@ public class ListingsManagerTests
|
|||
_config.GetLiveTvConfiguration().ListingProviders,
|
||||
p => p.Id.Equals(id, StringComparison.Ordinal));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetProgramsAsync_FirstProviderCannotListChannels_FallsBackToTheNext()
|
||||
{
|
||||
// Arrange
|
||||
var failing = new Mock<IListingsProvider>();
|
||||
failing.SetupGet(x => x.Type).Returns("failing");
|
||||
failing.Setup(x => x.GetChannels(It.IsAny<ListingsProviderInfo>(), It.IsAny<CancellationToken>()))
|
||||
.ThrowsAsync(new InvalidOperationException("offline"));
|
||||
|
||||
var working = new Mock<IListingsProvider>();
|
||||
working.SetupGet(x => x.Type).Returns("working");
|
||||
working.Setup(x => x.GetChannels(It.IsAny<ListingsProviderInfo>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<ChannelInfo> { new() { Id = "5", Number = "5" } });
|
||||
working.Setup(x => x.GetProgramsAsync(It.IsAny<ListingsProviderInfo>(), "5", It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new[] { new ProgramInfo { Id = "p1" } });
|
||||
|
||||
Mock.Get(_config)
|
||||
.Setup(x => x.GetConfiguration(It.IsAny<string>()))
|
||||
.Returns(new LiveTvOptions
|
||||
{
|
||||
ListingProviders =
|
||||
[
|
||||
new ListingsProviderInfo { Id = "a", Type = "failing" },
|
||||
new ListingsProviderInfo { Id = "b", Type = "working" }
|
||||
]
|
||||
});
|
||||
|
||||
var manager = new ListingsManager(_logger, _config, _taskManager, _tunerHostManager, [failing.Object, working.Object]);
|
||||
|
||||
// Act
|
||||
var programs = await manager.GetProgramsAsync(
|
||||
new ChannelInfo { Id = "5", Number = "5", TunerHostId = "tuner" },
|
||||
DateTime.UtcNow,
|
||||
DateTime.UtcNow.AddDays(1),
|
||||
CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.Single(programs);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,130 +0,0 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Common.Configuration;
|
||||
using MediaBrowser.Model.LiveTv;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Moq;
|
||||
using Moq.Protected;
|
||||
using Xunit;
|
||||
using SchedulesDirectProvider = Jellyfin.LiveTv.Listings.SchedulesDirect;
|
||||
|
||||
namespace Jellyfin.LiveTv.Tests.Listings;
|
||||
|
||||
public class SchedulesDirectLineupTests
|
||||
{
|
||||
private const string InvalidUserResponse = "{\"response\":\"INVALID_USER\",\"code\":4003,\"message\":\"Invalid user.\",\"serverID\":\"AWS-SD-web.1\"}";
|
||||
|
||||
private static readonly ListingsProviderInfo _info = new() { Username = "user", Password = "password" };
|
||||
|
||||
[Fact]
|
||||
public async Task GetLineups_ValidCredentials_ReturnsLineups()
|
||||
{
|
||||
var tokenResponse = await CreateSuccessfulLogin();
|
||||
using var provider = CreateProvider(tokenResponse, await GetHeadendsResponse());
|
||||
|
||||
var lineups = await provider.GetLineups(_info, "USA", "90210");
|
||||
|
||||
Assert.NotEmpty(lineups);
|
||||
Assert.Contains(lineups, i => string.Equals(i.Id, "USA-OTA-90210", StringComparison.Ordinal));
|
||||
Assert.Contains(lineups, i => string.Equals(i.Name, "Antenna", StringComparison.Ordinal));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetLineups_LoginFails_Throws()
|
||||
{
|
||||
using var provider = CreateProvider(CreateFailedLogin(), await GetHeadendsResponse());
|
||||
|
||||
// An empty lineup list is indistinguishable from "no lineups for this location", so a
|
||||
// failed login has to surface as an error instead.
|
||||
await Assert.ThrowsAnyAsync<Exception>(() => provider.GetLineups(_info, "USA", "90210"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Validate_LoginFails_Throws()
|
||||
{
|
||||
using var provider = CreateProvider(CreateFailedLogin(), await GetHeadendsResponse());
|
||||
|
||||
await Assert.ThrowsAnyAsync<Exception>(() => provider.Validate(_info, true, false));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Validate_AfterAccountError_RecoversWithoutRestart()
|
||||
{
|
||||
var login = CreateFailedLogin();
|
||||
using var provider = CreateProvider(login, await GetHeadendsResponse());
|
||||
|
||||
await Assert.ThrowsAnyAsync<Exception>(() => provider.GetLineups(_info, "USA", "90210"));
|
||||
|
||||
// The account error disables Schedules Direct; saving the provider again is the user
|
||||
// correcting their credentials, and that has to recover without a server restart.
|
||||
login.Status = HttpStatusCode.OK;
|
||||
login.Body = await GetTokenResponse();
|
||||
|
||||
await provider.Validate(_info, true, false);
|
||||
|
||||
Assert.NotEmpty(await provider.GetLineups(_info, "USA", "90210"));
|
||||
}
|
||||
|
||||
private static async Task<Response> CreateSuccessfulLogin()
|
||||
=> new() { Status = HttpStatusCode.OK, Body = await GetTokenResponse() };
|
||||
|
||||
private static Response CreateFailedLogin()
|
||||
=> new() { Status = HttpStatusCode.BadRequest, Body = InvalidUserResponse };
|
||||
|
||||
private static Task<string> GetTokenResponse()
|
||||
=> File.ReadAllTextAsync("Test Data/SchedulesDirect/token_live_response.json", TestContext.Current.CancellationToken);
|
||||
|
||||
private static Task<string> GetHeadendsResponse()
|
||||
=> File.ReadAllTextAsync("Test Data/SchedulesDirect/headends_response.json", TestContext.Current.CancellationToken);
|
||||
|
||||
private static SchedulesDirectProvider CreateProvider(Response login, string headendsResponse)
|
||||
{
|
||||
var messageHandler = new Mock<HttpMessageHandler>();
|
||||
messageHandler.Protected()
|
||||
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
|
||||
.Returns<HttpRequestMessage, CancellationToken>((m, _) =>
|
||||
{
|
||||
var path = m.RequestUri!.AbsolutePath;
|
||||
if (path.EndsWith("/token", StringComparison.Ordinal))
|
||||
{
|
||||
return Task.FromResult(new HttpResponseMessage(login.Status)
|
||||
{
|
||||
Content = new StringContent(login.Body)
|
||||
});
|
||||
}
|
||||
|
||||
if (path.EndsWith("/headends", StringComparison.Ordinal))
|
||||
{
|
||||
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
|
||||
{
|
||||
Content = new StringContent(headendsResponse)
|
||||
});
|
||||
}
|
||||
|
||||
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.NotFound));
|
||||
});
|
||||
|
||||
var httpClientFactory = new Mock<IHttpClientFactory>();
|
||||
httpClientFactory.Setup(x => x.CreateClient(It.IsAny<string>()))
|
||||
.Returns(() => new HttpClient(messageHandler.Object));
|
||||
|
||||
var appPaths = new Mock<IApplicationPaths>();
|
||||
appPaths.SetupGet(x => x.CachePath).Returns(Path.GetTempPath());
|
||||
|
||||
return new SchedulesDirectProvider(
|
||||
NullLogger<SchedulesDirectProvider>.Instance,
|
||||
httpClientFactory.Object,
|
||||
appPaths.Object);
|
||||
}
|
||||
|
||||
private sealed class Response
|
||||
{
|
||||
public HttpStatusCode Status { get; set; }
|
||||
|
||||
public string Body { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
915
tests/Jellyfin.LiveTv.Tests/Listings/SchedulesDirectTests.cs
Normal file
915
tests/Jellyfin.LiveTv.Tests/Listings/SchedulesDirectTests.cs
Normal file
|
|
@ -0,0 +1,915 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Common.Configuration;
|
||||
using MediaBrowser.Common.Net;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Controller.LiveTv;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using MediaBrowser.Model.LiveTv;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Moq;
|
||||
using Moq.Protected;
|
||||
using Xunit;
|
||||
using SchedulesDirectProvider = Jellyfin.LiveTv.Listings.SchedulesDirect;
|
||||
|
||||
namespace Jellyfin.LiveTv.Tests.Listings;
|
||||
|
||||
public class SchedulesDirectTests : IDisposable
|
||||
{
|
||||
private const string InvalidUserResponse = "{\"response\":\"INVALID_USER\",\"code\":4003,\"message\":\"Invalid user.\",\"serverID\":\"AWS-SD-web.1\"}";
|
||||
private const string ImageUrl = "https://json.schedulesdirect.org/20141201/image/assets/p1_b.jpg?token=abc";
|
||||
|
||||
private static readonly ListingsProviderInfo _info = new() { Username = "user", Password = "password" };
|
||||
|
||||
private readonly Mock<ILibraryManager> _libraryManager = new();
|
||||
|
||||
// The daily limit files live under CachePath, so every provider needs its own directory or
|
||||
// one test leaves a limit behind for the next.
|
||||
private readonly string _cachePath = Path.Combine(Path.GetTempPath(), "jf-sd-" + Guid.NewGuid().ToString("N"));
|
||||
|
||||
[Fact]
|
||||
public async Task GetLineups_ValidCredentials_ReturnsLineups()
|
||||
{
|
||||
var tokenResponse = await CreateSuccessfulLogin();
|
||||
using var provider = CreateProvider(tokenResponse, await GetHeadendsResponse());
|
||||
|
||||
var lineups = await provider.GetLineups(_info, "USA", "90210");
|
||||
|
||||
Assert.NotEmpty(lineups);
|
||||
Assert.Contains(lineups, i => string.Equals(i.Id, "USA-OTA-90210", StringComparison.Ordinal));
|
||||
Assert.Contains(lineups, i => string.Equals(i.Name, "Antenna", StringComparison.Ordinal));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetLineups_LoginFails_Throws()
|
||||
{
|
||||
using var provider = CreateProvider(CreateFailedLogin(), await GetHeadendsResponse());
|
||||
|
||||
// An empty lineup list is indistinguishable from "no lineups for this location", so a
|
||||
// failed login has to surface as an error instead.
|
||||
await Assert.ThrowsAnyAsync<Exception>(() => provider.GetLineups(_info, "USA", "90210"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Validate_LoginFails_Throws()
|
||||
{
|
||||
using var provider = CreateProvider(CreateFailedLogin(), await GetHeadendsResponse());
|
||||
|
||||
await Assert.ThrowsAnyAsync<Exception>(() => provider.Validate(_info, true, false));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Validate_AfterAccountError_RecoversWithoutRestart()
|
||||
{
|
||||
var login = CreateFailedLogin();
|
||||
using var provider = CreateProvider(login, await GetHeadendsResponse());
|
||||
|
||||
await Assert.ThrowsAnyAsync<Exception>(() => provider.GetLineups(_info, "USA", "90210"));
|
||||
|
||||
// The account error disables Schedules Direct; saving the provider again is the user
|
||||
// correcting their credentials, and that has to recover without a server restart.
|
||||
login.Status = HttpStatusCode.OK;
|
||||
login.Body = await GetTokenResponse();
|
||||
|
||||
await provider.Validate(_info, true, false);
|
||||
|
||||
Assert.NotEmpty(await provider.GetLineups(_info, "USA", "90210"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetLineups_TokenRejectedWithHttp200_DisablesService()
|
||||
{
|
||||
// The account lockout this guards against arrived as HTTP 200 carrying code 4005.
|
||||
var login = new Response
|
||||
{
|
||||
Status = HttpStatusCode.OK,
|
||||
Body = "{\"response\":\"JSON_ACCOUNT_ACCESS_DISABLED\",\"code\":4005,\"serverID\":\"20141201.web\",\"message\":\"Access to account via JSON has been disabled.\"}"
|
||||
};
|
||||
using var provider = CreateProvider(login, await GetHeadendsResponse());
|
||||
|
||||
await Assert.ThrowsAnyAsync<Exception>(() => provider.GetLineups(_info, "USA", "90210"));
|
||||
|
||||
// The account error disables Schedules Direct: a retry must not reach the network again.
|
||||
login.Body = await GetTokenResponse();
|
||||
await Assert.ThrowsAnyAsync<Exception>(() => provider.GetLineups(_info, "USA", "90210"));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(5002, "MAX_IMAGE_DOWNLOADS")]
|
||||
[InlineData(5003, "MAX_IMAGE_DOWNLOADS_TRIAL")]
|
||||
public async Task ReportImageDownloadFailure_ImageLimitCode_BlocksOnFirstReport(int code, string response)
|
||||
{
|
||||
using var provider = CreateProvider(await CreateSuccessfulLogin(), await GetHeadendsResponse());
|
||||
|
||||
var action = provider.ReportImageDownloadFailure(ImageUrl, HttpStatusCode.NotFound, ErrorBody(code, response));
|
||||
|
||||
Assert.Equal(ImageDownloadFailureAction.None, action);
|
||||
Assert.False(provider.CanDownloadImage(ImageUrl));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReportImageDownloadFailure_ImageNotFound_DropsUrlWithoutBlockingTheDay()
|
||||
{
|
||||
using var provider = CreateProvider(await CreateSuccessfulLogin(), await GetHeadendsResponse());
|
||||
|
||||
var action = provider.ReportImageDownloadFailure(ImageUrl, HttpStatusCode.NotFound, ErrorBody(5000, "IMAGE_NOT_FOUND"));
|
||||
|
||||
// The spec requires that url is never requested again, but the day is not blocked and
|
||||
// every other image stays downloadable.
|
||||
Assert.Equal(ImageDownloadFailureAction.RemoveImage, action);
|
||||
Assert.False(provider.CanDownloadImage(ImageUrl));
|
||||
Assert.True(provider.CanDownloadImage("https://json.schedulesdirect.org/20141201/image/assets/p2_b.jpg?token=abc"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReportImageDownloadFailure_InvalidUriLimit_BlocksAndDropsUrl()
|
||||
{
|
||||
using var provider = CreateProvider(await CreateSuccessfulLogin(), await GetHeadendsResponse());
|
||||
|
||||
var action = provider.ReportImageDownloadFailure(ImageUrl, HttpStatusCode.NotFound, ErrorBody(5004, "MAX_IMAGE_INVALID_URI_ERRORS"));
|
||||
|
||||
Assert.Equal(ImageDownloadFailureAction.RemoveImage, action);
|
||||
Assert.False(provider.CanDownloadImage(ImageUrl));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(1004, "TOKEN_MISSING")]
|
||||
[InlineData(4006, "TOKEN_EXPIRED")]
|
||||
public async Task ReportImageDownloadFailure_TokenCode_DoesNotBlockTheDay(int code, string response)
|
||||
{
|
||||
using var provider = CreateProvider(await CreateSuccessfulLogin(), await GetHeadendsResponse());
|
||||
|
||||
var action = provider.ReportImageDownloadFailure(ImageUrl, HttpStatusCode.Forbidden, ErrorBody(code, response));
|
||||
|
||||
Assert.Equal(ImageDownloadFailureAction.RemoveImage, action);
|
||||
Assert.True(provider.CanDownloadImage(ImageUrl));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReportImageDownloadFailure_UnreadableBody_FallsBackToFailureStreak()
|
||||
{
|
||||
using var provider = CreateProvider(await CreateSuccessfulLogin(), await GetHeadendsResponse());
|
||||
|
||||
for (var i = 0; i < 9; i++)
|
||||
{
|
||||
provider.ReportImageDownloadFailure(ImageUrl, HttpStatusCode.Forbidden, "<html>gateway error</html>");
|
||||
Assert.True(provider.CanDownloadImage(ImageUrl));
|
||||
}
|
||||
|
||||
provider.ReportImageDownloadFailure(ImageUrl, HttpStatusCode.Forbidden, "<html>gateway error</html>");
|
||||
|
||||
Assert.False(provider.CanDownloadImage(ImageUrl));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReportImageDownloadSuccess_ResetsUnexplainedFailureStreak()
|
||||
{
|
||||
using var provider = CreateProvider(await CreateSuccessfulLogin(), await GetHeadendsResponse());
|
||||
|
||||
for (var i = 0; i < 9; i++)
|
||||
{
|
||||
provider.ReportImageDownloadFailure(ImageUrl, HttpStatusCode.Forbidden, null);
|
||||
}
|
||||
|
||||
provider.ReportImageDownloadSuccess(ImageUrl);
|
||||
provider.ReportImageDownloadFailure(ImageUrl, HttpStatusCode.Forbidden, null);
|
||||
|
||||
Assert.True(provider.CanDownloadImage(ImageUrl));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReportImageDownloadFailure_ForeignUrl_IsIgnored()
|
||||
{
|
||||
using var provider = CreateProvider(await CreateSuccessfulLogin(), await GetHeadendsResponse());
|
||||
|
||||
var action = provider.ReportImageDownloadFailure("https://example.com/image.jpg", HttpStatusCode.NotFound, ErrorBody(5002, "MAX_IMAGE_DOWNLOADS"));
|
||||
|
||||
Assert.Equal(ImageDownloadFailureAction.None, action);
|
||||
Assert.True(provider.CanDownloadImage(ImageUrl));
|
||||
Assert.True(provider.CanDownloadImage("https://example.com/image.jpg"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ConvertImageToLocal_Blocked_DoesNotDownload()
|
||||
{
|
||||
using var provider = CreateProvider(await CreateSuccessfulLogin(), await GetHeadendsResponse());
|
||||
provider.ReportImageDownloadFailure(ImageUrl, HttpStatusCode.NotFound, ErrorBody(5002, "MAX_IMAGE_DOWNLOADS"));
|
||||
|
||||
var image = await provider.ConvertImageToLocal(new LiveTvProgram(), new ItemImageInfo { Path = ImageUrl, Type = ImageType.Primary }, 0);
|
||||
|
||||
Assert.Null(image);
|
||||
_libraryManager.Verify(
|
||||
l => l.ConvertImageToLocal(It.IsAny<BaseItem>(), It.IsAny<ItemImageInfo>(), It.IsAny<int>(), It.IsAny<bool>()),
|
||||
Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ConvertImageToLocal_ImageNotFound_StopsOfferingTheUri()
|
||||
{
|
||||
using var provider = CreateProvider(await CreateSuccessfulLogin(), await GetHeadendsResponse());
|
||||
|
||||
// LibraryManager wraps the rejection, which is the only place the SD error body survives.
|
||||
_libraryManager.Setup(l => l.ConvertImageToLocal(It.IsAny<BaseItem>(), It.IsAny<ItemImageInfo>(), 0, true))
|
||||
.ThrowsAsync(new InvalidOperationException(
|
||||
"test",
|
||||
new HttpResponseBodyException("test", HttpStatusCode.NotFound, ErrorBody(5000, "IMAGE_NOT_FOUND"))));
|
||||
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(
|
||||
() => provider.ConvertImageToLocal(new LiveTvProgram(), new ItemImageInfo { Path = ImageUrl, Type = ImageType.Primary }, 0));
|
||||
|
||||
Assert.False(provider.CanDownloadImage(ImageUrl));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetProgramsAsync_UnchangedMd5_ServesSecondRefreshFromCache()
|
||||
{
|
||||
var server = new FakeServer();
|
||||
using var provider = CreateProvider(server);
|
||||
|
||||
var first = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
|
||||
Assert.NotEmpty(first);
|
||||
|
||||
var schedulesAfterFirst = server.Counts["/schedules"];
|
||||
var programsAfterFirst = server.Counts.GetValueOrDefault("/programs");
|
||||
Assert.True(schedulesAfterFirst > 0);
|
||||
Assert.True(programsAfterFirst > 0);
|
||||
|
||||
var second = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
|
||||
|
||||
// The md5 is unchanged, so only the hash check should have gone to the server.
|
||||
Assert.Equal(first.Count(), second.Count());
|
||||
Assert.Equal(schedulesAfterFirst, server.Counts["/schedules"]);
|
||||
Assert.Equal(programsAfterFirst, server.Counts.GetValueOrDefault("/programs"));
|
||||
Assert.Equal(2, server.Counts["/schedules/md5"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetProgramsAsync_ChangedMd5_DownloadsTheScheduleAgain()
|
||||
{
|
||||
var server = new FakeServer();
|
||||
using var provider = CreateProvider(server);
|
||||
|
||||
await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
|
||||
var afterFirst = server.Counts["/schedules"];
|
||||
|
||||
server.Md5Salt = "changed";
|
||||
await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.True(server.Counts["/schedules"] > afterFirst);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetProgramsAsync_SystemOffline_ThrowsInsteadOfReportingNoPrograms()
|
||||
{
|
||||
var server = new FakeServer { SystemStatus = "Offline" };
|
||||
using var provider = CreateProvider(server);
|
||||
|
||||
// An empty list means "this channel has no programs", and the guide refresh acts on that
|
||||
// by deleting the ones it already has. A failure has to surface as one.
|
||||
await Assert.ThrowsAnyAsync<Exception>(
|
||||
() => provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken));
|
||||
|
||||
Assert.Equal(0, server.Counts.GetValueOrDefault("/schedules"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetProgramsAsync_OfflineCodeWithHttp200_ThrowsInsteadOfReportingNoPrograms()
|
||||
{
|
||||
// A 200 carrying an error code used to deserialize into an empty status that read as online.
|
||||
var server = new FakeServer { StatusCode = 3000 };
|
||||
using var provider = CreateProvider(server);
|
||||
|
||||
await Assert.ThrowsAnyAsync<Exception>(
|
||||
() => provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken));
|
||||
|
||||
Assert.Equal(0, server.Counts.GetValueOrDefault("/schedules"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetProgramsAsync_ImageLimitHit_StillReturnsPrograms()
|
||||
{
|
||||
// The image limit must not be mistaken for a fetch failure: the listings are still good,
|
||||
// only the artwork is unavailable.
|
||||
var server = new FakeServer { HasImageArtwork = true };
|
||||
using var provider = CreateProvider(server);
|
||||
|
||||
provider.ReportImageDownloadFailure(ImageUrl, HttpStatusCode.NotFound, ErrorBody(5002, "MAX_IMAGE_DOWNLOADS"));
|
||||
Assert.False(provider.CanDownloadImage(ImageUrl));
|
||||
|
||||
var programs = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.NotEmpty(programs);
|
||||
Assert.All(programs, p => Assert.Null(p.ImageUrl));
|
||||
Assert.Equal(0, server.Counts.GetValueOrDefault("/metadata/programs"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetProgramsAsync_Md5SaysDateIsOutOfRange_DoesNotRequestTheSchedule()
|
||||
{
|
||||
// 7020 is what the hash check exists to catch: asking /schedules for the day anyway just
|
||||
// earns the same error, which is why the spec points at the md5 endpoint for the range.
|
||||
var server = new FakeServer { Md5ErrorCode = 7020 };
|
||||
using var provider = CreateProvider(server);
|
||||
|
||||
var programs = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.Empty(programs);
|
||||
Assert.Equal(1, server.Counts["/schedules/md5"]);
|
||||
Assert.Equal(0, server.Counts.GetValueOrDefault("/schedules"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetProgramsAsync_ScheduleQueued_HonoursRetryTime()
|
||||
{
|
||||
// The queued response is a bare object rather than the usual array, and it carries the
|
||||
// earliest time the schedule may be asked for again.
|
||||
var server = new FakeServer { ScheduleRetryTime = DateTime.UtcNow.AddMinutes(10) };
|
||||
using var provider = CreateProvider(server);
|
||||
|
||||
// A queued schedule is a failure, not an empty channel: reporting no programs would have
|
||||
// the guide refresh delete the ones this channel already has.
|
||||
await Assert.ThrowsAnyAsync<Exception>(
|
||||
() => provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken));
|
||||
var afterFirst = server.Counts["/schedules"];
|
||||
|
||||
await Assert.ThrowsAnyAsync<Exception>(
|
||||
() => provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken));
|
||||
|
||||
Assert.Equal(afterFirst, server.Counts["/schedules"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetProgramsAsync_ScheduleQueuedRetryTimePassed_AsksAgain()
|
||||
{
|
||||
var server = new FakeServer { ScheduleRetryTime = DateTime.UtcNow.AddSeconds(-1) };
|
||||
using var provider = CreateProvider(server);
|
||||
|
||||
await Assert.ThrowsAnyAsync<Exception>(
|
||||
() => provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken));
|
||||
var afterFirst = server.Counts["/schedules"];
|
||||
|
||||
await Assert.ThrowsAnyAsync<Exception>(
|
||||
() => provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken));
|
||||
|
||||
Assert.True(server.Counts["/schedules"] > afterFirst);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetProgramsAsync_CacheHit_StillRefreshesArtwork()
|
||||
{
|
||||
// Artwork uris are ephemeral and their changes are not reflected in the schedule md5, so
|
||||
// the spec forbids storing them; the index has to be requested again on a cache hit.
|
||||
var server = new FakeServer { HasImageArtwork = true };
|
||||
using var provider = CreateProvider(server);
|
||||
|
||||
var first = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
|
||||
Assert.All(first, p => Assert.Contains("p1_b.jpg", p.ImageUrl, StringComparison.Ordinal));
|
||||
|
||||
var metadataAfterFirst = server.Counts["/metadata/programs"];
|
||||
|
||||
var second = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.Equal(1, server.Counts["/schedules"]);
|
||||
Assert.True(server.Counts["/metadata/programs"] > metadataAfterFirst);
|
||||
Assert.All(second, p => Assert.Contains("p1_b.jpg", p.ImageUrl, StringComparison.Ordinal));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetProgramsAsync_AfterImageNotFound_StopsOfferingTheUri()
|
||||
{
|
||||
var server = new FakeServer { HasImageArtwork = true };
|
||||
using var provider = CreateProvider(server);
|
||||
|
||||
var first = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
|
||||
var imageUrl = first.First().ImageUrl;
|
||||
Assert.NotNull(imageUrl);
|
||||
|
||||
Assert.Equal(
|
||||
ImageDownloadFailureAction.RemoveImage,
|
||||
provider.ReportImageDownloadFailure(imageUrl, HttpStatusCode.NotFound, ErrorBody(5000, "IMAGE_NOT_FOUND")));
|
||||
|
||||
// Handing the same dead uri back every refresh is what walks an account into
|
||||
// MAX_IMAGE_INVALID_URI_ERRORS, so it must not survive the rejection.
|
||||
var second = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.All(second, p => Assert.Null(p.ImageUrl));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetProgramsAsync_OneDayQueuedNextToCachedDays_ServesThePreviousCopy()
|
||||
{
|
||||
var server = new FakeServer();
|
||||
using var provider = CreateProvider(server);
|
||||
|
||||
var first = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
|
||||
|
||||
// One day changed and is now queued; the others still match the cache. Leaving the queued
|
||||
// day out would have the guide refresh delete its programs.
|
||||
server.ChangedDate = server.RequestedDates[0];
|
||||
server.ScheduleRetryTime = DateTime.UtcNow.AddMinutes(10);
|
||||
var second = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.Equal(2, server.Counts["/schedules"]);
|
||||
Assert.Equal(first.Select(p => p.Id).Order(), second.Select(p => p.Id).Order());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetProgramsAsync_ProgramQueued_DoesNotCacheTheIncompleteDay()
|
||||
{
|
||||
var server = new FakeServer { ProgramErrorCode = 6001 };
|
||||
using var provider = CreateProvider(server);
|
||||
|
||||
// With no previous copy to stand in, a queued program fails the channel rather than
|
||||
// dropping its airings, which the guide refresh would delete.
|
||||
await Assert.ThrowsAnyAsync<Exception>(
|
||||
() => provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken));
|
||||
|
||||
// The program finishing generation need not change the day's md5, so the day must be
|
||||
// downloaded again rather than served from the cache without it.
|
||||
server.ProgramErrorCode = 0;
|
||||
var second = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.NotEmpty(second);
|
||||
Assert.Equal(2, server.Counts["/programs"]);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("EP000000020001")]
|
||||
public async Task GetProgramsAsync_ProgramQueuedAfterGoodRefresh_KeepsItsAirings(string? queuedProgramId)
|
||||
{
|
||||
var server = new FakeServer { SecondProgram = true };
|
||||
using var provider = CreateProvider(server);
|
||||
|
||||
var first = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
|
||||
Assert.Contains(first, p => p.Id!.Contains("EP000000020001", StringComparison.Ordinal));
|
||||
|
||||
// The day changed and one (or every) program on it is still being generated.
|
||||
server.Md5Salt = "changed";
|
||||
server.ProgramErrorCode = 6001;
|
||||
server.QueuedProgramId = queuedProgramId;
|
||||
var second = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.Equal(first.Select(p => p.Id).Order(), second.Select(p => p.Id).Order());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(HttpStatusCode.OK, 3000, "SERVICE_OFFLINE")]
|
||||
[InlineData(HttpStatusCode.Forbidden, 4009, "TOO_MANY_LOGINS")]
|
||||
public async Task GetProgramsAsync_StopCodeOnMd5_DoesNotRequestSchedules(HttpStatusCode status, int code, string response)
|
||||
{
|
||||
var server = new FakeServer { Md5RejectionBody = ErrorBody(code, response), Md5RejectionStatus = status };
|
||||
using var provider = CreateProvider(server);
|
||||
|
||||
await Assert.ThrowsAnyAsync<Exception>(
|
||||
() => provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken));
|
||||
|
||||
Assert.Equal(0, server.Counts.GetValueOrDefault("/schedules"));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(1004, "TOKEN_MISSING")]
|
||||
[InlineData(1010, "INVALID_TOKEN")]
|
||||
public async Task GetProgramsAsync_TokenRejected_LogsInAgain(int code, string response)
|
||||
{
|
||||
var server = new FakeServer { Md5RejectionBody = ErrorBody(code, response) };
|
||||
using var provider = CreateProvider(server);
|
||||
|
||||
var programs = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.NotEmpty(programs);
|
||||
Assert.Equal(2, server.Counts["/token"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetProgramsAsync_ServiceOffline_DoesNotLogInAgainDuringTheOfflineHour()
|
||||
{
|
||||
var server = new FakeServer { StatusCode = 3000 };
|
||||
using var provider = CreateProvider(server);
|
||||
|
||||
for (var i = 0; i < 3; i++)
|
||||
{
|
||||
await Assert.ThrowsAnyAsync<Exception>(
|
||||
() => provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken));
|
||||
}
|
||||
|
||||
// MAX_LOGIN_ATTEMPTS counts logins, so each channel of a refresh must not log in again.
|
||||
Assert.Equal(1, server.Counts["/token"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetProgramsAsync_AfterImageNotFoundAndRestart_StopsOfferingTheUri()
|
||||
{
|
||||
var server = new FakeServer { HasImageArtwork = true };
|
||||
|
||||
using (var provider = CreateProvider(server))
|
||||
{
|
||||
var first = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
|
||||
provider.ReportImageDownloadFailure(first.First().ImageUrl!, HttpStatusCode.NotFound, ErrorBody(5000, "IMAGE_NOT_FOUND"));
|
||||
}
|
||||
|
||||
// A second provider over the same cache directory stands in for a restart.
|
||||
using (var provider = CreateProvider(server))
|
||||
{
|
||||
var second = await provider.GetProgramsAsync(_info, "20454", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), TestContext.Current.CancellationToken);
|
||||
Assert.All(second, p => Assert.Null(p.ImageUrl));
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(HttpStatusCode.Forbidden, 4009, "TOO_MANY_LOGINS")]
|
||||
[InlineData(HttpStatusCode.OK, 4009, "TOO_MANY_LOGINS")]
|
||||
[InlineData(HttpStatusCode.Forbidden, 4010, "TOO_MANY_UNIQUE_IPS")]
|
||||
public async Task GetLineups_AccountLimitReached_StopsLoggingIn(HttpStatusCode status, int code, string response)
|
||||
{
|
||||
var login = new Response
|
||||
{
|
||||
Status = status,
|
||||
Body = ErrorBody(code, response)
|
||||
};
|
||||
using var provider = CreateProvider(login, await GetHeadendsResponse());
|
||||
|
||||
await Assert.ThrowsAnyAsync<Exception>(() => provider.GetLineups(_info, "USA", "90210"));
|
||||
|
||||
// These codes count logins, so continuing to ask for a token is what earned them: a
|
||||
// retry must not go back to /token even once the credentials would work.
|
||||
login.Status = HttpStatusCode.OK;
|
||||
login.Body = await GetTokenResponse();
|
||||
await Assert.ThrowsAnyAsync<Exception>(() => provider.GetLineups(_info, "USA", "90210"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetChannels_SystemOffline_ThrowsInsteadOfReportingNoChannels()
|
||||
{
|
||||
var server = new FakeServer { SystemStatus = "Offline" };
|
||||
using var provider = CreateProvider(server);
|
||||
|
||||
// No channels means no channel is mapped, which the guide refresh reads as "no programs".
|
||||
await Assert.ThrowsAnyAsync<Exception>(
|
||||
() => provider.GetChannels(new ListingsProviderInfo { Username = "user", Password = "password", ListingsId = "USA-OTA-90210" }, TestContext.Current.CancellationToken));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetChannels_LoginBackedOff_ThrowsInsteadOfReportingNoChannels()
|
||||
{
|
||||
var login = new Response { Status = HttpStatusCode.Forbidden, Body = ErrorBody(4009, "TOO_MANY_LOGINS") };
|
||||
using var provider = CreateProvider(login, await GetHeadendsResponse());
|
||||
var info = new ListingsProviderInfo { Username = "user", Password = "password", ListingsId = "USA-OTA-90210" };
|
||||
|
||||
await Assert.ThrowsAnyAsync<Exception>(() => provider.GetChannels(info, TestContext.Current.CancellationToken));
|
||||
|
||||
// The second call never reaches /token, so this is the null-token path.
|
||||
await Assert.ThrowsAnyAsync<Exception>(() => provider.GetChannels(info, TestContext.Current.CancellationToken));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetChannels_UnchangedModifiedDate_ServesTheLineupFromCache()
|
||||
{
|
||||
var server = new FakeServer();
|
||||
using var provider = CreateProvider(server);
|
||||
var info = new ListingsProviderInfo { Username = "user", Password = "password", ListingsId = "USA-OTA-90210" };
|
||||
|
||||
var first = await provider.GetChannels(info, TestContext.Current.CancellationToken);
|
||||
Assert.NotEmpty(first);
|
||||
Assert.Equal(1, server.Counts["/lineups/USA-OTA-90210"]);
|
||||
|
||||
var second = await provider.GetChannels(info, TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.Equal(first.Count, second.Count);
|
||||
Assert.Equal(1, server.Counts["/lineups/USA-OTA-90210"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetChannels_NewerModifiedDate_DownloadsTheLineupAgain()
|
||||
{
|
||||
var server = new FakeServer();
|
||||
var info = new ListingsProviderInfo { Username = "user", Password = "password", ListingsId = "USA-OTA-90210" };
|
||||
|
||||
using (var provider = CreateProvider(server))
|
||||
{
|
||||
await provider.GetChannels(info, TestContext.Current.CancellationToken);
|
||||
}
|
||||
|
||||
server.LineupModified = server.LineupModified.AddDays(1);
|
||||
|
||||
// A second provider over the same cache directory stands in for a restart, which is also
|
||||
// what makes the status re-read rather than coming from its short-lived cache.
|
||||
using (var provider = CreateProvider(server))
|
||||
{
|
||||
await provider.GetChannels(info, TestContext.Current.CancellationToken);
|
||||
}
|
||||
|
||||
Assert.Equal(2, server.Counts["/lineups/USA-OTA-90210"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Validate_LineupCannotBeAdded_Throws()
|
||||
{
|
||||
var server = new FakeServer { LineupPutStatus = HttpStatusCode.BadRequest };
|
||||
using var provider = CreateProvider(server);
|
||||
|
||||
// Silently reporting success leaves the user with a provider that has no lineup.
|
||||
await Assert.ThrowsAnyAsync<Exception>(
|
||||
() => provider.Validate(new ListingsProviderInfo { Username = "user", Password = "password", ListingsId = "USA-OTA-90210" }, true, true));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && Directory.Exists(_cachePath))
|
||||
{
|
||||
Directory.Delete(_cachePath, true);
|
||||
}
|
||||
}
|
||||
|
||||
// Schedules Direct answers an exhausted image quota with HTTP 200 and a body like this, which
|
||||
// ProviderManager surfaces as a synthetic 404 because it is not an image content type.
|
||||
private static string ErrorBody(int code, string response)
|
||||
=> string.Format(
|
||||
CultureInfo.InvariantCulture,
|
||||
"{{\"response\":\"{0}\",\"code\":{1},\"serverID\":\"20141201.debug\",\"message\":\"test\",\"datetime\":\"2026-09-14T00:00:00Z\"}}",
|
||||
response,
|
||||
code);
|
||||
|
||||
private static async Task<Response> CreateSuccessfulLogin()
|
||||
=> new() { Status = HttpStatusCode.OK, Body = await GetTokenResponse() };
|
||||
|
||||
private static Response CreateFailedLogin()
|
||||
=> new() { Status = HttpStatusCode.BadRequest, Body = InvalidUserResponse };
|
||||
|
||||
private static Task<string> GetTokenResponse()
|
||||
=> File.ReadAllTextAsync("Test Data/SchedulesDirect/token_live_response.json", TestContext.Current.CancellationToken);
|
||||
|
||||
private static Task<string> GetHeadendsResponse()
|
||||
=> File.ReadAllTextAsync("Test Data/SchedulesDirect/headends_response.json", TestContext.Current.CancellationToken);
|
||||
|
||||
private SchedulesDirectProvider CreateProvider(Response login, string headendsResponse)
|
||||
{
|
||||
var messageHandler = new Mock<HttpMessageHandler>();
|
||||
messageHandler.Protected()
|
||||
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
|
||||
.Returns<HttpRequestMessage, CancellationToken>((m, _) =>
|
||||
{
|
||||
var path = m.RequestUri!.AbsolutePath;
|
||||
if (path.EndsWith("/token", StringComparison.Ordinal))
|
||||
{
|
||||
return Task.FromResult(new HttpResponseMessage(login.Status)
|
||||
{
|
||||
Content = new StringContent(login.Body)
|
||||
});
|
||||
}
|
||||
|
||||
if (path.EndsWith("/headends", StringComparison.Ordinal))
|
||||
{
|
||||
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
|
||||
{
|
||||
Content = new StringContent(headendsResponse)
|
||||
});
|
||||
}
|
||||
|
||||
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.NotFound));
|
||||
});
|
||||
|
||||
var httpClientFactory = new Mock<IHttpClientFactory>();
|
||||
httpClientFactory.Setup(x => x.CreateClient(It.IsAny<string>()))
|
||||
.Returns(() => new HttpClient(messageHandler.Object));
|
||||
|
||||
var appPaths = new Mock<IApplicationPaths>();
|
||||
appPaths.SetupGet(x => x.CachePath).Returns(_cachePath);
|
||||
|
||||
return new SchedulesDirectProvider(
|
||||
NullLogger<SchedulesDirectProvider>.Instance,
|
||||
httpClientFactory.Object,
|
||||
appPaths.Object,
|
||||
_libraryManager.Object);
|
||||
}
|
||||
|
||||
private SchedulesDirectProvider CreateProvider(FakeServer server)
|
||||
{
|
||||
var messageHandler = new Mock<HttpMessageHandler>();
|
||||
messageHandler.Protected()
|
||||
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
|
||||
.Returns<HttpRequestMessage, CancellationToken>((m, ct) => server.Respond(m, ct));
|
||||
|
||||
var httpClientFactory = new Mock<IHttpClientFactory>();
|
||||
httpClientFactory.Setup(x => x.CreateClient(It.IsAny<string>()))
|
||||
.Returns(() => new HttpClient(messageHandler.Object));
|
||||
|
||||
var appPaths = new Mock<IApplicationPaths>();
|
||||
appPaths.SetupGet(x => x.CachePath).Returns(_cachePath);
|
||||
|
||||
return new SchedulesDirectProvider(
|
||||
NullLogger<SchedulesDirectProvider>.Instance,
|
||||
httpClientFactory.Object,
|
||||
appPaths.Object,
|
||||
_libraryManager.Object);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A minimal stand-in for the Schedules Direct API that counts what was asked of it.
|
||||
/// </summary>
|
||||
private sealed class FakeServer
|
||||
{
|
||||
public Dictionary<string, int> Counts { get; } = new(StringComparer.Ordinal);
|
||||
|
||||
public string SystemStatus { get; set; } = "Online";
|
||||
|
||||
public int StatusCode { get; set; }
|
||||
|
||||
public string Md5Salt { get; set; } = "base";
|
||||
|
||||
public HttpStatusCode LineupPutStatus { get; set; } = HttpStatusCode.OK;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a per-day error code for the hash endpoint, as SCHEDULE_RANGE_EXCEEDED is
|
||||
/// reported. Zero means every day is available.
|
||||
/// </summary>
|
||||
public int Md5ErrorCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the retryTime of a SCHEDULE_QUEUED response, which the spec documents as a
|
||||
/// bare object rather than the usual array.
|
||||
/// </summary>
|
||||
public DateTime? ScheduleRetryTime { get; set; }
|
||||
|
||||
public bool HasImageArtwork { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a date whose schedule md5 differs from the one first reported.
|
||||
/// </summary>
|
||||
public string? ChangedDate { get; set; }
|
||||
|
||||
public List<string> RequestedDates { get; private set; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a per-program error code for the programs endpoint, such as PROGRAMID_QUEUED.
|
||||
/// </summary>
|
||||
public int ProgramErrorCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a body the hash endpoint rejects the next request's token with, as HTTP 403.
|
||||
/// </summary>
|
||||
public string? Md5RejectionBody { get; set; }
|
||||
|
||||
public HttpStatusCode Md5RejectionStatus { get; set; } = HttpStatusCode.Forbidden;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether each day airs a second program.
|
||||
/// </summary>
|
||||
public bool SecondProgram { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the only program <see cref="ProgramErrorCode"/> applies to; null means all of them.
|
||||
/// </summary>
|
||||
public string? QueuedProgramId { get; set; }
|
||||
|
||||
public string ImageUri { get; set; } = "assets/p1_b.jpg";
|
||||
|
||||
public DateTime LineupModified { get; set; } = new(2026, 9, 14, 0, 0, 0, DateTimeKind.Utc);
|
||||
|
||||
public async Task<HttpResponseMessage> Respond(HttpRequestMessage message, CancellationToken cancellationToken)
|
||||
{
|
||||
var path = message.RequestUri!.AbsolutePath.Replace("/20141201", string.Empty, StringComparison.Ordinal).TrimEnd('/');
|
||||
Counts[path] = Counts.GetValueOrDefault(path) + 1;
|
||||
|
||||
string body;
|
||||
switch (path)
|
||||
{
|
||||
case "/token":
|
||||
body = "{\"code\":0,\"message\":\"OK\",\"serverID\":\"test\",\"token\":\"f3fca79989cafe7dead71beefedc812b\"}";
|
||||
break;
|
||||
|
||||
case "/status":
|
||||
body = "{\"code\":" + StatusCode.ToString(CultureInfo.InvariantCulture) + ",\"account\":{\"maxLineups\":4},\"lineups\":[{\"lineup\":\"USA-OTA-90210\",\"modified\":\""
|
||||
+ LineupModified.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture)
|
||||
+ "\"}],\"systemStatus\":[{\"status\":\"" + SystemStatus + "\",\"message\":\"test\"}]}";
|
||||
break;
|
||||
|
||||
case "/schedules/md5":
|
||||
if (Md5RejectionBody is not null)
|
||||
{
|
||||
var rejection = Md5RejectionBody;
|
||||
Md5RejectionBody = null;
|
||||
return new HttpResponseMessage(Md5RejectionStatus) { Content = new StringContent(rejection) };
|
||||
}
|
||||
|
||||
RequestedDates = await ReadDates(message, cancellationToken).ConfigureAwait(false);
|
||||
body = BuildMd5Response(RequestedDates);
|
||||
break;
|
||||
|
||||
case "/schedules":
|
||||
body = BuildSchedulesResponse(await ReadDates(message, cancellationToken).ConfigureAwait(false));
|
||||
break;
|
||||
|
||||
case "/programs":
|
||||
var ids = JsonSerializer.Deserialize<List<string>>(await message.Content!.ReadAsStringAsync(cancellationToken).ConfigureAwait(false))!;
|
||||
body = "[" + string.Join(',', ids.Select(BuildProgramEntry)) + "]";
|
||||
break;
|
||||
|
||||
case "/metadata/programs":
|
||||
body = HasImageArtwork
|
||||
? "[{\"programID\":\"EP000000010001\",\"data\":[{\"width\":\"120\",\"height\":\"180\",\"uri\":\""
|
||||
+ ImageUri + "\",\"aspect\":\"2x3\",\"category\":\"Iconic\",\"text\":\"yes\",\"tier\":\"Series\"}]}]"
|
||||
: "[]";
|
||||
break;
|
||||
|
||||
case "/lineups/USA-OTA-90210":
|
||||
if (message.Method == HttpMethod.Get)
|
||||
{
|
||||
body = "{\"map\":[{\"stationID\":\"20454\",\"channel\":\"5\"}],\"stations\":[{\"stationID\":\"20454\",\"name\":\"Test\",\"callsign\":\"TST\"}]}";
|
||||
break;
|
||||
}
|
||||
|
||||
return new HttpResponseMessage(LineupPutStatus)
|
||||
{
|
||||
Content = new StringContent(LineupPutStatus == HttpStatusCode.OK
|
||||
? "{\"code\":0,\"message\":\"OK\"}"
|
||||
: "{\"response\":\"MAX_LINEUPS\",\"code\":4101,\"message\":\"Exceeded number of lineups.\"}")
|
||||
};
|
||||
|
||||
case "/lineups":
|
||||
body = "{\"code\":0,\"lineups\":[]}";
|
||||
break;
|
||||
|
||||
default:
|
||||
return new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent("{}") };
|
||||
}
|
||||
|
||||
return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(body) };
|
||||
}
|
||||
|
||||
private static async Task<List<string>> ReadDates(HttpRequestMessage message, CancellationToken cancellationToken)
|
||||
{
|
||||
var requestBody = await message.Content!.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
|
||||
using var doc = JsonDocument.Parse(requestBody);
|
||||
|
||||
var dates = new List<string>();
|
||||
foreach (var station in doc.RootElement.EnumerateArray())
|
||||
{
|
||||
foreach (var date in station.GetProperty("date").EnumerateArray())
|
||||
{
|
||||
dates.Add(date.GetString()!);
|
||||
}
|
||||
}
|
||||
|
||||
return dates;
|
||||
}
|
||||
|
||||
private string BuildMd5Response(List<string> dates)
|
||||
{
|
||||
var days = dates.ConvertAll(d => Md5ErrorCode == 0
|
||||
? "\"" + d + "\":{\"code\":0,\"message\":\"OK\",\"lastModified\":\"2026-09-14T00:00:00Z\",\"md5\":\"" + GetMd5(d) + "\"}"
|
||||
: "\"" + d + "\":{\"code\":" + Md5ErrorCode.ToString(CultureInfo.InvariantCulture) + ",\"message\":\"Date requested not within range.\"}");
|
||||
|
||||
return "{\"20454\":{" + string.Join(',', days) + "}}";
|
||||
}
|
||||
|
||||
private string GetMd5(string date)
|
||||
=> (string.Equals(date, ChangedDate, StringComparison.Ordinal) ? "changed" : Md5Salt) + date;
|
||||
|
||||
private string BuildProgramEntry(string id)
|
||||
=> ProgramErrorCode != 0 && (QueuedProgramId is null || string.Equals(id, QueuedProgramId, StringComparison.Ordinal))
|
||||
? "{\"programID\":\"" + id + "\",\"code\":" + ProgramErrorCode.ToString(CultureInfo.InvariantCulture) + ",\"message\":\"test\"}"
|
||||
: "{\"programID\":\"" + id + "\",\"titles\":[{\"title120\":\"Test\"}],\"hasImageArtwork\":"
|
||||
+ (HasImageArtwork ? "true" : "false") + "}";
|
||||
|
||||
private string BuildSchedulesResponse(List<string> dates)
|
||||
{
|
||||
if (ScheduleRetryTime is { } retryTime)
|
||||
{
|
||||
return "{\"response\":\"SCHEDULE_QUEUED\",\"code\":7100,\"serverID\":\"test\",\"message\":\"Queued.\",\"stationID\":\"20454\",\"retryTime\":\""
|
||||
+ retryTime.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture) + "\"}";
|
||||
}
|
||||
|
||||
var days = dates.ConvertAll(d =>
|
||||
"{\"stationID\":\"20454\",\"programs\":[{\"programID\":\"EP000000010001\",\"airDateTime\":\"" + d
|
||||
+ "T12:00:00Z\",\"duration\":1800,\"md5\":\"x\"}"
|
||||
+ (SecondProgram ? ",{\"programID\":\"EP000000020001\",\"airDateTime\":\"" + d + "T14:00:00Z\",\"duration\":1800,\"md5\":\"y\"}" : string.Empty)
|
||||
+ "],\"metadata\":{\"modified\":\"2026-09-14T00:00:00Z\",\"md5\":\""
|
||||
+ GetMd5(d) + "\",\"startDate\":\"" + d + "\"}}");
|
||||
|
||||
return "[" + string.Join(',', days) + "]";
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class Response
|
||||
{
|
||||
public HttpStatusCode Status { get; set; }
|
||||
|
||||
public string Body { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
|
|
@ -10,6 +10,7 @@ using MediaBrowser.Controller.Entities;
|
|||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Controller.LiveTv;
|
||||
using MediaBrowser.Model.Configuration;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using MediaBrowser.Model.LiveTv;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Moq;
|
||||
|
|
@ -19,6 +20,8 @@ namespace Jellyfin.LiveTv.Tests.Recordings;
|
|||
|
||||
public sealed class RecordingsMetadataManagerTests
|
||||
{
|
||||
private const string ImageUrl = "https://json.schedulesdirect.org/20141201/image/assets/p1_b.jpg?token=abc";
|
||||
|
||||
private readonly string _tempDir =
|
||||
Path.Combine(Path.GetTempPath(), "jellyfin-test-" + Guid.NewGuid());
|
||||
|
||||
|
|
@ -43,7 +46,8 @@ public sealed class RecordingsMetadataManagerTests
|
|||
var manager = new RecordingsMetadataManager(
|
||||
NullLogger<RecordingsMetadataManager>.Instance,
|
||||
config.Object,
|
||||
libraryManager.Object);
|
||||
libraryManager.Object,
|
||||
Mock.Of<ISchedulesDirectService>());
|
||||
|
||||
var timer = new TimerInfo { Name = "Test Recording", ProgramId = null };
|
||||
|
||||
|
|
@ -61,4 +65,47 @@ public sealed class RecordingsMetadataManagerTests
|
|||
|
||||
Assert.InRange(parsed, beforeUtc, afterUtc);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SaveRecordingMetadata_RemoteImage_GoesThroughSchedulesDirect()
|
||||
{
|
||||
// Schedules Direct counts every image request, so a recording must not fetch one behind its back.
|
||||
var (manager, libraryManager, schedulesDirect, recordingPath) = CreateImageManager();
|
||||
|
||||
await manager.SaveRecordingMetadata(new TimerInfo { Name = "Test Recording", ProgramId = "p1" }, recordingPath, null);
|
||||
|
||||
schedulesDirect.Verify(s => s.ConvertImageToLocal(It.IsAny<BaseItem>(), It.Is<ItemImageInfo>(i => i.Path == ImageUrl), 0), Times.Once);
|
||||
libraryManager.Verify(
|
||||
l => l.ConvertImageToLocal(It.IsAny<BaseItem>(), It.IsAny<ItemImageInfo>(), It.IsAny<int>(), It.IsAny<bool>()),
|
||||
Times.Never);
|
||||
}
|
||||
|
||||
private (RecordingsMetadataManager Manager, Mock<ILibraryManager> LibraryManager, Mock<ISchedulesDirectService> SchedulesDirect, string RecordingPath) CreateImageManager()
|
||||
{
|
||||
Directory.CreateDirectory(_tempDir);
|
||||
var recordingPath = Path.Combine(_tempDir, "test-recording.ts");
|
||||
|
||||
var config = new Mock<IConfigurationManager>();
|
||||
config.Setup(c => c.GetConfiguration("livetv"))
|
||||
.Returns(new LiveTvOptions { SaveRecordingNFO = false, SaveRecordingImages = true });
|
||||
|
||||
var program = new LiveTvProgram
|
||||
{
|
||||
Name = "Test",
|
||||
ImageInfos = [new ItemImageInfo { Path = ImageUrl, Type = ImageType.Primary }]
|
||||
};
|
||||
var libraryManager = new Mock<ILibraryManager>();
|
||||
libraryManager
|
||||
.Setup(l => l.GetItemList(It.IsAny<InternalItemsQuery>()))
|
||||
.Returns([program]);
|
||||
|
||||
var schedulesDirect = new Mock<ISchedulesDirectService>();
|
||||
var manager = new RecordingsMetadataManager(
|
||||
NullLogger<RecordingsMetadataManager>.Instance,
|
||||
config.Object,
|
||||
libraryManager.Object,
|
||||
schedulesDirect.Object);
|
||||
|
||||
return (manager, libraryManager, schedulesDirect, recordingPath);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,10 +2,13 @@ using System;
|
|||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Common.Net;
|
||||
using MediaBrowser.Controller;
|
||||
using MediaBrowser.Controller.BaseItemManager;
|
||||
using MediaBrowser.Controller.Configuration;
|
||||
|
|
@ -17,12 +20,14 @@ using MediaBrowser.Controller.MediaSegments;
|
|||
using MediaBrowser.Controller.Providers;
|
||||
using MediaBrowser.Controller.Subtitles;
|
||||
using MediaBrowser.Model.Configuration;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using MediaBrowser.Model.IO;
|
||||
using MediaBrowser.Providers.Manager;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Moq;
|
||||
using Moq.Protected;
|
||||
using Xunit;
|
||||
|
||||
// Allow Moq to see internal class
|
||||
|
|
@ -488,6 +493,36 @@ namespace Jellyfin.Providers.Tests.Manager
|
|||
Assert.Equal(ItemCount - 1, processed.Count);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(HttpStatusCode.OK, "application/json", HttpStatusCode.NotFound)]
|
||||
[InlineData(HttpStatusCode.Forbidden, "application/json", HttpStatusCode.Forbidden)]
|
||||
public async Task SaveImage_ProviderRefusesWithBody_KeepsTheBody(HttpStatusCode status, string contentType, HttpStatusCode expectedStatus)
|
||||
{
|
||||
// Schedules Direct reports an exhausted image quota as HTTP 200 with a JSON body, and
|
||||
// only the code in that body tells the guide refresh to stop requesting images.
|
||||
const string Body = "{\"response\":\"MAX_IMAGE_DOWNLOADS\",\"code\":5002}";
|
||||
|
||||
var messageHandler = new Mock<HttpMessageHandler>();
|
||||
messageHandler.Protected()
|
||||
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
|
||||
.ReturnsAsync(() => new HttpResponseMessage(status)
|
||||
{
|
||||
Content = new StringContent(Body, Encoding.UTF8, contentType)
|
||||
});
|
||||
|
||||
var httpClientFactory = new Mock<IHttpClientFactory>();
|
||||
httpClientFactory.Setup(x => x.CreateClient(It.IsAny<string>()))
|
||||
.Returns(() => new HttpClient(messageHandler.Object));
|
||||
|
||||
using var providerManager = GetProviderManager(httpClientFactory: httpClientFactory.Object);
|
||||
|
||||
var ex = await Assert.ThrowsAsync<HttpResponseBodyException>(
|
||||
() => providerManager.SaveImage(new Movie(), "https://json.schedulesdirect.org/20141201/image/a.jpg", ImageType.Primary, null, CancellationToken.None));
|
||||
|
||||
Assert.Equal(expectedStatus, ex.StatusCode);
|
||||
Assert.Equal(Body, ex.ResponseBody);
|
||||
}
|
||||
|
||||
private static void GetMetadataProviders_CanRefreshMetadata_Tester(
|
||||
string providerType,
|
||||
bool expected,
|
||||
|
|
@ -666,7 +701,8 @@ namespace Jellyfin.Providers.Tests.Manager
|
|||
ServerConfiguration? serverConfiguration = null,
|
||||
LibraryOptions? libraryOptions = null,
|
||||
IBaseItemManager? baseItemManager = null,
|
||||
ILibraryManager? libraryManager = null)
|
||||
ILibraryManager? libraryManager = null,
|
||||
IHttpClientFactory? httpClientFactory = null)
|
||||
{
|
||||
var serverConfigurationManager = new Mock<IServerConfigurationManager>(MockBehavior.Strict);
|
||||
serverConfigurationManager.Setup(i => i.Configuration)
|
||||
|
|
@ -683,7 +719,7 @@ namespace Jellyfin.Providers.Tests.Manager
|
|||
}
|
||||
|
||||
var providerManager = new ProviderManager(
|
||||
Mock.Of<IHttpClientFactory>(),
|
||||
httpClientFactory ?? Mock.Of<IHttpClientFactory>(),
|
||||
Mock.Of<ISubtitleManager>(),
|
||||
serverConfigurationManager.Object,
|
||||
Mock.Of<ILibraryMonitor>(),
|
||||
|
|
|
|||
|
|
@ -0,0 +1,50 @@
|
|||
using System;
|
||||
using System.Net;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AutoFixture;
|
||||
using AutoFixture.AutoMoq;
|
||||
using Emby.Naming.Common;
|
||||
using MediaBrowser.Common.Net;
|
||||
using MediaBrowser.Controller.Configuration;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Entities.Movies;
|
||||
using MediaBrowser.Controller.Providers;
|
||||
using MediaBrowser.Model.Configuration;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
using ServerLibraryManager = Emby.Server.Implementations.Library.LibraryManager;
|
||||
|
||||
namespace Jellyfin.Server.Implementations.Tests.Library;
|
||||
|
||||
public class LibraryManagerConvertImageTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(HttpStatusCode.NotFound)]
|
||||
[InlineData(HttpStatusCode.Forbidden)]
|
||||
public async Task ConvertImageToLocal_RejectedDownload_KeepsTheResponseAsInnerException(HttpStatusCode status)
|
||||
{
|
||||
// The Live TV guide reads the provider's error body off the inner exception to stop
|
||||
// requesting images from an account that is refusing them.
|
||||
const string Body = "{\"response\":\"MAX_IMAGE_DOWNLOADS\",\"code\":5002}";
|
||||
var rejection = new HttpResponseBodyException("test", status, Body);
|
||||
|
||||
var fixture = new Fixture().Customize(new AutoMoqCustomization());
|
||||
fixture.Register(() => new NamingOptions());
|
||||
var configuration = fixture.Freeze<Mock<IServerConfigurationManager>>();
|
||||
configuration.Setup(c => c.Configuration).Returns(new ServerConfiguration());
|
||||
configuration.Setup(c => c.ApplicationPaths.ProgramDataPath).Returns("/data");
|
||||
var providerManager = fixture.Freeze<Mock<IProviderManager>>();
|
||||
providerManager.Setup(p => p.SaveImage(It.IsAny<BaseItem>(), It.IsAny<string>(), It.IsAny<ImageType>(), It.IsAny<int?>(), It.IsAny<CancellationToken>()))
|
||||
.ThrowsAsync(rejection);
|
||||
var manager = fixture.Create<ServerLibraryManager>();
|
||||
|
||||
var item = new Movie { Id = Guid.NewGuid(), Name = "Test" };
|
||||
var image = new ItemImageInfo { Path = "https://example.com/image.jpg", Type = ImageType.Primary };
|
||||
|
||||
var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => manager.ConvertImageToLocal(item, image, 0, removeOnFailure: false));
|
||||
|
||||
Assert.Same(rejection, ex.InnerException);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue