2020-02-23 04:11:43 -07:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 13:03:10 -07:00
|
|
|
using System;
|
2019-01-27 07:40:37 -07:00
|
|
|
using System.Threading.Tasks;
|
2019-01-13 12:26:56 -07:00
|
|
|
using MediaBrowser.Common.Configuration;
|
2014-06-29 20:04:50 -07:00
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Controller.Plugins;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.XbmcMetadata.Configuration;
|
2014-09-26 19:28:13 -07:00
|
|
|
using MediaBrowser.XbmcMetadata.Savers;
|
2019-01-13 12:26:56 -07:00
|
|
|
using Microsoft.Extensions.Logging;
|
2014-06-29 20:04:50 -07:00
|
|
|
|
|
|
|
namespace MediaBrowser.XbmcMetadata
|
|
|
|
{
|
2020-02-23 04:11:43 -07:00
|
|
|
public sealed class EntryPoint : IServerEntryPoint
|
2014-06-29 20:04:50 -07:00
|
|
|
{
|
|
|
|
private readonly IUserDataManager _userDataManager;
|
2020-06-05 17:15:56 -07:00
|
|
|
private readonly ILogger<EntryPoint> _logger;
|
2014-06-29 20:04:50 -07:00
|
|
|
private readonly IProviderManager _providerManager;
|
|
|
|
private readonly IConfigurationManager _config;
|
|
|
|
|
2019-09-01 09:39:23 -07:00
|
|
|
public EntryPoint(
|
|
|
|
IUserDataManager userDataManager,
|
2020-03-03 15:07:10 -07:00
|
|
|
ILogger<EntryPoint> logger,
|
2019-09-01 09:39:23 -07:00
|
|
|
IProviderManager providerManager,
|
|
|
|
IConfigurationManager config)
|
2014-06-29 20:04:50 -07:00
|
|
|
{
|
|
|
|
_userDataManager = userDataManager;
|
|
|
|
_logger = logger;
|
|
|
|
_providerManager = providerManager;
|
|
|
|
_config = config;
|
|
|
|
}
|
|
|
|
|
2019-09-01 09:39:23 -07:00
|
|
|
/// <inheritdoc />
|
2019-01-27 07:40:37 -07:00
|
|
|
public Task RunAsync()
|
2014-06-29 20:04:50 -07:00
|
|
|
{
|
2019-09-01 09:39:23 -07:00
|
|
|
_userDataManager.UserDataSaved += OnUserDataSaved;
|
2019-01-27 07:40:37 -07:00
|
|
|
|
|
|
|
return Task.CompletedTask;
|
2014-06-29 20:04:50 -07:00
|
|
|
}
|
|
|
|
|
2020-12-31 04:09:25 -07:00
|
|
|
private void OnUserDataSaved(object? sender, UserDataSaveEventArgs e)
|
2014-06-29 20:04:50 -07:00
|
|
|
{
|
2014-09-26 19:28:13 -07:00
|
|
|
if (e.SaveReason == UserDataSaveReason.PlaybackFinished || e.SaveReason == UserDataSaveReason.TogglePlayed || e.SaveReason == UserDataSaveReason.UpdateUserRating)
|
2014-06-29 20:04:50 -07:00
|
|
|
{
|
2014-09-26 19:28:13 -07:00
|
|
|
if (!string.IsNullOrWhiteSpace(_config.GetNfoConfiguration().UserId))
|
2014-06-29 20:04:50 -07:00
|
|
|
{
|
2022-01-22 15:36:42 -07:00
|
|
|
_ = SaveMetadataForItemAsync(e.Item, ItemUpdateType.MetadataDownload);
|
2014-06-29 20:04:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-01 09:39:23 -07:00
|
|
|
/// <inheritdoc />
|
2014-06-29 20:04:50 -07:00
|
|
|
public void Dispose()
|
|
|
|
{
|
2019-09-01 09:39:23 -07:00
|
|
|
_userDataManager.UserDataSaved -= OnUserDataSaved;
|
2014-06-29 20:04:50 -07:00
|
|
|
}
|
|
|
|
|
2022-01-22 15:36:42 -07:00
|
|
|
private async Task SaveMetadataForItemAsync(BaseItem item, ItemUpdateType updateReason)
|
2014-06-29 20:04:50 -07:00
|
|
|
{
|
2021-01-23 11:05:55 -07:00
|
|
|
if (!item.IsFileProtocol || !item.SupportsLocalMetadata)
|
2016-09-25 11:39:13 -07:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-29 20:04:50 -07:00
|
|
|
try
|
|
|
|
{
|
2022-01-22 15:36:42 -07:00
|
|
|
await _providerManager.SaveMetadataAsync(item, updateReason, new[] { BaseNfoSaver.SaverName }).ConfigureAwait(false);
|
2014-06-29 20:04:50 -07:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2021-11-09 05:14:31 -07:00
|
|
|
_logger.LogError(ex, "Error saving metadata for {Path}", item.Path ?? item.Name);
|
2014-06-29 20:04:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|