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