2013-02-20 05:05:15 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
2013-03-07 07:34:56 +03:00
|
|
|
using NzbDrone.Core.Configuration;
|
2013-05-02 08:50:34 +03:00
|
|
|
using NzbDrone.Core.Datastore;
|
2013-03-07 07:34:56 +03:00
|
|
|
using NzbDrone.Core.MediaFiles.Events;
|
2013-09-14 09:36:07 +03:00
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2013-03-04 01:26:41 +03:00
|
|
|
using NzbDrone.Core.Tv.Events;
|
2013-02-20 05:05:15 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Tv
|
|
|
|
{
|
|
|
|
public interface IEpisodeService
|
|
|
|
{
|
|
|
|
Episode GetEpisode(int id);
|
2014-02-27 09:51:41 +03:00
|
|
|
Episode FindEpisode(int seriesId, int seasonNumber, int episodeNumber);
|
2013-11-08 03:24:09 +03:00
|
|
|
Episode FindEpisode(int seriesId, int absoluteEpisodeNumber);
|
2014-01-07 11:24:50 +03:00
|
|
|
Episode FindEpisodeByName(int seriesId, int seasonNumber, string episodeTitle);
|
2014-02-27 09:51:41 +03:00
|
|
|
List<Episode> FindEpisodesBySceneNumbering(int seriesId, int seasonNumber, int episodeNumber);
|
2014-05-19 22:14:41 +03:00
|
|
|
Episode FindEpisodeBySceneNumbering(int seriesId, int sceneAbsoluteEpisodeNumber);
|
2013-10-23 08:17:02 +03:00
|
|
|
Episode GetEpisode(int seriesId, String date);
|
|
|
|
Episode FindEpisode(int seriesId, String date);
|
2013-03-02 22:13:23 +03:00
|
|
|
List<Episode> GetEpisodeBySeries(int seriesId);
|
2013-03-04 02:18:43 +03:00
|
|
|
List<Episode> GetEpisodesBySeason(int seriesId, int seasonNumber);
|
2014-03-06 12:46:19 +03:00
|
|
|
List<Episode> EpisodesWithFiles(int seriesId);
|
2014-02-13 22:02:58 +03:00
|
|
|
PagingSpec<Episode> EpisodesWithoutFiles(PagingSpec<Episode> pagingSpec);
|
2013-03-04 02:18:43 +03:00
|
|
|
List<Episode> GetEpisodesByFileId(int episodeFileId);
|
2013-02-20 05:05:15 +03:00
|
|
|
void UpdateEpisode(Episode episode);
|
2013-07-09 04:22:02 +03:00
|
|
|
void SetEpisodeMonitored(int episodeId, bool monitored);
|
2013-02-20 05:05:15 +03:00
|
|
|
void UpdateEpisodes(List<Episode> episodes);
|
2013-02-26 06:30:24 +03:00
|
|
|
List<Episode> EpisodesBetweenDates(DateTime start, DateTime end);
|
2013-06-02 22:29:00 +03:00
|
|
|
void InsertMany(List<Episode> episodes);
|
|
|
|
void UpdateMany(List<Episode> episodes);
|
2013-08-01 05:02:36 +03:00
|
|
|
void DeleteMany(List<Episode> episodes);
|
2013-08-07 04:25:06 +03:00
|
|
|
void SetEpisodeMonitoredBySeason(int seriesId, int seasonNumber, bool monitored);
|
2013-02-20 05:05:15 +03:00
|
|
|
}
|
|
|
|
|
2013-03-05 22:49:34 +03:00
|
|
|
public class EpisodeService : IEpisodeService,
|
2013-03-07 07:34:56 +03:00
|
|
|
IHandle<EpisodeFileDeletedEvent>,
|
2013-04-15 04:41:39 +03:00
|
|
|
IHandle<EpisodeFileAddedEvent>,
|
2013-06-02 22:29:00 +03:00
|
|
|
IHandleAsync<SeriesDeletedEvent>
|
2013-02-20 05:05:15 +03:00
|
|
|
{
|
2013-02-24 23:24:31 +03:00
|
|
|
private readonly IEpisodeRepository _episodeRepository;
|
2013-03-07 07:34:56 +03:00
|
|
|
private readonly IConfigService _configService;
|
|
|
|
private readonly Logger _logger;
|
2013-02-20 05:05:15 +03:00
|
|
|
|
2014-02-27 09:51:41 +03:00
|
|
|
public EpisodeService(IEpisodeRepository episodeRepository, IConfigService configService, Logger logger)
|
2013-02-20 05:05:15 +03:00
|
|
|
{
|
|
|
|
_episodeRepository = episodeRepository;
|
2013-03-07 07:34:56 +03:00
|
|
|
_configService = configService;
|
|
|
|
_logger = logger;
|
2013-02-20 05:05:15 +03:00
|
|
|
}
|
|
|
|
|
2013-03-02 22:13:23 +03:00
|
|
|
public Episode GetEpisode(int id)
|
2013-02-20 05:05:15 +03:00
|
|
|
{
|
|
|
|
return _episodeRepository.Get(id);
|
|
|
|
}
|
|
|
|
|
2014-02-27 09:51:41 +03:00
|
|
|
public Episode FindEpisode(int seriesId, int seasonNumber, int episodeNumber)
|
2013-05-29 04:28:47 +03:00
|
|
|
{
|
|
|
|
return _episodeRepository.Find(seriesId, seasonNumber, episodeNumber);
|
2013-02-20 05:05:15 +03:00
|
|
|
}
|
|
|
|
|
2013-11-08 03:24:09 +03:00
|
|
|
public Episode FindEpisode(int seriesId, int absoluteEpisodeNumber)
|
|
|
|
{
|
|
|
|
return _episodeRepository.Find(seriesId, absoluteEpisodeNumber);
|
|
|
|
}
|
|
|
|
|
2014-02-27 09:51:41 +03:00
|
|
|
public List<Episode> FindEpisodesBySceneNumbering(int seriesId, int seasonNumber, int episodeNumber)
|
|
|
|
{
|
|
|
|
return _episodeRepository.FindEpisodesBySceneNumbering(seriesId, seasonNumber, episodeNumber);
|
|
|
|
}
|
|
|
|
|
2014-05-19 22:14:41 +03:00
|
|
|
public Episode FindEpisodeBySceneNumbering(int seriesId, int sceneAbsoluteEpisodeNumber)
|
|
|
|
{
|
|
|
|
return _episodeRepository.FindEpisodeBySceneNumbering(seriesId, sceneAbsoluteEpisodeNumber);
|
|
|
|
}
|
|
|
|
|
2013-10-23 08:17:02 +03:00
|
|
|
public Episode GetEpisode(int seriesId, String date)
|
2013-02-20 05:05:15 +03:00
|
|
|
{
|
|
|
|
return _episodeRepository.Get(seriesId, date);
|
|
|
|
}
|
|
|
|
|
2013-10-23 08:17:02 +03:00
|
|
|
public Episode FindEpisode(int seriesId, String date)
|
2013-05-29 04:28:47 +03:00
|
|
|
{
|
|
|
|
return _episodeRepository.Find(seriesId, date);
|
|
|
|
}
|
|
|
|
|
2013-03-02 22:13:23 +03:00
|
|
|
public List<Episode> GetEpisodeBySeries(int seriesId)
|
2013-02-20 05:05:15 +03:00
|
|
|
{
|
2013-03-02 22:13:23 +03:00
|
|
|
return _episodeRepository.GetEpisodes(seriesId).ToList();
|
2013-02-20 05:05:15 +03:00
|
|
|
}
|
|
|
|
|
2013-03-04 02:18:43 +03:00
|
|
|
public List<Episode> GetEpisodesBySeason(int seriesId, int seasonNumber)
|
2013-02-20 05:05:15 +03:00
|
|
|
{
|
|
|
|
return _episodeRepository.GetEpisodes(seriesId, seasonNumber);
|
|
|
|
}
|
2014-02-09 22:03:49 +03:00
|
|
|
|
2014-01-07 11:24:50 +03:00
|
|
|
public Episode FindEpisodeByName(int seriesId, int seasonNumber, string episodeTitle)
|
|
|
|
{
|
|
|
|
// TODO: can replace this search mechanism with something smarter/faster/better
|
|
|
|
var search = Parser.Parser.NormalizeEpisodeTitle(episodeTitle);
|
|
|
|
return _episodeRepository.GetEpisodes(seriesId, seasonNumber)
|
|
|
|
.FirstOrDefault(e =>
|
|
|
|
{
|
|
|
|
// normalize episode title
|
|
|
|
string title = Parser.Parser.NormalizeEpisodeTitle(e.Title);
|
|
|
|
// find episode title within search string
|
|
|
|
return (title.Length > 0) && search.Contains(title);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-03-06 12:46:19 +03:00
|
|
|
public List<Episode> EpisodesWithFiles(int seriesId)
|
|
|
|
{
|
|
|
|
return _episodeRepository.EpisodesWithFiles(seriesId);
|
|
|
|
}
|
|
|
|
|
2013-05-13 09:12:19 +03:00
|
|
|
public PagingSpec<Episode> EpisodesWithoutFiles(PagingSpec<Episode> pagingSpec)
|
2013-02-20 05:05:15 +03:00
|
|
|
{
|
2014-02-13 22:02:58 +03:00
|
|
|
var episodeResult = _episodeRepository.EpisodesWithoutFiles(pagingSpec, false);
|
2013-05-01 06:04:06 +03:00
|
|
|
|
2013-05-02 08:50:34 +03:00
|
|
|
return episodeResult;
|
2013-02-20 05:05:15 +03:00
|
|
|
}
|
|
|
|
|
2013-03-04 02:18:43 +03:00
|
|
|
public List<Episode> GetEpisodesByFileId(int episodeFileId)
|
2013-02-20 05:05:15 +03:00
|
|
|
{
|
|
|
|
return _episodeRepository.GetEpisodeByFileId(episodeFileId);
|
|
|
|
}
|
|
|
|
|
2013-03-02 22:13:23 +03:00
|
|
|
public void UpdateEpisode(Episode episode)
|
2013-02-20 05:05:15 +03:00
|
|
|
{
|
|
|
|
_episodeRepository.Update(episode);
|
|
|
|
}
|
|
|
|
|
2013-07-09 04:22:02 +03:00
|
|
|
public void SetEpisodeMonitored(int episodeId, bool monitored)
|
2013-02-20 05:05:15 +03:00
|
|
|
{
|
|
|
|
var episode = _episodeRepository.Get(episodeId);
|
2013-07-09 04:22:02 +03:00
|
|
|
_episodeRepository.SetMonitoredFlat(episode, monitored);
|
2013-02-20 05:05:15 +03:00
|
|
|
|
2013-09-17 08:16:16 +03:00
|
|
|
_logger.Debug("Monitored flag for Episode:{0} was set to {1}", episodeId, monitored);
|
2013-02-20 05:05:15 +03:00
|
|
|
}
|
|
|
|
|
2013-07-10 05:11:00 +03:00
|
|
|
public void SetEpisodeMonitoredBySeason(int seriesId, int seasonNumber, bool monitored)
|
|
|
|
{
|
|
|
|
_episodeRepository.SetMonitoredBySeason(seriesId, seasonNumber, monitored);
|
|
|
|
}
|
|
|
|
|
2013-03-02 22:13:23 +03:00
|
|
|
public void UpdateEpisodes(List<Episode> episodes)
|
2013-02-20 05:05:15 +03:00
|
|
|
{
|
|
|
|
_episodeRepository.UpdateMany(episodes);
|
|
|
|
}
|
|
|
|
|
2013-02-26 06:30:24 +03:00
|
|
|
public List<Episode> EpisodesBetweenDates(DateTime start, DateTime end)
|
2013-02-24 02:08:22 +03:00
|
|
|
{
|
2013-04-17 09:55:39 +03:00
|
|
|
var episodes = _episodeRepository.EpisodesBetweenDates(start.ToUniversalTime(), end.ToUniversalTime());
|
|
|
|
|
2013-05-30 07:06:24 +03:00
|
|
|
return episodes;
|
2013-02-24 02:08:22 +03:00
|
|
|
}
|
2013-02-24 22:18:48 +03:00
|
|
|
|
2013-06-02 22:29:00 +03:00
|
|
|
public void InsertMany(List<Episode> episodes)
|
|
|
|
{
|
|
|
|
_episodeRepository.InsertMany(episodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UpdateMany(List<Episode> episodes)
|
|
|
|
{
|
2013-06-03 06:56:17 +03:00
|
|
|
_episodeRepository.UpdateMany(episodes);
|
2013-06-02 22:29:00 +03:00
|
|
|
}
|
|
|
|
|
2013-08-01 05:02:36 +03:00
|
|
|
public void DeleteMany(List<Episode> episodes)
|
|
|
|
{
|
|
|
|
_episodeRepository.DeleteMany(episodes);
|
|
|
|
}
|
|
|
|
|
2013-03-05 22:49:34 +03:00
|
|
|
public void HandleAsync(SeriesDeletedEvent message)
|
|
|
|
{
|
|
|
|
var episodes = GetEpisodeBySeries(message.Series.Id);
|
|
|
|
_episodeRepository.DeleteMany(episodes);
|
|
|
|
}
|
2013-03-07 07:34:56 +03:00
|
|
|
|
|
|
|
public void Handle(EpisodeFileDeletedEvent message)
|
|
|
|
{
|
|
|
|
foreach (var episode in GetEpisodesByFileId(message.EpisodeFile.Id))
|
|
|
|
{
|
2014-03-13 23:12:42 +03:00
|
|
|
_logger.Debug("Detaching episode {0} from file.", episode.Id);
|
2013-05-13 03:36:23 +03:00
|
|
|
episode.EpisodeFileId = 0;
|
2013-07-17 06:01:38 +03:00
|
|
|
|
2013-07-23 03:50:37 +03:00
|
|
|
if (!message.ForUpgrade && _configService.AutoUnmonitorPreviouslyDownloadedEpisodes)
|
2013-07-17 06:01:38 +03:00
|
|
|
{
|
|
|
|
episode.Monitored = false;
|
|
|
|
}
|
|
|
|
|
2013-03-07 07:34:56 +03:00
|
|
|
UpdateEpisode(episode);
|
|
|
|
}
|
|
|
|
}
|
2013-03-31 01:43:19 +03:00
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
public void Handle(EpisodeFileAddedEvent message)
|
|
|
|
{
|
|
|
|
foreach (var episode in message.EpisodeFile.Episodes.Value)
|
|
|
|
{
|
|
|
|
_episodeRepository.SetFileId(episode.Id, message.EpisodeFile.Id);
|
2014-07-24 02:43:54 +03:00
|
|
|
_logger.Debug("Linking [{0}] > [{1}]", message.EpisodeFile.RelativePath, episode);
|
2013-04-15 04:41:39 +03:00
|
|
|
}
|
|
|
|
}
|
2013-02-20 05:05:15 +03:00
|
|
|
}
|
|
|
|
}
|