1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-10 04:52:42 +01:00
Radarr/NzbDrone.Core/DataAugmentation/Scene/SceneMappingService.cs

88 lines
2.3 KiB
C#
Raw Normal View History

2013-03-02 19:25:39 +01:00
using System;
2013-04-01 04:43:58 +02:00
using System.Linq;
2013-03-02 19:25:39 +01:00
using NLog;
using NzbDrone.Common.Messaging;
2013-03-02 19:25:39 +01:00
using NzbDrone.Core.Lifecycle;
2013-04-01 04:43:58 +02:00
namespace NzbDrone.Core.DataAugmentation.Scene
2013-03-02 19:25:39 +01:00
{
2013-03-07 04:45:36 +01:00
public interface ISceneMappingService
{
string GetSceneName(int tvdbId, int seasonNumber = -1);
2013-04-01 04:43:58 +02:00
Nullable<int> GetTvDbId(string cleanName);
2013-03-07 04:45:36 +01:00
}
public class SceneMappingService : ISceneMappingService,
IHandleAsync<ApplicationStartedEvent>,
IExecute<UpdateSceneMappingCommand>
2013-03-02 19:25:39 +01:00
{
private readonly ISceneMappingRepository _repository;
private readonly ISceneMappingProxy _sceneMappingProxy;
private readonly Logger _logger;
public SceneMappingService(ISceneMappingRepository repository, ISceneMappingProxy sceneMappingProxy, Logger logger)
2013-03-02 19:25:39 +01:00
{
_repository = repository;
_sceneMappingProxy = sceneMappingProxy;
_logger = logger;
}
public string GetSceneName(int tvdbId, int seasonNumber = -1)
2013-03-02 19:25:39 +01:00
{
var mapping = _repository.FindByTvdbId(tvdbId);
2013-03-02 19:25:39 +01:00
2013-04-01 04:43:58 +02:00
if (mapping == null) return null;
2013-03-02 19:25:39 +01:00
return mapping.SceneName;
}
2013-04-07 09:30:37 +02:00
public Nullable<Int32> GetTvDbId(string cleanName)
2013-03-02 19:25:39 +01:00
{
var mapping = _repository.FindByCleanTitle(cleanName);
if (mapping == null)
return null;
return mapping.TvdbId;
}
public void HandleAsync(ApplicationStartedEvent message)
2013-03-02 19:25:39 +01:00
{
if (!_repository.HasItems())
{
UpdateMappings();
}
}
private void UpdateMappings()
{
try
{
var mappings = _sceneMappingProxy.Fetch();
if (mappings.Any())
{
_repository.Purge();
_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);
}
}
public void Execute(UpdateSceneMappingCommand message)
{
UpdateMappings();
}
2013-03-02 19:25:39 +01:00
}
}