mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-14 10:47:08 -07:00
cleaned up scene mapping code.
This commit is contained in:
parent
c1fba9093d
commit
48880e4964
25 changed files with 222 additions and 319 deletions
17
NzbDrone.Core/DataAugmentation/Scene/SceneMapping.cs
Normal file
17
NzbDrone.Core/DataAugmentation/Scene/SceneMapping.cs
Normal file
|
@ -0,0 +1,17 @@
|
|||
using Newtonsoft.Json;
|
||||
using NzbDrone.Core.Datastore;
|
||||
|
||||
namespace NzbDrone.Core.DataAugmentation.Scene
|
||||
{
|
||||
public class SceneMapping : ModelBase
|
||||
{
|
||||
public string CleanTitle { get; set; }
|
||||
|
||||
[JsonProperty("Title")]
|
||||
public string SceneName { get; set; }
|
||||
|
||||
[JsonProperty("Id")]
|
||||
public int TvdbId { get; set; }
|
||||
public int SeasonNumber { get; set; }
|
||||
}
|
||||
}
|
88
NzbDrone.Core/DataAugmentation/Scene/SceneMappingProvider.cs
Normal file
88
NzbDrone.Core/DataAugmentation/Scene/SceneMappingProvider.cs
Normal file
|
@ -0,0 +1,88 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using NLog;
|
||||
using NzbDrone.Core.Lifecycle;
|
||||
|
||||
namespace NzbDrone.Core.DataAugmentation.Scene
|
||||
{
|
||||
public interface ISceneMappingService
|
||||
{
|
||||
void UpdateMappings();
|
||||
string GetSceneName(int tvdbId, int seasonNumber = -1);
|
||||
Nullable<int> GetTvDbId(string cleanName);
|
||||
string GetCleanName(int tvdbId);
|
||||
}
|
||||
|
||||
public class SceneMappingService : IInitializable, ISceneMappingService
|
||||
{
|
||||
private readonly ISceneMappingRepository _repository;
|
||||
private readonly ISceneMappingProxy _sceneMappingProxy;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public SceneMappingService(ISceneMappingRepository repository, ISceneMappingProxy sceneMappingProxy, Logger logger)
|
||||
{
|
||||
_repository = repository;
|
||||
_sceneMappingProxy = sceneMappingProxy;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public 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 virtual string GetSceneName(int tvdbId, int seasonNumber = -1)
|
||||
{
|
||||
var mapping = _repository.FindByTvdbId(tvdbId);
|
||||
|
||||
if (mapping == null) return null;
|
||||
|
||||
return mapping.SceneName;
|
||||
}
|
||||
|
||||
public virtual Nullable<Int32> GetTvDbId(string cleanName)
|
||||
{
|
||||
var mapping = _repository.FindByCleanTitle(cleanName);
|
||||
|
||||
if (mapping == null)
|
||||
return null;
|
||||
|
||||
return mapping.TvdbId;
|
||||
}
|
||||
|
||||
|
||||
public virtual string GetCleanName(int tvdbId)
|
||||
{
|
||||
var mapping = _repository.FindByTvdbId(tvdbId);
|
||||
|
||||
if (mapping == null) return null;
|
||||
|
||||
return mapping.CleanTitle;
|
||||
}
|
||||
|
||||
public void Init()
|
||||
{
|
||||
if (!_repository.HasItems())
|
||||
{
|
||||
UpdateMappings();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
30
NzbDrone.Core/DataAugmentation/Scene/SceneMappingProxy.cs
Normal file
30
NzbDrone.Core/DataAugmentation/Scene/SceneMappingProxy.cs
Normal file
|
@ -0,0 +1,30 @@
|
|||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json;
|
||||
using NzbDrone.Common;
|
||||
using NzbDrone.Core.Configuration;
|
||||
|
||||
namespace NzbDrone.Core.DataAugmentation.Scene
|
||||
{
|
||||
public interface ISceneMappingProxy
|
||||
{
|
||||
List<SceneMapping> Fetch();
|
||||
}
|
||||
|
||||
public class SceneMappingProxy : ISceneMappingProxy
|
||||
{
|
||||
private readonly HttpProvider _httpProvider;
|
||||
private readonly IConfigService _configService;
|
||||
|
||||
public SceneMappingProxy(HttpProvider httpProvider, IConfigService configService)
|
||||
{
|
||||
_httpProvider = httpProvider;
|
||||
_configService = configService;
|
||||
}
|
||||
|
||||
public List<SceneMapping> Fetch()
|
||||
{
|
||||
var mappingsJson = _httpProvider.DownloadString(_configService.ServiceRootUrl + "/SceneMapping/Active");
|
||||
return JsonConvert.DeserializeObject<List<SceneMapping>>(mappingsJson);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
using System.Linq;
|
||||
using NzbDrone.Core.Datastore;
|
||||
|
||||
namespace NzbDrone.Core.DataAugmentation.Scene
|
||||
{
|
||||
public interface ISceneMappingRepository : IBasicRepository<SceneMapping>
|
||||
{
|
||||
SceneMapping FindByTvdbId(int tvdbId);
|
||||
SceneMapping FindByCleanTitle(string cleanTitle);
|
||||
|
||||
}
|
||||
|
||||
public class SceneMappingRepository : BasicRepository<SceneMapping>, ISceneMappingRepository
|
||||
{
|
||||
public SceneMappingRepository(IDatabase database)
|
||||
: base(database)
|
||||
{
|
||||
}
|
||||
|
||||
public SceneMapping FindByTvdbId(int tvdbId)
|
||||
{
|
||||
return Query.SingleOrDefault(c => c.TvdbId == tvdbId);
|
||||
}
|
||||
|
||||
public SceneMapping FindByCleanTitle(string cleanTitle)
|
||||
{
|
||||
return Query.SingleOrDefault(c => c.CleanTitle == cleanTitle);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue