using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Threading.Tasks;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Library;
using Microsoft.Extensions.Logging;
using Jellyfin.Data.Enums;
namespace ConfusedPolarBear.Plugin.IntroSkipper;
///
/// Server entrypoint.
///
public class Entrypoint : IServerEntryPoint
{
private readonly IUserManager _userManager;
private readonly IUserViewManager _userViewManager;
private readonly ILibraryManager _libraryManager;
private readonly ILogger _logger;
private readonly object _queueLock = new object();
///
/// Constructor.
///
/// User manager.
/// User view manager.
/// Library manager.
/// Logger.
public Entrypoint(
IUserManager userManager,
IUserViewManager userViewManager,
ILibraryManager libraryManager,
ILogger logger)
{
_userManager = userManager;
_userViewManager = userViewManager;
_libraryManager = libraryManager;
_logger = logger;
}
///
/// Registers event handler.
///
public Task RunAsync()
{
FPCalc.Logger = _logger;
// Assert that fpcalc is installed
if (!FPCalc.CheckFPCalcInstalled()) {
_logger.LogError("fpcalc is not installed on this system - episodes will not be analyzed");
return Task.CompletedTask;
}
// As soon as a new episode is added, queue it for later analysis.
_libraryManager.ItemAdded += ItemAdded;
// For all TV show libraries, enqueue all contained items.
foreach (var folder in _libraryManager.GetVirtualFolders()) {
if (folder.CollectionType != CollectionTypeOptions.TvShows) {
continue;
}
_logger.LogInformation(
"Running startup enqueue of items in library {Name} ({ItemId})",
folder.Name,
folder.ItemId);
QueueLibraryContents(folder.ItemId);
}
return Task.CompletedTask;
}
private void QueueLibraryContents(string rawId) {
// FIXME: do smarterer
var query = new UserViewQuery() {
UserId = GetAdministrator(),
};
// Get all items from this library. Since intros may change within a season, sort the items before adding them.
var folder = _userViewManager.GetUserViews(query)[0];
var items = folder.GetItems(new InternalItemsQuery() {
ParentId = Guid.Parse(rawId),
OrderBy = new [] { ("SortName", SortOrder.Ascending) },
IncludeItemTypes = new BaseItemKind[] { BaseItemKind.Episode },
Recursive = true,
});
// Queue all episodes on the server for fingerprinting.
foreach (var item in items.Items) {
if (item is not Episode episode) {
_logger.LogError("Item {Name} is not an episode", item.Name);
continue;
}
QueueEpisode(episode);
}
}
///
/// Called when an item is added to the server.
///
/// Sender.
/// ItemChangeEventArgs.
private void ItemAdded(object? sender, ItemChangeEventArgs e)
{
if (e.Item is not Episode episode) {
return;
}
_logger.LogDebug("Queuing fingerprint of new episode {Name}", episode.Name);
QueueEpisode(episode);
}
private void QueueEpisode(Episode episode) {
if (Plugin.Instance is null) {
throw new InvalidOperationException("plugin instance was null");
}
lock (_queueLock) {
var queue = Plugin.Instance.AnalysisQueue;
// Allocate a new list for each new season
if (!queue.ContainsKey(episode.SeasonId)) {
Plugin.Instance.AnalysisQueue[episode.SeasonId] = new List();
}
// Only fingerprint up to 25% of the episode and at most 10 minutes.
var duration = TimeSpan.FromTicks(episode.RunTimeTicks ?? 0).TotalSeconds;
if (duration >= 5*60) {
duration /= 4;
}
duration = Math.Min(duration, 10 * 60);
Plugin.Instance.AnalysisQueue[episode.SeasonId].Add(new QueuedEpisode() {
SeriesName = episode.SeriesName,
SeasonNumber = episode.AiredSeasonNumber ?? 0,
EpisodeId = episode.Id,
Path = episode.Path,
FingerprintDuration = Convert.ToInt32(duration)
});
Plugin.Instance!.TotalQueued++;
}
}
///
/// FIXME: don't do this.
///
private Guid GetAdministrator() {
foreach (var user in _userManager.Users) {
if (!user.HasPermission(Jellyfin.Data.Enums.PermissionKind.IsAdministrator)) {
continue;
}
_logger.LogDebug("Accessing libraries as {Username}", user.Username);
return user.Id;
}
throw new FingerprintException("Unable to find an administrator on this server.");
}
///
/// Dispose.
///
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
///
/// Dispose.
///
protected virtual void Dispose(bool dispose)
{
if (!dispose)
{
return;
}
_libraryManager.ItemAdded -= ItemAdded;
}
}