6bb54ab3a5
* Hotfix for the 10.10.2 API change * Support 10.10.1 retroactively for now * Log a message indicating the action
74 lines
3.5 KiB
C#
74 lines
3.5 KiB
C#
// Copyright (C) 2024 Intro-Skipper contributors <intro-skipper.org>
|
|
// SPDX-License-Identifier: GPL-3.0-only.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using IntroSkipper.Data;
|
|
using MediaBrowser.Controller;
|
|
using MediaBrowser.Model;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace IntroSkipper.Manager
|
|
{
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="MediaSegmentUpdateManager" /> class.
|
|
/// </summary>
|
|
/// <param name="mediaSegmentManager">MediaSegmentManager.</param>
|
|
/// <param name="logger">logger.</param>
|
|
/// <param name="segmentProvider">segmentProvider.</param>
|
|
public class MediaSegmentUpdateManager(IMediaSegmentManager mediaSegmentManager, ILogger<MediaSegmentUpdateManager> logger, IMediaSegmentProvider segmentProvider)
|
|
{
|
|
private readonly IMediaSegmentManager _mediaSegmentManager = mediaSegmentManager;
|
|
private readonly ILogger<MediaSegmentUpdateManager> _logger = logger;
|
|
private readonly IMediaSegmentProvider _segmentProvider = segmentProvider;
|
|
private readonly string _name = Plugin.Instance!.Name;
|
|
|
|
/// <summary>
|
|
/// Updates all media items in a List.
|
|
/// </summary>
|
|
/// <param name="episodes">Queued media items.</param>
|
|
/// <param name="cancellationToken">CancellationToken.</param>
|
|
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
|
public async Task UpdateMediaSegmentsAsync(IReadOnlyList<QueuedEpisode> episodes, CancellationToken cancellationToken)
|
|
{
|
|
foreach (var episode in episodes)
|
|
{
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
try
|
|
{
|
|
try
|
|
{
|
|
var existingSegments = await _mediaSegmentManager.GetSegmentsAsync(episode.EpisodeId, null, true).ConfigureAwait(false);
|
|
await Task.WhenAll(existingSegments.Select(s => _mediaSegmentManager.DeleteSegmentAsync(s.Id))).ConfigureAwait(false);
|
|
}
|
|
catch (Exception vs)
|
|
{
|
|
_logger.LogError(vs, "GetSegmentsAsync failed. 10.10.1 compatibility enabled.");
|
|
var existingSegments = await _mediaSegmentManager.GetSegmentsAsync(episode.EpisodeId, null).ConfigureAwait(false);
|
|
await Task.WhenAll(existingSegments.Select(s => _mediaSegmentManager.DeleteSegmentAsync(s.Id))).ConfigureAwait(false);
|
|
}
|
|
|
|
var newSegments = await _segmentProvider.GetMediaSegments(new MediaSegmentGenerationRequest { ItemId = episode.EpisodeId }, cancellationToken).ConfigureAwait(false);
|
|
|
|
if (newSegments.Count == 0)
|
|
{
|
|
_logger.LogDebug("No segments found for episode {EpisodeId}", episode.EpisodeId);
|
|
continue;
|
|
}
|
|
|
|
await Task.WhenAll(newSegments.Select(s => _mediaSegmentManager.CreateSegmentAsync(s, _name))).ConfigureAwait(false);
|
|
|
|
_logger.LogDebug("Updated {SegmentCount} segments for episode {EpisodeId}", newSegments.Count, episode.EpisodeId);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error processing episode {EpisodeId}", episode.EpisodeId);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|