2011-04-23 20:33:24 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2011-06-18 17:19:24 +00:00
|
|
|
|
using System.Threading;
|
2011-06-14 01:23:04 +00:00
|
|
|
|
using Ninject;
|
2011-04-23 20:33:24 +00:00
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Core.Model;
|
|
|
|
|
using NzbDrone.Core.Repository;
|
2011-06-15 02:31:41 +00:00
|
|
|
|
using PetaPoco;
|
2011-04-23 20:33:24 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
|
{
|
|
|
|
|
public class EpisodeProvider
|
|
|
|
|
{
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2011-04-26 00:28:33 +00:00
|
|
|
|
private readonly TvDbProvider _tvDbProvider;
|
2011-06-15 02:31:41 +00:00
|
|
|
|
private readonly IDatabase _database;
|
2011-04-26 00:28:33 +00:00
|
|
|
|
|
2011-06-14 01:23:04 +00:00
|
|
|
|
[Inject]
|
2011-06-16 06:33:01 +00:00
|
|
|
|
public EpisodeProvider(TvDbProvider tvDbProviderProvider, IDatabase database)
|
2011-04-23 20:33:24 +00:00
|
|
|
|
{
|
2011-04-26 00:28:33 +00:00
|
|
|
|
_tvDbProvider = tvDbProviderProvider;
|
2011-06-15 02:31:41 +00:00
|
|
|
|
_database = database;
|
2011-04-23 20:33:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EpisodeProvider()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-04 01:56:53 +00:00
|
|
|
|
public virtual void AddEpisode(Episode episode)
|
2011-05-20 03:47:07 +00:00
|
|
|
|
{
|
2011-06-16 06:33:01 +00:00
|
|
|
|
_database.Insert(episode);
|
2011-05-20 03:47:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-04-23 20:33:24 +00:00
|
|
|
|
public virtual Episode GetEpisode(long id)
|
|
|
|
|
{
|
2011-06-18 23:03:58 +00:00
|
|
|
|
return _database.Single<Episode>(id);
|
2011-04-23 20:33:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual Episode GetEpisode(int seriesId, int seasonNumber, int episodeNumber)
|
|
|
|
|
{
|
2011-06-18 19:56:17 +00:00
|
|
|
|
return _database.SingleOrDefault<Episode>("WHERE SeriesId = @0 AND SeasonNumber = @1 AND EpisodeNumber = @2", seriesId, seasonNumber, episodeNumber);
|
2011-04-23 20:33:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-04-25 21:23:02 +00:00
|
|
|
|
public virtual Episode GetEpisode(int seriesId, DateTime date)
|
|
|
|
|
{
|
2011-06-18 19:56:17 +00:00
|
|
|
|
return _database.SingleOrDefault<Episode>("WHERE SeriesId = @0 AND AirDate = @1", seriesId, date.Date);
|
2011-04-25 21:23:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-06-16 06:33:01 +00:00
|
|
|
|
public virtual IList<Episode> GetEpisodeBySeries(long seriesId)
|
2011-04-23 20:33:24 +00:00
|
|
|
|
{
|
2011-06-16 06:33:01 +00:00
|
|
|
|
return _database.Fetch<Episode>("WHERE SeriesId = @0", seriesId);
|
2011-04-23 20:33:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-06-04 01:56:53 +00:00
|
|
|
|
public virtual IList<Episode> GetEpisodesBySeason(long seriesId, int seasonNumber)
|
2011-04-23 20:33:24 +00:00
|
|
|
|
{
|
2011-06-16 06:33:01 +00:00
|
|
|
|
return _database.Fetch<Episode>("WHERE SeriesId = @0 AND SeasonNumber = @1", seriesId, seasonNumber);
|
2011-04-23 20:33:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-06-07 06:29:07 +00:00
|
|
|
|
public virtual List<Episode> GetEpisodes(EpisodeParseResult parseResult)
|
|
|
|
|
{
|
|
|
|
|
if (parseResult.Series == null)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Episode Parse Result is Invalid, skipping");
|
|
|
|
|
return new List<Episode>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var episodes = new List<Episode>();
|
|
|
|
|
|
|
|
|
|
foreach (var ep in parseResult.EpisodeNumbers)
|
|
|
|
|
{
|
|
|
|
|
var episode = GetEpisode(parseResult.Series.SeriesId, parseResult.SeasonNumber, ep);
|
|
|
|
|
|
|
|
|
|
if (episode == null)
|
|
|
|
|
return new List<Episode>();
|
|
|
|
|
|
|
|
|
|
episodes.Add(episode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return episodes;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-01 08:04:44 +00:00
|
|
|
|
public virtual IList<Episode> EpisodesWithoutFiles(bool includeSpecials)
|
|
|
|
|
{
|
2011-06-16 06:33:01 +00:00
|
|
|
|
var episodes = _database.Query<Episode>("WHERE (EpisodeFileId=0 OR EpisodeFileId=NULL) AND AirDate<=@0",
|
|
|
|
|
DateTime.Now.Date);
|
2011-05-01 08:04:44 +00:00
|
|
|
|
if (includeSpecials)
|
2011-06-16 06:33:01 +00:00
|
|
|
|
return episodes.Where(e => e.SeasonNumber > 0).ToList();
|
2011-05-01 08:04:44 +00:00
|
|
|
|
|
2011-06-16 06:33:01 +00:00
|
|
|
|
return episodes.ToList();
|
2011-05-01 08:04:44 +00:00
|
|
|
|
}
|
2011-04-23 20:33:24 +00:00
|
|
|
|
|
2011-06-06 00:37:32 +00:00
|
|
|
|
public virtual IList<Episode> EpisodesByFileId(int episodeFileId)
|
|
|
|
|
{
|
2011-06-16 06:33:01 +00:00
|
|
|
|
return _database.Fetch<Episode>("WHERE EpisodeFileId = @0", episodeFileId);
|
2011-06-06 00:37:32 +00:00
|
|
|
|
}
|
2011-05-29 03:01:35 +00:00
|
|
|
|
|
|
|
|
|
public virtual void RefreshEpisodeInfo(Series series)
|
2011-04-23 20:33:24 +00:00
|
|
|
|
{
|
2011-06-18 17:19:24 +00:00
|
|
|
|
Logger.Info("Starting episode info refresh for series: {0}", series.Title.WithDefault(series.SeriesId));
|
2011-04-23 20:33:24 +00:00
|
|
|
|
int successCount = 0;
|
|
|
|
|
int failCount = 0;
|
2011-05-29 03:01:35 +00:00
|
|
|
|
var tvDbSeriesInfo = _tvDbProvider.GetSeries(series.SeriesId, true);
|
2011-04-23 20:33:24 +00:00
|
|
|
|
|
2011-06-16 06:33:01 +00:00
|
|
|
|
var seriesEpisodes = GetEpisodeBySeries(series.SeriesId);
|
2011-04-23 20:33:24 +00:00
|
|
|
|
var updateList = new List<Episode>();
|
|
|
|
|
var newList = new List<Episode>();
|
|
|
|
|
|
2011-05-29 03:01:35 +00:00
|
|
|
|
foreach (var episode in tvDbSeriesInfo.Episodes)
|
2011-04-23 20:33:24 +00:00
|
|
|
|
{
|
|
|
|
|
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-05-29 03:01:35 +00:00
|
|
|
|
Logger.Trace("Updating info for [{0}] - S{1}E{2}", tvDbSeriesInfo.SeriesName, episode.SeasonNumber, episode.EpisodeNumber);
|
|
|
|
|
|
2011-05-29 03:37:19 +00:00
|
|
|
|
//first check using tvdbId, this should cover cases when and episode number in a season is changed
|
2011-06-16 06:33:01 +00:00
|
|
|
|
var episodeToUpdate = seriesEpisodes.Where(e => e.TvDbEpisodeId == episode.Id).FirstOrDefault();
|
2011-05-29 03:01:35 +00:00
|
|
|
|
|
2011-05-29 03:37:19 +00:00
|
|
|
|
//not found, try using season/episode number
|
|
|
|
|
if (episodeToUpdate == null)
|
|
|
|
|
{
|
2011-06-16 06:33:01 +00:00
|
|
|
|
episodeToUpdate = seriesEpisodes.Where(e => e.SeasonNumber == episode.SeasonNumber && e.EpisodeNumber == episode.EpisodeNumber).FirstOrDefault();
|
2011-05-29 03:37:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Episode doesn't exist locally
|
2011-05-29 03:01:35 +00:00
|
|
|
|
if (episodeToUpdate == null)
|
2011-04-23 20:33:24 +00:00
|
|
|
|
{
|
2011-05-29 03:01:35 +00:00
|
|
|
|
episodeToUpdate = new Episode();
|
|
|
|
|
newList.Add(episodeToUpdate);
|
2011-04-23 20:33:24 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2011-05-29 03:01:35 +00:00
|
|
|
|
updateList.Add(episodeToUpdate);
|
2011-04-23 20:33:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-05-29 03:01:35 +00:00
|
|
|
|
episodeToUpdate.SeriesId = series.SeriesId;
|
|
|
|
|
episodeToUpdate.TvDbEpisodeId = episode.Id;
|
|
|
|
|
episodeToUpdate.AirDate = episode.FirstAired.Date;
|
|
|
|
|
episodeToUpdate.EpisodeNumber = episode.EpisodeNumber;
|
|
|
|
|
episodeToUpdate.SeasonNumber = episode.SeasonNumber;
|
|
|
|
|
episodeToUpdate.Title = episode.EpisodeName;
|
|
|
|
|
episodeToUpdate.Overview = episode.Overview;
|
|
|
|
|
|
2011-04-23 20:33:24 +00:00
|
|
|
|
successCount++;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Logger.FatalException(
|
2011-06-18 17:19:24 +00:00
|
|
|
|
String.Format("An error has occurred while updating episode info for series {0}", tvDbSeriesInfo.SeriesName), e);
|
2011-04-23 20:33:24 +00:00
|
|
|
|
failCount++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-16 06:33:01 +00:00
|
|
|
|
using (var tran = _database.GetTransaction())
|
|
|
|
|
{
|
|
|
|
|
newList.ForEach(episode => _database.Insert(episode));
|
|
|
|
|
updateList.ForEach(episode => _database.Update(episode));
|
2011-06-16 07:00:55 +00:00
|
|
|
|
|
2011-06-16 06:33:01 +00:00
|
|
|
|
//Shouldn't run if Database is a mock since transaction will be null
|
|
|
|
|
if (_database.GetType().Namespace != "Castle.Proxies" && tran != null)
|
|
|
|
|
{
|
|
|
|
|
tran.Complete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2011-04-23 20:33:24 +00:00
|
|
|
|
|
2011-06-18 17:19:24 +00:00
|
|
|
|
Logger.Info("Finished episode refresh for series: {0}. Successful: {1} - Failed: {2} ",
|
2011-05-29 03:01:35 +00:00
|
|
|
|
tvDbSeriesInfo.SeriesName, successCount, failCount);
|
2011-04-23 20:33:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-06-16 06:33:01 +00:00
|
|
|
|
|
2011-04-23 20:33:24 +00:00
|
|
|
|
|
|
|
|
|
public virtual void UpdateEpisode(Episode episode)
|
|
|
|
|
{
|
2011-06-16 06:33:01 +00:00
|
|
|
|
_database.Update(episode);
|
2011-04-23 20:33:24 +00:00
|
|
|
|
}
|
2011-06-04 01:56:53 +00:00
|
|
|
|
|
|
|
|
|
public virtual bool IsIgnored(int seriesId, int seasonNumber)
|
|
|
|
|
{
|
2011-06-16 06:33:01 +00:00
|
|
|
|
|
|
|
|
|
var unIgnoredCount = _database.ExecuteScalar<int>(
|
2011-06-16 07:00:55 +00:00
|
|
|
|
"SELECT COUNT (*) FROM Episodes WHERE SeriesId=@0 AND SeasonNumber=@1 AND Ignored='0'", seriesId, seasonNumber);
|
2011-06-16 06:33:01 +00:00
|
|
|
|
|
2011-06-16 07:00:55 +00:00
|
|
|
|
return unIgnoredCount == 0;
|
2011-06-04 01:56:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual IList<int> GetSeasons(int seriesId)
|
|
|
|
|
{
|
2011-06-16 06:33:01 +00:00
|
|
|
|
return _database.Fetch<Int32>("SELECT DISTINCT SeasonNumber FROM Episodes WHERE SeriesId=@0", seriesId).OrderBy(c => c).ToList();
|
2011-06-04 01:56:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void SetSeasonIgnore(long seriesId, int seasonNumber, bool isIgnored)
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("Setting ignore flag on Series:{0} Season:{1} to {2}", seriesId, seasonNumber, isIgnored);
|
|
|
|
|
var episodes = GetEpisodesBySeason(seriesId, seasonNumber);
|
|
|
|
|
|
2011-06-16 06:33:01 +00:00
|
|
|
|
using (var tran = _database.GetTransaction())
|
2011-06-04 01:56:53 +00:00
|
|
|
|
{
|
2011-06-16 06:33:01 +00:00
|
|
|
|
foreach (var episode in episodes)
|
|
|
|
|
{
|
|
|
|
|
episode.Ignored = isIgnored;
|
|
|
|
|
_database.Update(episode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Shouldn't run if Database is a mock since transaction will be null
|
|
|
|
|
if (_database.GetType().Namespace != "Castle.Proxies" && tran != null)
|
|
|
|
|
{
|
|
|
|
|
tran.Complete();
|
|
|
|
|
}
|
2011-06-16 07:00:55 +00:00
|
|
|
|
|
2011-06-16 06:33:01 +00:00
|
|
|
|
Logger.Info("Ignore flag for Series:{0} Season:{1} successfully set to {2}", seriesId, seasonNumber, isIgnored);
|
2011-06-04 01:56:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2011-04-23 20:33:24 +00:00
|
|
|
|
}
|
2011-04-22 22:24:05 +00:00
|
|
|
|
} |