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