2013-02-20 03:05:15 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
2013-04-24 03:56:00 +02:00
|
|
|
using NzbDrone.Common.Messaging;
|
2013-03-07 05:34:56 +01:00
|
|
|
using NzbDrone.Core.Configuration;
|
2013-05-02 07:50:34 +02:00
|
|
|
using NzbDrone.Core.Datastore;
|
2013-03-07 05:34:56 +01:00
|
|
|
using NzbDrone.Core.MediaFiles.Events;
|
2013-03-03 23:26:41 +01:00
|
|
|
using NzbDrone.Core.Tv.Events;
|
2013-02-20 03:05:15 +01:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Tv
|
|
|
|
{
|
|
|
|
public interface IEpisodeService
|
|
|
|
{
|
|
|
|
Episode GetEpisode(int id);
|
2013-04-15 03:41:39 +02:00
|
|
|
Episode GetEpisode(int seriesId, int seasonNumber, int episodeNumber, bool useScene = false);
|
2013-05-29 03:28:47 +02:00
|
|
|
Episode FindEpisode(int seriesId, int seasonNumber, int episodeNumber, bool useScene = false);
|
2013-02-20 03:05:15 +01:00
|
|
|
Episode GetEpisode(int seriesId, DateTime date);
|
2013-05-29 03:28:47 +02:00
|
|
|
Episode FindEpisode(int seriesId, DateTime date);
|
2013-03-02 20:13:23 +01:00
|
|
|
List<Episode> GetEpisodeBySeries(int seriesId);
|
2013-03-04 00:18:43 +01:00
|
|
|
List<Episode> GetEpisodesBySeason(int seriesId, int seasonNumber);
|
2013-05-13 08:12:19 +02:00
|
|
|
PagingSpec<Episode> EpisodesWithoutFiles(PagingSpec<Episode> pagingSpec);
|
2013-03-04 00:18:43 +01:00
|
|
|
List<Episode> GetEpisodesByFileId(int episodeFileId);
|
|
|
|
List<Episode> EpisodesWithFiles();
|
2013-02-20 03:05:15 +01:00
|
|
|
void UpdateEpisode(Episode episode);
|
2013-03-04 00:18:43 +01:00
|
|
|
List<int> GetEpisodeNumbersBySeason(int seriesId, int seasonNumber);
|
2013-02-20 03:05:15 +01:00
|
|
|
void SetEpisodeIgnore(int episodeId, bool isIgnored);
|
2013-04-15 03:41:39 +02:00
|
|
|
bool IsFirstOrLastEpisodeOfSeason(int episodeId);
|
2013-02-20 03:05:15 +01:00
|
|
|
void UpdateEpisodes(List<Episode> episodes);
|
2013-02-26 04:30:24 +01:00
|
|
|
List<Episode> EpisodesBetweenDates(DateTime start, DateTime end);
|
2013-06-02 21:29:00 +02:00
|
|
|
void InsertMany(List<Episode> episodes);
|
|
|
|
void UpdateMany(List<Episode> episodes);
|
2013-02-20 03:05:15 +01:00
|
|
|
}
|
|
|
|
|
2013-03-05 20:49:34 +01:00
|
|
|
public class EpisodeService : IEpisodeService,
|
2013-03-07 05:34:56 +01:00
|
|
|
IHandle<EpisodeFileDeletedEvent>,
|
2013-04-15 03:41:39 +02:00
|
|
|
IHandle<EpisodeFileAddedEvent>,
|
2013-06-02 21:29:00 +02:00
|
|
|
IHandleAsync<SeriesDeletedEvent>
|
2013-02-20 03:05:15 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
2013-02-24 21:24:31 +01:00
|
|
|
private readonly IEpisodeRepository _episodeRepository;
|
2013-03-07 05:34:56 +01:00
|
|
|
private readonly IConfigService _configService;
|
|
|
|
private readonly Logger _logger;
|
2013-02-20 03:05:15 +01:00
|
|
|
|
2013-06-02 21:29:00 +02:00
|
|
|
public EpisodeService(IEpisodeRepository episodeRepository, IConfigService configService, Logger logger)
|
2013-02-20 03:05:15 +01:00
|
|
|
{
|
|
|
|
_episodeRepository = episodeRepository;
|
2013-03-07 05:34:56 +01:00
|
|
|
_configService = configService;
|
|
|
|
_logger = logger;
|
2013-02-20 03:05:15 +01:00
|
|
|
}
|
|
|
|
|
2013-03-02 20:13:23 +01:00
|
|
|
public Episode GetEpisode(int id)
|
2013-02-20 03:05:15 +01:00
|
|
|
{
|
|
|
|
return _episodeRepository.Get(id);
|
|
|
|
}
|
|
|
|
|
2013-04-15 03:41:39 +02:00
|
|
|
public Episode GetEpisode(int seriesId, int seasonNumber, int episodeNumber, bool useSceneNumbering = false)
|
2013-02-20 03:05:15 +01:00
|
|
|
{
|
2013-04-15 03:41:39 +02:00
|
|
|
if (useSceneNumbering)
|
|
|
|
{
|
|
|
|
return _episodeRepository.GetEpisodeBySceneNumbering(seriesId, seasonNumber, episodeNumber);
|
|
|
|
}
|
2013-05-29 03:28:47 +02:00
|
|
|
return _episodeRepository.Find(seriesId, seasonNumber, episodeNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Episode FindEpisode(int seriesId, int seasonNumber, int episodeNumber, bool useSceneNumbering = false)
|
|
|
|
{
|
|
|
|
if (useSceneNumbering)
|
|
|
|
{
|
|
|
|
return _episodeRepository.FindEpisodeBySceneNumbering(seriesId, seasonNumber, episodeNumber);
|
|
|
|
}
|
|
|
|
return _episodeRepository.Find(seriesId, seasonNumber, episodeNumber);
|
2013-02-20 03:05:15 +01:00
|
|
|
}
|
|
|
|
|
2013-03-02 20:13:23 +01:00
|
|
|
public Episode GetEpisode(int seriesId, DateTime date)
|
2013-02-20 03:05:15 +01:00
|
|
|
{
|
|
|
|
return _episodeRepository.Get(seriesId, date);
|
|
|
|
}
|
|
|
|
|
2013-05-29 03:28:47 +02:00
|
|
|
public Episode FindEpisode(int seriesId, DateTime date)
|
|
|
|
{
|
|
|
|
return _episodeRepository.Find(seriesId, date);
|
|
|
|
}
|
|
|
|
|
2013-03-02 20:13:23 +01:00
|
|
|
public List<Episode> GetEpisodeBySeries(int seriesId)
|
2013-02-20 03:05:15 +01:00
|
|
|
{
|
2013-03-02 20:13:23 +01:00
|
|
|
return _episodeRepository.GetEpisodes(seriesId).ToList();
|
2013-02-20 03:05:15 +01:00
|
|
|
}
|
|
|
|
|
2013-03-04 00:18:43 +01:00
|
|
|
public List<Episode> GetEpisodesBySeason(int seriesId, int seasonNumber)
|
2013-02-20 03:05:15 +01:00
|
|
|
{
|
|
|
|
return _episodeRepository.GetEpisodes(seriesId, seasonNumber);
|
|
|
|
}
|
|
|
|
|
2013-05-13 08:12:19 +02:00
|
|
|
public PagingSpec<Episode> EpisodesWithoutFiles(PagingSpec<Episode> pagingSpec)
|
2013-02-20 03:05:15 +01:00
|
|
|
{
|
2013-05-13 08:12:19 +02:00
|
|
|
var episodeResult = _episodeRepository.EpisodesWithoutFiles(pagingSpec, false);
|
2013-05-01 05:04:06 +02:00
|
|
|
|
2013-05-02 07:50:34 +02:00
|
|
|
return episodeResult;
|
2013-02-20 03:05:15 +01:00
|
|
|
}
|
|
|
|
|
2013-03-04 00:18:43 +01:00
|
|
|
public List<Episode> GetEpisodesByFileId(int episodeFileId)
|
2013-02-20 03:05:15 +01:00
|
|
|
{
|
|
|
|
return _episodeRepository.GetEpisodeByFileId(episodeFileId);
|
|
|
|
}
|
|
|
|
|
2013-03-04 00:18:43 +01:00
|
|
|
public List<Episode> EpisodesWithFiles()
|
2013-02-20 03:05:15 +01:00
|
|
|
{
|
|
|
|
return _episodeRepository.EpisodesWithFiles();
|
|
|
|
}
|
|
|
|
|
2013-04-20 19:03:19 +02:00
|
|
|
|
2013-02-20 03:05:15 +01:00
|
|
|
|
2013-03-02 20:13:23 +01:00
|
|
|
public void UpdateEpisode(Episode episode)
|
2013-02-20 03:05:15 +01:00
|
|
|
{
|
|
|
|
_episodeRepository.Update(episode);
|
|
|
|
}
|
|
|
|
|
2013-03-04 00:18:43 +01:00
|
|
|
public List<int> GetEpisodeNumbersBySeason(int seriesId, int seasonNumber)
|
2013-02-20 03:05:15 +01:00
|
|
|
{
|
2013-02-26 04:58:57 +01:00
|
|
|
return GetEpisodesBySeason(seriesId, seasonNumber).Select(c => c.Id).ToList();
|
2013-02-20 03:05:15 +01:00
|
|
|
}
|
|
|
|
|
2013-03-02 20:13:23 +01:00
|
|
|
public void SetEpisodeIgnore(int episodeId, bool isIgnored)
|
2013-02-20 03:05:15 +01:00
|
|
|
{
|
|
|
|
var episode = _episodeRepository.Get(episodeId);
|
2013-03-01 08:03:41 +01:00
|
|
|
_episodeRepository.SetIgnoreFlat(episode, isIgnored);
|
2013-02-20 03:05:15 +01:00
|
|
|
|
|
|
|
logger.Info("Ignore flag for Episode:{0} was set to {1}", episodeId, isIgnored);
|
|
|
|
}
|
|
|
|
|
2013-04-15 03:41:39 +02:00
|
|
|
public bool IsFirstOrLastEpisodeOfSeason(int episodeId)
|
2013-02-20 03:05:15 +01:00
|
|
|
{
|
2013-04-15 03:41:39 +02:00
|
|
|
var episode = GetEpisode(episodeId);
|
|
|
|
var seasonEpisodes = GetEpisodesBySeason(episode.SeriesId, episode.SeasonNumber);
|
2013-02-20 03:05:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
//Ensure that this is either the first episode
|
|
|
|
//or is the last episode in a season that has 10 or more episodes
|
2013-04-15 03:41:39 +02:00
|
|
|
if (seasonEpisodes.First().EpisodeNumber == episode.EpisodeNumber || (seasonEpisodes.Count() >= 10 && seasonEpisodes.Last().EpisodeNumber == episode.EpisodeNumber))
|
2013-02-20 03:05:15 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-02 20:13:23 +01:00
|
|
|
public void UpdateEpisodes(List<Episode> episodes)
|
2013-02-20 03:05:15 +01:00
|
|
|
{
|
|
|
|
_episodeRepository.UpdateMany(episodes);
|
|
|
|
}
|
|
|
|
|
2013-02-26 04:30:24 +01:00
|
|
|
public List<Episode> EpisodesBetweenDates(DateTime start, DateTime end)
|
2013-02-24 00:08:22 +01:00
|
|
|
{
|
2013-04-17 08:55:39 +02:00
|
|
|
var episodes = _episodeRepository.EpisodesBetweenDates(start.ToUniversalTime(), end.ToUniversalTime());
|
|
|
|
|
2013-05-30 06:06:24 +02:00
|
|
|
return episodes;
|
2013-02-24 00:08:22 +01:00
|
|
|
}
|
2013-02-24 20:18:48 +01:00
|
|
|
|
2013-06-02 21:29:00 +02:00
|
|
|
public void InsertMany(List<Episode> episodes)
|
|
|
|
{
|
|
|
|
_episodeRepository.InsertMany(episodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UpdateMany(List<Episode> episodes)
|
|
|
|
{
|
2013-06-03 05:56:17 +02:00
|
|
|
_episodeRepository.UpdateMany(episodes);
|
2013-06-02 21:29:00 +02:00
|
|
|
}
|
|
|
|
|
2013-03-05 20:49:34 +01:00
|
|
|
public void HandleAsync(SeriesDeletedEvent message)
|
|
|
|
{
|
|
|
|
var episodes = GetEpisodeBySeries(message.Series.Id);
|
|
|
|
_episodeRepository.DeleteMany(episodes);
|
|
|
|
}
|
2013-03-07 05:34:56 +01:00
|
|
|
|
|
|
|
public void Handle(EpisodeFileDeletedEvent message)
|
|
|
|
{
|
|
|
|
foreach (var episode in GetEpisodesByFileId(message.EpisodeFile.Id))
|
|
|
|
{
|
|
|
|
_logger.Trace("Detaching episode {0} from file.", episode.Id);
|
2013-05-13 02:36:23 +02:00
|
|
|
episode.EpisodeFileId = 0;
|
2013-03-07 05:34:56 +01:00
|
|
|
episode.Ignored = _configService.AutoIgnorePreviouslyDownloadedEpisodes;
|
|
|
|
UpdateEpisode(episode);
|
|
|
|
}
|
|
|
|
}
|
2013-03-30 23:43:19 +01:00
|
|
|
|
2013-04-15 03:41:39 +02:00
|
|
|
public void Handle(EpisodeFileAddedEvent message)
|
|
|
|
{
|
|
|
|
foreach (var episode in message.EpisodeFile.Episodes.Value)
|
|
|
|
{
|
|
|
|
_episodeRepository.SetFileId(episode.Id, message.EpisodeFile.Id);
|
|
|
|
_logger.Debug("Linking [{0}] > [{1}]", message.EpisodeFile.Path, episode);
|
|
|
|
}
|
|
|
|
}
|
2013-04-20 19:03:19 +02:00
|
|
|
|
|
|
|
|
2013-02-20 03:05:15 +01:00
|
|
|
}
|
|
|
|
}
|