refactor to unify data fetching and define abstract API for re-use
This commit is contained in:
@@ -6,6 +6,7 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Plugin.SRFPlay.Api;
|
||||
using Jellyfin.Plugin.SRFPlay.Api.Models;
|
||||
using Jellyfin.Plugin.SRFPlay.Services.Interfaces;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Plugin.SRFPlay.Services;
|
||||
@@ -13,7 +14,7 @@ namespace Jellyfin.Plugin.SRFPlay.Services;
|
||||
/// <summary>
|
||||
/// Service for managing topic/category data and filtering.
|
||||
/// </summary>
|
||||
public class CategoryService
|
||||
public class CategoryService : ICategoryService
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly ILoggerFactory _loggerFactory;
|
||||
|
||||
@@ -3,10 +3,9 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Plugin.SRFPlay.Api;
|
||||
using Jellyfin.Plugin.SRFPlay.Services.Interfaces;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Model.IO;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Plugin.SRFPlay.Services;
|
||||
@@ -14,13 +13,12 @@ namespace Jellyfin.Plugin.SRFPlay.Services;
|
||||
/// <summary>
|
||||
/// Service for managing content expiration.
|
||||
/// </summary>
|
||||
public class ContentExpirationService
|
||||
public class ContentExpirationService : IContentExpirationService
|
||||
{
|
||||
private readonly ILogger<ContentExpirationService> _logger;
|
||||
private readonly ILoggerFactory _loggerFactory;
|
||||
private readonly ILibraryManager _libraryManager;
|
||||
private readonly StreamUrlResolver _streamResolver;
|
||||
private readonly MetadataCache _metadataCache;
|
||||
private readonly IStreamUrlResolver _streamResolver;
|
||||
private readonly IMediaCompositionFetcher _compositionFetcher;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ContentExpirationService"/> class.
|
||||
@@ -28,18 +26,17 @@ public class ContentExpirationService
|
||||
/// <param name="loggerFactory">The logger factory.</param>
|
||||
/// <param name="libraryManager">The library manager.</param>
|
||||
/// <param name="streamResolver">The stream URL resolver.</param>
|
||||
/// <param name="metadataCache">The metadata cache.</param>
|
||||
/// <param name="compositionFetcher">The media composition fetcher.</param>
|
||||
public ContentExpirationService(
|
||||
ILoggerFactory loggerFactory,
|
||||
ILibraryManager libraryManager,
|
||||
StreamUrlResolver streamResolver,
|
||||
MetadataCache metadataCache)
|
||||
IStreamUrlResolver streamResolver,
|
||||
IMediaCompositionFetcher compositionFetcher)
|
||||
{
|
||||
_loggerFactory = loggerFactory;
|
||||
_logger = loggerFactory.CreateLogger<ContentExpirationService>();
|
||||
_libraryManager = libraryManager;
|
||||
_streamResolver = streamResolver;
|
||||
_metadataCache = metadataCache;
|
||||
_compositionFetcher = compositionFetcher;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -119,26 +116,7 @@ public class ContentExpirationService
|
||||
return false;
|
||||
}
|
||||
|
||||
var config = Plugin.Instance?.Configuration;
|
||||
if (config == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Try cache first
|
||||
var mediaComposition = _metadataCache.GetMediaComposition(urn, config.CacheDurationMinutes);
|
||||
|
||||
// If not in cache, fetch from API
|
||||
if (mediaComposition == null)
|
||||
{
|
||||
using var apiClient = new SRFApiClient(_loggerFactory);
|
||||
mediaComposition = await apiClient.GetMediaCompositionByUrnAsync(urn, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (mediaComposition != null)
|
||||
{
|
||||
_metadataCache.SetMediaComposition(urn, mediaComposition);
|
||||
}
|
||||
}
|
||||
var mediaComposition = await _compositionFetcher.GetMediaCompositionAsync(urn, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (mediaComposition?.ChapterList == null || mediaComposition.ChapterList.Count == 0)
|
||||
{
|
||||
@@ -196,24 +174,7 @@ public class ContentExpirationService
|
||||
continue;
|
||||
}
|
||||
|
||||
var config = Plugin.Instance?.Configuration;
|
||||
if (config == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var mediaComposition = _metadataCache.GetMediaComposition(urn, config.CacheDurationMinutes);
|
||||
|
||||
if (mediaComposition == null)
|
||||
{
|
||||
using var apiClient = new SRFApiClient(_loggerFactory);
|
||||
mediaComposition = await apiClient.GetMediaCompositionByUrnAsync(urn, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (mediaComposition != null)
|
||||
{
|
||||
_metadataCache.SetMediaComposition(urn, mediaComposition);
|
||||
}
|
||||
}
|
||||
var mediaComposition = await _compositionFetcher.GetMediaCompositionAsync(urn, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (mediaComposition?.ChapterList != null && mediaComposition.ChapterList.Count > 0)
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@ using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Plugin.SRFPlay.Api;
|
||||
using Jellyfin.Plugin.SRFPlay.Api.Models;
|
||||
using Jellyfin.Plugin.SRFPlay.Services.Interfaces;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Plugin.SRFPlay.Services;
|
||||
@@ -12,24 +12,22 @@ namespace Jellyfin.Plugin.SRFPlay.Services;
|
||||
/// <summary>
|
||||
/// Service for refreshing content from SRF API.
|
||||
/// </summary>
|
||||
public class ContentRefreshService
|
||||
public class ContentRefreshService : IContentRefreshService
|
||||
{
|
||||
private readonly ILogger<ContentRefreshService> _logger;
|
||||
private readonly ILoggerFactory _loggerFactory;
|
||||
private readonly MetadataCache _metadataCache;
|
||||
private readonly ISRFApiClientFactory _apiClientFactory;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ContentRefreshService"/> class.
|
||||
/// </summary>
|
||||
/// <param name="loggerFactory">The logger factory.</param>
|
||||
/// <param name="metadataCache">The metadata cache.</param>
|
||||
/// <param name="apiClientFactory">The API client factory.</param>
|
||||
public ContentRefreshService(
|
||||
ILoggerFactory loggerFactory,
|
||||
MetadataCache metadataCache)
|
||||
ISRFApiClientFactory apiClientFactory)
|
||||
{
|
||||
_loggerFactory = loggerFactory;
|
||||
_logger = loggerFactory.CreateLogger<ContentRefreshService>();
|
||||
_metadataCache = metadataCache;
|
||||
_apiClientFactory = apiClientFactory;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -52,7 +50,7 @@ public class ContentRefreshService
|
||||
|
||||
_logger.LogInformation("Refreshing latest content for business unit: {BusinessUnit}", config.BusinessUnit);
|
||||
|
||||
using var apiClient = new SRFApiClient(_loggerFactory);
|
||||
using var apiClient = _apiClientFactory.CreateClient();
|
||||
var businessUnit = config.BusinessUnit.ToString().ToLowerInvariant();
|
||||
|
||||
// Get all shows from Play v3 API
|
||||
@@ -167,7 +165,7 @@ public class ContentRefreshService
|
||||
|
||||
_logger.LogInformation("Refreshing trending content for business unit: {BusinessUnit}", config.BusinessUnit);
|
||||
|
||||
using var apiClient = new SRFApiClient(_loggerFactory);
|
||||
using var apiClient = _apiClientFactory.CreateClient();
|
||||
var businessUnit = config.BusinessUnit.ToString().ToLowerInvariant();
|
||||
|
||||
// Get all shows from Play v3 API
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Plugin.SRFPlay.Api.Models;
|
||||
|
||||
namespace Jellyfin.Plugin.SRFPlay.Services.Interfaces;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for managing topic/category data and filtering.
|
||||
/// </summary>
|
||||
public interface ICategoryService
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets all topics for a business unit.
|
||||
/// </summary>
|
||||
/// <param name="businessUnit">The business unit.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>List of topics.</returns>
|
||||
Task<List<PlayV3Topic>> GetTopicsAsync(string businessUnit, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a topic by ID.
|
||||
/// </summary>
|
||||
/// <param name="topicId">The topic ID.</param>
|
||||
/// <param name="businessUnit">The business unit.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>The topic, or null if not found.</returns>
|
||||
Task<PlayV3Topic?> GetTopicByIdAsync(string topicId, string businessUnit, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Filters shows by topic ID.
|
||||
/// </summary>
|
||||
/// <param name="shows">The shows to filter.</param>
|
||||
/// <param name="topicId">The topic ID to filter by.</param>
|
||||
/// <returns>Filtered list of shows.</returns>
|
||||
IReadOnlyList<PlayV3Show> FilterShowsByTopic(IReadOnlyList<PlayV3Show> shows, string topicId);
|
||||
|
||||
/// <summary>
|
||||
/// Groups shows by their topics.
|
||||
/// </summary>
|
||||
/// <param name="shows">The shows to group.</param>
|
||||
/// <returns>Dictionary mapping topic IDs to shows.</returns>
|
||||
IReadOnlyDictionary<string, List<PlayV3Show>> GroupShowsByTopics(IReadOnlyList<PlayV3Show> shows);
|
||||
|
||||
/// <summary>
|
||||
/// Gets shows for a specific topic, sorted by number of episodes.
|
||||
/// </summary>
|
||||
/// <param name="topicId">The topic ID.</param>
|
||||
/// <param name="businessUnit">The business unit.</param>
|
||||
/// <param name="maxResults">Maximum number of results to return.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>List of shows for the topic.</returns>
|
||||
Task<List<PlayV3Show>> GetShowsByTopicAsync(
|
||||
string topicId,
|
||||
string businessUnit,
|
||||
int maxResults = 50,
|
||||
CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Gets video count for each topic.
|
||||
/// </summary>
|
||||
/// <param name="shows">The shows to analyze.</param>
|
||||
/// <returns>Dictionary mapping topic IDs to video counts.</returns>
|
||||
IReadOnlyDictionary<string, int> GetVideoCountByTopic(IReadOnlyList<PlayV3Show> shows);
|
||||
|
||||
/// <summary>
|
||||
/// Clears the topics cache.
|
||||
/// </summary>
|
||||
void ClearCache();
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Jellyfin.Plugin.SRFPlay.Services.Interfaces;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for managing content expiration.
|
||||
/// </summary>
|
||||
public interface IContentExpirationService
|
||||
{
|
||||
/// <summary>
|
||||
/// Checks for expired content and removes it from the library.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>The number of items removed.</returns>
|
||||
Task<int> CheckAndRemoveExpiredContentAsync(CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Gets statistics about content expiration.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>Tuple with total count, expired count, and items expiring soon.</returns>
|
||||
Task<(int Total, int Expired, int ExpiringSoon)> GetExpirationStatisticsAsync(CancellationToken cancellationToken);
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Jellyfin.Plugin.SRFPlay.Services.Interfaces;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for refreshing content from SRF API.
|
||||
/// </summary>
|
||||
public interface IContentRefreshService
|
||||
{
|
||||
/// <summary>
|
||||
/// Refreshes latest content from SRF API using Play v3.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>List of URNs for new content.</returns>
|
||||
Task<List<string>> RefreshLatestContentAsync(CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Refreshes trending content from SRF API using Play v3.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>List of URNs for trending content.</returns>
|
||||
Task<List<string>> RefreshTrendingContentAsync(CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Refreshes all content (latest and trending).
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>Tuple with counts of latest and trending items.</returns>
|
||||
Task<(int LatestCount, int TrendingCount)> RefreshAllContentAsync(CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Gets content recommendations (combines latest and trending).
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>List of recommended URNs.</returns>
|
||||
Task<List<string>> GetRecommendedContentAsync(CancellationToken cancellationToken);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Plugin.SRFPlay.Api.Models;
|
||||
|
||||
namespace Jellyfin.Plugin.SRFPlay.Services.Interfaces;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for fetching media composition with caching support.
|
||||
/// </summary>
|
||||
public interface IMediaCompositionFetcher
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets media composition by URN, using cache if available.
|
||||
/// </summary>
|
||||
/// <param name="urn">The URN to fetch.</param>
|
||||
/// <param name="cancellationToken">Cancellation token.</param>
|
||||
/// <param name="cacheDurationOverride">Optional override for cache duration (e.g., 5 min for livestreams).</param>
|
||||
/// <returns>The media composition, or null if not found.</returns>
|
||||
Task<MediaComposition?> GetMediaCompositionAsync(
|
||||
string urn,
|
||||
CancellationToken cancellationToken,
|
||||
int? cacheDurationOverride = null);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using Jellyfin.Plugin.SRFPlay.Api.Models;
|
||||
|
||||
namespace Jellyfin.Plugin.SRFPlay.Services.Interfaces;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for caching metadata from SRF API.
|
||||
/// </summary>
|
||||
public interface IMetadataCache
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets cached media composition by URN.
|
||||
/// </summary>
|
||||
/// <param name="urn">The URN.</param>
|
||||
/// <param name="cacheDurationMinutes">The cache duration in minutes.</param>
|
||||
/// <returns>The cached media composition, or null if not found or expired.</returns>
|
||||
MediaComposition? GetMediaComposition(string urn, int cacheDurationMinutes);
|
||||
|
||||
/// <summary>
|
||||
/// Sets media composition in cache.
|
||||
/// </summary>
|
||||
/// <param name="urn">The URN.</param>
|
||||
/// <param name="mediaComposition">The media composition to cache.</param>
|
||||
void SetMediaComposition(string urn, MediaComposition mediaComposition);
|
||||
|
||||
/// <summary>
|
||||
/// Removes media composition from cache.
|
||||
/// </summary>
|
||||
/// <param name="urn">The URN.</param>
|
||||
void RemoveMediaComposition(string urn);
|
||||
|
||||
/// <summary>
|
||||
/// Clears all cached data.
|
||||
/// </summary>
|
||||
void Clear();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the cache statistics.
|
||||
/// </summary>
|
||||
/// <returns>A tuple with cache count and size estimate.</returns>
|
||||
(int Count, long SizeEstimate) GetStatistics();
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Jellyfin.Plugin.SRFPlay.Services.Interfaces;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for proxying SRF Play streams and managing authentication.
|
||||
/// </summary>
|
||||
public interface IStreamProxyService
|
||||
{
|
||||
/// <summary>
|
||||
/// Registers a stream for proxying.
|
||||
/// </summary>
|
||||
/// <param name="itemId">The item ID.</param>
|
||||
/// <param name="authenticatedUrl">The authenticated stream URL.</param>
|
||||
/// <param name="urn">The SRF URN for this content (used for re-fetching fresh URLs).</param>
|
||||
/// <param name="isLiveStream">Whether this is a livestream (livestreams always fetch fresh URLs).</param>
|
||||
void RegisterStream(string itemId, string authenticatedUrl, string? urn = null, bool isLiveStream = false);
|
||||
|
||||
/// <summary>
|
||||
/// Gets stream metadata for an item (URN and isLiveStream flag).
|
||||
/// </summary>
|
||||
/// <param name="itemId">The item ID.</param>
|
||||
/// <returns>A tuple of (URN, IsLiveStream), or null if not found.</returns>
|
||||
(string? Urn, bool IsLiveStream)? GetStreamMetadata(string itemId);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the authenticated URL for an item.
|
||||
/// </summary>
|
||||
/// <param name="itemId">The item ID.</param>
|
||||
/// <returns>The authenticated URL, or null if not found or expired.</returns>
|
||||
string? GetAuthenticatedUrl(string itemId);
|
||||
|
||||
/// <summary>
|
||||
/// Fetches and rewrites an HLS manifest to use proxy URLs.
|
||||
/// </summary>
|
||||
/// <param name="itemId">The item ID.</param>
|
||||
/// <param name="baseProxyUrl">The base proxy URL.</param>
|
||||
/// <param name="cancellationToken">Cancellation token.</param>
|
||||
/// <returns>The rewritten manifest content.</returns>
|
||||
Task<string?> GetRewrittenManifestAsync(string itemId, string baseProxyUrl, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Fetches a segment from the original source.
|
||||
/// </summary>
|
||||
/// <param name="itemId">The item ID.</param>
|
||||
/// <param name="segmentPath">The segment path.</param>
|
||||
/// <param name="cancellationToken">Cancellation token.</param>
|
||||
/// <returns>The segment content as bytes.</returns>
|
||||
Task<byte[]?> GetSegmentAsync(string itemId, string segmentPath, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Cleans up old and expired stream mappings.
|
||||
/// </summary>
|
||||
void CleanupOldMappings();
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Plugin.SRFPlay.Api.Models;
|
||||
using Jellyfin.Plugin.SRFPlay.Configuration;
|
||||
|
||||
namespace Jellyfin.Plugin.SRFPlay.Services.Interfaces;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for resolving stream URLs from media composition resources.
|
||||
/// </summary>
|
||||
public interface IStreamUrlResolver
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the best stream URL from a chapter based on quality preference.
|
||||
/// </summary>
|
||||
/// <param name="chapter">The chapter containing resources.</param>
|
||||
/// <param name="qualityPreference">The quality preference.</param>
|
||||
/// <returns>The stream URL, or null if no suitable stream found.</returns>
|
||||
string? GetStreamUrl(Chapter chapter, QualityPreference qualityPreference);
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a chapter has non-DRM playable content.
|
||||
/// </summary>
|
||||
/// <param name="chapter">The chapter to check.</param>
|
||||
/// <returns>True if playable content is available.</returns>
|
||||
bool HasPlayableContent(Chapter chapter);
|
||||
|
||||
/// <summary>
|
||||
/// Checks if content is expired based on ValidTo date.
|
||||
/// </summary>
|
||||
/// <param name="chapter">The chapter to check.</param>
|
||||
/// <returns>True if the content is expired.</returns>
|
||||
bool IsContentExpired(Chapter chapter);
|
||||
|
||||
/// <summary>
|
||||
/// Authenticates a stream URL by fetching an Akamai token.
|
||||
/// </summary>
|
||||
/// <param name="streamUrl">The unauthenticated stream URL.</param>
|
||||
/// <param name="cancellationToken">Cancellation token.</param>
|
||||
/// <returns>The authenticated stream URL with token.</returns>
|
||||
Task<string> GetAuthenticatedStreamUrlAsync(string streamUrl, CancellationToken cancellationToken = default);
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Plugin.SRFPlay.Api;
|
||||
using Jellyfin.Plugin.SRFPlay.Api.Models;
|
||||
using Jellyfin.Plugin.SRFPlay.Services.Interfaces;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Plugin.SRFPlay.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Service for fetching media composition with caching support.
|
||||
/// This consolidates the cache-check → API-fetch → cache-store pattern.
|
||||
/// </summary>
|
||||
public class MediaCompositionFetcher : IMediaCompositionFetcher
|
||||
{
|
||||
private readonly ILogger<MediaCompositionFetcher> _logger;
|
||||
private readonly IMetadataCache _metadataCache;
|
||||
private readonly ISRFApiClientFactory _apiClientFactory;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MediaCompositionFetcher"/> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">The logger.</param>
|
||||
/// <param name="metadataCache">The metadata cache.</param>
|
||||
/// <param name="apiClientFactory">The API client factory.</param>
|
||||
public MediaCompositionFetcher(
|
||||
ILogger<MediaCompositionFetcher> logger,
|
||||
IMetadataCache metadataCache,
|
||||
ISRFApiClientFactory apiClientFactory)
|
||||
{
|
||||
_logger = logger;
|
||||
_metadataCache = metadataCache;
|
||||
_apiClientFactory = apiClientFactory;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<MediaComposition?> GetMediaCompositionAsync(
|
||||
string urn,
|
||||
CancellationToken cancellationToken,
|
||||
int? cacheDurationOverride = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(urn))
|
||||
{
|
||||
_logger.LogDebug("GetMediaCompositionAsync called with null/empty URN");
|
||||
return null;
|
||||
}
|
||||
|
||||
var config = Plugin.Instance?.Configuration;
|
||||
if (config == null)
|
||||
{
|
||||
_logger.LogWarning("Plugin configuration is null, cannot fetch media composition");
|
||||
return null;
|
||||
}
|
||||
|
||||
var cacheDuration = cacheDurationOverride ?? config.CacheDurationMinutes;
|
||||
|
||||
// Try cache first
|
||||
var mediaComposition = _metadataCache.GetMediaComposition(urn, cacheDuration);
|
||||
if (mediaComposition != null)
|
||||
{
|
||||
_logger.LogDebug("Cache hit for URN: {Urn}", urn);
|
||||
return mediaComposition;
|
||||
}
|
||||
|
||||
// Fetch from API
|
||||
_logger.LogDebug("Cache miss for URN: {Urn}, fetching from API", urn);
|
||||
using var apiClient = _apiClientFactory.CreateClient();
|
||||
mediaComposition = await apiClient.GetMediaCompositionByUrnAsync(urn, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (mediaComposition != null)
|
||||
{
|
||||
_metadataCache.SetMediaComposition(urn, mediaComposition);
|
||||
_logger.LogDebug("Cached media composition for URN: {Urn}", urn);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("Failed to fetch media composition for URN: {Urn}", urn);
|
||||
}
|
||||
|
||||
return mediaComposition;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Threading;
|
||||
using Jellyfin.Plugin.SRFPlay.Api.Models;
|
||||
using Jellyfin.Plugin.SRFPlay.Services.Interfaces;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Plugin.SRFPlay.Services;
|
||||
@@ -9,7 +10,7 @@ namespace Jellyfin.Plugin.SRFPlay.Services;
|
||||
/// <summary>
|
||||
/// Service for caching metadata from SRF API.
|
||||
/// </summary>
|
||||
public sealed class MetadataCache : IDisposable
|
||||
public sealed class MetadataCache : IMetadataCache, IDisposable
|
||||
{
|
||||
private readonly ILogger<MetadataCache> _logger;
|
||||
private readonly ConcurrentDictionary<string, CacheEntry<MediaComposition>> _mediaCompositionCache;
|
||||
|
||||
@@ -8,6 +8,7 @@ using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using Jellyfin.Plugin.SRFPlay.Api;
|
||||
using Jellyfin.Plugin.SRFPlay.Configuration;
|
||||
using Jellyfin.Plugin.SRFPlay.Services.Interfaces;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Plugin.SRFPlay.Services;
|
||||
@@ -15,11 +16,11 @@ namespace Jellyfin.Plugin.SRFPlay.Services;
|
||||
/// <summary>
|
||||
/// Service for proxying SRF Play streams and managing authentication.
|
||||
/// </summary>
|
||||
public class StreamProxyService : IDisposable
|
||||
public class StreamProxyService : IStreamProxyService, IDisposable
|
||||
{
|
||||
private readonly ILogger<StreamProxyService> _logger;
|
||||
private readonly ILoggerFactory _loggerFactory;
|
||||
private readonly StreamUrlResolver _streamResolver;
|
||||
private readonly IStreamUrlResolver _streamResolver;
|
||||
private readonly IMediaCompositionFetcher _compositionFetcher;
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly ConcurrentDictionary<string, StreamInfo> _streamMappings;
|
||||
private bool _disposed;
|
||||
@@ -28,13 +29,16 @@ public class StreamProxyService : IDisposable
|
||||
/// Initializes a new instance of the <see cref="StreamProxyService"/> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">The logger.</param>
|
||||
/// <param name="loggerFactory">The logger factory (for creating API clients).</param>
|
||||
/// <param name="streamResolver">The stream URL resolver.</param>
|
||||
public StreamProxyService(ILogger<StreamProxyService> logger, ILoggerFactory loggerFactory, StreamUrlResolver streamResolver)
|
||||
/// <param name="compositionFetcher">The media composition fetcher.</param>
|
||||
public StreamProxyService(
|
||||
ILogger<StreamProxyService> logger,
|
||||
IStreamUrlResolver streamResolver,
|
||||
IMediaCompositionFetcher compositionFetcher)
|
||||
{
|
||||
_logger = logger;
|
||||
_loggerFactory = loggerFactory;
|
||||
_streamResolver = streamResolver;
|
||||
_compositionFetcher = compositionFetcher;
|
||||
_httpClient = new HttpClient
|
||||
{
|
||||
Timeout = TimeSpan.FromSeconds(30)
|
||||
@@ -306,8 +310,8 @@ public class StreamProxyService : IDisposable
|
||||
|
||||
try
|
||||
{
|
||||
using var apiClient = new SRFApiClient(_loggerFactory);
|
||||
var mediaComposition = apiClient.GetMediaCompositionByUrnAsync(streamInfo.Urn, CancellationToken.None)
|
||||
// Use short cache duration (5 min) for livestreams
|
||||
var mediaComposition = _compositionFetcher.GetMediaCompositionAsync(streamInfo.Urn, CancellationToken.None, 5)
|
||||
.GetAwaiter().GetResult();
|
||||
|
||||
if (mediaComposition?.ChapterList == null || mediaComposition.ChapterList.Count == 0)
|
||||
|
||||
@@ -6,6 +6,7 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Plugin.SRFPlay.Api.Models;
|
||||
using Jellyfin.Plugin.SRFPlay.Configuration;
|
||||
using Jellyfin.Plugin.SRFPlay.Services.Interfaces;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Plugin.SRFPlay.Services;
|
||||
@@ -13,7 +14,7 @@ namespace Jellyfin.Plugin.SRFPlay.Services;
|
||||
/// <summary>
|
||||
/// Service for resolving stream URLs from media composition resources.
|
||||
/// </summary>
|
||||
public class StreamUrlResolver : IDisposable
|
||||
public class StreamUrlResolver : IStreamUrlResolver, IDisposable
|
||||
{
|
||||
private readonly ILogger<StreamUrlResolver> _logger;
|
||||
private readonly HttpClient _httpClient;
|
||||
|
||||
Reference in New Issue
Block a user