2012-12-19 17:41:51 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using NLog;
|
2013-03-04 06:53:02 +01:00
|
|
|
|
using NzbDrone.Core.Providers;
|
2013-03-02 19:25:39 +01:00
|
|
|
|
using NzbDrone.Core.ReferenceData;
|
2013-02-19 07:01:03 +01:00
|
|
|
|
using NzbDrone.Core.Tv;
|
2012-12-19 17:41:51 +01:00
|
|
|
|
using NzbDrone.Core.Model.TvRage;
|
|
|
|
|
|
2013-03-04 06:53:02 +01:00
|
|
|
|
namespace NzbDrone.Core.MetadataSource
|
2012-12-19 17:41:51 +01:00
|
|
|
|
{
|
|
|
|
|
public class TvRageMappingProvider
|
|
|
|
|
{
|
2013-03-07 04:45:36 +01:00
|
|
|
|
private readonly ISceneMappingService _sceneMappingService;
|
2013-03-04 06:53:02 +01:00
|
|
|
|
private readonly TvRageProxy _tvRageProxy;
|
2013-02-22 01:47:09 +01:00
|
|
|
|
private readonly IEpisodeService _episodeService;
|
2012-12-19 17:41:51 +01:00
|
|
|
|
|
|
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
2013-03-07 04:45:36 +01:00
|
|
|
|
public TvRageMappingProvider(ISceneMappingService sceneMappingService,
|
2013-03-04 06:53:02 +01:00
|
|
|
|
TvRageProxy tvRageProxy, IEpisodeService episodeService)
|
2012-12-19 17:41:51 +01:00
|
|
|
|
{
|
2013-03-02 19:25:39 +01:00
|
|
|
|
_sceneMappingService = sceneMappingService;
|
2013-03-04 06:53:02 +01:00
|
|
|
|
_tvRageProxy = tvRageProxy;
|
2013-02-20 03:05:15 +01:00
|
|
|
|
_episodeService = episodeService;
|
2012-12-19 17:41:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TvRageMappingProvider()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Series FindMatchingTvRageSeries(Series series)
|
|
|
|
|
{
|
2013-02-26 04:58:57 +01:00
|
|
|
|
var firstEpisode = _episodeService.GetEpisode(series.Id, 1, 1);
|
2012-12-19 17:41:51 +01:00
|
|
|
|
|
2013-03-02 19:25:39 +01:00
|
|
|
|
var cleanName = _sceneMappingService.GetCleanName(series.Id);
|
2013-03-04 06:53:02 +01:00
|
|
|
|
var results = _tvRageProxy.SearchSeries(series.Title);
|
2012-12-19 17:41:51 +01:00
|
|
|
|
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;
|
2013-03-04 06:53:02 +01:00
|
|
|
|
series.UtcOffset = _tvRageProxy.GetSeries(result.ShowId).UtcOffset;
|
2012-12-19 17:41:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2012-12-27 01:16:15 +01:00
|
|
|
|
if (series.FirstAired.HasValue && result.Started == series.FirstAired.Value)
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
if (firstEpisode != null && firstEpisode.AirDate.HasValue && result.Started == firstEpisode.AirDate.Value)
|
2012-12-19 17:41:51 +01:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|