2011-04-23 20:33:24 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Core.Model;
|
|
|
|
|
using NzbDrone.Core.Repository;
|
|
|
|
|
using SubSonic.Repository;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
|
{
|
|
|
|
|
public class EpisodeProvider
|
|
|
|
|
{
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2011-04-26 00:28:33 +00:00
|
|
|
|
private readonly SeasonProvider _seasonsProvider;
|
|
|
|
|
private readonly IRepository _repository;
|
|
|
|
|
private readonly TvDbProvider _tvDbProvider;
|
|
|
|
|
|
|
|
|
|
public EpisodeProvider(IRepository repository, SeasonProvider seasonProviderProvider, TvDbProvider tvDbProviderProvider)
|
2011-04-23 20:33:24 +00:00
|
|
|
|
{
|
2011-04-26 00:28:33 +00:00
|
|
|
|
_repository = repository;
|
|
|
|
|
_tvDbProvider = tvDbProviderProvider;
|
|
|
|
|
_seasonsProvider = seasonProviderProvider;
|
2011-04-23 20:33:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EpisodeProvider()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual Episode GetEpisode(long id)
|
|
|
|
|
{
|
2011-04-26 00:28:33 +00:00
|
|
|
|
return _repository.Single<Episode>(id);
|
2011-04-23 20:33:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual Episode GetEpisode(int seriesId, int seasonNumber, int episodeNumber)
|
|
|
|
|
{
|
|
|
|
|
return
|
2011-04-26 00:28:33 +00:00
|
|
|
|
_repository.Single<Episode>(
|
2011-04-23 20:33:24 +00:00
|
|
|
|
c => c.SeriesId == seriesId && c.SeasonNumber == seasonNumber && c.EpisodeNumber == episodeNumber);
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-25 21:23:02 +00:00
|
|
|
|
public virtual Episode GetEpisode(int seriesId, DateTime date)
|
|
|
|
|
{
|
|
|
|
|
return
|
2011-04-26 00:28:33 +00:00
|
|
|
|
_repository.Single<Episode>(
|
2011-04-25 21:23:02 +00:00
|
|
|
|
c => c.SeriesId == seriesId && c.AirDate == date.Date);
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-23 20:33:24 +00:00
|
|
|
|
public virtual IList<Episode> GetEpisodeBySeries(long seriesId)
|
|
|
|
|
{
|
2011-04-26 00:28:33 +00:00
|
|
|
|
return _repository.Find<Episode>(e => e.SeriesId == seriesId);
|
2011-04-23 20:33:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual IList<Episode> GetEpisodeBySeason(long seasonId)
|
|
|
|
|
{
|
2011-04-26 00:28:33 +00:00
|
|
|
|
return _repository.Find<Episode>(e => e.SeasonId == seasonId);
|
2011-04-23 20:33:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual IList<Episode> GetEpisodeByParseResult(EpisodeParseResult parseResult)
|
|
|
|
|
{
|
2011-04-26 00:28:33 +00:00
|
|
|
|
var seasonEpisodes = _repository.All<Episode>().Where(e =>
|
2011-04-23 20:33:24 +00:00
|
|
|
|
e.SeriesId == parseResult.SeriesId &&
|
|
|
|
|
e.SeasonNumber == parseResult.SeasonNumber).ToList();
|
|
|
|
|
|
|
|
|
|
//Has to be done separately since subsonic doesn't support contain method
|
|
|
|
|
return seasonEpisodes.Where(c => parseResult.Episodes.Contains(c.EpisodeNumber)).ToList();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Comprehensive check on whether or not this episode is needed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name = "parsedReport">Episode that needs to be checked</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public virtual bool IsNeeded(EpisodeParseResult parsedReport)
|
|
|
|
|
{
|
|
|
|
|
foreach (var episode in parsedReport.Episodes)
|
|
|
|
|
{
|
|
|
|
|
var episodeInfo = GetEpisode(parsedReport.SeriesId, parsedReport.SeasonNumber, episode);
|
|
|
|
|
|
|
|
|
|
if (episodeInfo == null)
|
|
|
|
|
{
|
2011-04-25 17:48:16 +00:00
|
|
|
|
Logger.Debug("Episode S{0:00}E{1:00} doesn't exist in db. adding it now.", parsedReport.SeasonNumber, episode);
|
2011-04-23 20:33:24 +00:00
|
|
|
|
//Todo: How do we want to handle this really? Episode could be released before information is on TheTvDB
|
|
|
|
|
//(Parks and Rec did this a lot in the first season, from experience)
|
|
|
|
|
//Keivan: Should automatically add the episode to db with minimal information. then update the description/title when available.
|
|
|
|
|
episodeInfo = new Episode
|
|
|
|
|
{
|
|
|
|
|
SeriesId = parsedReport.SeriesId,
|
|
|
|
|
AirDate = DateTime.Now.Date,
|
|
|
|
|
EpisodeNumber = episode,
|
|
|
|
|
SeasonNumber = parsedReport.SeasonNumber,
|
|
|
|
|
Title = String.Empty,
|
|
|
|
|
Overview = String.Empty,
|
|
|
|
|
Language = "en"
|
|
|
|
|
};
|
|
|
|
|
|
2011-04-26 00:28:33 +00:00
|
|
|
|
_repository.Add(episodeInfo);
|
2011-04-23 20:33:24 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var file = episodeInfo.EpisodeFile;
|
|
|
|
|
|
|
|
|
|
if (file != null)
|
|
|
|
|
{
|
2011-04-25 17:48:16 +00:00
|
|
|
|
Logger.Debug("File is {0} Proper:{1}", file.Quality, file.Proper);
|
2011-04-23 20:33:24 +00:00
|
|
|
|
|
|
|
|
|
//There will never be a time when the episode quality is less than what we have and we want it... ever.... I think.
|
2011-04-25 17:48:16 +00:00
|
|
|
|
if (file.Quality > parsedReport.Quality)
|
|
|
|
|
{
|
|
|
|
|
Logger.Trace("file has better quality. skipping");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//If not null we need to see if this episode has the quality as the download (or if it is better)
|
|
|
|
|
if (file.Quality == parsedReport.Quality && file.Proper == parsedReport.Proper)
|
|
|
|
|
{
|
|
|
|
|
Logger.Trace("Same quality/proper. existing proper. skipping");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2011-04-23 20:33:24 +00:00
|
|
|
|
|
2011-04-25 17:48:16 +00:00
|
|
|
|
//Now we need to handle upgrades and actually pay attention to the Cut-off Value
|
2011-04-23 20:33:24 +00:00
|
|
|
|
if (file.Quality < parsedReport.Quality)
|
|
|
|
|
{
|
2011-04-25 17:48:16 +00:00
|
|
|
|
if (episodeInfo.Series.QualityProfile.Cutoff <= file.Quality)
|
|
|
|
|
{
|
|
|
|
|
Logger.Trace("Quality is past cut-off skipping.");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2011-04-23 20:33:24 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-25 17:48:16 +00:00
|
|
|
|
Logger.Debug("Episode {0} is needed", parsedReport);
|
2011-04-23 20:33:24 +00:00
|
|
|
|
return true; //If we get to this point and the file has not yet been rejected then accept it
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-25 17:48:16 +00:00
|
|
|
|
Logger.Debug("Episode {0} is not needed", parsedReport);
|
2011-04-23 20:33:24 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void RefreshEpisodeInfo(int seriesId)
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("Starting episode info refresh for series:{0}", seriesId);
|
|
|
|
|
int successCount = 0;
|
|
|
|
|
int failCount = 0;
|
2011-04-26 00:28:33 +00:00
|
|
|
|
var targetSeries = _tvDbProvider.GetSeries(seriesId, true);
|
2011-04-23 20:33:24 +00:00
|
|
|
|
|
|
|
|
|
var updateList = new List<Episode>();
|
|
|
|
|
var newList = new List<Episode>();
|
|
|
|
|
|
|
|
|
|
Logger.Debug("Updating season info for series:{0}", targetSeries.SeriesName);
|
|
|
|
|
targetSeries.Episodes.Select(e => new { e.SeasonId, e.SeasonNumber })
|
|
|
|
|
.Distinct().ToList()
|
2011-04-26 00:28:33 +00:00
|
|
|
|
.ForEach(s => _seasonsProvider.EnsureSeason(seriesId, s.SeasonId, s.SeasonNumber));
|
2011-04-23 20:33:24 +00:00
|
|
|
|
|
|
|
|
|
foreach (var episode in targetSeries.Episodes)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
//DateTime throws an error in SQLServer per message below:
|
|
|
|
|
//SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
|
|
|
|
|
//So lets hack it so it works for SQLServer (as well as SQLite), perhaps we can find a better solution
|
|
|
|
|
//Todo: Fix this hack
|
|
|
|
|
if (episode.FirstAired < new DateTime(1753, 1, 1))
|
|
|
|
|
episode.FirstAired = new DateTime(1753, 1, 1);
|
|
|
|
|
|
|
|
|
|
Logger.Trace("Updating info for [{0}] - S{1}E{2}", targetSeries.SeriesName, episode.SeasonNumber, episode.EpisodeNumber);
|
|
|
|
|
var newEpisode = new Episode
|
|
|
|
|
{
|
2011-04-25 21:23:02 +00:00
|
|
|
|
AirDate = episode.FirstAired.Date,
|
2011-04-23 20:33:24 +00:00
|
|
|
|
TvDbEpisodeId = episode.Id,
|
|
|
|
|
EpisodeNumber = episode.EpisodeNumber,
|
|
|
|
|
Language = episode.Language.Abbriviation,
|
|
|
|
|
Overview = episode.Overview,
|
|
|
|
|
SeasonId = episode.SeasonId,
|
|
|
|
|
SeasonNumber = episode.SeasonNumber,
|
|
|
|
|
SeriesId = seriesId,
|
2011-04-25 22:27:03 +00:00
|
|
|
|
Title = episode.EpisodeName,
|
|
|
|
|
LastInfoSync = DateTime.Now
|
|
|
|
|
|
2011-04-23 20:33:24 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var existingEpisode = GetEpisode(episode.SeriesId, episode.SeasonNumber, episode.EpisodeNumber);
|
|
|
|
|
|
|
|
|
|
if (existingEpisode != null)
|
|
|
|
|
{
|
2011-04-25 22:27:03 +00:00
|
|
|
|
//TODO: Write test for this, possibly make it future proof, we should only copy fields that come from tvdb
|
2011-04-23 20:33:24 +00:00
|
|
|
|
newEpisode.EpisodeId = existingEpisode.EpisodeId;
|
2011-04-25 22:27:03 +00:00
|
|
|
|
newEpisode.EpisodeFileId = existingEpisode.EpisodeFileId;
|
|
|
|
|
newEpisode.LastDiskSync = existingEpisode.LastDiskSync;
|
|
|
|
|
newEpisode.Status = existingEpisode.Status;
|
2011-04-23 20:33:24 +00:00
|
|
|
|
updateList.Add(newEpisode);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
newList.Add(newEpisode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
successCount++;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Logger.FatalException(
|
|
|
|
|
String.Format("An error has occurred while updating episode info for series {0}", seriesId), e);
|
|
|
|
|
failCount++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-26 00:28:33 +00:00
|
|
|
|
_repository.AddMany(newList);
|
|
|
|
|
_repository.UpdateMany(updateList);
|
2011-04-23 20:33:24 +00:00
|
|
|
|
|
|
|
|
|
Logger.Debug("Finished episode refresh for series:{0}. Successful:{1} - Failed:{2} ",
|
|
|
|
|
targetSeries.SeriesName, successCount, failCount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void DeleteEpisode(int episodeId)
|
|
|
|
|
{
|
2011-04-26 00:28:33 +00:00
|
|
|
|
_repository.Delete<Episode>(episodeId);
|
2011-04-23 20:33:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void UpdateEpisode(Episode episode)
|
|
|
|
|
{
|
2011-04-26 00:28:33 +00:00
|
|
|
|
_repository.Update(episode);
|
2011-04-23 20:33:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-04-22 22:24:05 +00:00
|
|
|
|
} |