jellyfin/MediaBrowser.XbmcMetadata/EntryPoint.cs

83 lines
2.6 KiB
C#
Raw Normal View History

using System;
2019-01-27 07:40:37 -07: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 class EntryPoint : IServerEntryPoint
{
private readonly IUserDataManager _userDataManager;
private readonly ILogger _logger;
private readonly ILibraryManager _libraryManager;
private readonly IProviderManager _providerManager;
private readonly IConfigurationManager _config;
public EntryPoint(IUserDataManager userDataManager, ILibraryManager libraryManager, ILogger logger, IProviderManager providerManager, IConfigurationManager config)
{
_userDataManager = userDataManager;
_libraryManager = libraryManager;
_logger = logger;
_providerManager = providerManager;
_config = config;
}
2019-01-27 07:40:37 -07:00
public Task RunAsync()
{
_userDataManager.UserDataSaved += _userDataManager_UserDataSaved;
2019-01-27 07:40:37 -07:00
return Task.CompletedTask;
}
void _userDataManager_UserDataSaved(object sender, UserDataSaveEventArgs e)
{
if (e.SaveReason == UserDataSaveReason.PlaybackFinished || e.SaveReason == UserDataSaveReason.TogglePlayed || e.SaveReason == UserDataSaveReason.UpdateUserRating)
{
if (!string.IsNullOrWhiteSpace(_config.GetNfoConfiguration().UserId))
{
2018-09-12 10:26:21 -07:00
SaveMetadataForItem(e.Item, ItemUpdateType.MetadataDownload);
}
}
}
public void Dispose()
{
_userDataManager.UserDataSaved -= _userDataManager_UserDataSaved;
}
2017-10-03 11:39:37 -07:00
private void SaveMetadataForItem(BaseItem item, ItemUpdateType updateReason)
{
2018-09-12 10:26:21 -07:00
if (!item.IsFileProtocol)
{
return;
}
2016-09-25 11:39:13 -07:00
if (!item.SupportsLocalMetadata)
{
return;
}
if (!item.IsSaveLocalMetadataEnabled())
{
return;
}
try
{
2017-10-03 11:39:37 -07:00
_providerManager.SaveMetadata(item, updateReason, new[] { BaseNfoSaver.SaverName });
}
catch (Exception ex)
{
2018-12-20 05:11:26 -07:00
_logger.LogError(ex, "Error saving metadata for {path}", item.Path ?? item.Name);
}
}
}
}