Radarr/NzbDrone.Core/DataAugmentation/Scene/SceneMappingService.cs

118 lines
3.3 KiB
C#
Raw Normal View History

2013-03-02 18:25:39 +00:00
using System;
2013-04-01 02:43:58 +00:00
using System.Linq;
2013-03-02 18:25:39 +00:00
using NLog;
using NzbDrone.Common.Cache;
using NzbDrone.Common.Messaging;
2013-03-02 18:25:39 +00:00
using NzbDrone.Core.Lifecycle;
using NzbDrone.Core.Parser;
2013-03-02 18:25:39 +00:00
2013-04-01 02:43:58 +00:00
namespace NzbDrone.Core.DataAugmentation.Scene
2013-03-02 18:25:39 +00:00
{
2013-03-07 03:45:36 +00:00
public interface ISceneMappingService
{
string GetSceneName(int tvdbId, int seasonNumber = -1);
2013-04-01 02:43:58 +00:00
Nullable<int> GetTvDbId(string cleanName);
2013-03-07 03:45:36 +00:00
}
public class SceneMappingService : ISceneMappingService,
IHandleAsync<ApplicationStartedEvent>,
IExecute<UpdateSceneMappingCommand>
2013-03-02 18:25:39 +00:00
{
2013-03-02 18:25:39 +00:00
private readonly ISceneMappingRepository _repository;
private readonly ISceneMappingProxy _sceneMappingProxy;
private readonly Logger _logger;
private readonly ICached<SceneMapping> _getSceneNameCache;
private readonly ICached<SceneMapping> _gettvdbIdCache;
2013-03-02 18:25:39 +00:00
public SceneMappingService(ISceneMappingRepository repository, ISceneMappingProxy sceneMappingProxy, ICacheManger cacheManger, Logger logger)
2013-03-02 18:25:39 +00:00
{
_repository = repository;
_sceneMappingProxy = sceneMappingProxy;
_getSceneNameCache = cacheManger.GetCache<SceneMapping>(GetType(), "scene_name");
_gettvdbIdCache = cacheManger.GetCache<SceneMapping>(GetType(), "tvdb_id");
2013-03-02 18:25:39 +00:00
_logger = logger;
}
public string GetSceneName(int tvdbId, int seasonNumber = -1)
2013-03-02 18:25:39 +00:00
{
var mapping = _getSceneNameCache.Find(tvdbId.ToString());
2013-03-02 18:25:39 +00:00
if (mapping == null) return null;
2013-03-02 18:25:39 +00:00
return mapping.SearchTerm;
2013-03-02 18:25:39 +00:00
}
2013-04-07 07:30:37 +00:00
public Nullable<Int32> GetTvDbId(string cleanName)
2013-03-02 18:25:39 +00:00
{
var mapping = _gettvdbIdCache.Find(cleanName.CleanSeriesTitle());
2013-03-02 18:25:39 +00:00
if (mapping == null)
return null;
2013-03-02 18:25:39 +00:00
return mapping.TvdbId;
2013-03-02 18:25:39 +00:00
}
private void UpdateMappings()
{
_logger.Info("Updating Scene mapping");
try
{
var mappings = _sceneMappingProxy.Fetch();
if (mappings.Any())
{
_repository.Purge();
foreach (var sceneMapping in mappings)
{
sceneMapping.ParseTerm = sceneMapping.ParseTerm.CleanSeriesTitle();
}
_repository.InsertMany(mappings);
}
else
{
_logger.Warn("Received empty list of mapping. will not update.");
}
}
catch (Exception ex)
{
_logger.ErrorException("Failed to Update Scene Mappings:", ex);
}
RefreshCache();
}
private void RefreshCache()
{
var mappings = _repository.All();
_gettvdbIdCache.Clear();
_getSceneNameCache.Clear();
foreach (var sceneMapping in mappings)
{
_getSceneNameCache.Set(sceneMapping.TvdbId.ToString(), sceneMapping);
_gettvdbIdCache.Set(sceneMapping.ParseTerm.CleanSeriesTitle(), sceneMapping);
}
}
public void HandleAsync(ApplicationStartedEvent message)
{
UpdateMappings();
}
public void Execute(UpdateSceneMappingCommand message)
{
UpdateMappings();
}
2013-03-02 18:25:39 +00:00
}
}