From 690e603b90ae9d856386d5fb6bf3e32d4cb46a9c Mon Sep 17 00:00:00 2001 From: Patrick Barron Date: Tue, 6 Feb 2024 16:18:17 -0500 Subject: [PATCH] Use IHostedService for NFO user data --- Jellyfin.Server/Startup.cs | 2 + MediaBrowser.XbmcMetadata/EntryPoint.cs | 78 ----------------- MediaBrowser.XbmcMetadata/NfoUserDataSaver.cs | 87 +++++++++++++++++++ 3 files changed, 89 insertions(+), 78 deletions(-) delete mode 100644 MediaBrowser.XbmcMetadata/EntryPoint.cs create mode 100644 MediaBrowser.XbmcMetadata/NfoUserDataSaver.cs diff --git a/Jellyfin.Server/Startup.cs b/Jellyfin.Server/Startup.cs index 84f9bff61..1d78b1602 100644 --- a/Jellyfin.Server/Startup.cs +++ b/Jellyfin.Server/Startup.cs @@ -19,6 +19,7 @@ using Jellyfin.Server.Infrastructure; using MediaBrowser.Common.Net; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Extensions; +using MediaBrowser.XbmcMetadata; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; @@ -128,6 +129,7 @@ namespace Jellyfin.Server services.AddHostedService(); services.AddHostedService(); + services.AddHostedService(); services.AddHostedService(); services.AddHostedService(); services.AddHostedService(); diff --git a/MediaBrowser.XbmcMetadata/EntryPoint.cs b/MediaBrowser.XbmcMetadata/EntryPoint.cs deleted file mode 100644 index a6216ef30..000000000 --- a/MediaBrowser.XbmcMetadata/EntryPoint.cs +++ /dev/null @@ -1,78 +0,0 @@ -#pragma warning disable CS1591 - -using System; -using System.Threading.Tasks; -using MediaBrowser.Common.Configuration; -using MediaBrowser.Controller.Entities; -using MediaBrowser.Controller.Library; -using MediaBrowser.Controller.Plugins; -using MediaBrowser.Controller.Providers; -using MediaBrowser.Model.Entities; -using MediaBrowser.XbmcMetadata.Configuration; -using MediaBrowser.XbmcMetadata.Savers; -using Microsoft.Extensions.Logging; - -namespace MediaBrowser.XbmcMetadata -{ - public sealed class EntryPoint : IServerEntryPoint - { - private readonly IUserDataManager _userDataManager; - private readonly ILogger _logger; - private readonly IProviderManager _providerManager; - private readonly IConfigurationManager _config; - - public EntryPoint( - IUserDataManager userDataManager, - ILogger logger, - IProviderManager providerManager, - IConfigurationManager config) - { - _userDataManager = userDataManager; - _logger = logger; - _providerManager = providerManager; - _config = config; - } - - /// - public Task RunAsync() - { - _userDataManager.UserDataSaved += OnUserDataSaved; - - return Task.CompletedTask; - } - - private void OnUserDataSaved(object? sender, UserDataSaveEventArgs e) - { - if (e.SaveReason == UserDataSaveReason.PlaybackFinished || e.SaveReason == UserDataSaveReason.TogglePlayed || e.SaveReason == UserDataSaveReason.UpdateUserRating) - { - if (!string.IsNullOrWhiteSpace(_config.GetNfoConfiguration().UserId)) - { - _ = SaveMetadataForItemAsync(e.Item, ItemUpdateType.MetadataDownload); - } - } - } - - /// - public void Dispose() - { - _userDataManager.UserDataSaved -= OnUserDataSaved; - } - - private async Task SaveMetadataForItemAsync(BaseItem item, ItemUpdateType updateReason) - { - if (!item.IsFileProtocol || !item.SupportsLocalMetadata) - { - return; - } - - try - { - await _providerManager.SaveMetadataAsync(item, updateReason, new[] { BaseNfoSaver.SaverName }).ConfigureAwait(false); - } - catch (Exception ex) - { - _logger.LogError(ex, "Error saving metadata for {Path}", item.Path ?? item.Name); - } - } - } -} diff --git a/MediaBrowser.XbmcMetadata/NfoUserDataSaver.cs b/MediaBrowser.XbmcMetadata/NfoUserDataSaver.cs new file mode 100644 index 000000000..b2882194d --- /dev/null +++ b/MediaBrowser.XbmcMetadata/NfoUserDataSaver.cs @@ -0,0 +1,87 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using MediaBrowser.Common.Configuration; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.Providers; +using MediaBrowser.Model.Entities; +using MediaBrowser.XbmcMetadata.Configuration; +using MediaBrowser.XbmcMetadata.Savers; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace MediaBrowser.XbmcMetadata; + +/// +/// responsible for updating NFO files' user data. +/// +public sealed class NfoUserDataSaver : IHostedService +{ + private readonly ILogger _logger; + private readonly IConfigurationManager _config; + private readonly IUserDataManager _userDataManager; + private readonly IProviderManager _providerManager; + + /// + /// Initializes a new instance of the class. + /// + /// The . + /// The . + /// The . + /// The . + public NfoUserDataSaver( + ILogger logger, + IConfigurationManager config, + IUserDataManager userDataManager, + IProviderManager providerManager) + { + _logger = logger; + _config = config; + _userDataManager = userDataManager; + _providerManager = providerManager; + } + + /// + public Task StartAsync(CancellationToken cancellationToken) + { + _userDataManager.UserDataSaved += OnUserDataSaved; + return Task.CompletedTask; + } + + /// + public Task StopAsync(CancellationToken cancellationToken) + { + _userDataManager.UserDataSaved -= OnUserDataSaved; + return Task.CompletedTask; + } + + private async void OnUserDataSaved(object? sender, UserDataSaveEventArgs e) + { + if (e.SaveReason is not (UserDataSaveReason.PlaybackFinished + or UserDataSaveReason.TogglePlayed or UserDataSaveReason.UpdateUserRating)) + { + return; + } + + if (string.IsNullOrWhiteSpace(_config.GetNfoConfiguration().UserId)) + { + return; + } + + var item = e.Item; + if (!item.IsFileProtocol || !item.SupportsLocalMetadata) + { + return; + } + + try + { + await _providerManager.SaveMetadataAsync(item, ItemUpdateType.MetadataDownload, [BaseNfoSaver.SaverName]) + .ConfigureAwait(false); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error saving metadata for {Path}", item.Path ?? item.Name); + } + } +}