2010-09-28 22:32:19 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2011-04-21 04:26:13 +03:00
|
|
|
using System.IO;
|
2011-04-10 05:44:01 +03:00
|
|
|
using System.Linq;
|
2010-10-05 09:21:18 +03:00
|
|
|
using NLog;
|
2010-10-21 04:49:23 +03:00
|
|
|
using NzbDrone.Core.Model;
|
|
|
|
using NzbDrone.Core.Repository;
|
2010-09-28 22:32:19 +03:00
|
|
|
using SubSonic.Repository;
|
2010-09-28 08:01:54 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
{
|
2011-04-10 03:14:51 +03:00
|
|
|
public class EpisodeProvider
|
2010-09-28 08:01:54 +03:00
|
|
|
{
|
2011-04-10 05:44:01 +03:00
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2011-04-08 07:03:46 +03:00
|
|
|
private readonly QualityProvider _quality;
|
2011-04-10 05:44:01 +03:00
|
|
|
private readonly SeasonProvider _seasons;
|
|
|
|
private readonly SeriesProvider _series;
|
|
|
|
private readonly IRepository _sonicRepo;
|
|
|
|
private readonly TvDbProvider _tvDb;
|
2010-09-28 22:32:19 +03:00
|
|
|
|
2011-04-09 02:55:23 +03:00
|
|
|
public EpisodeProvider(IRepository sonicRepo, SeriesProvider seriesProvider,
|
2011-04-10 05:44:01 +03:00
|
|
|
SeasonProvider seasonProvider, TvDbProvider tvDbProvider,
|
2011-04-22 05:23:31 +03:00
|
|
|
QualityProvider quality)
|
2010-09-28 08:01:54 +03:00
|
|
|
{
|
2010-09-28 22:32:19 +03:00
|
|
|
_sonicRepo = sonicRepo;
|
2010-10-05 09:21:18 +03:00
|
|
|
_series = seriesProvider;
|
|
|
|
_tvDb = tvDbProvider;
|
|
|
|
_seasons = seasonProvider;
|
2011-02-16 18:37:28 +02:00
|
|
|
_quality = quality;
|
2010-09-28 22:32:19 +03:00
|
|
|
}
|
|
|
|
|
2011-04-10 03:14:51 +03:00
|
|
|
public EpisodeProvider()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual Episode GetEpisode(long id)
|
2010-09-28 22:32:19 +03:00
|
|
|
{
|
2010-10-24 10:46:58 +03:00
|
|
|
return _sonicRepo.Single<Episode>(id);
|
|
|
|
}
|
|
|
|
|
2011-04-10 03:14:51 +03:00
|
|
|
public virtual Episode GetEpisode(int seriesId, int seasonNumber, int episodeNumber)
|
2010-10-24 10:46:58 +03:00
|
|
|
{
|
2011-04-10 05:44:01 +03:00
|
|
|
return
|
|
|
|
_sonicRepo.Single<Episode>(
|
|
|
|
c => c.SeriesId == seriesId && c.SeasonNumber == seasonNumber && c.EpisodeNumber == episodeNumber);
|
2010-09-28 22:32:19 +03:00
|
|
|
}
|
|
|
|
|
2011-04-10 03:14:51 +03:00
|
|
|
public virtual IList<Episode> GetEpisodeBySeries(long seriesId)
|
2010-09-28 22:32:19 +03:00
|
|
|
{
|
2010-10-21 04:49:23 +03:00
|
|
|
return _sonicRepo.Find<Episode>(e => e.SeriesId == seriesId);
|
2010-09-28 22:32:19 +03:00
|
|
|
}
|
|
|
|
|
2011-04-10 03:14:51 +03:00
|
|
|
public virtual IList<Episode> GetEpisodeBySeason(long seasonId)
|
2010-10-30 05:46:32 +03:00
|
|
|
{
|
|
|
|
return _sonicRepo.Find<Episode>(e => e.SeasonId == seasonId);
|
|
|
|
}
|
|
|
|
|
2011-04-22 05:23:31 +03:00
|
|
|
public virtual IList<Episode> GetEpisodeByParseResult(EpisodeParseResult parseResult)
|
|
|
|
{
|
2011-04-22 23:14:02 +03:00
|
|
|
var seasonEpisodes = _sonicRepo.All<Episode>().Where(e =>
|
|
|
|
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();
|
2011-04-22 05:23:31 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-04-21 04:26:13 +03:00
|
|
|
public virtual String GetSabTitle(EpisodeParseResult parseResult)
|
2010-09-28 22:32:19 +03:00
|
|
|
{
|
2011-04-21 04:26:13 +03:00
|
|
|
//Show Name - 1x01-1x02 - Episode Name
|
|
|
|
//Show Name - 1x01 - Episode Name
|
|
|
|
var episodeString = new List<String>();
|
2010-09-28 22:32:19 +03:00
|
|
|
|
2011-04-21 04:26:13 +03:00
|
|
|
foreach (var episode in parseResult.Episodes)
|
|
|
|
{
|
|
|
|
episodeString.Add(String.Format("{0}x{1}", parseResult.SeasonNumber, episode));
|
|
|
|
}
|
|
|
|
|
|
|
|
var epNumberString = String.Join("-", episodeString);
|
|
|
|
var series = _series.GetSeries(parseResult.SeriesId);
|
|
|
|
var folderName = new DirectoryInfo(series.Path).Name;
|
|
|
|
|
|
|
|
var result = String.Format("{0} - {1} - {2} {3}", folderName, epNumberString, parseResult.EpisodeTitle, parseResult.Quality);
|
|
|
|
|
|
|
|
if (parseResult.Proper)
|
|
|
|
{
|
|
|
|
result += " [Proper]";
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2010-09-28 08:01:54 +03:00
|
|
|
}
|
|
|
|
|
2010-09-28 22:32:19 +03:00
|
|
|
/// <summary>
|
2011-04-10 05:44:01 +03:00
|
|
|
/// Comprehensive check on whether or not this episode is needed.
|
2010-09-28 22:32:19 +03:00
|
|
|
/// </summary>
|
2011-04-10 05:44:01 +03:00
|
|
|
/// <param name = "parsedReport">Episode that needs to be checked</param>
|
2010-09-28 22:32:19 +03:00
|
|
|
/// <returns></returns>
|
2011-04-10 03:14:51 +03:00
|
|
|
public virtual bool IsNeeded(EpisodeParseResult parsedReport)
|
2010-09-28 08:01:54 +03:00
|
|
|
{
|
2011-04-04 07:54:58 +03:00
|
|
|
foreach (var episode in parsedReport.Episodes)
|
2011-02-16 18:37:28 +02:00
|
|
|
{
|
2011-04-04 07:54:58 +03:00
|
|
|
var episodeInfo = GetEpisode(parsedReport.SeriesId, parsedReport.SeasonNumber, episode);
|
2011-01-29 08:10:22 +02:00
|
|
|
|
2011-04-04 07:54:58 +03:00
|
|
|
if (episodeInfo == null)
|
2011-01-29 08:10:22 +02:00
|
|
|
{
|
2011-04-21 04:26:13 +03:00
|
|
|
|
2011-04-04 07:54:58 +03: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)
|
2011-04-21 04:26:13 +03:00
|
|
|
//Keivan: Should automatically add the episode to db with minimal information. then update the description/title when available.
|
2011-04-22 05:23:31 +03:00
|
|
|
episodeInfo = new Episode
|
2011-04-21 04:26:13 +03:00
|
|
|
{
|
|
|
|
SeriesId = parsedReport.SeriesId,
|
|
|
|
AirDate = DateTime.Now.Date,
|
|
|
|
EpisodeNumber = episode,
|
|
|
|
SeasonNumber = parsedReport.SeasonNumber,
|
|
|
|
Title = String.Empty,
|
|
|
|
Overview = String.Empty,
|
|
|
|
Language = "en"
|
|
|
|
};
|
|
|
|
|
|
|
|
_sonicRepo.Add(episodeInfo);
|
|
|
|
|
2011-02-22 10:13:16 +02:00
|
|
|
}
|
|
|
|
|
2011-04-04 07:54:58 +03:00
|
|
|
var file = episodeInfo.EpisodeFile;
|
2011-02-22 10:13:16 +02:00
|
|
|
|
2011-04-04 07:54:58 +03:00
|
|
|
if (file != null)
|
2011-02-22 10:13:16 +02:00
|
|
|
{
|
2011-04-04 07:54:58 +03:00
|
|
|
//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) continue;
|
2011-02-22 10:13:16 +02:00
|
|
|
|
2011-04-04 07:54:58 +03:00
|
|
|
//There will never be a time when the episode quality is less than what we have and we want it... ever.... I think.
|
|
|
|
if (file.Quality > parsedReport.Quality) continue;
|
|
|
|
|
|
|
|
//Now we need to handle upgrades and actually pay attention to the Cutoff Value
|
|
|
|
if (file.Quality < parsedReport.Quality)
|
2011-02-16 18:37:28 +02:00
|
|
|
{
|
2011-04-04 07:54:58 +03:00
|
|
|
var quality = _quality.Find(episodeInfo.Series.QualityProfileId);
|
|
|
|
|
|
|
|
if (quality.Cutoff <= file.Quality && file.Proper) continue;
|
2011-02-16 18:37:28 +02:00
|
|
|
}
|
2011-01-29 08:10:22 +02:00
|
|
|
}
|
|
|
|
|
2011-04-10 05:44:01 +03:00
|
|
|
return true; //If we get to this point and the file has not yet been rejected then accept it
|
2011-01-29 08:10:22 +02:00
|
|
|
}
|
|
|
|
|
2011-04-04 07:54:58 +03:00
|
|
|
return false;
|
2010-09-28 22:32:19 +03:00
|
|
|
}
|
|
|
|
|
2011-04-10 03:14:51 +03:00
|
|
|
public virtual void RefreshEpisodeInfo(int seriesId)
|
2010-10-05 09:21:18 +03:00
|
|
|
{
|
|
|
|
Logger.Info("Starting episode info refresh for series:{0}", seriesId);
|
|
|
|
int successCount = 0;
|
|
|
|
int failCount = 0;
|
|
|
|
var targetSeries = _tvDb.GetSeries(seriesId, true);
|
2010-10-17 20:22:48 +03:00
|
|
|
|
2010-10-21 04:49:23 +03:00
|
|
|
var updateList = new List<Episode>();
|
|
|
|
var newList = new List<Episode>();
|
2010-10-17 20:22:48 +03:00
|
|
|
|
2010-10-30 05:46:32 +03:00
|
|
|
Logger.Debug("Updating season info for series:{0}", targetSeries.SeriesName);
|
2011-04-20 10:44:13 +03:00
|
|
|
targetSeries.Episodes.Select(e => new { e.SeasonId, e.SeasonNumber })
|
2010-10-17 20:22:48 +03:00
|
|
|
.Distinct().ToList()
|
|
|
|
.ForEach(s => _seasons.EnsureSeason(seriesId, s.SeasonId, s.SeasonNumber));
|
|
|
|
|
2010-10-05 09:21:18 +03:00
|
|
|
foreach (var episode in targetSeries.Episodes)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2011-01-29 08:10:22 +02:00
|
|
|
//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);
|
|
|
|
|
2011-04-20 10:44:13 +03:00
|
|
|
Logger.Trace("Updating info for [{0}] - S{1}E{2}", targetSeries.SeriesName, episode.SeasonNumber, episode.EpisodeNumber);
|
2011-04-10 05:44:01 +03:00
|
|
|
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
|
|
|
|
};
|
2010-10-17 20:22:48 +03:00
|
|
|
|
2010-10-21 04:49:23 +03:00
|
|
|
if (_sonicRepo.Exists<Episode>(e => e.EpisodeId == newEpisode.EpisodeId))
|
2010-10-05 09:21:18 +03:00
|
|
|
{
|
2010-10-17 20:22:48 +03:00
|
|
|
updateList.Add(newEpisode);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newList.Add(newEpisode);
|
|
|
|
}
|
2010-10-05 09:21:18 +03:00
|
|
|
|
2010-10-17 20:22:48 +03:00
|
|
|
successCount++;
|
2010-10-05 09:21:18 +03:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2011-04-10 05:44:01 +03:00
|
|
|
Logger.FatalException(
|
|
|
|
String.Format("An error has occurred while updating episode info for series {0}", seriesId), e);
|
2010-10-05 09:21:18 +03:00
|
|
|
failCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-17 20:22:48 +03:00
|
|
|
_sonicRepo.AddMany(newList);
|
|
|
|
_sonicRepo.UpdateMany(updateList);
|
|
|
|
|
2011-04-10 05:44:01 +03:00
|
|
|
Logger.Debug("Finished episode refresh for series:{0}. Successful:{1} - Failed:{2} ",
|
|
|
|
targetSeries.SeriesName, successCount, failCount);
|
2010-10-05 09:21:18 +03:00
|
|
|
}
|
2011-01-29 08:10:22 +02:00
|
|
|
|
2011-04-10 03:14:51 +03:00
|
|
|
public virtual void RefreshEpisodeInfo(Season season)
|
2011-02-10 08:42:46 +02:00
|
|
|
{
|
2011-04-10 05:44:01 +03:00
|
|
|
Logger.Info("Starting episode info refresh for season {0} of series:{1}", season.SeasonNumber,
|
|
|
|
season.SeriesId);
|
2011-02-10 08:42:46 +02:00
|
|
|
int successCount = 0;
|
|
|
|
int failCount = 0;
|
|
|
|
var targetSeries = _tvDb.GetSeries(season.SeriesId, true);
|
|
|
|
|
|
|
|
var updateList = new List<Episode>();
|
|
|
|
var newList = new List<Episode>();
|
|
|
|
|
|
|
|
foreach (var episode in targetSeries.Episodes.Where(e => e.SeasonId == season.SeasonId))
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2011-04-10 05:44:01 +03:00
|
|
|
Logger.Trace("Updating info for series:{0} - episode:{1}", targetSeries.SeriesName,
|
|
|
|
episode.EpisodeNumber);
|
|
|
|
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 = season.SeriesId,
|
|
|
|
Title = episode.EpisodeName
|
|
|
|
};
|
2011-02-10 08:42:46 +02:00
|
|
|
|
2011-04-21 04:26:13 +03:00
|
|
|
|
|
|
|
//TODO: Replace this db check with a local check. Should make things even faster
|
2011-02-10 08:42:46 +02:00
|
|
|
if (_sonicRepo.Exists<Episode>(e => e.EpisodeId == newEpisode.EpisodeId))
|
|
|
|
{
|
|
|
|
updateList.Add(newEpisode);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newList.Add(newEpisode);
|
|
|
|
}
|
|
|
|
|
|
|
|
successCount++;
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2011-04-10 05:44:01 +03:00
|
|
|
Logger.FatalException(
|
|
|
|
String.Format("An error has occurred while updating episode info for season {0} of series {1}",
|
|
|
|
season.SeasonNumber, season.SeriesId), e);
|
2011-02-10 08:42:46 +02:00
|
|
|
failCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_sonicRepo.AddMany(newList);
|
|
|
|
_sonicRepo.UpdateMany(updateList);
|
|
|
|
|
2011-04-10 05:44:01 +03:00
|
|
|
Logger.Debug("Finished episode refresh for series:{0}. Successful:{1} - Failed:{2} ",
|
|
|
|
targetSeries.SeriesName, successCount, failCount);
|
2011-02-10 08:42:46 +02:00
|
|
|
}
|
|
|
|
|
2011-04-10 03:14:51 +03:00
|
|
|
public virtual void DeleteEpisode(int episodeId)
|
2011-02-18 08:49:23 +02:00
|
|
|
{
|
|
|
|
_sonicRepo.Delete<Episode>(episodeId);
|
|
|
|
}
|
|
|
|
|
2011-04-10 03:14:51 +03:00
|
|
|
public virtual void UpdateEpisode(Episode episode)
|
2011-02-23 08:23:59 +02:00
|
|
|
{
|
|
|
|
_sonicRepo.Update(episode);
|
|
|
|
}
|
2010-09-28 08:01:54 +03:00
|
|
|
}
|
2011-04-23 01:24:05 +03:00
|
|
|
} |