2011-04-23 23:33:24 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2012-02-03 09:49:34 +03:00
|
|
|
using System.Text.RegularExpressions;
|
2011-06-14 04:23:04 +03:00
|
|
|
using Ninject;
|
2011-04-23 23:33:24 +03:00
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Core.Model;
|
|
|
|
using NzbDrone.Core.Repository;
|
2011-06-15 05:31:41 +03:00
|
|
|
using PetaPoco;
|
2012-10-13 06:49:59 +03:00
|
|
|
using XemLib.Data;
|
2011-04-23 23:33:24 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
{
|
|
|
|
public class EpisodeProvider
|
|
|
|
{
|
2012-02-03 09:49:34 +03:00
|
|
|
|
2012-02-13 09:38:57 +03:00
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
2012-02-03 09:49:34 +03:00
|
|
|
|
2011-04-26 03:28:33 +03:00
|
|
|
private readonly TvDbProvider _tvDbProvider;
|
2012-02-21 06:25:19 +03:00
|
|
|
private readonly SeasonProvider _seasonProvider;
|
2011-06-15 05:31:41 +03:00
|
|
|
private readonly IDatabase _database;
|
2011-04-26 03:28:33 +03:00
|
|
|
|
2011-06-14 04:23:04 +03:00
|
|
|
[Inject]
|
2012-10-15 08:52:12 +03:00
|
|
|
public EpisodeProvider(IDatabase database, TvDbProvider tvDbProviderProvider,
|
|
|
|
SeasonProvider seasonProvider)
|
2011-04-23 23:33:24 +03:00
|
|
|
{
|
2011-04-26 03:28:33 +03:00
|
|
|
_tvDbProvider = tvDbProviderProvider;
|
2012-02-21 06:25:19 +03:00
|
|
|
_seasonProvider = seasonProvider;
|
2011-06-15 05:31:41 +03:00
|
|
|
_database = database;
|
2011-04-23 23:33:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public EpisodeProvider()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-06-04 04:56:53 +03:00
|
|
|
public virtual void AddEpisode(Episode episode)
|
2011-05-20 06:47:07 +03:00
|
|
|
{
|
2011-07-23 03:57:52 +03:00
|
|
|
//If Season is ignored ignore this episode
|
2012-02-21 06:25:19 +03:00
|
|
|
episode.Ignored = _seasonProvider.IsIgnored(episode.SeriesId, episode.SeasonNumber);
|
2011-07-23 03:57:52 +03:00
|
|
|
|
2011-06-16 09:33:01 +03:00
|
|
|
_database.Insert(episode);
|
2011-05-20 06:47:07 +03:00
|
|
|
}
|
|
|
|
|
2011-04-23 23:33:24 +03:00
|
|
|
public virtual Episode GetEpisode(long id)
|
|
|
|
{
|
2011-09-04 12:20:49 +03:00
|
|
|
var episode = _database.Fetch<Episode, Series, EpisodeFile>(@"SELECT * FROM Episodes
|
|
|
|
INNER JOIN Series ON Episodes.SeriesId = Series.SeriesId
|
2011-06-20 04:46:32 +03:00
|
|
|
LEFT JOIN EpisodeFiles ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId
|
2011-09-04 12:20:49 +03:00
|
|
|
WHERE EpisodeId = @0", id).Single();
|
2011-06-20 10:40:45 +03:00
|
|
|
|
2011-06-20 04:46:32 +03:00
|
|
|
if (episode.EpisodeFileId == 0)
|
|
|
|
episode.EpisodeFile = null;
|
|
|
|
|
|
|
|
return episode;
|
2011-04-23 23:33:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public virtual Episode GetEpisode(int seriesId, int seasonNumber, int episodeNumber)
|
|
|
|
{
|
2011-09-04 12:20:49 +03:00
|
|
|
var episode = _database.Fetch<Episode, Series, EpisodeFile>(@"SELECT * FROM Episodes
|
|
|
|
INNER JOIN Series ON Episodes.SeriesId = Series.SeriesId
|
2011-06-20 04:46:32 +03:00
|
|
|
LEFT JOIN EpisodeFiles ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId
|
2011-09-04 12:20:49 +03:00
|
|
|
WHERE Episodes.SeriesId = @0 AND Episodes.SeasonNumber = @1 AND Episodes.EpisodeNumber = @2", seriesId, seasonNumber, episodeNumber).SingleOrDefault();
|
2011-06-20 04:46:32 +03:00
|
|
|
|
|
|
|
if (episode == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
if (episode.EpisodeFileId == 0)
|
|
|
|
episode.EpisodeFile = null;
|
|
|
|
|
|
|
|
return episode;
|
2011-04-23 23:33:24 +03:00
|
|
|
}
|
|
|
|
|
2011-04-26 00:23:02 +03:00
|
|
|
public virtual Episode GetEpisode(int seriesId, DateTime date)
|
|
|
|
{
|
2011-09-04 12:20:49 +03:00
|
|
|
var episode = _database.Fetch<Episode, Series, EpisodeFile>(@"SELECT * FROM Episodes
|
|
|
|
INNER JOIN Series ON Episodes.SeriesId = Series.SeriesId
|
2011-06-20 04:46:32 +03:00
|
|
|
LEFT JOIN EpisodeFiles ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId
|
2011-09-04 12:20:49 +03:00
|
|
|
WHERE Episodes.SeriesId = @0 AND AirDate = @1", seriesId, date.Date).SingleOrDefault();
|
2011-06-20 04:46:32 +03:00
|
|
|
|
|
|
|
if (episode == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
if (episode.EpisodeFileId == 0)
|
|
|
|
episode.EpisodeFile = null;
|
|
|
|
|
|
|
|
return episode;
|
2011-04-26 00:23:02 +03:00
|
|
|
}
|
|
|
|
|
2011-06-16 09:33:01 +03:00
|
|
|
public virtual IList<Episode> GetEpisodeBySeries(long seriesId)
|
2011-04-23 23:33:24 +03:00
|
|
|
{
|
2011-09-05 05:56:45 +03:00
|
|
|
var episodes = _database.Fetch<Episode, Series, EpisodeFile>(@"SELECT * FROM Episodes
|
|
|
|
INNER JOIN Series ON Episodes.SeriesId = Series.SeriesId
|
2011-06-20 04:46:32 +03:00
|
|
|
LEFT JOIN EpisodeFiles ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId
|
2011-09-05 05:56:45 +03:00
|
|
|
WHERE Episodes.SeriesId = @0", seriesId);
|
2011-06-20 04:46:32 +03:00
|
|
|
|
|
|
|
foreach (var episode in episodes)
|
|
|
|
{
|
|
|
|
if (episode.EpisodeFileId == 0)
|
|
|
|
episode.EpisodeFile = null;
|
|
|
|
}
|
2011-06-22 04:12:20 +03:00
|
|
|
|
2011-06-20 04:46:32 +03:00
|
|
|
return episodes;
|
2011-04-23 23:33:24 +03:00
|
|
|
}
|
|
|
|
|
2011-06-04 04:56:53 +03:00
|
|
|
public virtual IList<Episode> GetEpisodesBySeason(long seriesId, int seasonNumber)
|
2011-04-23 23:33:24 +03:00
|
|
|
{
|
2011-09-05 05:56:45 +03:00
|
|
|
var episodes = _database.Fetch<Episode, Series, EpisodeFile>(@"SELECT * FROM Episodes
|
|
|
|
INNER JOIN Series ON Episodes.SeriesId = Series.SeriesId
|
2011-06-20 04:46:32 +03:00
|
|
|
LEFT JOIN EpisodeFiles ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId
|
2011-09-05 05:56:45 +03:00
|
|
|
WHERE Episodes.SeriesId = @0 AND Episodes.SeasonNumber = @1", seriesId, seasonNumber);
|
2011-06-20 04:46:32 +03:00
|
|
|
|
|
|
|
foreach (var episode in episodes)
|
|
|
|
{
|
|
|
|
if (episode.EpisodeFileId == 0)
|
|
|
|
episode.EpisodeFile = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return episodes;
|
2011-04-23 23:33:24 +03:00
|
|
|
}
|
|
|
|
|
2011-07-04 02:04:57 +03:00
|
|
|
public virtual void MarkEpisodeAsFetched(int episodeId)
|
|
|
|
{
|
2012-02-13 09:38:57 +03:00
|
|
|
logger.Trace("Marking episode {0} as fetched.", episodeId);
|
2011-07-04 02:04:57 +03:00
|
|
|
_database.Execute("UPDATE Episodes SET GrabDate=@0 WHERE EpisodeId=@1", DateTime.Now, episodeId);
|
|
|
|
}
|
|
|
|
|
2012-02-07 08:08:07 +03:00
|
|
|
public virtual IList<Episode> GetEpisodesByParseResult(EpisodeParseResult parseResult)
|
2011-06-22 04:12:20 +03:00
|
|
|
{
|
2012-01-07 00:25:27 +03:00
|
|
|
//Disabling auto add, need to make it a lot more conservative.
|
2012-02-07 08:08:07 +03:00
|
|
|
var autoAddNew = false;
|
2012-01-07 00:25:27 +03:00
|
|
|
|
2011-06-22 04:12:20 +03:00
|
|
|
var result = new List<Episode>();
|
2011-06-07 09:29:07 +03:00
|
|
|
|
2011-11-24 11:28:20 +03:00
|
|
|
if (parseResult.AirDate.HasValue)
|
|
|
|
{
|
2011-12-20 03:45:47 +03:00
|
|
|
if (!parseResult.Series.IsDaily)
|
|
|
|
{
|
|
|
|
//Todo: Collect this as a Series we want to treat as a daily series, or possible parsing error
|
2012-02-13 09:38:57 +03:00
|
|
|
logger.Warn("Found daily-style episode for non-daily series: {0}. {1}", parseResult.Series.Title, parseResult.OriginalString);
|
2011-12-20 03:45:47 +03:00
|
|
|
return new List<Episode>();
|
|
|
|
}
|
|
|
|
|
2011-11-24 11:28:20 +03:00
|
|
|
var episodeInfo = GetEpisode(parseResult.Series.SeriesId, parseResult.AirDate.Value);
|
|
|
|
|
|
|
|
if (episodeInfo == null && autoAddNew)
|
|
|
|
{
|
2012-02-13 09:38:57 +03:00
|
|
|
logger.Info("Episode {0} doesn't exist in db. adding it now. {1}", parseResult, parseResult.OriginalString);
|
2011-11-24 11:28:20 +03:00
|
|
|
episodeInfo = new Episode
|
2011-12-01 22:05:17 +03:00
|
|
|
{
|
|
|
|
SeriesId = parseResult.Series.SeriesId,
|
|
|
|
AirDate = parseResult.AirDate.Value,
|
|
|
|
Title = "TBD",
|
|
|
|
Overview = String.Empty
|
|
|
|
};
|
2011-11-24 11:28:20 +03:00
|
|
|
|
2011-11-25 10:56:07 +03:00
|
|
|
var episodesInSeries = GetEpisodeBySeries(parseResult.Series.SeriesId);
|
|
|
|
|
|
|
|
//Find the current season number
|
|
|
|
var maxSeasonNumber = episodesInSeries.Select(s => s.SeasonNumber).MaxOrDefault();
|
|
|
|
|
|
|
|
//Set the season number
|
|
|
|
episodeInfo.SeasonNumber = (maxSeasonNumber == 0) ? 1 : maxSeasonNumber;
|
|
|
|
|
|
|
|
//Find the latest episode number
|
|
|
|
var maxEpisodeNumber = episodesInSeries
|
2011-12-01 22:05:17 +03:00
|
|
|
.Where(w => w.SeasonNumber == episodeInfo.SeasonNumber)
|
|
|
|
.Select(s => s.EpisodeNumber).MaxOrDefault();
|
2011-11-25 10:56:07 +03:00
|
|
|
|
|
|
|
//Set the episode number to max + 1
|
|
|
|
episodeInfo.EpisodeNumber = maxEpisodeNumber + 1;
|
|
|
|
|
2011-11-24 11:28:20 +03:00
|
|
|
AddEpisode(episodeInfo);
|
|
|
|
}
|
2011-12-01 22:05:17 +03:00
|
|
|
if (episodeInfo != null)
|
|
|
|
{
|
|
|
|
result.Add(episodeInfo);
|
2011-12-02 09:26:24 +03:00
|
|
|
parseResult.EpisodeTitle = episodeInfo.Title;
|
2011-12-01 22:05:17 +03:00
|
|
|
}
|
2011-11-24 11:28:20 +03:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-10-21 02:07:47 +03:00
|
|
|
if (parseResult.EpisodeNumbers == null)
|
|
|
|
return result;
|
|
|
|
|
2011-12-06 05:22:55 +03:00
|
|
|
//Set it to empty before looping through the episode numbers
|
|
|
|
parseResult.EpisodeTitle = String.Empty;
|
|
|
|
|
2011-06-22 04:12:20 +03:00
|
|
|
foreach (var episodeNumber in parseResult.EpisodeNumbers)
|
2011-06-20 04:46:32 +03:00
|
|
|
{
|
2011-06-22 04:12:20 +03:00
|
|
|
var episodeInfo = GetEpisode(parseResult.Series.SeriesId, parseResult.SeasonNumber, episodeNumber);
|
2011-06-23 09:56:17 +03:00
|
|
|
if (episodeInfo == null && parseResult.AirDate != null)
|
2011-06-22 04:12:20 +03:00
|
|
|
{
|
2011-06-23 09:56:17 +03:00
|
|
|
episodeInfo = GetEpisode(parseResult.Series.SeriesId, parseResult.AirDate.Value);
|
2011-06-22 04:12:20 +03:00
|
|
|
}
|
|
|
|
//if still null we should add the temp episode
|
|
|
|
if (episodeInfo == null && autoAddNew)
|
|
|
|
{
|
2012-02-13 09:38:57 +03:00
|
|
|
logger.Info("Episode {0} doesn't exist in db. adding it now. {1}", parseResult, parseResult.OriginalString);
|
2011-06-22 04:12:20 +03:00
|
|
|
episodeInfo = new Episode
|
|
|
|
{
|
|
|
|
SeriesId = parseResult.Series.SeriesId,
|
|
|
|
AirDate = DateTime.Now.Date,
|
|
|
|
EpisodeNumber = episodeNumber,
|
|
|
|
SeasonNumber = parseResult.SeasonNumber,
|
|
|
|
Title = "TBD",
|
|
|
|
Overview = String.Empty,
|
|
|
|
};
|
|
|
|
|
2011-10-04 05:01:39 +03:00
|
|
|
if (parseResult.EpisodeNumbers.Count == 1 && parseResult.EpisodeNumbers.First() == 0)
|
|
|
|
episodeInfo.Ignored = true;
|
|
|
|
|
2011-06-22 04:12:20 +03:00
|
|
|
AddEpisode(episodeInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (episodeInfo != null)
|
|
|
|
{
|
|
|
|
result.Add(episodeInfo);
|
2012-02-03 09:49:34 +03:00
|
|
|
|
|
|
|
if (parseResult.EpisodeNumbers.Count == 1)
|
|
|
|
{
|
|
|
|
parseResult.EpisodeTitle = episodeInfo.Title.Trim();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-15 08:52:12 +03:00
|
|
|
parseResult.EpisodeTitle = Parser.CleanupEpisodeTitle(episodeInfo.Title);
|
2012-02-03 09:49:34 +03:00
|
|
|
}
|
2011-06-22 04:12:20 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-02-22 07:43:19 +03:00
|
|
|
logger.Debug("Unable to find {0}", parseResult);
|
2011-06-22 04:12:20 +03:00
|
|
|
}
|
2011-06-20 04:46:32 +03:00
|
|
|
}
|
|
|
|
|
2011-06-22 04:12:20 +03:00
|
|
|
return result;
|
2011-06-07 09:29:07 +03:00
|
|
|
}
|
|
|
|
|
2011-05-01 11:04:44 +03:00
|
|
|
public virtual IList<Episode> EpisodesWithoutFiles(bool includeSpecials)
|
|
|
|
{
|
2012-01-25 04:11:16 +03:00
|
|
|
var episodes = _database.Query<Episode, Series>(@"SELECT Episodes.*, Series.* FROM Episodes
|
2011-08-04 08:44:24 +03:00
|
|
|
INNER JOIN Series
|
|
|
|
ON Episodes.SeriesId = Series.SeriesId
|
2011-08-04 08:56:42 +03:00
|
|
|
WHERE (EpisodeFileId=0 OR EpisodeFileId=NULL) AND Ignored = 0 AND AirDate<=@0",
|
2011-06-16 09:33:01 +03:00
|
|
|
DateTime.Now.Date);
|
2011-08-04 08:44:24 +03:00
|
|
|
if (!includeSpecials)
|
2011-06-16 09:33:01 +03:00
|
|
|
return episodes.Where(e => e.SeasonNumber > 0).ToList();
|
2011-05-01 11:04:44 +03:00
|
|
|
|
2011-08-04 08:44:24 +03:00
|
|
|
return episodes.ToList();
|
2011-05-01 11:04:44 +03:00
|
|
|
}
|
2011-04-23 23:33:24 +03:00
|
|
|
|
2011-06-20 04:59:31 +03:00
|
|
|
public virtual IList<Episode> GetEpisodesByFileId(int episodeFileId)
|
2011-06-06 03:37:32 +03:00
|
|
|
{
|
2011-09-05 05:56:45 +03:00
|
|
|
return _database.Fetch<Episode, Series>(@"SELECT * FROM Episodes
|
|
|
|
INNER JOIN Series ON Episodes.SeriesId = Series.SeriesId
|
|
|
|
WHERE EpisodeFileId = @0", episodeFileId);
|
2011-06-06 03:37:32 +03:00
|
|
|
}
|
2011-05-29 06:01:35 +03:00
|
|
|
|
2011-09-04 06:05:44 +03:00
|
|
|
public virtual IList<Episode> EpisodesWithFiles()
|
|
|
|
{
|
2011-09-04 10:45:58 +03:00
|
|
|
var episodes = _database.Fetch<Episode, Series, EpisodeFile>(@"SELECT Episodes.*, Series.*, EpisodeFiles.* FROM Episodes
|
2011-09-04 06:05:44 +03:00
|
|
|
INNER JOIN Series ON Episodes.SeriesId = Series.SeriesId
|
|
|
|
INNER JOIN EpisodeFiles ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId");
|
|
|
|
|
|
|
|
return episodes;
|
|
|
|
}
|
|
|
|
|
2011-05-29 06:01:35 +03:00
|
|
|
public virtual void RefreshEpisodeInfo(Series series)
|
2011-04-23 23:33:24 +03:00
|
|
|
{
|
2012-02-25 05:01:53 +03:00
|
|
|
logger.Trace("Starting episode info refresh for series: {0}", series.Title.WithDefault(series.SeriesId));
|
2012-02-28 08:50:56 +03:00
|
|
|
var successCount = 0;
|
|
|
|
var failCount = 0;
|
|
|
|
|
|
|
|
var tvdbEpisodes = _tvDbProvider.GetSeries(series.SeriesId, true)
|
|
|
|
.Episodes
|
|
|
|
.Where(episode => !string.IsNullOrWhiteSpace(episode.EpisodeName) ||
|
|
|
|
(episode.FirstAired < DateTime.Now.AddDays(2) && episode.FirstAired.Year > 1900))
|
|
|
|
.ToList();
|
2011-04-23 23:33:24 +03:00
|
|
|
|
2011-06-16 09:33:01 +03:00
|
|
|
var seriesEpisodes = GetEpisodeBySeries(series.SeriesId);
|
2011-04-23 23:33:24 +03:00
|
|
|
var updateList = new List<Episode>();
|
|
|
|
var newList = new List<Episode>();
|
|
|
|
|
2012-02-28 08:50:56 +03:00
|
|
|
_seasonProvider.EnsureSeasons(series.SeriesId, tvdbEpisodes.Select(c => c.SeasonNumber).Distinct());
|
|
|
|
|
|
|
|
foreach (var episode in tvdbEpisodes.OrderBy(e => e.SeasonNumber).ThenBy(e => e.EpisodeNumber))
|
2011-04-23 23:33:24 +03:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2012-02-28 08:50:56 +03:00
|
|
|
logger.Trace("Updating info for [{0}] - S{1:00}E{2:00}", series.Title, episode.SeasonNumber, episode.EpisodeNumber);
|
2011-05-29 06:01:35 +03:00
|
|
|
|
2011-05-29 06:37:19 +03:00
|
|
|
//first check using tvdbId, this should cover cases when and episode number in a season is changed
|
2012-02-13 09:38:57 +03:00
|
|
|
var episodeToUpdate = seriesEpisodes.SingleOrDefault(e => e.TvDbEpisodeId == episode.Id);
|
2011-05-29 06:01:35 +03:00
|
|
|
|
2011-05-29 06:37:19 +03:00
|
|
|
//not found, try using season/episode number
|
|
|
|
if (episodeToUpdate == null)
|
|
|
|
{
|
2012-02-13 09:38:57 +03:00
|
|
|
episodeToUpdate = seriesEpisodes.SingleOrDefault(e => e.SeasonNumber == episode.SeasonNumber && e.EpisodeNumber == episode.EpisodeNumber);
|
2011-05-29 06:37:19 +03:00
|
|
|
}
|
|
|
|
|
2011-05-29 06:01:35 +03:00
|
|
|
if (episodeToUpdate == null)
|
2011-04-23 23:33:24 +03:00
|
|
|
{
|
2011-05-29 06:01:35 +03:00
|
|
|
episodeToUpdate = new Episode();
|
|
|
|
newList.Add(episodeToUpdate);
|
2011-10-04 05:01:39 +03:00
|
|
|
|
2012-02-28 08:50:56 +03:00
|
|
|
//If it is Episode Zero Ignore it (specials, sneak peeks.)
|
|
|
|
if (episode.EpisodeNumber == 0 && episode.SeasonNumber != 1)
|
2011-12-01 22:05:17 +03:00
|
|
|
{
|
2011-10-04 05:01:39 +03:00
|
|
|
episodeToUpdate.Ignored = true;
|
2011-12-01 22:05:17 +03:00
|
|
|
}
|
2011-11-30 11:21:18 +03:00
|
|
|
else
|
2011-12-01 22:05:17 +03:00
|
|
|
{
|
2012-02-21 06:25:19 +03:00
|
|
|
episodeToUpdate.Ignored = _seasonProvider.IsIgnored(series.SeriesId, episode.SeasonNumber);
|
2011-12-01 22:05:17 +03:00
|
|
|
}
|
2011-04-23 23:33:24 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-05-29 06:01:35 +03:00
|
|
|
updateList.Add(episodeToUpdate);
|
2011-04-23 23:33:24 +03:00
|
|
|
}
|
|
|
|
|
2012-10-07 08:30:12 +03:00
|
|
|
if ((episodeToUpdate.EpisodeNumber != episode.EpisodeNumber ||
|
|
|
|
episodeToUpdate.SeasonNumber != episode.SeasonNumber) &&
|
|
|
|
episodeToUpdate.EpisodeFileId > 0)
|
|
|
|
{
|
|
|
|
logger.Info("Unlinking episode file because TheTVDB changed the epsiode number...");
|
|
|
|
|
|
|
|
_database.Delete<EpisodeFile>(episodeToUpdate.EpisodeFileId);
|
|
|
|
episodeToUpdate.EpisodeFileId = 0;
|
|
|
|
}
|
|
|
|
|
2011-05-29 06:01:35 +03:00
|
|
|
episodeToUpdate.SeriesId = series.SeriesId;
|
|
|
|
episodeToUpdate.TvDbEpisodeId = episode.Id;
|
|
|
|
episodeToUpdate.EpisodeNumber = episode.EpisodeNumber;
|
|
|
|
episodeToUpdate.SeasonNumber = episode.SeasonNumber;
|
2012-10-13 10:07:02 +03:00
|
|
|
episodeToUpdate.AbsoluteEpisodeNumber = episode.AbsoluteEpisodeNumber;
|
2011-05-29 06:01:35 +03:00
|
|
|
episodeToUpdate.Title = episode.EpisodeName;
|
2012-04-04 06:37:56 +03:00
|
|
|
|
2012-04-30 06:32:12 +03:00
|
|
|
episodeToUpdate.Overview = episode.Overview.Truncate(3500);
|
2011-05-29 06:01:35 +03:00
|
|
|
|
2011-06-23 09:56:17 +03:00
|
|
|
if (episode.FirstAired.Year > 1900)
|
|
|
|
episodeToUpdate.AirDate = episode.FirstAired.Date;
|
2011-11-24 09:28:52 +03:00
|
|
|
else
|
|
|
|
episodeToUpdate.AirDate = null;
|
2011-06-23 09:56:17 +03:00
|
|
|
|
2011-04-23 23:33:24 +03:00
|
|
|
successCount++;
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2012-02-28 08:50:56 +03:00
|
|
|
logger.FatalException(String.Format("An error has occurred while updating episode info for series {0}", series.Title), e);
|
2011-04-23 23:33:24 +03:00
|
|
|
failCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-04 22:24:17 +03:00
|
|
|
_database.InsertMany(newList);
|
2012-02-03 09:49:34 +03:00
|
|
|
_database.UpdateMany(updateList);
|
2011-04-23 23:33:24 +03:00
|
|
|
|
2012-02-25 05:01:53 +03:00
|
|
|
if (failCount != 0)
|
|
|
|
{
|
|
|
|
logger.Info("Finished episode refresh for series: {0}. Successful: {1} - Failed: {2} ",
|
2012-02-28 08:50:56 +03:00
|
|
|
series.Title, successCount, failCount);
|
2012-02-25 05:01:53 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-02-28 08:50:56 +03:00
|
|
|
logger.Info("Finished episode refresh for series: {0}.", series.Title);
|
2012-02-25 05:01:53 +03:00
|
|
|
}
|
2012-02-28 08:50:56 +03:00
|
|
|
|
|
|
|
DeleteEpisodesNotInTvdb(series, tvdbEpisodes);
|
2011-04-23 23:33:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void UpdateEpisode(Episode episode)
|
|
|
|
{
|
2011-06-16 09:33:01 +03:00
|
|
|
_database.Update(episode);
|
2011-04-23 23:33:24 +03:00
|
|
|
}
|
2011-06-04 04:56:53 +03:00
|
|
|
|
|
|
|
public virtual IList<int> GetSeasons(int seriesId)
|
|
|
|
{
|
2011-06-16 09:33:01 +03:00
|
|
|
return _database.Fetch<Int32>("SELECT DISTINCT SeasonNumber FROM Episodes WHERE SeriesId=@0", seriesId).OrderBy(c => c).ToList();
|
2011-06-04 04:56:53 +03:00
|
|
|
}
|
|
|
|
|
2011-08-28 22:07:56 +03: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-08-04 04:45:45 +03:00
|
|
|
public virtual void SetEpisodeIgnore(int episodeId, bool isIgnored)
|
|
|
|
{
|
|
|
|
_database.Execute(@"UPDATE Episodes SET Ignored = @0
|
|
|
|
WHERE EpisodeId = @1",
|
|
|
|
isIgnored, episodeId);
|
2011-06-04 04:56:53 +03:00
|
|
|
|
2012-02-13 10:28:01 +03:00
|
|
|
logger.Info("Ignore flag for Episode:{0} was set to {1}", episodeId, isIgnored);
|
2011-06-04 04:56:53 +03:00
|
|
|
}
|
2011-06-19 08:57:24 +03:00
|
|
|
|
2011-09-14 07:37:22 +03:00
|
|
|
public virtual bool IsFirstOrLastEpisodeOfSeason(int seriesId, int seasonNumber, int episodeNumber)
|
|
|
|
{
|
|
|
|
var episodes = GetEpisodesBySeason(seriesId, seasonNumber).OrderBy(e => e.EpisodeNumber);
|
|
|
|
|
2012-02-13 09:38:57 +03:00
|
|
|
if (!episodes.Any())
|
2011-09-14 07:37:22 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-02-28 08:50:56 +03:00
|
|
|
public virtual void DeleteEpisodesNotInTvdb(Series series, IList<TvdbEpisode> tvdbEpisodes)
|
2011-09-30 07:40:00 +03:00
|
|
|
{
|
2012-02-13 09:38:57 +03:00
|
|
|
logger.Trace("Starting deletion of episodes that no longer exist in TVDB: {0}", series.Title.WithDefault(series.SeriesId));
|
2011-09-30 07:40:00 +03:00
|
|
|
|
2012-02-28 08:50:56 +03:00
|
|
|
if (!tvdbEpisodes.Any()) return;
|
|
|
|
|
2011-09-30 07:40:00 +03:00
|
|
|
//Delete Episodes not matching TvDbIds for this series
|
2012-02-28 08:50:56 +03:00
|
|
|
var tvDbIds = tvdbEpisodes.Select(e => e.Id);
|
2011-09-30 07:40:00 +03:00
|
|
|
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);
|
|
|
|
|
|
|
|
_database.Execute(tvDbIdQuery);
|
|
|
|
|
2012-02-13 09:38:57 +03:00
|
|
|
logger.Trace("Deleted episodes that no longer exist in TVDB for {0}", series.SeriesId);
|
2011-09-30 07:40:00 +03:00
|
|
|
}
|
2011-10-12 06:44:19 +03:00
|
|
|
|
2011-10-17 22:23:34 +03:00
|
|
|
public virtual void SetPostDownloadStatus(List<int> episodeIds, PostDownloadStatusType postDownloadStatus)
|
2011-10-12 06:44:19 +03:00
|
|
|
{
|
2011-10-17 22:23:34 +03:00
|
|
|
if (episodeIds.Count == 0) throw new ArgumentException("episodeIds should contain one or more episode ids.");
|
|
|
|
|
2011-10-12 06:44:19 +03:00
|
|
|
var episodeIdString = String.Join(", ", episodeIds);
|
|
|
|
|
|
|
|
var episodeIdQuery = String.Format(@"UPDATE Episodes SET PostDownloadStatus = {0}
|
|
|
|
WHERE EpisodeId IN ({1})", (int)postDownloadStatus, episodeIdString);
|
|
|
|
|
2012-02-13 09:38:57 +03:00
|
|
|
logger.Trace("Updating PostDownloadStatus for all episodeIds in {0}", episodeIdString);
|
2011-10-12 06:44:19 +03:00
|
|
|
_database.Execute(episodeIdQuery);
|
|
|
|
}
|
2011-04-23 23:33:24 +03:00
|
|
|
}
|
2011-04-23 01:24:05 +03:00
|
|
|
} |