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;
|
2013-04-24 01:56:00 +00:00
|
|
|
using NzbDrone.Common.Messaging;
|
2013-03-02 18:25:39 +00:00
|
|
|
using NzbDrone.Core.Lifecycle;
|
|
|
|
|
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
|
|
|
|
{
|
2013-04-29 00:39:17 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-04-29 00:39:17 +00:00
|
|
|
public class SceneMappingService : ISceneMappingService,
|
|
|
|
IHandleAsync<ApplicationStartedEvent>,
|
|
|
|
IExecute<UpdateSceneMappingCommand>
|
2013-03-02 18:25:39 +00:00
|
|
|
{
|
2013-05-09 06:38:20 +00:00
|
|
|
|
|
|
|
private static readonly object mutex = new object();
|
|
|
|
|
2013-03-02 18:25:39 +00:00
|
|
|
private readonly ISceneMappingRepository _repository;
|
|
|
|
private readonly ISceneMappingProxy _sceneMappingProxy;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2013-04-29 00:39:17 +00:00
|
|
|
public SceneMappingService(ISceneMappingRepository repository, ISceneMappingProxy sceneMappingProxy, Logger logger)
|
2013-03-02 18:25:39 +00:00
|
|
|
{
|
|
|
|
_repository = repository;
|
|
|
|
_sceneMappingProxy = sceneMappingProxy;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2013-04-29 00:39:17 +00:00
|
|
|
public string GetSceneName(int tvdbId, int seasonNumber = -1)
|
2013-03-02 18:25:39 +00:00
|
|
|
{
|
2013-04-29 00:39:17 +00:00
|
|
|
var mapping = _repository.FindByTvdbId(tvdbId);
|
2013-03-02 18:25:39 +00:00
|
|
|
|
2013-04-01 02:43:58 +00:00
|
|
|
if (mapping == null) return null;
|
2013-03-02 18:25:39 +00:00
|
|
|
|
|
|
|
return mapping.SceneName;
|
|
|
|
}
|
|
|
|
|
2013-04-07 07:30:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
public Nullable<Int32> GetTvDbId(string cleanName)
|
2013-03-02 18:25:39 +00:00
|
|
|
{
|
|
|
|
var mapping = _repository.FindByCleanTitle(cleanName);
|
|
|
|
|
|
|
|
if (mapping == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return mapping.TvdbId;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-10 00:47:04 +00:00
|
|
|
public void HandleAsync(ApplicationStartedEvent message)
|
2013-03-02 18:25:39 +00:00
|
|
|
{
|
|
|
|
if (!_repository.HasItems())
|
|
|
|
{
|
|
|
|
UpdateMappings();
|
|
|
|
}
|
|
|
|
}
|
2013-04-29 00:39:17 +00:00
|
|
|
|
|
|
|
private void UpdateMappings()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var mappings = _sceneMappingProxy.Fetch();
|
2013-05-09 06:38:20 +00:00
|
|
|
|
|
|
|
lock (mutex)
|
2013-04-29 00:39:17 +00:00
|
|
|
{
|
2013-05-09 06:38:20 +00:00
|
|
|
if (mappings.Any())
|
|
|
|
{
|
|
|
|
_repository.Purge();
|
|
|
|
_repository.InsertMany(mappings);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_logger.Warn("Received empty list of mapping. will not update.");
|
|
|
|
}
|
2013-04-29 00:39:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.ErrorException("Failed to Update Scene Mappings:", ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Execute(UpdateSceneMappingCommand message)
|
|
|
|
{
|
|
|
|
UpdateMappings();
|
|
|
|
}
|
2013-03-02 18:25:39 +00:00
|
|
|
}
|
|
|
|
}
|