Sonarr/NzbDrone.Core/Providers/EpisodeProvider.cs

122 lines
4.5 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using System.Collections.Generic;
2010-09-28 05:01:54 +00:00
using System.Text.RegularExpressions;
2010-10-05 06:21:18 +00:00
using NLog;
using NzbDrone.Core.Model;
using NzbDrone.Core.Repository;
using SubSonic.Repository;
2010-09-28 05:01:54 +00:00
namespace NzbDrone.Core.Providers
{
public class EpisodeProvider : IEpisodeProvider
2010-09-28 05:01:54 +00:00
{
//TODO: Remove parsing of the series name, it should be done in series provider
private readonly IRepository _sonicRepo;
2010-10-05 06:21:18 +00:00
private readonly ISeriesProvider _series;
private readonly ISeasonProvider _seasons;
private readonly ITvDbProvider _tvDb;
2010-10-07 22:17:24 +00:00
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
2010-10-05 06:21:18 +00:00
public EpisodeProvider(IRepository sonicRepo, ISeriesProvider seriesProvider, ISeasonProvider seasonProvider, ITvDbProvider tvDbProvider)
2010-09-28 05:01:54 +00:00
{
_sonicRepo = sonicRepo;
2010-10-05 06:21:18 +00:00
_series = seriesProvider;
_tvDb = tvDbProvider;
_seasons = seasonProvider;
}
public Episode GetEpisode(long id)
{
return _sonicRepo.Single<Episode>(id);
}
public Episode GetEpisode(int seriesId, int seasonNumber, int episodeNumber)
{
return _sonicRepo.Single<Episode>(c => c.SeriesId == seriesId && c.SeasonNumber == seasonNumber && c.EpisodeNumber == episodeNumber);
}
public IList<Episode> GetEpisodeBySeries(long seriesId)
{
return _sonicRepo.Find<Episode>(e => e.SeriesId == seriesId);
}
public String GetSabTitle(Episode episode)
{
2010-10-05 06:21:18 +00:00
var series = _series.GetSeries(episode.SeriesId);
if (series == null) throw new ArgumentException("Unknown series. ID: " + episode.SeriesId);
//TODO: This method should return a standard title for the sab episode.
throw new NotImplementedException();
2010-09-28 05:01:54 +00:00
}
/// <summary>
/// Comprehensive check on whether or not this episode is needed.
/// </summary>
/// <param name="episode">Episode that needs to be checked</param>
/// <returns></returns>
public bool IsNeeded(EpisodeModel episode)
2010-09-28 05:01:54 +00:00
{
throw new NotImplementedException();
}
public void RefreshEpisodeInfo(int seriesId)
2010-10-05 06:21:18 +00:00
{
Logger.Info("Starting episode info refresh for series:{0}", seriesId);
int successCount = 0;
int failCount = 0;
var targetSeries = _tvDb.GetSeries(seriesId, true);
var updateList = new List<Episode>();
var newList = new List<Episode>();
Logger.Debug("Updating season info for series:{0}", seriesId);
targetSeries.Episodes.Select(e => new { e.SeasonId, e.SeasonNumber })
.Distinct().ToList()
.ForEach(s => _seasons.EnsureSeason(seriesId, s.SeasonId, s.SeasonNumber));
2010-10-05 06:21:18 +00:00
foreach (var episode in targetSeries.Episodes)
{
try
{
Logger.Debug("Updating info for series:{0} - episode:{1}", seriesId, episode.Id);
var newEpisode = new Episode()
{
AirDate = episode.FirstAired,
EpisodeId = episode.Id,
EpisodeNumber = episode.EpisodeNumber,
Language = episode.Language.Abbriviation,
Overview = episode.Overview,
SeasonId = episode.SeasonId,
SeasonNumber = episode.SeasonNumber,
SeriesId = seriesId,
Title = episode.EpisodeName
};
if (_sonicRepo.Exists<Episode>(e => e.EpisodeId == newEpisode.EpisodeId))
2010-10-05 06:21:18 +00:00
{
updateList.Add(newEpisode);
}
else
{
newList.Add(newEpisode);
}
2010-10-05 06:21:18 +00:00
successCount++;
2010-10-05 06:21:18 +00:00
}
catch (Exception e)
{
Logger.FatalException(String.Format("An error has occurred while updating episode info for series {0}", seriesId), e);
2010-10-05 06:21:18 +00:00
failCount++;
}
}
_sonicRepo.AddMany(newList);
_sonicRepo.UpdateMany(updateList);
2010-10-05 06:21:18 +00:00
Logger.Info("Finished episode refresh for series:{0}. Success:{1} - Fail:{2} ", seriesId, successCount, failCount);
}
2010-09-28 05:01:54 +00:00
}
}