2012-12-19 16:41:51 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using NLog;
|
2013-02-24 19:57:33 +00:00
|
|
|
|
using NzbDrone.Core.Datastore;
|
2013-02-19 06:01:03 +00:00
|
|
|
|
using NzbDrone.Core.Tv;
|
2012-12-19 16:41:51 +00:00
|
|
|
|
using NzbDrone.Core.Model.TvRage;
|
|
|
|
|
using NzbDrone.Core.Repository;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
|
{
|
|
|
|
|
public class TvRageMappingProvider
|
|
|
|
|
{
|
|
|
|
|
private readonly SceneMappingProvider _sceneMappingProvider;
|
|
|
|
|
private readonly TvRageProvider _tvRageProvider;
|
2013-02-22 00:47:09 +00:00
|
|
|
|
private readonly IEpisodeService _episodeService;
|
2012-12-19 16:41:51 +00:00
|
|
|
|
|
|
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
|
|
|
|
public TvRageMappingProvider(SceneMappingProvider sceneMappingProvider,
|
2013-02-22 00:47:09 +00:00
|
|
|
|
TvRageProvider tvRageProvider, IEpisodeService episodeService)
|
2012-12-19 16:41:51 +00:00
|
|
|
|
{
|
|
|
|
|
_sceneMappingProvider = sceneMappingProvider;
|
|
|
|
|
_tvRageProvider = tvRageProvider;
|
2013-02-20 02:05:15 +00:00
|
|
|
|
_episodeService = episodeService;
|
2012-12-19 16:41:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TvRageMappingProvider()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Series FindMatchingTvRageSeries(Series series)
|
|
|
|
|
{
|
2013-02-26 03:58:57 +00:00
|
|
|
|
var firstEpisode = _episodeService.GetEpisode(series.Id, 1, 1);
|
2012-12-19 16:41:51 +00:00
|
|
|
|
|
2013-02-26 03:58:57 +00:00
|
|
|
|
var cleanName = _sceneMappingProvider.GetCleanName(series.Id);
|
2012-12-19 16:41:51 +00:00
|
|
|
|
var results = _tvRageProvider.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 = _tvRageProvider.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;
|
|
|
|
|
|
2012-12-27 00:16:15 +00: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 16:41:51 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|