sonarr-repo-only/NzbDrone.Core/MetadataSource/TvRageMappingProvider.cs

72 lines
2.5 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using NLog;
using NzbDrone.Core.Providers;
2013-03-02 18:25:39 +00:00
using NzbDrone.Core.ReferenceData;
using NzbDrone.Core.Tv;
using NzbDrone.Core.Model.TvRage;
namespace NzbDrone.Core.MetadataSource
{
public class TvRageMappingProvider
{
2013-03-07 03:45:36 +00:00
private readonly ISceneMappingService _sceneMappingService;
private readonly TvRageProxy _tvRageProxy;
private readonly IEpisodeService _episodeService;
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
2013-03-07 03:45:36 +00:00
public TvRageMappingProvider(ISceneMappingService sceneMappingService,
TvRageProxy tvRageProxy, IEpisodeService episodeService)
{
2013-03-02 18:25:39 +00:00
_sceneMappingService = sceneMappingService;
_tvRageProxy = tvRageProxy;
_episodeService = episodeService;
}
public TvRageMappingProvider()
{
}
public Series FindMatchingTvRageSeries(Series series)
{
var firstEpisode = _episodeService.GetEpisode(series.Id, 1, 1);
2013-03-02 18:25:39 +00:00
var cleanName = _sceneMappingService.GetCleanName(series.Id);
var results = _tvRageProxy.SearchSeries(series.Title);
var result = ProcessResults(results, series, cleanName, firstEpisode);
if (result != null)
{
logger.Trace("TV Rage: {0} matches TVDB: {1}", result.Name, series.Title);
series.TvRageId = result.ShowId;
series.TvRageTitle = result.Name;
series.UtcOffset = _tvRageProxy.GetSeries(result.ShowId).UtcOffset;
}
return series;
}
public TvRageSearchResult ProcessResults(IList<TvRageSearchResult> searchResults, Series series, string sceneCleanName, Episode firstEpisode)
{
foreach (var result in searchResults)
{
if (Parser.NormalizeTitle(result.Name).Equals(series.CleanTitle))
return result;
if (!String.IsNullOrWhiteSpace(sceneCleanName) && Parser.NormalizeTitle(result.Name).Equals(sceneCleanName))
return result;
if (series.FirstAired.HasValue && result.Started == series.FirstAired.Value)
return result;
if (firstEpisode != null && firstEpisode.AirDate.HasValue && result.Started == firstEpisode.AirDate.Value)
return result;
}
return null;
}
}
}