Skip to content
This repository has been archived by the owner on Sep 25, 2023. It is now read-only.

Commit

Permalink
Remove dependency on shared media item queue
Browse files Browse the repository at this point in the history
  • Loading branch information
ConfusedPolarBear committed Nov 23, 2022
1 parent 2c3577e commit bcb5e5e
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ public ActionResult<string> GetSupportBundle()

bundle.Append("* Queue contents: ");
bundle.Append(Plugin.Instance!.TotalQueued);
bundle.Append(" episodes, ");
bundle.Append(Plugin.Instance!.AnalysisQueue.Count);
bundle.Append(" seasons");
bundle.Append('\n');
bundle.Append(" episodes\n");

bundle.Append(FFmpegWrapper.GetChromaprintLogs());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public ActionResult<Dictionary<string, HashSet<string>>> GetShowSeasons()
var showSeasons = new Dictionary<string, HashSet<string>>();

// Loop through all seasons in the analysis queue
foreach (var kvp in Plugin.Instance!.AnalysisQueue)
foreach (var kvp in Plugin.Instance!.QueuedMediaItems)
{
// Check that this season contains at least one episode.
var episodes = kvp.Value;
Expand Down Expand Up @@ -104,10 +104,8 @@ public ActionResult<List<EpisodeVisualization>> GetSeasonEpisodes(
[HttpGet("Episode/{Id}/Chromaprint")]
public ActionResult<uint[]> GetEpisodeFingerprint([FromRoute] Guid id)
{
var queue = Plugin.Instance!.AnalysisQueue;

// Search through all queued episodes to find the requested id
foreach (var season in queue)
foreach (var season in Plugin.Instance!.QueuedMediaItems)
{
foreach (var needle in season.Value)
{
Expand Down Expand Up @@ -180,7 +178,7 @@ private string GetSeasonName(QueuedEpisode episode)
/// <returns>Boolean indicating if the requested season was found.</returns>
private bool LookupSeasonByName(string series, string season, out List<QueuedEpisode> episodes)
{
foreach (var queuedEpisodes in Plugin.Instance!.AnalysisQueue)
foreach (var queuedEpisodes in Plugin.Instance!.QueuedMediaItems)
{
var first = queuedEpisodes.Value[0];
var firstSeasonName = GetSeasonName(first);
Expand Down
7 changes: 1 addition & 6 deletions ConfusedPolarBear.Plugin.IntroSkipper/Entrypoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public Task RunAsync()

try
{
// Enqueue all episodes at startup so the fingerprint visualizer works before the task is started.
// Enqueue all episodes at startup to ensure any FFmpeg errors appear as early as possible
_logger.LogInformation("Running startup enqueue");
var queueManager = new QueueManager(_loggerFactory.CreateLogger<QueueManager>(), _libraryManager);
queueManager.EnqueueAllEpisodes();
Expand All @@ -67,11 +67,6 @@ public Task RunAsync()
_logger.LogError("Unable to run startup enqueue: {Exception}", ex);
}

_logger.LogDebug(
"Total enqueued seasons: {Count} ({Episodes} episodes)",
Plugin.Instance!.AnalysisQueue.Count,
Plugin.Instance!.TotalQueued);

return Task.CompletedTask;
}

Expand Down
4 changes: 2 additions & 2 deletions ConfusedPolarBear.Plugin.IntroSkipper/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ public Plugin(
public Dictionary<Guid, Intro> Intros { get; } = new();

/// <summary>
/// Gets the mapping of season ids to episodes that have been queued for fingerprinting.
/// Gets the most recent media item queue.
/// </summary>
public Dictionary<Guid, List<QueuedEpisode>> AnalysisQueue { get; } = new();
public Dictionary<Guid, List<QueuedEpisode>> QueuedMediaItems { get; } = new();

/// <summary>
/// Gets or sets the total number of episodes in the queue.
Expand Down
34 changes: 19 additions & 15 deletions ConfusedPolarBear.Plugin.IntroSkipper/QueueManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ namespace ConfusedPolarBear.Plugin.IntroSkipper;

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Jellyfin.Data.Enums;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Entities;
using Microsoft.Extensions.Logging;

/// <summary>
Expand All @@ -19,7 +19,8 @@ public class QueueManager
private ILogger<QueueManager> _logger;

private double analysisPercent;
private IList<string> selectedLibraries;
private List<string> selectedLibraries;
private Dictionary<Guid, List<QueuedEpisode>> _queuedEpisodes;

/// <summary>
/// Initializes a new instance of the <see cref="QueueManager"/> class.
Expand All @@ -31,13 +32,15 @@ public QueueManager(ILogger<QueueManager> logger, ILibraryManager libraryManager
_logger = logger;
_libraryManager = libraryManager;

selectedLibraries = new List<string>();
selectedLibraries = new();
_queuedEpisodes = new();
}

/// <summary>
/// Iterates through all libraries on the server and queues all episodes for analysis.
/// </summary>
public void EnqueueAllEpisodes()
/// <returns>Queued media items.</returns>
public ReadOnlyDictionary<Guid, List<QueuedEpisode>> EnqueueAllEpisodes()
{
// Assert that ffmpeg with chromaprint is installed
if (!FFmpegWrapper.CheckFFmpegVersion())
Expand All @@ -46,20 +49,13 @@ public void EnqueueAllEpisodes()
"ffmpeg with chromaprint is not installed on this system - episodes will not be analyzed. If Jellyfin is running natively, install jellyfin-ffmpeg5. If Jellyfin is running in a container, upgrade it to the latest version of 10.8.0.");
}

Plugin.Instance!.AnalysisQueue.Clear();
Plugin.Instance!.TotalQueued = 0;

LoadAnalysisSettings();

// For all selected TV show libraries, enqueue all contained items.
// For all selected libraries, enqueue all contained episodes.
foreach (var folder in _libraryManager.GetVirtualFolders())
{
if (folder.CollectionType != CollectionTypeOptions.TvShows)
{
_logger.LogDebug("Not analyzing library \"{Name}\": not a TV show library", folder.Name);
continue;
}

// If libraries have been selected for analysis, ensure this library was selected.
if (selectedLibraries.Count > 0 && !selectedLibraries.Contains(folder.Name))
{
Expand All @@ -81,6 +77,14 @@ public void EnqueueAllEpisodes()
_logger.LogError("Failed to enqueue items from library {Name}: {Exception}", folder.Name, ex);
}
}

Plugin.Instance!.QueuedMediaItems.Clear();
foreach (var kvp in _queuedEpisodes)
{
Plugin.Instance!.QueuedMediaItems[kvp.Key] = kvp.Value;
}

return new(_queuedEpisodes);
}

/// <summary>
Expand Down Expand Up @@ -156,7 +160,7 @@ private void QueueLibraryContents(string rawId)
{
if (item is not Episode episode)
{
_logger.LogError("Item {Name} is not an episode", item.Name);
_logger.LogDebug("Item {Name} is not an episode", item.Name);
continue;
}

Expand Down Expand Up @@ -198,11 +202,11 @@ private void QueueEpisode(Episode episode)
60 * Plugin.Instance!.Configuration.AnalysisLengthLimit);

// Allocate a new list for each new season
Plugin.Instance!.AnalysisQueue.TryAdd(episode.SeasonId, new List<QueuedEpisode>());
_queuedEpisodes.TryAdd(episode.SeasonId, new List<QueuedEpisode>());

// Queue the episode for analysis
var maxCreditsDuration = Plugin.Instance!.Configuration.MaximumEpisodeCreditsDuration * 60;
Plugin.Instance.AnalysisQueue[episode.SeasonId].Add(new QueuedEpisode()
_queuedEpisodes[episode.SeasonId].Add(new QueuedEpisode()
{
SeriesName = episode.SeriesName,
SeasonNumber = episode.AiredSeasonNumber ?? 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellat
_loggerFactory.CreateLogger<QueueManager>(),
_libraryManager);

queueManager.EnqueueAllEpisodes();

var queue = Plugin.Instance!.AnalysisQueue;
var queue = queueManager.EnqueueAllEpisodes();

if (queue.Count == 0)
{
Expand Down Expand Up @@ -142,7 +140,11 @@ public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellat
ex);
}

progress.Report((totalProcessed * 100) / Plugin.Instance!.TotalQueued);
var total = Plugin.Instance!.TotalQueued;
if (total > 0)
{
progress.Report((totalProcessed * 100) / total);
}
});

return Task.CompletedTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellat
_loggerFactory.CreateLogger<QueueManager>(),
_libraryManager);

queueManager.EnqueueAllEpisodes();

var queue = Plugin.Instance!.AnalysisQueue;
var queue = queueManager.EnqueueAllEpisodes();

if (queue.Count == 0)
{
Expand Down Expand Up @@ -162,7 +160,11 @@ public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellat
EdlManager.UpdateEDLFiles(episodes);
}

progress.Report((totalProcessed * 100) / Plugin.Instance!.TotalQueued);
var total = Plugin.Instance!.TotalQueued;
if (total > 0)
{
progress.Report((totalProcessed * 100) / total);
}
});

// Turn the regenerate EDL flag off after the scan completes.
Expand Down

0 comments on commit bcb5e5e

Please sign in to comment.