Add TvDbEpisodeId to Episode. now we can index episodes before they showup in thetvdb

pull/4/head
kay.one 14 years ago
parent be74c67df8
commit 3beeff4e18

@ -1,15 +0,0 @@
using System;
namespace NzbDrone.Core.Model
{
public class NzbInfoModel
{
public string Title { get; set; }
public Uri Link { get; set; }
public bool IsPassworded()
{
return Title.EndsWith("(Passworded)", StringComparison.InvariantCultureIgnoreCase);
}
}
}

@ -180,7 +180,6 @@
<Compile Include="Model\EpisodeRenameModel.cs" /> <Compile Include="Model\EpisodeRenameModel.cs" />
<Compile Include="Model\EpisodeSortingType.cs" /> <Compile Include="Model\EpisodeSortingType.cs" />
<Compile Include="Model\EpisodeStatusType.cs" /> <Compile Include="Model\EpisodeStatusType.cs" />
<Compile Include="Model\NzbInfoModel.cs" />
<Compile Include="Model\SabnzbdPriorityType.cs" /> <Compile Include="Model\SabnzbdPriorityType.cs" />
<Compile Include="Model\SceneNameModel.cs" /> <Compile Include="Model\SceneNameModel.cs" />
<Compile Include="Model\SeasonParseResult.cs" /> <Compile Include="Model\SeasonParseResult.cs" />

