refactor to unify data fetching and define abstract API for re-use
This commit is contained in:
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user