jellyfin/MediaBrowser.XbmcMetadata/EntryPoint.cs

79 lines
2.5 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
using System;
2019-01-27 14:40:37 +00:00
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;
2020-06-06 00:15:56 +00:00
private readonly ILogger<EntryPoint> _logger;
private readonly IProviderManager _providerManager;
private readonly IConfigurationManager _config;
2019-09-01 16:39:23 +00:00
public EntryPoint(
IUserDataManager userDataManager,
ILogger<EntryPoint> logger,
2019-09-01 16:39:23 +00:00
IProviderManager providerManager,
IConfigurationManager config)
{
_userDataManager = userDataManager;
_logger = logger;
_providerManager = providerManager;
_config = config;
}
2019-09-01 16:39:23 +00:00
/// <inheritdoc />
2019-01-27 14:40:37 +00:00
public Task RunAsync()
{
2019-09-01 16:39:23 +00:00
_userDataManager.UserDataSaved += OnUserDataSaved;
2019-01-27 14:40:37 +00:00
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))
{
2022-01-22 22:36:42 +00:00
_ = SaveMetadataForItemAsync(e.Item, ItemUpdateType.MetadataDownload);
}
}
}
2019-09-01 16:39:23 +00:00
/// <inheritdoc />
public void Dispose()
{
2019-09-01 16:39:23 +00:00
_userDataManager.UserDataSaved -= OnUserDataSaved;
}
2022-01-22 22:36:42 +00:00
private async Task SaveMetadataForItemAsync(BaseItem item, ItemUpdateType updateReason)
{
if (!item.IsFileProtocol || !item.SupportsLocalMetadata)
2016-09-25 18:39:13 +00:00
{
return;
}
try
{
2022-01-22 22:36:42 +00:00
await _providerManager.SaveMetadataAsync(item, updateReason, new[] { BaseNfoSaver.SaverName }).ConfigureAwait(false);
}
catch (Exception ex)
{
2021-11-09 12:14:31 +00:00
_logger.LogError(ex, "Error saving metadata for {Path}", item.Path ?? item.Name);
}
}
}
}