|
|
@ -11,243 +11,243 @@ using NzbDrone.Core.Tv.Events;
|
|
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.DataAugmentation.Scene
|
|
|
|
namespace NzbDrone.Core.DataAugmentation.Scene
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//public interface ISceneMappingService
|
|
|
|
public interface ISceneMappingService
|
|
|
|
//{
|
|
|
|
{
|
|
|
|
// List<string> GetSceneNames(int tvdbId, List<int> seasonNumbers, List<int> sceneSeasonNumbers);
|
|
|
|
List<string> GetSceneNames(int tvdbId, List<int> seasonNumbers, List<int> sceneSeasonNumbers);
|
|
|
|
// int? FindTvdbId(string title);
|
|
|
|
int? FindTvdbId(string title);
|
|
|
|
// List<SceneMapping> FindByTvdbId(int tvdbId);
|
|
|
|
List<SceneMapping> FindByTvdbId(int tvdbId);
|
|
|
|
// SceneMapping FindSceneMapping(string title);
|
|
|
|
SceneMapping FindSceneMapping(string title);
|
|
|
|
// int? GetSceneSeasonNumber(string title);
|
|
|
|
int? GetSceneSeasonNumber(string title);
|
|
|
|
// int? GetTvdbSeasonNumber(string title);
|
|
|
|
int? GetTvdbSeasonNumber(string title);
|
|
|
|
// int? GetSceneSeasonNumber(int tvdbId, int seasonNumber);
|
|
|
|
int? GetSceneSeasonNumber(int tvdbId, int seasonNumber);
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//public class SceneMappingService : ISceneMappingService,
|
|
|
|
public class SceneMappingService : ISceneMappingService,
|
|
|
|
// IHandle<SeriesRefreshStartingEvent>,
|
|
|
|
IHandle<SeriesRefreshStartingEvent>,
|
|
|
|
// IExecute<UpdateSceneMappingCommand>
|
|
|
|
IExecute<UpdateSceneMappingCommand>
|
|
|
|
//{
|
|
|
|
{
|
|
|
|
// private readonly ISceneMappingRepository _repository;
|
|
|
|
private readonly ISceneMappingRepository _repository;
|
|
|
|
// private readonly IEnumerable<ISceneMappingProvider> _sceneMappingProviders;
|
|
|
|
private readonly IEnumerable<ISceneMappingProvider> _sceneMappingProviders;
|
|
|
|
// private readonly IEventAggregator _eventAggregator;
|
|
|
|
private readonly IEventAggregator _eventAggregator;
|
|
|
|
// private readonly Logger _logger;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
// private readonly ICachedDictionary<List<SceneMapping>> _getTvdbIdCache;
|
|
|
|
private readonly ICachedDictionary<List<SceneMapping>> _getTvdbIdCache;
|
|
|
|
// private readonly ICachedDictionary<List<SceneMapping>> _findByTvdbIdCache;
|
|
|
|
private readonly ICachedDictionary<List<SceneMapping>> _findByTvdbIdCache;
|
|
|
|
|
|
|
|
|
|
|
|
//public SceneMappingService(ISceneMappingRepository repository,
|
|
|
|
public SceneMappingService(ISceneMappingRepository repository,
|
|
|
|
// ICacheManager cacheManager,
|
|
|
|
ICacheManager cacheManager,
|
|
|
|
// IEnumerable<ISceneMappingProvider> sceneMappingProviders,
|
|
|
|
IEnumerable<ISceneMappingProvider> sceneMappingProviders,
|
|
|
|
// IEventAggregator eventAggregator,
|
|
|
|
IEventAggregator eventAggregator,
|
|
|
|
// Logger logger)
|
|
|
|
Logger logger)
|
|
|
|
//{
|
|
|
|
{
|
|
|
|
// _repository = repository;
|
|
|
|
_repository = repository;
|
|
|
|
// _sceneMappingProviders = sceneMappingProviders;
|
|
|
|
_sceneMappingProviders = sceneMappingProviders;
|
|
|
|
// _eventAggregator = eventAggregator;
|
|
|
|
_eventAggregator = eventAggregator;
|
|
|
|
// _logger = logger;
|
|
|
|
_logger = logger;
|
|
|
|
|
|
|
|
|
|
|
|
// _getTvdbIdCache = cacheManager.GetCacheDictionary<List<SceneMapping>>(GetType(), "tvdb_id");
|
|
|
|
_getTvdbIdCache = cacheManager.GetCacheDictionary<List<SceneMapping>>(GetType(), "tvdb_id");
|
|
|
|
// _findByTvdbIdCache = cacheManager.GetCacheDictionary<List<SceneMapping>>(GetType(), "find_tvdb_id");
|
|
|
|
_findByTvdbIdCache = cacheManager.GetCacheDictionary<List<SceneMapping>>(GetType(), "find_tvdb_id");
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// public List<string> GetSceneNames(int tvdbId, List<int> seasonNumbers, List<int> sceneSeasonNumbers)
|
|
|
|
public List<string> GetSceneNames(int tvdbId, List<int> seasonNumbers, List<int> sceneSeasonNumbers)
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// var mappings = FindByTvdbId(tvdbId);
|
|
|
|
var mappings = FindByTvdbId(tvdbId);
|
|
|
|
|
|
|
|
|
|
|
|
// if (mappings == null)
|
|
|
|
if (mappings == null)
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// return new List<string>();
|
|
|
|
return new List<string>();
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// var names = mappings.Where(n => n.SeasonNumber.HasValue && seasonNumbers.Contains(n.SeasonNumber.Value) ||
|
|
|
|
var names = mappings.Where(n => n.SeasonNumber.HasValue && seasonNumbers.Contains(n.SeasonNumber.Value) ||
|
|
|
|
// n.SceneSeasonNumber.HasValue && sceneSeasonNumbers.Contains(n.SceneSeasonNumber.Value) ||
|
|
|
|
n.SceneSeasonNumber.HasValue && sceneSeasonNumbers.Contains(n.SceneSeasonNumber.Value) ||
|
|
|
|
// (n.SeasonNumber ?? -1) == -1 && (n.SceneSeasonNumber ?? -1) == -1)
|
|
|
|
(n.SeasonNumber ?? -1) == -1 && (n.SceneSeasonNumber ?? -1) == -1)
|
|
|
|
// .Select(n => n.SearchTerm).Distinct().ToList();
|
|
|
|
.Select(n => n.SearchTerm).Distinct().ToList();
|
|
|
|
|
|
|
|
|
|
|
|
// return FilterNonEnglish(names);
|
|
|
|
return FilterNonEnglish(names);
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// public int? FindTvdbId(string title)
|
|
|
|
public int? FindTvdbId(string title)
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// var mapping = FindMapping(title);
|
|
|
|
var mapping = FindMapping(title);
|
|
|
|
|
|
|
|
|
|
|
|
// if (mapping == null)
|
|
|
|
if (mapping == null)
|
|
|
|
// return null;
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
// return mapping.TvdbId;
|
|
|
|
return mapping.TvdbId;
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// public List<SceneMapping> FindByTvdbId(int tvdbId)
|
|
|
|
public List<SceneMapping> FindByTvdbId(int tvdbId)
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// if (_findByTvdbIdCache.Count == 0)
|
|
|
|
if (_findByTvdbIdCache.Count == 0)
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// RefreshCache();
|
|
|
|
RefreshCache();
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// var mappings = _findByTvdbIdCache.Find(tvdbId.ToString());
|
|
|
|
var mappings = _findByTvdbIdCache.Find(tvdbId.ToString());
|
|
|
|
|
|
|
|
|
|
|
|
// if (mappings == null)
|
|
|
|
if (mappings == null)
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// return new List<SceneMapping>();
|
|
|
|
return new List<SceneMapping>();
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// return mappings;
|
|
|
|
return mappings;
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// public SceneMapping FindSceneMapping(string title)
|
|
|
|
public SceneMapping FindSceneMapping(string title)
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// return FindMapping(title);
|
|
|
|
return FindMapping(title);
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// public int? GetSceneSeasonNumber(string title)
|
|
|
|
public int? GetSceneSeasonNumber(string title)
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// var mapping = FindMapping(title);
|
|
|
|
var mapping = FindMapping(title);
|
|
|
|
|
|
|
|
|
|
|
|
// if (mapping == null)
|
|
|
|
if (mapping == null)
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// return null;
|
|
|
|
return null;
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// return mapping.SceneSeasonNumber;
|
|
|
|
return mapping.SceneSeasonNumber;
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// public int? GetTvdbSeasonNumber(string title)
|
|
|
|
public int? GetTvdbSeasonNumber(string title)
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// var mapping = FindMapping(title);
|
|
|
|
var mapping = FindMapping(title);
|
|
|
|
|
|
|
|
|
|
|
|
// if (mapping == null)
|
|
|
|
if (mapping == null)
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// return null;
|
|
|
|
return null;
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// return mapping.SeasonNumber;
|
|
|
|
return mapping.SeasonNumber;
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// public int? GetSceneSeasonNumber(int tvdbId, int seasonNumber)
|
|
|
|
public int? GetSceneSeasonNumber(int tvdbId, int seasonNumber)
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// var mappings = FindByTvdbId(tvdbId);
|
|
|
|
var mappings = FindByTvdbId(tvdbId);
|
|
|
|
|
|
|
|
|
|
|
|
// if (mappings == null)
|
|
|
|
if (mappings == null)
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// return null;
|
|
|
|
return null;
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// var mapping = mappings.FirstOrDefault(e => e.SeasonNumber == seasonNumber && e.SceneSeasonNumber.HasValue);
|
|
|
|
var mapping = mappings.FirstOrDefault(e => e.SeasonNumber == seasonNumber && e.SceneSeasonNumber.HasValue);
|
|
|
|
|
|
|
|
|
|
|
|
// if (mapping == null)
|
|
|
|
if (mapping == null)
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// return null;
|
|
|
|
return null;
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// return mapping.SceneSeasonNumber;
|
|
|
|
return mapping.SceneSeasonNumber;
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// private void UpdateMappings()
|
|
|
|
private void UpdateMappings()
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// _logger.Info("Updating Scene mappings");
|
|
|
|
_logger.Info("Updating Scene mappings");
|
|
|
|
|
|
|
|
|
|
|
|
// foreach (var sceneMappingProvider in _sceneMappingProviders)
|
|
|
|
foreach (var sceneMappingProvider in _sceneMappingProviders)
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// try
|
|
|
|
try
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// var mappings = sceneMappingProvider.GetSceneMappings();
|
|
|
|
var mappings = sceneMappingProvider.GetSceneMappings();
|
|
|
|
|
|
|
|
|
|
|
|
// if (mappings.Any())
|
|
|
|
if (mappings.Any())
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// _repository.Clear(sceneMappingProvider.GetType().Name);
|
|
|
|
_repository.Clear(sceneMappingProvider.GetType().Name);
|
|
|
|
|
|
|
|
|
|
|
|
// mappings.RemoveAll(sceneMapping =>
|
|
|
|
mappings.RemoveAll(sceneMapping =>
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// if (sceneMapping.Title.IsNullOrWhiteSpace() ||
|
|
|
|
if (sceneMapping.Title.IsNullOrWhiteSpace() ||
|
|
|
|
// sceneMapping.SearchTerm.IsNullOrWhiteSpace())
|
|
|
|
sceneMapping.SearchTerm.IsNullOrWhiteSpace())
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// _logger.Warn("Invalid scene mapping found for: {0}, skipping", sceneMapping.TvdbId);
|
|
|
|
_logger.Warn("Invalid scene mapping found for: {0}, skipping", sceneMapping.TvdbId);
|
|
|
|
// return true;
|
|
|
|
return true;
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// return false;
|
|
|
|
return false;
|
|
|
|
// });
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// foreach (var sceneMapping in mappings)
|
|
|
|
foreach (var sceneMapping in mappings)
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// sceneMapping.ParseTerm = sceneMapping.Title.CleanSeriesTitle();
|
|
|
|
sceneMapping.ParseTerm = sceneMapping.Title.CleanSeriesTitle();
|
|
|
|
// sceneMapping.Type = sceneMappingProvider.GetType().Name;
|
|
|
|
sceneMapping.Type = sceneMappingProvider.GetType().Name;
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// _repository.InsertMany(mappings.ToList());
|
|
|
|
_repository.InsertMany(mappings.ToList());
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
// else
|
|
|
|
else
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// _logger.Warn("Received empty list of mapping. will not update.");
|
|
|
|
_logger.Warn("Received empty list of mapping. will not update.");
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
// catch (Exception ex)
|
|
|
|
catch (Exception ex)
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// _logger.Error(ex, "Failed to Update Scene Mappings.");
|
|
|
|
_logger.Error(ex, "Failed to Update Scene Mappings.");
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// RefreshCache();
|
|
|
|
RefreshCache();
|
|
|
|
|
|
|
|
|
|
|
|
// _eventAggregator.PublishEvent(new SceneMappingsUpdatedEvent());
|
|
|
|
_eventAggregator.PublishEvent(new SceneMappingsUpdatedEvent());
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// private SceneMapping FindMapping(string title)
|
|
|
|
private SceneMapping FindMapping(string title)
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// if (_getTvdbIdCache.Count == 0)
|
|
|
|
if (_getTvdbIdCache.Count == 0)
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// RefreshCache();
|
|
|
|
RefreshCache();
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// var candidates = _getTvdbIdCache.Find(title.CleanSeriesTitle());
|
|
|
|
var candidates = _getTvdbIdCache.Find(title.CleanSeriesTitle());
|
|
|
|
|
|
|
|
|
|
|
|
// if (candidates == null)
|
|
|
|
if (candidates == null)
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// return null;
|
|
|
|
return null;
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// if (candidates.Count == 1)
|
|
|
|
if (candidates.Count == 1)
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// return candidates.First();
|
|
|
|
return candidates.First();
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// var exactMatch = candidates.OrderByDescending(v => v.SeasonNumber)
|
|
|
|
var exactMatch = candidates.OrderByDescending(v => v.SeasonNumber)
|
|
|
|
// .FirstOrDefault(v => v.Title == title);
|
|
|
|
.FirstOrDefault(v => v.Title == title);
|
|
|
|
|
|
|
|
|
|
|
|
// if (exactMatch != null)
|
|
|
|
if (exactMatch != null)
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// return exactMatch;
|
|
|
|
return exactMatch;
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// var closestMatch = candidates.OrderBy(v => title.LevenshteinDistance(v.Title, 10, 1, 10))
|
|
|
|
var closestMatch = candidates.OrderBy(v => title.LevenshteinDistance(v.Title, 10, 1, 10))
|
|
|
|
// .ThenByDescending(v => v.SeasonNumber)
|
|
|
|
.ThenByDescending(v => v.SeasonNumber)
|
|
|
|
// .First();
|
|
|
|
.First();
|
|
|
|
|
|
|
|
|
|
|
|
// return closestMatch;
|
|
|
|
return closestMatch;
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// private void RefreshCache()
|
|
|
|
private void RefreshCache()
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// var mappings = _repository.All().ToList();
|
|
|
|
var mappings = _repository.All().ToList();
|
|
|
|
|
|
|
|
|
|
|
|
// _getTvdbIdCache.Update(mappings.GroupBy(v => v.ParseTerm).ToDictionary(v => v.Key, v => v.ToList()));
|
|
|
|
_getTvdbIdCache.Update(mappings.GroupBy(v => v.ParseTerm).ToDictionary(v => v.Key, v => v.ToList()));
|
|
|
|
// _findByTvdbIdCache.Update(mappings.GroupBy(v => v.TvdbId).ToDictionary(v => v.Key.ToString(), v => v.ToList()));
|
|
|
|
_findByTvdbIdCache.Update(mappings.GroupBy(v => v.TvdbId).ToDictionary(v => v.Key.ToString(), v => v.ToList()));
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// private List<string> FilterNonEnglish(List<string> titles)
|
|
|
|
private List<string> FilterNonEnglish(List<string> titles)
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// return titles.Where(title => title.All(c => c <= 255)).ToList();
|
|
|
|
return titles.Where(title => title.All(c => c <= 255)).ToList();
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// public void Handle(SeriesRefreshStartingEvent message)
|
|
|
|
public void Handle(SeriesRefreshStartingEvent message)
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// if (message.ManualTrigger && _findByTvdbIdCache.IsExpired(TimeSpan.FromMinutes(1)))
|
|
|
|
if (message.ManualTrigger && _findByTvdbIdCache.IsExpired(TimeSpan.FromMinutes(1)))
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// UpdateMappings();
|
|
|
|
UpdateMappings();
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// public void Execute(UpdateSceneMappingCommand message)
|
|
|
|
public void Execute(UpdateSceneMappingCommand message)
|
|
|
|
// {
|
|
|
|
{
|
|
|
|
// UpdateMappings();
|
|
|
|
UpdateMappings();
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|