2014-06-29 20:04:50 -07:00
|
|
|
|
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.Model.Logging;
|
|
|
|
|
using MediaBrowser.XbmcMetadata.Configuration;
|
2014-09-26 19:28:13 -07:00
|
|
|
|
using MediaBrowser.XbmcMetadata.Savers;
|
2014-06-29 20:04:50 -07:00
|
|
|
|
using System;
|
2017-05-21 00:25:49 -07:00
|
|
|
|
using MediaBrowser.Controller.Dto;
|
2014-06-29 20:04:50 -07:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Run()
|
|
|
|
|
{
|
|
|
|
|
_userDataManager.UserDataSaved += _userDataManager_UserDataSaved;
|
|
|
|
|
_libraryManager.ItemUpdated += _libraryManager_ItemUpdated;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _libraryManager_ItemUpdated(object sender, ItemChangeEventArgs e)
|
|
|
|
|
{
|
2015-07-13 14:26:11 -07:00
|
|
|
|
if (e.UpdateReason >= ItemUpdateType.ImageUpdate)
|
2014-06-29 20:32:43 -07:00
|
|
|
|
{
|
|
|
|
|
var person = e.Item as Person;
|
2014-06-29 20:04:50 -07:00
|
|
|
|
|
2014-06-29 20:32:43 -07:00
|
|
|
|
if (person != null)
|
2014-06-29 20:04:50 -07:00
|
|
|
|
{
|
2014-09-26 19:28:13 -07:00
|
|
|
|
var config = _config.GetNfoConfiguration();
|
|
|
|
|
|
|
|
|
|
if (!config.SaveImagePathsInNfo)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-19 23:46:51 -07:00
|
|
|
|
var items = _libraryManager.GetItemList(new InternalItemsQuery
|
2015-07-06 19:25:23 -07:00
|
|
|
|
{
|
2017-05-21 00:25:49 -07:00
|
|
|
|
PersonIds = new [] { person.Id.ToString("N") },
|
|
|
|
|
DtoOptions = new DtoOptions(true)
|
2015-07-06 19:25:23 -07:00
|
|
|
|
|
2017-08-09 12:56:38 -07:00
|
|
|
|
});
|
2014-06-29 20:32:43 -07:00
|
|
|
|
|
|
|
|
|
foreach (var item in items)
|
|
|
|
|
{
|
2015-07-13 14:26:11 -07:00
|
|
|
|
SaveMetadataForItem(item, e.UpdateReason);
|
2014-06-29 20:32:43 -07:00
|
|
|
|
}
|
2014-06-29 20:04:50 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _userDataManager_UserDataSaved(object sender, UserDataSaveEventArgs e)
|
|
|
|
|
{
|
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
|
|
|
|
{
|
|
|
|
|
var item = e.Item as BaseItem;
|
|
|
|
|
|
2014-09-26 19:28:13 -07:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(_config.GetNfoConfiguration().UserId))
|
2014-06-29 20:04:50 -07:00
|
|
|
|
{
|
2015-07-13 14:26:11 -07:00
|
|
|
|
SaveMetadataForItem(item, ItemUpdateType.MetadataDownload);
|
2014-06-29 20:04:50 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_userDataManager.UserDataSaved -= _userDataManager_UserDataSaved;
|
2017-09-05 12:49:02 -07:00
|
|
|
|
GC.SuppressFinalize(this);
|
2014-06-29 20:04:50 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void SaveMetadataForItem(BaseItem item, ItemUpdateType updateReason)
|
|
|
|
|
{
|
|
|
|
|
var locationType = item.LocationType;
|
|
|
|
|
if (locationType == LocationType.Remote ||
|
|
|
|
|
locationType == LocationType.Virtual)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-25 11:39:13 -07:00
|
|
|
|
if (!item.SupportsLocalMetadata)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!item.IsSaveLocalMetadataEnabled())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-29 20:04:50 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
2014-09-26 19:28:13 -07:00
|
|
|
|
await _providerManager.SaveMetadata(item, updateReason, new[] { BaseNfoSaver.SaverName }).ConfigureAwait(false);
|
2014-06-29 20:04:50 -07:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error saving metadata for {0}", ex, item.Path ?? item.Name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|