@ -1,293 +1,230 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using NLog; using NLog;
using NzbDrone.Core.Model; using NzbDrone.Core.Model;
using NzbDrone.Core.Repository; using NzbDrone.Core.Repository;
using SubSonic.Repository; using SubSonic.Repository;
namespace NzbDrone.Core.Providers namespace NzbDrone.Core.Providers
{ {
public class EpisodeProvider public class EpisodeProvider
{ {
private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly QualityProvider _quality; private readonly QualityProvider _quality;
private readonly SeasonProvider _seasons; private readonly SeasonProvider _seasons;
private readonly SeriesProvider _series; private readonly SeriesProvider _series;
private readonly IRepository _sonicRepo; private readonly IRepository _sonicRepo;
private readonly TvDbProvider _tvDb; private readonly TvDbProvider _tvDb;
public EpisodeProvider(IRepository sonicRepo, SeriesProvider seriesProvider, public EpisodeProvider(IRepository sonicRepo, SeriesProvider seriesProvider,
SeasonProvider seasonProvider, TvDbProvider tvDbProvider, SeasonProvider seasonProvider, TvDbProvider tvDbProvider,
QualityProvider quality) QualityProvider quality)
{ {
_sonicRepo = sonicRepo; _sonicRepo = sonicRepo;
_series = seriesProvider; _series = seriesProvider;
_tvDb = tvDbProvider; _tvDb = tvDbProvider;
_seasons = seasonProvider; _seasons = seasonProvider;
_quality = quality; _quality = quality;
} }
public EpisodeProvider() public EpisodeProvider()
{ {
} }
public virtual Episode GetEpisode(long id) public virtual Episode GetEpisode(long id)
{ {
return _sonicRepo.Single<Episode>(id); return _sonicRepo.Single<Episode>(id);
} }
public virtual Episode GetEpisode(int seriesId, int seasonNumber, int episodeNumber) public virtual Episode GetEpisode(int seriesId, int seasonNumber, int episodeNumber)
{ {
return return
_sonicRepo.Single<Episode>( _sonicRepo.Single<Episode>(
c => c.SeriesId == seriesId && c.SeasonNumber == seasonNumber && c.EpisodeNumber == episodeNumber); c => c.SeriesId == seriesId && c.SeasonNumber == seasonNumber && c.EpisodeNumber == episodeNumber);
} }
public virtual IList<Episode> GetEpisodeBySeries(long seriesId) public virtual IList<Episode> GetEpisodeBySeries(long seriesId)
{ {
return _sonicRepo.Find<Episode>(e => e.SeriesId == seriesId); return _sonicRepo.Find<Episode>(e => e.SeriesId == seriesId);
} }
public virtual IList<Episode> GetEpisodeBySeason(long seasonId) public virtual IList<Episode> GetEpisodeBySeason(long seasonId)
{ {
return _sonicRepo.Find<Episode>(e => e.SeasonId == seasonId); return _sonicRepo.Find<Episode>(e => e.SeasonId == seasonId);
} }
public virtual IList<Episode> GetEpisodeByParseResult(EpisodeParseResult parseResult) public virtual IList<Episode> GetEpisodeByParseResult(EpisodeParseResult parseResult)
{ {
var seasonEpisodes = _sonicRepo.All<Episode>().Where(e => var seasonEpisodes = _sonicRepo.All<Episode>().Where(e =>
e.SeriesId == parseResult.SeriesId && e.SeriesId == parseResult.SeriesId &&
e.SeasonNumber == parseResult.SeasonNumber).ToList(); e.SeasonNumber == parseResult.SeasonNumber).ToList();
//Has to be done separately since subsonic doesn't support contain method //Has to be done separately since subsonic doesn't support contain method
return seasonEpisodes.Where(c => parseResult.Episodes.Contains(c.EpisodeNumber)).ToList(); return seasonEpisodes.Where(c => parseResult.Episodes.Contains(c.EpisodeNumber)).ToList();
} }
public virtual String GetSabTitle(EpisodeParseResult parseResult) public virtual String GetSabTitle(EpisodeParseResult parseResult)
{ {
//Show Name - 1x01-1x02 - Episode Name //Show Name - 1x01-1x02 - Episode Name
//Show Name - 1x01 - Episode Name //Show Name - 1x01 - Episode Name
var episodeString = new List<String>(); var episodeString = new List<String>();
foreach (var episode in parseResult.Episodes) foreach (var episode in parseResult.Episodes)
{ {
episodeString.Add(String.Format("{0}x{1}", parseResult.SeasonNumber, episode)); episodeString.Add(String.Format("{0}x{1}", parseResult.SeasonNumber, episode));
} }
var epNumberString = String.Join("-", episodeString); var epNumberString = String.Join("-", episodeString);
var series = _series.GetSeries(parseResult.SeriesId); var series = _series.GetSeries(parseResult.SeriesId);
var folderName = new DirectoryInfo(series.Path).Name; var folderName = new DirectoryInfo(series.Path).Name;
var result = String.Format("{0} - {1} - {2} {3}", folderName, epNumberString, parseResult.EpisodeTitle, parseResult.Quality); var result = String.Format("{0} - {1} - {2} {3}", folderName, epNumberString, parseResult.EpisodeTitle, parseResult.Quality);
if (parseResult.Proper) if (parseResult.Proper)
{ {
result += " [Proper]"; result += " [Proper]";
} }
return result; return result;
} }
/// <summary> /// <summary>
/// Comprehensive check on whether or not this episode is needed. /// Comprehensive check on whether or not this episode is needed.
/// </summary> /// </summary>
/// <param name = "parsedReport">Episode that needs to be checked</param> /// <param name = "parsedReport">Episode that needs to be checked</param>
/// <returns></returns> /// <returns></returns>
public virtual bool IsNeeded(EpisodeParseResult parsedReport) public virtual bool IsNeeded(EpisodeParseResult parsedReport)
{ {
foreach (var episode in parsedReport.Episodes) foreach (var episode in parsedReport.Episodes)
{ {
var episodeInfo = GetEpisode(parsedReport.SeriesId, parsedReport.SeasonNumber, episode); var episodeInfo = GetEpisode(parsedReport.SeriesId, parsedReport.SeasonNumber, episode);
if (episodeInfo == null) if (episodeInfo == null)
{ {
//Todo: How do we want to handle this really? Episode could be released before information is on TheTvDB //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) //(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. //Keivan: Should automatically add the episode to db with minimal information. then update the description/title when available.
episodeInfo = new Episode episodeInfo = new Episode
{ {
SeriesId = parsedReport.SeriesId, SeriesId = parsedReport.SeriesId,
AirDate = DateTime.Now.Date, AirDate = DateTime.Now.Date,
EpisodeNumber = episode, EpisodeNumber = episode,
SeasonNumber = parsedReport.SeasonNumber, SeasonNumber = parsedReport.SeasonNumber,
Title = String.Empty, Title = String.Empty,
Overview = String.Empty, Overview = String.Empty,
Language = "en" Language = "en"
}; };
_sonicRepo.Add(episodeInfo); _sonicRepo.Add(episodeInfo);
} }
var file = episodeInfo.EpisodeFile; var file = episodeInfo.EpisodeFile;
if (file != null) if (file != null)
{ {
//If not null we need to see if this episode has the quality as the download (or if it is better) //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; if (file.Quality == parsedReport.Quality && file.Proper) continue;
//There will never be a time when the episode quality is less than what we have and we want it... ever.... I think. //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; if (file.Quality > parsedReport.Quality) continue;
//Now we need to handle upgrades and actually pay attention to the Cutoff Value //Now we need to handle upgrades and actually pay attention to the Cutoff Value
if (file.Quality < parsedReport.Quality) if (file.Quality < parsedReport.Quality)
{ {
var quality = _quality.Find(episodeInfo.Series.QualityProfileId); var quality = _quality.Find(episodeInfo.Series.QualityProfileId);
if (quality.Cutoff <= file.Quality && file.Proper) continue; if (quality.Cutoff <= file.Quality && file.Proper) continue;
} }
} }
return true; //If we get to this point and the file has not yet been rejected then accept it return true; //If we get to this point and the file has not yet been rejected then accept it
} }
return false; return false;
} }
public virtual void RefreshEpisodeInfo(int seriesId) public virtual void RefreshEpisodeInfo(int seriesId)
{ {
Logger.Info("Starting episode info refresh for series:{0}", seriesId); Logger.Info("Starting episode info refresh for series:{0}", seriesId);
int successCount = 0; int successCount = 0;
int failCount = 0; int failCount = 0;
var targetSeries = _tvDb.GetSeries(seriesId, true); var targetSeries = _tvDb.GetSeries(seriesId, true);
var updateList = new List<Episode>(); var updateList = new List<Episode>();
var newList = new List<Episode>(); var newList = new List<Episode>();
Logger.Debug("Updating season info for series:{0}", targetSeries.SeriesName); Logger.Debug("Updating season info for series:{0}", targetSeries.SeriesName);
targetSeries.Episodes.Select(e => new { e.SeasonId, e.SeasonNumber }) targetSeries.Episodes.Select(e => new { e.SeasonId, e.SeasonNumber })
.Distinct().ToList() .Distinct().ToList()
.ForEach(s => _seasons.EnsureSeason(seriesId, s.SeasonId, s.SeasonNumber)); .ForEach(s => _seasons.EnsureSeason(seriesId, s.SeasonId, s.SeasonNumber));
foreach (var episode in targetSeries.Episodes) foreach (var episode in targetSeries.Episodes)
{ {
try try
{ {
//DateTime throws an error in SQLServer per message below: //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. //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 //So lets hack it so it works for SQLServer (as well as SQLite), perhaps we can find a better solution
//Todo: Fix this hack //Todo: Fix this hack
if (episode.FirstAired < new DateTime(1753, 1, 1)) if (episode.FirstAired < new DateTime(1753, 1, 1))
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); Logger.Trace("Updating info for [{0}] - S{1}E{2}", targetSeries.SeriesName, episode.SeasonNumber, episode.EpisodeNumber);
var newEpisode = new Episode var newEpisode = new Episode
{ {
AirDate = episode.FirstAired, AirDate = episode.FirstAired,
EpisodeId = episode.Id, TvDbEpisodeId = episode.Id,
EpisodeNumber = episode.EpisodeNumber, EpisodeNumber = episode.EpisodeNumber,
Language = episode.Language.Abbriviation, Language = episode.Language.Abbriviation,
Overview = episode.Overview, Overview = episode.Overview,
SeasonId = episode.SeasonId, SeasonId = episode.SeasonId,
SeasonNumber = episode.SeasonNumber, SeasonNumber = episode.SeasonNumber,
SeriesId = seriesId, SeriesId = seriesId,
Title = episode.EpisodeName Title = episode.EpisodeName
}; };
if (_sonicRepo.Exists<Episode>(e => e.EpisodeId == newEpisode.EpisodeId)) var existingEpisode = GetEpisode(episode.SeriesId, episode.SeasonNumber, episode.EpisodeNumber);
{
updateList.Add(newEpisode); if (existingEpisode != null)
} {
else newEpisode.EpisodeId = existingEpisode.EpisodeId;
{ updateList.Add(newEpisode);
newList.Add(newEpisode); }
} else
{
successCount++; newList.Add(newEpisode);
} }
catch (Exception e)
{ successCount++;
Logger.FatalException( }
String.Format("An error has occurred while updating episode info for series {0}", seriesId), e); catch (Exception e)
failCount++; {
} Logger.FatalException(
} String.Format("An error has occurred while updating episode info for series {0}", seriesId), e);
failCount++;
_sonicRepo.AddMany(newList); }
_sonicRepo.UpdateMany(updateList); }
Logger.Debug("Finished episode refresh for series:{0}. Successful:{1} - Failed:{2} ", _sonicRepo.AddMany(newList);
targetSeries.SeriesName, successCount, failCount); _sonicRepo.UpdateMany(updateList);
}
Logger.Debug("Finished episode refresh for series:{0}. Successful:{1} - Failed:{2} ",
public virtual void RefreshEpisodeInfo(Season season) targetSeries.SeriesName, successCount, failCount);
{ }
Logger.Info("Starting episode info refresh for season {0} of series:{1}", season.SeasonNumber,
season.SeriesId); public virtual void DeleteEpisode(int episodeId)
int successCount = 0; {
int failCount = 0; _sonicRepo.Delete<Episode>(episodeId);
var targetSeries = _tvDb.GetSeries(season.SeriesId, true); }
var updateList = new List<Episode>(); public virtual void UpdateEpisode(Episode episode)
var newList = new List<Episode>(); {
_sonicRepo.Update(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);
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
};
//TODO: Replace this db check with a local check. Should make things even faster
if (_sonicRepo.Exists<Episode>(e => e.EpisodeId == newEpisode.EpisodeId))
{
updateList.Add(newEpisode);
}
else
{
newList.Add(newEpisode);
}
successCount++;
}
catch (Exception e)
{
Logger.FatalException(
String.Format("An error has occurred while updating episode info for season {0} of series {1}",
season.SeasonNumber, season.SeriesId), e);
failCount++;
}
}
_sonicRepo.AddMany(newList);
_sonicRepo.UpdateMany(updateList);
Logger.Debug("Finished episode refresh for series:{0}. Successful:{1} - Failed:{2} ",
targetSeries.SeriesName, successCount, failCount);
}
public virtual void DeleteEpisode(int episodeId)
{
_sonicRepo.Delete<Episode>(episodeId);
}
public virtual void UpdateEpisode(Episode episode)
{
_sonicRepo.Update(episode);
}
}
} }

@ -7,9 +7,11 @@ namespace NzbDrone.Core.Repository
{ {
public class Episode public class Episode
{ {
[SubSonicPrimaryKey(false)] [SubSonicPrimaryKey]
public virtual int EpisodeId { get; set; } public virtual int EpisodeId { get; set; }
public int? TvDbEpisodeId { get; set; }
public virtual int SeriesId { get; set; } public virtual int SeriesId { get; set; }
public virtual int EpisodeFileId { get; set; } public virtual int EpisodeFileId { get; set; }
public virtual int SeasonId { get; set; } public virtual int SeasonId { get; set; }

Loading…
Cancel
Save