| | 1 | | using System; |
| | 2 | | using System.Threading; |
| | 3 | | using System.Threading.Tasks; |
| | 4 | | using MediaBrowser.Common.Configuration; |
| | 5 | | using MediaBrowser.Controller.Library; |
| | 6 | | using MediaBrowser.Controller.Providers; |
| | 7 | | using MediaBrowser.Model.Entities; |
| | 8 | | using MediaBrowser.XbmcMetadata.Configuration; |
| | 9 | | using MediaBrowser.XbmcMetadata.Savers; |
| | 10 | | using Microsoft.Extensions.Hosting; |
| | 11 | | using Microsoft.Extensions.Logging; |
| | 12 | |
|
| | 13 | | namespace MediaBrowser.XbmcMetadata; |
| | 14 | |
|
| | 15 | | /// <summary> |
| | 16 | | /// <see cref="IHostedService"/> responsible for updating NFO files' user data. |
| | 17 | | /// </summary> |
| | 18 | | public sealed class NfoUserDataSaver : IHostedService |
| | 19 | | { |
| | 20 | | private readonly ILogger<NfoUserDataSaver> _logger; |
| | 21 | | private readonly IConfigurationManager _config; |
| | 22 | | private readonly IUserDataManager _userDataManager; |
| | 23 | | private readonly IProviderManager _providerManager; |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// Initializes a new instance of the <see cref="NfoUserDataSaver"/> class. |
| | 27 | | /// </summary> |
| | 28 | | /// <param name="logger">The <see cref="ILogger"/>.</param> |
| | 29 | | /// <param name="config">The <see cref="IConfigurationManager"/>.</param> |
| | 30 | | /// <param name="userDataManager">The <see cref="IUserDataManager"/>.</param> |
| | 31 | | /// <param name="providerManager">The <see cref="IProviderManager"/>.</param> |
| | 32 | | public NfoUserDataSaver( |
| | 33 | | ILogger<NfoUserDataSaver> logger, |
| | 34 | | IConfigurationManager config, |
| | 35 | | IUserDataManager userDataManager, |
| | 36 | | IProviderManager providerManager) |
| | 37 | | { |
| 21 | 38 | | _logger = logger; |
| 21 | 39 | | _config = config; |
| 21 | 40 | | _userDataManager = userDataManager; |
| 21 | 41 | | _providerManager = providerManager; |
| 21 | 42 | | } |
| | 43 | |
|
| | 44 | | /// <inheritdoc /> |
| | 45 | | public Task StartAsync(CancellationToken cancellationToken) |
| | 46 | | { |
| 21 | 47 | | _userDataManager.UserDataSaved += OnUserDataSaved; |
| 21 | 48 | | return Task.CompletedTask; |
| | 49 | | } |
| | 50 | |
|
| | 51 | | /// <inheritdoc /> |
| | 52 | | public Task StopAsync(CancellationToken cancellationToken) |
| | 53 | | { |
| 21 | 54 | | _userDataManager.UserDataSaved -= OnUserDataSaved; |
| 21 | 55 | | return Task.CompletedTask; |
| | 56 | | } |
| | 57 | |
|
| | 58 | | private async void OnUserDataSaved(object? sender, UserDataSaveEventArgs e) |
| | 59 | | { |
| | 60 | | if (e.SaveReason is not (UserDataSaveReason.PlaybackFinished |
| | 61 | | or UserDataSaveReason.TogglePlayed or UserDataSaveReason.UpdateUserRating)) |
| | 62 | | { |
| | 63 | | return; |
| | 64 | | } |
| | 65 | |
|
| | 66 | | if (string.IsNullOrWhiteSpace(_config.GetNfoConfiguration().UserId)) |
| | 67 | | { |
| | 68 | | return; |
| | 69 | | } |
| | 70 | |
|
| | 71 | | var item = e.Item; |
| | 72 | | if (!item.IsFileProtocol || !item.SupportsLocalMetadata) |
| | 73 | | { |
| | 74 | | return; |
| | 75 | | } |
| | 76 | |
|
| | 77 | | try |
| | 78 | | { |
| | 79 | | await _providerManager.SaveMetadataAsync(item, ItemUpdateType.MetadataDownload, [BaseNfoSaver.SaverName]) |
| | 80 | | .ConfigureAwait(false); |
| | 81 | | } |
| | 82 | | catch (Exception ex) |
| | 83 | | { |
| | 84 | | _logger.LogError(ex, "Error saving metadata for {Path}", item.Path ?? item.Name); |
| | 85 | | } |
| | 86 | | } |
| | 87 | | } |