2013-02-20 02:05:15 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
2013-02-24 19:18:48 +00:00
|
|
|
using NzbDrone.Common.Eventing;
|
2013-02-24 19:57:33 +00:00
|
|
|
using NzbDrone.Core.Datastore;
|
2013-02-24 19:18:48 +00:00
|
|
|
using NzbDrone.Core.Download;
|
2013-02-20 02:05:15 +00:00
|
|
|
using NzbDrone.Core.Model;
|
|
|
|
using NzbDrone.Core.Providers;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Tv
|
|
|
|
{
|
|
|
|
public interface IEpisodeService
|
|
|
|
{
|
|
|
|
void AddEpisode(Episode episode);
|
|
|
|
Episode GetEpisode(int id);
|
|
|
|
Episode GetEpisode(int seriesId, int seasonNumber, int episodeNumber);
|
|
|
|
Episode GetEpisode(int seriesId, DateTime date);
|
2013-03-02 19:13:23 +00:00
|
|
|
List<Episode> GetEpisodeBySeries(int seriesId);
|
2013-02-20 02:05:15 +00:00
|
|
|
IList<Episode> GetEpisodesBySeason(int seriesId, int seasonNumber);
|
|
|
|
IList<Episode> GetEpisodesByParseResult(EpisodeParseResult parseResult);
|
|
|
|
IList<Episode> EpisodesWithoutFiles(bool includeSpecials);
|
|
|
|
IList<Episode> GetEpisodesByFileId(int episodeFileId);
|
|
|
|
IList<Episode> EpisodesWithFiles();
|
|
|
|
void RefreshEpisodeInfo(Series series);
|
|
|
|
void UpdateEpisode(Episode episode);
|
|
|
|
IList<int> GetEpisodeNumbersBySeason(int seriesId, int seasonNumber);
|
|
|
|
void SetEpisodeIgnore(int episodeId, bool isIgnored);
|
|
|
|
bool IsFirstOrLastEpisodeOfSeason(int seriesId, int seasonNumber, int episodeNumber);
|
|
|
|
void SetPostDownloadStatus(List<int> episodeIds, PostDownloadStatusType postDownloadStatus);
|
|
|
|
void UpdateEpisodes(List<Episode> episodes);
|
|
|
|
Episode GetEpisodeBySceneNumbering(int seriesId, int seasonNumber, int episodeNumber);
|
2013-02-26 03:30:24 +00:00
|
|
|
List<Episode> EpisodesBetweenDates(DateTime start, DateTime end);
|
2013-02-20 02:05:15 +00:00
|
|
|
}
|
|
|
|
|
2013-02-24 19:18:48 +00:00
|
|
|
public class EpisodeService : IEpisodeService, IHandle<EpisodeGrabbedEvent>
|
2013-02-20 02:05:15 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
|
|
private readonly TvDbProvider _tvDbProvider;
|
|
|
|
private readonly ISeasonRepository _seasonRepository;
|
2013-02-24 20:24:31 +00:00
|
|
|
private readonly IEpisodeRepository _episodeRepository;
|
2013-02-20 02:05:15 +00:00
|
|
|
|
2013-02-24 20:24:31 +00:00
|
|
|
public EpisodeService(TvDbProvider tvDbProviderProvider, ISeasonRepository seasonRepository, IEpisodeRepository episodeRepository)
|
2013-02-20 02:05:15 +00:00
|
|
|
{
|
|
|
|
_tvDbProvider = tvDbProviderProvider;
|
|
|
|
_seasonRepository = seasonRepository;
|
|
|
|
_episodeRepository = episodeRepository;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void AddEpisode(Episode episode)
|
|
|
|
{
|
|
|
|
episode.Ignored = _seasonRepository.IsIgnored(episode.SeriesId, episode.SeasonNumber);
|
|
|
|
_episodeRepository.Insert(episode);
|
|
|
|
}
|
|
|
|
|
2013-03-02 19:13:23 +00:00
|
|
|
public Episode GetEpisode(int id)
|
2013-02-20 02:05:15 +00:00
|
|
|
{
|
|
|
|
return _episodeRepository.Get(id);
|
|
|
|
}
|
|
|
|
|
2013-03-02 19:13:23 +00:00
|
|
|
public Episode GetEpisode(int seriesId, int seasonNumber, int episodeNumber)
|
2013-02-20 02:05:15 +00:00
|
|
|
{
|
|
|
|
return _episodeRepository.Get(seriesId, seasonNumber, episodeNumber);
|
|
|
|
}
|
|
|
|
|
2013-03-02 19:13:23 +00:00
|
|
|
public Episode GetEpisode(int seriesId, DateTime date)
|
2013-02-20 02:05:15 +00:00
|
|
|
{
|
|
|
|
return _episodeRepository.Get(seriesId, date);
|
|
|
|
}
|
|
|
|
|
2013-03-02 19:13:23 +00:00
|
|
|
public List<Episode> GetEpisodeBySeries(int seriesId)
|
2013-02-20 02:05:15 +00:00
|
|
|
{
|
2013-03-02 19:13:23 +00:00
|
|
|
return _episodeRepository.GetEpisodes(seriesId).ToList();
|
2013-02-20 02:05:15 +00:00
|
|
|
}
|
|
|
|
|
2013-03-02 19:13:23 +00:00
|
|
|
public IList<Episode> GetEpisodesBySeason(int seriesId, int seasonNumber)
|
2013-02-20 02:05:15 +00:00
|
|
|
{
|
|
|
|
return _episodeRepository.GetEpisodes(seriesId, seasonNumber);
|
|
|
|
}
|
|
|
|
|
2013-03-02 19:13:23 +00:00
|
|
|
public IList<Episode> GetEpisodesByParseResult(EpisodeParseResult parseResult)
|
2013-02-20 02:05:15 +00:00
|
|
|
{
|
|
|
|
var result = new List<Episode>();
|
|
|
|
|
|
|
|
if (parseResult.AirDate.HasValue)
|
|
|
|
{
|
|
|
|
if (parseResult.Series.SeriesType == SeriesType.Standard)
|
|
|
|
{
|
|
|
|
//Todo: Collect this as a Series we want to treat as a daily series, or possible parsing error
|
|
|
|
logger.Warn("Found daily-style episode for non-daily series: {0}. {1}", parseResult.Series.Title, parseResult.OriginalString);
|
|
|
|
return new List<Episode>();
|
|
|
|
}
|
|
|
|
|
2013-02-26 03:58:57 +00:00
|
|
|
var episodeInfo = GetEpisode(((ModelBase)parseResult.Series).Id, parseResult.AirDate.Value);
|
2013-02-20 02:05:15 +00:00
|
|
|
|
|
|
|
if (episodeInfo != null)
|
|
|
|
{
|
|
|
|
result.Add(episodeInfo);
|
|
|
|
parseResult.EpisodeTitle = episodeInfo.Title;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parseResult.EpisodeNumbers == null)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
//Set it to empty before looping through the episode numbers
|
|
|
|
parseResult.EpisodeTitle = String.Empty;
|
|
|
|
|
|
|
|
foreach (var episodeNumber in parseResult.EpisodeNumbers)
|
|
|
|
{
|
|
|
|
Episode episodeInfo = null;
|
|
|
|
|
|
|
|
if (parseResult.SceneSource && parseResult.Series.UseSceneNumbering)
|
2013-02-26 03:58:57 +00:00
|
|
|
episodeInfo = GetEpisodeBySceneNumbering(((ModelBase)parseResult.Series).Id, parseResult.SeasonNumber, episodeNumber);
|
2013-02-20 02:05:15 +00:00
|
|
|
|
|
|
|
if (episodeInfo == null)
|
|
|
|
{
|
2013-02-26 03:58:57 +00:00
|
|
|
episodeInfo = GetEpisode(((ModelBase)parseResult.Series).Id, parseResult.SeasonNumber, episodeNumber);
|
2013-02-20 02:05:15 +00:00
|
|
|
if (episodeInfo == null && parseResult.AirDate != null)
|
|
|
|
{
|
2013-02-26 03:58:57 +00:00
|
|
|
episodeInfo = GetEpisode(((ModelBase)parseResult.Series).Id, parseResult.AirDate.Value);
|
2013-02-20 02:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (episodeInfo != null)
|
|
|
|
{
|
|
|
|
result.Add(episodeInfo);
|
|
|
|
|
|
|
|
if (parseResult.Series.UseSceneNumbering)
|
|
|
|
{
|
|
|
|
logger.Info("Using Scene to TVDB Mapping for: {0} - Scene: {1}x{2:00} - TVDB: {3}x{4:00}",
|
|
|
|
parseResult.Series.Title,
|
|
|
|
episodeInfo.SceneSeasonNumber,
|
|
|
|
episodeInfo.SceneEpisodeNumber,
|
|
|
|
episodeInfo.SeasonNumber,
|
|
|
|
episodeInfo.EpisodeNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parseResult.EpisodeNumbers.Count == 1)
|
|
|
|
{
|
|
|
|
parseResult.EpisodeTitle = episodeInfo.Title.Trim();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
parseResult.EpisodeTitle = Parser.CleanupEpisodeTitle(episodeInfo.Title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logger.Debug("Unable to find {0}", parseResult);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-03-02 19:13:23 +00:00
|
|
|
public IList<Episode> EpisodesWithoutFiles(bool includeSpecials)
|
2013-02-20 02:05:15 +00:00
|
|
|
{
|
|
|
|
return _episodeRepository.EpisodesWithoutFiles(includeSpecials);
|
|
|
|
}
|
|
|
|
|
2013-03-02 19:13:23 +00:00
|
|
|
public IList<Episode> GetEpisodesByFileId(int episodeFileId)
|
2013-02-20 02:05:15 +00:00
|
|
|
{
|
|
|
|
return _episodeRepository.GetEpisodeByFileId(episodeFileId);
|
|
|
|
}
|
|
|
|
|
2013-03-02 19:13:23 +00:00
|
|
|
public IList<Episode> EpisodesWithFiles()
|
2013-02-20 02:05:15 +00:00
|
|
|
{
|
|
|
|
return _episodeRepository.EpisodesWithFiles();
|
|
|
|
}
|
|
|
|
|
2013-03-02 19:13:23 +00:00
|
|
|
public void RefreshEpisodeInfo(Series series)
|
2013-02-20 02:05:15 +00:00
|
|
|
{
|
2013-02-26 03:58:57 +00:00
|
|
|
logger.Trace("Starting episode info refresh for series: {0}", series.Title.WithDefault(series.Id));
|
2013-02-20 02:05:15 +00:00
|
|
|
var successCount = 0;
|
|
|
|
var failCount = 0;
|
|
|
|
|
2013-02-26 03:58:57 +00:00
|
|
|
var tvdbEpisodes = _tvDbProvider.GetEpisodes(series.TvDbId);
|
2013-02-26 02:20:42 +00:00
|
|
|
|
2013-02-26 03:58:57 +00:00
|
|
|
var seriesEpisodes = GetEpisodeBySeries(series.Id);
|
2013-02-20 02:05:15 +00:00
|
|
|
var updateList = new List<Episode>();
|
|
|
|
var newList = new List<Episode>();
|
|
|
|
|
|
|
|
foreach (var episode in tvdbEpisodes.OrderBy(e => e.SeasonNumber).ThenBy(e => e.EpisodeNumber))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
logger.Trace("Updating info for [{0}] - S{1:00}E{2:00}", series.Title, episode.SeasonNumber, episode.EpisodeNumber);
|
|
|
|
|
|
|
|
//first check using tvdbId, this should cover cases when and episode number in a season is changed
|
2013-03-02 20:14:18 +00:00
|
|
|
|
|
|
|
var episodes = seriesEpisodes.Where(e => e.TvDbEpisodeId == episode.TvDbEpisodeId).ToList();
|
2013-02-26 02:20:42 +00:00
|
|
|
var episodeToUpdate = seriesEpisodes.SingleOrDefault(e => e.TvDbEpisodeId == episode.TvDbEpisodeId);
|
2013-02-20 02:05:15 +00:00
|
|
|
|
|
|
|
//not found, try using season/episode number
|
|
|
|
if (episodeToUpdate == null)
|
|
|
|
{
|
|
|
|
episodeToUpdate = seriesEpisodes.SingleOrDefault(e => e.SeasonNumber == episode.SeasonNumber && e.EpisodeNumber == episode.EpisodeNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (episodeToUpdate == null)
|
|
|
|
{
|
|
|
|
episodeToUpdate = new Episode();
|
|
|
|
newList.Add(episodeToUpdate);
|
|
|
|
|
|
|
|
//If it is Episode Zero Ignore it (specials, sneak peeks.)
|
|
|
|
if (episode.EpisodeNumber == 0 && episode.SeasonNumber != 1)
|
|
|
|
{
|
|
|
|
episodeToUpdate.Ignored = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-02-26 03:58:57 +00:00
|
|
|
episodeToUpdate.Ignored = _seasonRepository.IsIgnored(series.Id, episode.SeasonNumber);
|
2013-02-20 02:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
updateList.Add(episodeToUpdate);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((episodeToUpdate.EpisodeNumber != episode.EpisodeNumber ||
|
|
|
|
episodeToUpdate.SeasonNumber != episode.SeasonNumber) &&
|
|
|
|
episodeToUpdate.EpisodeFileId > 0)
|
|
|
|
{
|
2013-02-26 02:20:42 +00:00
|
|
|
logger.Info("Unlinking episode file because TheTVDB changed the episode number...");
|
2013-02-20 02:05:15 +00:00
|
|
|
episodeToUpdate.EpisodeFile = null;
|
|
|
|
}
|
|
|
|
|
2013-02-26 03:58:57 +00:00
|
|
|
episodeToUpdate.SeriesId = series.Id;
|
2013-02-25 00:00:17 +00:00
|
|
|
episodeToUpdate.Series = series;
|
2013-02-26 02:20:42 +00:00
|
|
|
episodeToUpdate.TvDbEpisodeId = episode.TvDbEpisodeId;
|
2013-02-20 02:05:15 +00:00
|
|
|
episodeToUpdate.EpisodeNumber = episode.EpisodeNumber;
|
|
|
|
episodeToUpdate.SeasonNumber = episode.SeasonNumber;
|
2013-02-26 02:20:42 +00:00
|
|
|
episodeToUpdate.AbsoluteEpisodeNumber = episode.AbsoluteEpisodeNumber;
|
|
|
|
episodeToUpdate.Title = episode.Title;
|
|
|
|
episodeToUpdate.Overview = episode.Overview;
|
|
|
|
episodeToUpdate.AirDate = episode.AirDate;
|
2013-02-20 02:05:15 +00:00
|
|
|
|
2013-03-01 04:59:06 +00:00
|
|
|
if(!String.IsNullOrWhiteSpace(series.AirTime) && episodeToUpdate.AirDate.HasValue)
|
|
|
|
{
|
|
|
|
episodeToUpdate.AirDate = episodeToUpdate.AirDate.Value.Add(Convert.ToDateTime(series.AirTime).TimeOfDay)
|
|
|
|
.AddHours(series.UtcOffset * -1);
|
|
|
|
}
|
|
|
|
|
2013-02-20 02:05:15 +00:00
|
|
|
successCount++;
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
logger.FatalException(String.Format("An error has occurred while updating episode info for series {0}", series.Title), e);
|
|
|
|
failCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_episodeRepository.InsertMany(newList);
|
|
|
|
_episodeRepository.UpdateMany(updateList);
|
|
|
|
|
|
|
|
if (failCount != 0)
|
|
|
|
{
|
|
|
|
logger.Info("Finished episode refresh for series: {0}. Successful: {1} - Failed: {2} ",
|
|
|
|
series.Title, successCount, failCount);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logger.Info("Finished episode refresh for series: {0}.", series.Title);
|
|
|
|
}
|
|
|
|
|
|
|
|
DeleteEpisodesNotInTvdb(series, tvdbEpisodes);
|
|
|
|
}
|
|
|
|
|
2013-03-02 19:13:23 +00:00
|
|
|
public void UpdateEpisode(Episode episode)
|
2013-02-20 02:05:15 +00:00
|
|
|
{
|
|
|
|
_episodeRepository.Update(episode);
|
|
|
|
}
|
|
|
|
|
2013-03-02 19:13:23 +00:00
|
|
|
public IList<int> GetEpisodeNumbersBySeason(int seriesId, int seasonNumber)
|
2013-02-20 02:05:15 +00:00
|
|
|
{
|
2013-02-26 03:58:57 +00:00
|
|
|
return GetEpisodesBySeason(seriesId, seasonNumber).Select(c => c.Id).ToList();
|
2013-02-20 02:05:15 +00:00
|
|
|
}
|
|
|
|
|
2013-03-02 19:13:23 +00:00
|
|
|
public void SetEpisodeIgnore(int episodeId, bool isIgnored)
|
2013-02-20 02:05:15 +00:00
|
|
|
{
|
|
|
|
var episode = _episodeRepository.Get(episodeId);
|
2013-03-01 07:03:41 +00:00
|
|
|
_episodeRepository.SetIgnoreFlat(episode, isIgnored);
|
2013-02-20 02:05:15 +00:00
|
|
|
|
|
|
|
logger.Info("Ignore flag for Episode:{0} was set to {1}", episodeId, isIgnored);
|
|
|
|
}
|
|
|
|
|
2013-03-02 19:13:23 +00:00
|
|
|
public bool IsFirstOrLastEpisodeOfSeason(int seriesId, int seasonNumber, int episodeNumber)
|
2013-02-20 02:05:15 +00:00
|
|
|
{
|
|
|
|
var episodes = GetEpisodesBySeason(seriesId, seasonNumber).OrderBy(e => e.EpisodeNumber);
|
|
|
|
|
|
|
|
if (!episodes.Any())
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-02-26 02:20:42 +00:00
|
|
|
private void DeleteEpisodesNotInTvdb(Series series, IEnumerable<Episode> tvdbEpisodes)
|
2013-02-20 02:05:15 +00:00
|
|
|
{
|
2013-02-26 07:47:29 +00:00
|
|
|
//Todo: This will not work as currently implemented - what are we trying to do here?
|
|
|
|
return;
|
2013-02-26 03:58:57 +00:00
|
|
|
logger.Trace("Starting deletion of episodes that no longer exist in TVDB: {0}", series.Title.WithDefault(series.Id));
|
2013-02-26 02:20:42 +00:00
|
|
|
foreach (var episode in tvdbEpisodes)
|
2013-02-20 02:05:15 +00:00
|
|
|
{
|
2013-02-26 03:58:57 +00:00
|
|
|
_episodeRepository.Delete(episode.Id);
|
2013-02-20 02:05:15 +00:00
|
|
|
}
|
|
|
|
|
2013-02-26 03:58:57 +00:00
|
|
|
logger.Trace("Deleted episodes that no longer exist in TVDB for {0}", series.Id);
|
2013-02-20 02:05:15 +00:00
|
|
|
}
|
|
|
|
|
2013-03-02 19:13:23 +00:00
|
|
|
public void SetPostDownloadStatus(List<int> episodeIds, PostDownloadStatusType postDownloadStatus)
|
2013-02-20 02:05:15 +00:00
|
|
|
{
|
|
|
|
if (episodeIds.Count == 0) throw new ArgumentException("episodeIds should contain one or more episode ids.");
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var episodeId in episodeIds)
|
|
|
|
{
|
|
|
|
var episode = _episodeRepository.Get(episodeId);
|
|
|
|
episode.PostDownloadStatus = postDownloadStatus;
|
|
|
|
_episodeRepository.Update(episode);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
logger.Trace("Updating PostDownloadStatus for {0} episode(s) to {1}", episodeIds.Count, postDownloadStatus);
|
|
|
|
}
|
|
|
|
|
2013-03-02 19:13:23 +00:00
|
|
|
public void UpdateEpisodes(List<Episode> episodes)
|
2013-02-20 02:05:15 +00:00
|
|
|
{
|
|
|
|
_episodeRepository.UpdateMany(episodes);
|
|
|
|
}
|
|
|
|
|
2013-03-02 19:13:23 +00:00
|
|
|
public Episode GetEpisodeBySceneNumbering(int seriesId, int seasonNumber, int episodeNumber)
|
2013-02-20 02:05:15 +00:00
|
|
|
{
|
|
|
|
return _episodeRepository.GetEpisodeBySceneNumbering(seriesId, seasonNumber, episodeNumber);
|
|
|
|
}
|
2013-02-23 23:08:22 +00:00
|
|
|
|
2013-02-26 03:30:24 +00:00
|
|
|
public List<Episode> EpisodesBetweenDates(DateTime start, DateTime end)
|
2013-02-23 23:08:22 +00:00
|
|
|
{
|
2013-02-26 03:30:24 +00:00
|
|
|
return _episodeRepository.EpisodesBetweenDates(start.ToUniversalTime(), end.ToUniversalTime());
|
2013-02-23 23:08:22 +00:00
|
|
|
}
|
2013-02-24 19:18:48 +00:00
|
|
|
|
|
|
|
public void Handle(EpisodeGrabbedEvent message)
|
|
|
|
{
|
|
|
|
foreach (var episode in message.ParseResult.Episodes)
|
|
|
|
{
|
2013-02-26 03:58:57 +00:00
|
|
|
logger.Trace("Marking episode {0} as fetched.", episode.Id);
|
2013-02-24 19:18:48 +00:00
|
|
|
episode.GrabDate = DateTime.UtcNow;
|
|
|
|
_episodeRepository.Update(episode);
|
|
|
|
}
|
|
|
|
}
|
2013-02-20 02:05:15 +00:00
|
|
|
}
|
|
|
|
}
|