2011-04-23 22:33:24 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2011-06-14 03:23:04 +02:00
|
|
|
|
using Ninject;
|
2011-04-23 22:33:24 +02:00
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Core.Model;
|
|
|
|
|
using NzbDrone.Core.Repository;
|
2011-06-15 04:31:41 +02:00
|
|
|
|
using PetaPoco;
|
2011-09-30 06:40:00 +02:00
|
|
|
|
using TvdbLib.Data;
|
2011-04-23 22:33:24 +02:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
|
{
|
|
|
|
|
public class EpisodeProvider
|
|
|
|
|
{
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2011-04-26 02:28:33 +02:00
|
|
|
|
private readonly TvDbProvider _tvDbProvider;
|
2011-06-15 04:31:41 +02:00
|
|
|
|
private readonly IDatabase _database;
|
2011-06-19 07:57:24 +02:00
|
|
|
|
private readonly SeriesProvider _seriesProvider;
|
2011-04-26 02:28:33 +02:00
|
|
|
|
|
2011-06-14 03:23:04 +02:00
|
|
|
|
[Inject]
|
2011-06-19 07:57:24 +02:00
|
|
|
|
public EpisodeProvider(IDatabase database, SeriesProvider seriesProvider, TvDbProvider tvDbProviderProvider)
|
2011-04-23 22:33:24 +02:00
|
|
|
|
{
|
2011-04-26 02:28:33 +02:00
|
|
|
|
_tvDbProvider = tvDbProviderProvider;
|
2011-06-15 04:31:41 +02:00
|
|
|
|
_database = database;
|
2011-06-19 07:57:24 +02:00
|
|
|
|
_seriesProvider = seriesProvider;
|
2011-04-23 22:33:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EpisodeProvider()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-04 03:56:53 +02:00
|
|
|
|
public virtual void AddEpisode(Episode episode)
|
2011-05-20 05:47:07 +02:00
|
|
|
|
{
|
2011-07-23 02:57:52 +02:00
|
|
|
|
//If Season is ignored ignore this episode
|
|
|
|
|
if (IsIgnored(episode.SeriesId, episode.SeasonNumber))
|
|
|
|
|
episode.Ignored = true;
|
|
|
|
|
|
2011-06-16 08:33:01 +02:00
|
|
|
|
_database.Insert(episode);
|
2011-05-20 05:47:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-04-23 22:33:24 +02:00
|
|
|
|
public virtual Episode GetEpisode(long id)
|
|
|
|
|
{
|
2011-09-04 11:20:49 +02:00
|
|
|
|
var episode = _database.Fetch<Episode, Series, EpisodeFile>(@"SELECT * FROM Episodes
|
|
|
|
|
INNER JOIN Series ON Episodes.SeriesId = Series.SeriesId
|
2011-06-20 03:46:32 +02:00
|
|
|
|
LEFT JOIN EpisodeFiles ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId
|
2011-09-04 11:20:49 +02:00
|
|
|
|
WHERE EpisodeId = @0", id).Single();
|
2011-06-20 09:40:45 +02:00
|
|
|
|
|
2011-06-20 03:46:32 +02:00
|
|
|
|
if (episode.EpisodeFileId == 0)
|
|
|
|
|
episode.EpisodeFile = null;
|
|
|
|
|
|
|
|
|
|
return episode;
|
2011-04-23 22:33:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual Episode GetEpisode(int seriesId, int seasonNumber, int episodeNumber)
|
|
|
|
|
{
|
2011-09-04 11:20:49 +02:00
|
|
|
|
var episode = _database.Fetch<Episode, Series, EpisodeFile>(@"SELECT * FROM Episodes
|
|
|
|
|
INNER JOIN Series ON Episodes.SeriesId = Series.SeriesId
|
2011-06-20 03:46:32 +02:00
|
|
|
|
LEFT JOIN EpisodeFiles ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId
|
2011-09-04 11:20:49 +02:00
|
|
|
|
WHERE Episodes.SeriesId = @0 AND Episodes.SeasonNumber = @1 AND Episodes.EpisodeNumber = @2", seriesId, seasonNumber, episodeNumber).SingleOrDefault();
|
2011-06-20 03:46:32 +02:00
|
|
|
|
|
|
|
|
|
if (episode == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
if (episode.EpisodeFileId == 0)
|
|
|
|
|
episode.EpisodeFile = null;
|
|
|
|
|
|
|
|
|
|
return episode;
|
2011-04-23 22:33:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-04-25 23:23:02 +02:00
|
|
|
|
public virtual Episode GetEpisode(int seriesId, DateTime date)
|
|
|
|
|
{
|
2011-09-04 11:20:49 +02:00
|
|
|
|
var episode = _database.Fetch<Episode, Series, EpisodeFile>(@"SELECT * FROM Episodes
|
|
|
|
|
INNER JOIN Series ON Episodes.SeriesId = Series.SeriesId
|
2011-06-20 03:46:32 +02:00
|
|
|
|
LEFT JOIN EpisodeFiles ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId
|
2011-09-04 11:20:49 +02:00
|
|
|
|
WHERE Episodes.SeriesId = @0 AND AirDate = @1", seriesId, date.Date).SingleOrDefault();
|
2011-06-20 03:46:32 +02:00
|
|
|
|
|
|
|
|
|
if (episode == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
if (episode.EpisodeFileId == 0)
|
|
|
|
|
episode.EpisodeFile = null;
|
|
|
|
|
|
|
|
|
|
return episode;
|
2011-04-25 23:23:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-06-16 08:33:01 +02:00
|
|
|
|
public virtual IList<Episode> GetEpisodeBySeries(long seriesId)
|
2011-04-23 22:33:24 +02:00
|
|
|
|
{
|
2011-09-05 04:56:45 +02:00
|
|
|
|
var episodes = _database.Fetch<Episode, Series, EpisodeFile>(@"SELECT * FROM Episodes
|
|
|
|
|
INNER JOIN Series ON Episodes.SeriesId = Series.SeriesId
|
2011-06-20 03:46:32 +02:00
|
|
|
|
LEFT JOIN EpisodeFiles ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId
|
2011-09-05 04:56:45 +02:00
|
|
|
|
WHERE Episodes.SeriesId = @0", seriesId);
|
2011-06-20 03:46:32 +02:00
|
|
|
|
|
|
|
|
|
foreach (var episode in episodes)
|
|
|
|
|
{
|
|
|
|
|
if (episode.EpisodeFileId == 0)
|
|
|
|
|
episode.EpisodeFile = null;
|
|
|
|
|
}
|
2011-06-22 03:12:20 +02:00
|
|
|
|
|
2011-06-20 03:46:32 +02:00
|
|
|
|
return episodes;
|
2011-04-23 22:33:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-06-04 03:56:53 +02:00
|
|
|
|
public virtual IList<Episode> GetEpisodesBySeason(long seriesId, int seasonNumber)
|
2011-04-23 22:33:24 +02:00
|
|
|
|
{
|
2011-09-05 04:56:45 +02:00
|
|
|
|
var episodes = _database.Fetch<Episode, Series, EpisodeFile>(@"SELECT * FROM Episodes
|
|
|
|
|
INNER JOIN Series ON Episodes.SeriesId = Series.SeriesId
|
2011-06-20 03:46:32 +02:00
|
|
|
|
LEFT JOIN EpisodeFiles ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId
|
2011-09-05 04:56:45 +02:00
|
|
|
|
WHERE Episodes.SeriesId = @0 AND Episodes.SeasonNumber = @1", seriesId, seasonNumber);
|
2011-06-20 03:46:32 +02:00
|
|
|
|
|
|
|
|
|
foreach (var episode in episodes)
|
|
|
|
|
{
|
|
|
|
|
if (episode.EpisodeFileId == 0)
|
|
|
|
|
episode.EpisodeFile = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return episodes;
|
2011-04-23 22:33:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-07-04 01:04:57 +02:00
|
|
|
|
public virtual void MarkEpisodeAsFetched(int episodeId)
|
|
|
|
|
{
|
|
|
|
|
Logger.Trace("Marking episode {0} as fetched.", episodeId);
|
|
|
|
|
_database.Execute("UPDATE Episodes SET GrabDate=@0 WHERE EpisodeId=@1", DateTime.Now, episodeId);
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-22 03:12:20 +02:00
|
|
|
|
public virtual IList<Episode> GetEpisodesByParseResult(EpisodeParseResult parseResult, Boolean autoAddNew = false)
|
|
|
|
|
{
|
|
|
|
|
var result = new List<Episode>();
|
2011-06-07 08:29:07 +02:00
|
|
|
|
|
2011-10-12 05:44:19 +02:00
|
|
|
|
if (parseResult.EpisodeNumbers.Count == 0 && parseResult.FullSeason)
|
|
|
|
|
{
|
|
|
|
|
result.AddRange(GetEpisodesBySeason(parseResult.Series.SeriesId, parseResult.SeasonNumber));
|
|
|
|
|
|
|
|
|
|
//Return now as no further processing is required
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-22 03:12:20 +02:00
|
|
|
|
foreach (var episodeNumber in parseResult.EpisodeNumbers)
|
2011-06-20 03:46:32 +02:00
|
|
|
|
{
|
2011-06-22 03:12:20 +02:00
|
|
|
|
var episodeInfo = GetEpisode(parseResult.Series.SeriesId, parseResult.SeasonNumber, episodeNumber);
|
2011-06-23 08:56:17 +02:00
|
|
|
|
if (episodeInfo == null && parseResult.AirDate != null)
|
2011-06-22 03:12:20 +02:00
|
|
|
|
{
|
2011-06-23 08:56:17 +02:00
|
|
|
|
episodeInfo = GetEpisode(parseResult.Series.SeriesId, parseResult.AirDate.Value);
|
2011-06-22 03:12:20 +02:00
|
|
|
|
}
|
|
|
|
|
//if still null we should add the temp episode
|
|
|
|
|
if (episodeInfo == null && autoAddNew)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Episode {0} doesn't exist in db. adding it now.", parseResult);
|
|
|
|
|
episodeInfo = new Episode
|
|
|
|
|
{
|
|
|
|
|
SeriesId = parseResult.Series.SeriesId,
|
|
|
|
|
AirDate = DateTime.Now.Date,
|
|
|
|
|
EpisodeNumber = episodeNumber,
|
|
|
|
|
SeasonNumber = parseResult.SeasonNumber,
|
|
|
|
|
Title = "TBD",
|
|
|
|
|
Overview = String.Empty,
|
|
|
|
|
};
|
|
|
|
|
|
2011-10-04 04:01:39 +02:00
|
|
|
|
if (parseResult.EpisodeNumbers.Count == 1 && parseResult.EpisodeNumbers.First() == 0)
|
|
|
|
|
episodeInfo.Ignored = true;
|
|
|
|
|
|
2011-06-22 03:12:20 +02:00
|
|
|
|
AddEpisode(episodeInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (episodeInfo != null)
|
|
|
|
|
{
|
|
|
|
|
result.Add(episodeInfo);
|
2011-06-22 05:38:46 +02:00
|
|
|
|
parseResult.EpisodeTitle = episodeInfo.Title;
|
2011-06-22 03:12:20 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Unable to file {0}-S{1:00}E{2:00}", parseResult.Series.Title, parseResult.SeasonNumber, episodeNumber);
|
|
|
|
|
}
|
2011-06-20 03:46:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-06-22 03:12:20 +02:00
|
|
|
|
return result;
|
2011-06-07 08:29:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-05-01 10:04:44 +02:00
|
|
|
|
public virtual IList<Episode> EpisodesWithoutFiles(bool includeSpecials)
|
|
|
|
|
{
|
2011-08-04 07:44:24 +02:00
|
|
|
|
var episodes = _database.Query<Episode, Series>(@"SELECT Episodes.*, Series.Title FROM Episodes
|
|
|
|
|
INNER JOIN Series
|
|
|
|
|
ON Episodes.SeriesId = Series.SeriesId
|
2011-08-04 07:56:42 +02:00
|
|
|
|
WHERE (EpisodeFileId=0 OR EpisodeFileId=NULL) AND Ignored = 0 AND AirDate<=@0",
|
2011-06-16 08:33:01 +02:00
|
|
|
|
DateTime.Now.Date);
|
2011-08-04 07:44:24 +02:00
|
|
|
|
if (!includeSpecials)
|
2011-06-16 08:33:01 +02:00
|
|
|
|
return episodes.Where(e => e.SeasonNumber > 0).ToList();
|
2011-05-01 10:04:44 +02:00
|
|
|
|
|
2011-08-04 07:44:24 +02:00
|
|
|
|
return episodes.ToList();
|
2011-05-01 10:04:44 +02:00
|
|
|
|
}
|
2011-04-23 22:33:24 +02:00
|
|
|
|
|
2011-06-20 03:59:31 +02:00
|
|
|
|
public virtual IList<Episode> GetEpisodesByFileId(int episodeFileId)
|
2011-06-06 02:37:32 +02:00
|
|
|
|
{
|
2011-09-05 04:56:45 +02:00
|
|
|
|
return _database.Fetch<Episode, Series>(@"SELECT * FROM Episodes
|
|
|
|
|
INNER JOIN Series ON Episodes.SeriesId = Series.SeriesId
|
|
|
|
|
WHERE EpisodeFileId = @0", episodeFileId);
|
2011-06-06 02:37:32 +02:00
|
|
|
|
}
|
2011-05-29 05:01:35 +02:00
|
|
|
|
|
2011-09-04 05:05:44 +02:00
|
|
|
|
public virtual IList<Episode> EpisodesWithFiles()
|
|
|
|
|
{
|
2011-09-04 09:45:58 +02:00
|
|
|
|
var episodes = _database.Fetch<Episode, Series, EpisodeFile>(@"SELECT Episodes.*, Series.*, EpisodeFiles.* FROM Episodes
|
2011-09-04 05:05:44 +02:00
|
|
|
|
INNER JOIN Series ON Episodes.SeriesId = Series.SeriesId
|
|
|
|
|
INNER JOIN EpisodeFiles ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId");
|
|
|
|
|
|
|
|
|
|
return episodes;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-29 05:01:35 +02:00
|
|
|
|
public virtual void RefreshEpisodeInfo(Series series)
|
2011-04-23 22:33:24 +02:00
|
|
|
|
{
|
2011-06-18 19:19:24 +02:00
|
|
|
|
Logger.Info("Starting episode info refresh for series: {0}", series.Title.WithDefault(series.SeriesId));
|
2011-04-23 22:33:24 +02:00
|
|
|
|
int successCount = 0;
|
|
|
|
|
int failCount = 0;
|
2011-05-29 05:01:35 +02:00
|
|
|
|
var tvDbSeriesInfo = _tvDbProvider.GetSeries(series.SeriesId, true);
|
2011-04-23 22:33:24 +02:00
|
|
|
|
|
2011-06-16 08:33:01 +02:00
|
|
|
|
var seriesEpisodes = GetEpisodeBySeries(series.SeriesId);
|
2011-04-23 22:33:24 +02:00
|
|
|
|
var updateList = new List<Episode>();
|
|
|
|
|
var newList = new List<Episode>();
|
|
|
|
|
|
2011-05-29 05:01:35 +02:00
|
|
|
|
foreach (var episode in tvDbSeriesInfo.Episodes)
|
2011-04-23 22:33:24 +02: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 05:01:35 +02:00
|
|
|
|
Logger.Trace("Updating info for [{0}] - S{1}E{2}", tvDbSeriesInfo.SeriesName, episode.SeasonNumber, episode.EpisodeNumber);
|
|
|
|
|
|
2011-05-29 05:37:19 +02:00
|
|
|
|
//first check using tvdbId, this should cover cases when and episode number in a season is changed
|
2011-07-28 00:59:48 +02:00
|
|
|
|
var episodeToUpdate = seriesEpisodes.Where(e => e.TvDbEpisodeId == episode.Id).SingleOrDefault();
|
2011-05-29 05:01:35 +02:00
|
|
|
|
|
2011-05-29 05:37:19 +02:00
|
|
|
|
//not found, try using season/episode number
|
|
|
|
|
if (episodeToUpdate == null)
|
|
|
|
|
{
|
2011-07-28 00:59:48 +02:00
|
|
|
|
episodeToUpdate = seriesEpisodes.Where(e => e.SeasonNumber == episode.SeasonNumber && e.EpisodeNumber == episode.EpisodeNumber).SingleOrDefault();
|
2011-05-29 05:37:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Episode doesn't exist locally
|
2011-05-29 05:01:35 +02:00
|
|
|
|
if (episodeToUpdate == null)
|
2011-04-23 22:33:24 +02:00
|
|
|
|
{
|
2011-05-29 05:01:35 +02:00
|
|
|
|
episodeToUpdate = new Episode();
|
|
|
|
|
newList.Add(episodeToUpdate);
|
2011-10-04 04:01:39 +02:00
|
|
|
|
|
|
|
|
|
//We need to check if this episode should be ignored based on IsIgnored rules
|
|
|
|
|
IsIgnored(series.SeriesId, episode.SeasonNumber);
|
|
|
|
|
|
|
|
|
|
//If it is Episode Zero Ignore it, since it is new
|
|
|
|
|
if (episode.EpisodeNumber == 0)
|
|
|
|
|
episodeToUpdate.Ignored = true;
|
2011-04-23 22:33:24 +02:00
|
|
|
|
}
|
2011-10-04 04:01:39 +02:00
|
|
|
|
|
2011-04-23 22:33:24 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
2011-05-29 05:01:35 +02:00
|
|
|
|
updateList.Add(episodeToUpdate);
|
2011-04-23 22:33:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-05-29 05:01:35 +02:00
|
|
|
|
episodeToUpdate.SeriesId = series.SeriesId;
|
|
|
|
|
episodeToUpdate.TvDbEpisodeId = episode.Id;
|
|
|
|
|
episodeToUpdate.EpisodeNumber = episode.EpisodeNumber;
|
|
|
|
|
episodeToUpdate.SeasonNumber = episode.SeasonNumber;
|
|
|
|
|
episodeToUpdate.Title = episode.EpisodeName;
|
|
|
|
|
episodeToUpdate.Overview = episode.Overview;
|
|
|
|
|
|
2011-06-23 08:56:17 +02:00
|
|
|
|
if (episode.FirstAired.Year > 1900)
|
|
|
|
|
{
|
|
|
|
|
episodeToUpdate.AirDate = episode.FirstAired.Date;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-23 22:33:24 +02:00
|
|
|
|
successCount++;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Logger.FatalException(
|
2011-06-18 19:19:24 +02:00
|
|
|
|
String.Format("An error has occurred while updating episode info for series {0}", tvDbSeriesInfo.SeriesName), e);
|
2011-04-23 22:33:24 +02:00
|
|
|
|
failCount++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-29 05:03:40 +02:00
|
|
|
|
_database.InsertMany(newList);
|
|
|
|
|
_database.UpdateMany(updateList);
|
2011-04-23 22:33:24 +02:00
|
|
|
|
|
2011-06-18 19:19:24 +02:00
|
|
|
|
Logger.Info("Finished episode refresh for series: {0}. Successful: {1} - Failed: {2} ",
|
2011-05-29 05:01:35 +02:00
|
|
|
|
tvDbSeriesInfo.SeriesName, successCount, failCount);
|
2011-09-30 06:40:00 +02:00
|
|
|
|
|
|
|
|
|
//DeleteInvalidEpisodes
|
|
|
|
|
DeleteInvalidEpisodes(series, tvDbSeriesInfo);
|
2011-04-23 22:33:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void UpdateEpisode(Episode episode)
|
|
|
|
|
{
|
2011-06-16 08:33:01 +02:00
|
|
|
|
_database.Update(episode);
|
2011-04-23 22:33:24 +02:00
|
|
|
|
}
|
2011-06-04 03:56:53 +02:00
|
|
|
|
|
|
|
|
|
public virtual bool IsIgnored(int seriesId, int seasonNumber)
|
|
|
|
|
{
|
2011-08-29 05:03:40 +02:00
|
|
|
|
var episodes = _database.Fetch<Episode>(@"SELECT * FROM Episodes WHERE SeriesId=@0 AND SeasonNumber=@1", seriesId, seasonNumber);
|
2011-07-23 02:57:52 +02:00
|
|
|
|
|
|
|
|
|
if (episodes == null || episodes.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
if (seasonNumber == 0)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
//Don't check for a previous season if season is 1
|
|
|
|
|
if (seasonNumber == 1)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
//else
|
2011-08-29 05:03:40 +02:00
|
|
|
|
var lastSeasonsEpisodes = _database.Fetch<Episode>(@"SELECT * FROM Episodes
|
2011-07-23 02:57:52 +02:00
|
|
|
|
WHERE SeriesId=@0 AND SeasonNumber=@1", seriesId, seasonNumber - 1);
|
|
|
|
|
|
2011-08-29 05:03:40 +02:00
|
|
|
|
if (lastSeasonsEpisodes != null && lastSeasonsEpisodes.Count > 0 && lastSeasonsEpisodes.Count == lastSeasonsEpisodes.Where(e => e.Ignored).Count())
|
2011-07-23 02:57:52 +02:00
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2011-06-16 08:33:01 +02:00
|
|
|
|
|
2011-07-23 02:57:52 +02:00
|
|
|
|
if (episodes.Count == episodes.Where(e => e.Ignored).Count())
|
|
|
|
|
return true;
|
2011-06-16 08:33:01 +02:00
|
|
|
|
|
2011-07-23 02:57:52 +02:00
|
|
|
|
return false;
|
2011-06-04 03:56:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual IList<int> GetSeasons(int seriesId)
|
|
|
|
|
{
|
2011-06-16 08:33:01 +02:00
|
|
|
|
return _database.Fetch<Int32>("SELECT DISTINCT SeasonNumber FROM Episodes WHERE SeriesId=@0", seriesId).OrderBy(c => c).ToList();
|
2011-06-04 03:56:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-08-28 21:07:56 +02:00
|
|
|
|
public virtual IList<int> GetEpisodeNumbersBySeason(int seriesId, int seasonNumber)
|
|
|
|
|
{
|
|
|
|
|
return _database.Fetch<int>("SELECT EpisodeNumber FROM Episodes WHERE SeriesId=@0 AND SeasonNumber=@1", seriesId, seasonNumber).OrderBy(c => c).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-04 03:56:53 +02: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);
|
|
|
|
|
|
2011-08-04 03:45:45 +02:00
|
|
|
|
_database.Execute(@"UPDATE Episodes SET Ignored = @0
|
2011-08-04 07:44:24 +02:00
|
|
|
|
WHERE SeriesId = @1 AND SeasonNumber = @2 AND Ignored = @3",
|
|
|
|
|
isIgnored, seriesId, seasonNumber, !isIgnored);
|
2011-06-16 09:00:55 +02:00
|
|
|
|
|
2011-08-29 05:03:40 +02:00
|
|
|
|
Logger.Info("Ignore flag for Series:{0} Season:{1} successfully set to {2}", seriesId, seasonNumber, isIgnored);
|
2011-08-04 03:45:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void SetEpisodeIgnore(int episodeId, bool isIgnored)
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("Setting ignore flag on Episode:{0} to {1}", episodeId, isIgnored);
|
2011-06-04 03:56:53 +02:00
|
|
|
|
|
2011-08-04 03:45:45 +02:00
|
|
|
|
_database.Execute(@"UPDATE Episodes SET Ignored = @0
|
|
|
|
|
WHERE EpisodeId = @1",
|
|
|
|
|
isIgnored, episodeId);
|
2011-06-04 03:56:53 +02:00
|
|
|
|
|
2011-08-04 03:45:45 +02:00
|
|
|
|
Logger.Info("Ignore flag for Episode:{0} successfully set to {1}", episodeId, isIgnored);
|
2011-06-04 03:56:53 +02:00
|
|
|
|
}
|
2011-06-19 07:57:24 +02:00
|
|
|
|
|
2011-09-14 06:37:22 +02:00
|
|
|
|
public virtual bool IsFirstOrLastEpisodeOfSeason(int seriesId, int seasonNumber, int episodeNumber)
|
|
|
|
|
{
|
|
|
|
|
var episodes = GetEpisodesBySeason(seriesId, seasonNumber).OrderBy(e => e.EpisodeNumber);
|
|
|
|
|
|
|
|
|
|
if (episodes.Count() == 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
//Ensure that this is either the first episode
|
|
|
|
|
//or is the last episode in a season that has 10 or more episodes
|
|
|
|
|
if (episodes.First().EpisodeNumber == episodeNumber || (episodes.Count() >= 10 && episodes.Last().EpisodeNumber == episodeNumber))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-19 07:57:24 +02:00
|
|
|
|
public IList<Episode> AttachSeries(IList<Episode> episodes)
|
|
|
|
|
{
|
|
|
|
|
if (episodes.Count == 0) return episodes;
|
|
|
|
|
|
|
|
|
|
if (episodes.Select(c => c.SeriesId).Distinct().Count() > 1)
|
|
|
|
|
throw new ArgumentException("Episodes belong to more than one series.");
|
|
|
|
|
|
|
|
|
|
var series = _seriesProvider.GetSeries(episodes.First().SeriesId);
|
|
|
|
|
episodes.ToList().ForEach(c => c.Series = series);
|
|
|
|
|
|
|
|
|
|
return episodes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Episode AttachSeries(Episode episode)
|
|
|
|
|
{
|
2011-06-20 05:25:24 +02:00
|
|
|
|
if (episode == null) return episode;
|
2011-06-19 07:57:24 +02:00
|
|
|
|
episode.Series = _seriesProvider.GetSeries(episode.SeriesId);
|
|
|
|
|
return episode;
|
|
|
|
|
}
|
2011-09-30 06:40:00 +02:00
|
|
|
|
|
|
|
|
|
public virtual void DeleteInvalidEpisodes(Series series, TvdbSeries tvDbSeriesInfo)
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("Starting deletion of invalid episode for series: {0}", series.Title.WithDefault(series.SeriesId));
|
|
|
|
|
|
|
|
|
|
//Delete Episodes not matching TvDbIds for this series
|
|
|
|
|
var tvDbIds = tvDbSeriesInfo.Episodes.Select(e => e.Id);
|
|
|
|
|
var tvDbIdString = String.Join(", ", tvDbIds);
|
|
|
|
|
|
|
|
|
|
var tvDbIdQuery = String.Format("DELETE FROM Episodes WHERE SeriesId = {0} AND TvDbEpisodeId > 0 AND TvDbEpisodeId NOT IN ({1})",
|
|
|
|
|
series.SeriesId, tvDbIdString);
|
|
|
|
|
|
|
|
|
|
Logger.Trace("Deleting nivalid episodes by TvDbId for {0}", series.SeriesId);
|
|
|
|
|
_database.Execute(tvDbIdQuery);
|
|
|
|
|
|
|
|
|
|
Logger.Trace("Finished deleting invalid episodes for {0}", series.SeriesId);
|
|
|
|
|
}
|
2011-10-12 05:44:19 +02:00
|
|
|
|
|
2011-10-14 17:37:19 +02:00
|
|
|
|
public virtual void SetPostDownloadStatus(IEnumerable<int> episodeIds, PostDownloadStatusType postDownloadStatus)
|
2011-10-12 05:44:19 +02:00
|
|
|
|
{
|
|
|
|
|
var episodeIdString = String.Join(", ", episodeIds);
|
|
|
|
|
|
|
|
|
|
var episodeIdQuery = String.Format(@"UPDATE Episodes SET PostDownloadStatus = {0}
|
|
|
|
|
WHERE EpisodeId IN ({1})", (int)postDownloadStatus, episodeIdString);
|
|
|
|
|
|
|
|
|
|
Logger.Trace("Updating PostDownloadStatus for all episodeIds in {0}", episodeIdString);
|
|
|
|
|
_database.Execute(episodeIdQuery);
|
|
|
|
|
}
|
2011-04-23 22:33:24 +02:00
|
|
|
|
}
|
2011-04-23 00:24:05 +02:00
|
|
|
|
} |