Files
Libation/Source/FileLiberator/WidevineRecommendation.cs
William Tanksley Jr 452b0ebf2c refactor(recovery): remove defensive AAX fallback workaround (#2083) (#2147)
AudibleApi 14.1.3 preserves and rethrows the primary ApiErrorException
when the secondary AAX download fallback fails, rather than swallowing
it and throwing ApplicationException("Book has no codec specified").

With AudibleApi 14.1.3:
- Bump AudibleApi package reference to 14.1.3
- Remove NoCodecSpecifiedMarker, NoAvailableCodecsMarker, and
  IsAaxFallbackMissingCodecError from WidevineRecommendation
- Restore ShouldRecommendWidevine signature to take ApiErrorException
- Update catch clauses in ProcessBookViewModel and _ProcessableOptionsBase
  to catch ApiErrorException directly
- Remove obsolete codec-missing tests in WidevineRecommendationTests
2026-11-12 18:41:28 -07:00

65 lines
2.5 KiB
C#

using AudibleApi;
using LibationFileManager;
using System;
namespace FileLiberator;
/// <summary>
/// Detects a rare Audible API failure mode where ADRM licenserequest returns Sable error 000307
/// with acr:null.
/// Some titles (e.g. B08XDZCH78 in the US marketplace, or non-US titles where legacy AAX is disabled)
/// only deliver via Widevine.
/// Keep all detection here so this edge case does not spread through download or UI code.
/// </summary>
public static class WidevineRecommendation
{
/// <summary>
/// Audible error code seen when Sable cannot resolve asset details for ADRM licenserequest.
/// Not diagnostic on its own — also returned for unrelated API issues (e.g. empty response_groups).
/// </summary>
public const string SableAssetErrorCode = "000307";
/// <summary>
/// Substring in the Sable error message when no ADRM content reference exists for the title.
/// Together with <see cref="SableAssetErrorCode"/> on licenserequest, this is the best signal we have.
/// </summary>
public const string SableAcrNullMarker = "acr:null";
/// <summary>
/// True when <paramref name="ex"/> matches the rare ADRM licenserequest / Sable acr:null pattern.
/// </summary>
public static bool IsAdrmLicenseUnavailableError(ApiErrorException ex)
{
ArgumentNullException.ThrowIfNull(ex);
if (ex.RequestUri is not { } requestUri
|| !requestUri.Contains("/licenserequest", StringComparison.OrdinalIgnoreCase))
return false;
if (ex.JsonMessage is not { } json)
return false;
return json.Contains(SableAssetErrorCode, StringComparison.Ordinal)
&& json.Contains(SableAcrNullMarker, StringComparison.Ordinal);
}
/// <summary>
/// True when Libation should suggest enabling Widevine for this failure.
/// </summary>
public static bool ShouldRecommendWidevine(ApiErrorException ex, Configuration config)
{
ArgumentNullException.ThrowIfNull(ex);
ArgumentNullException.ThrowIfNull(config);
return !config.UseWidevine && IsAdrmLicenseUnavailableError(ex);
}
/// <summary>One line for logs and CLI when <see cref="ShouldRecommendWidevine(ApiErrorException, Configuration)"/> applies.</summary>
public static string BuildLogSummary(string bookTitleWithSubtitle, string? accountId = null)
{
var accountSuffix = string.IsNullOrWhiteSpace(accountId) ? "" : $" for account '{accountId}'";
return $"{bookTitleWithSubtitle}: Standard (ADRM) license unavailable. "
+ $"Libation enabled Widevine DRM and marked account{accountSuffix} for re-registration. Sign in to complete setup.";
}
}