1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-23 02:05:27 +02:00
Sonarr/src/NzbDrone.Core/Tv/EpisodeService.cs

213 lines
7.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using NLog;
using NzbDrone.Core.Configuration;
2013-05-02 08:50:34 +03:00
using NzbDrone.Core.Datastore;
using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.MediaFiles.Events;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Tv.Events;
namespace NzbDrone.Core.Tv
{
public interface IEpisodeService
{
Episode GetEpisode(int id);
List<Episode> GetEpisodes(IEnumerable<Int32> ids);
Episode FindEpisode(int seriesId, int seasonNumber, int episodeNumber);
Episode FindEpisode(int seriesId, int absoluteEpisodeNumber);
Episode FindEpisodeByName(int seriesId, int seasonNumber, string episodeTitle);
List<Episode> FindEpisodesBySceneNumbering(int seriesId, int seasonNumber, int episodeNumber);
Episode FindEpisodeBySceneNumbering(int seriesId, int sceneAbsoluteEpisodeNumber);
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);
List<Episode> GetEpisodesBySeason(int seriesId, int seasonNumber);
List<Episode> EpisodesWithFiles(int seriesId);
PagingSpec<Episode> EpisodesWithoutFiles(PagingSpec<Episode> pagingSpec);
List<Episode> GetEpisodesByFileId(int episodeFileId);
void UpdateEpisode(Episode episode);
void SetEpisodeMonitored(int episodeId, bool monitored);
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);
void SetEpisodeMonitoredBySeason(int seriesId, int seasonNumber, bool monitored);
}
public class EpisodeService : IEpisodeService,
IHandle<EpisodeFileDeletedEvent>,
IHandle<EpisodeFileAddedEvent>,
IHandleAsync<SeriesDeletedEvent>
{
2013-02-24 23:24:31 +03:00
private readonly IEpisodeRepository _episodeRepository;
private readonly IConfigService _configService;
private readonly Logger _logger;
public EpisodeService(IEpisodeRepository episodeRepository, IConfigService configService, Logger logger)
{
_episodeRepository = episodeRepository;
_configService = configService;
_logger = logger;
}
2013-03-02 22:13:23 +03:00
public Episode GetEpisode(int id)
{
return _episodeRepository.Get(id);
}
public List<Episode> GetEpisodes(IEnumerable<int> ids)
{
return _episodeRepository.Get(ids).ToList();
}
public Episode FindEpisode(int seriesId, int seasonNumber, int episodeNumber)
{
return _episodeRepository.Find(seriesId, seasonNumber, episodeNumber);
}
public Episode FindEpisode(int seriesId, int absoluteEpisodeNumber)
{
return _episodeRepository.Find(seriesId, absoluteEpisodeNumber);
}
public List<Episode> FindEpisodesBySceneNumbering(int seriesId, int seasonNumber, int episodeNumber)
{
return _episodeRepository.FindEpisodesBySceneNumbering(seriesId, seasonNumber, episodeNumber);
}
public Episode FindEpisodeBySceneNumbering(int seriesId, int sceneAbsoluteEpisodeNumber)
{
return _episodeRepository.FindEpisodeBySceneNumbering(seriesId, sceneAbsoluteEpisodeNumber);
}
public Episode GetEpisode(int seriesId, String date)
{
return _episodeRepository.Get(seriesId, date);
}
public Episode FindEpisode(int seriesId, String date)
{
return _episodeRepository.Find(seriesId, date);
}
2013-03-02 22:13:23 +03:00
public List<Episode> GetEpisodeBySeries(int seriesId)
{
2013-03-02 22:13:23 +03:00
return _episodeRepository.GetEpisodes(seriesId).ToList();
}
public List<Episode> GetEpisodesBySeason(int seriesId, int seasonNumber)
{
return _episodeRepository.GetEpisodes(seriesId, seasonNumber);
}
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);
});
}
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)
{
var episodeResult = _episodeRepository.EpisodesWithoutFiles(pagingSpec, false);
2013-05-02 08:50:34 +03:00
return episodeResult;
}
public List<Episode> GetEpisodesByFileId(int episodeFileId)
{
return _episodeRepository.GetEpisodeByFileId(episodeFileId);
}
2013-03-02 22:13:23 +03:00
public void UpdateEpisode(Episode episode)
{
_episodeRepository.Update(episode);
}
public void SetEpisodeMonitored(int episodeId, bool monitored)
{
var episode = _episodeRepository.Get(episodeId);
_episodeRepository.SetMonitoredFlat(episode, monitored);
2013-09-17 08:16:16 +03:00
_logger.Debug("Monitored flag for Episode:{0} was set to {1}", episodeId, monitored);
}
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)
{
_episodeRepository.UpdateMany(episodes);
}
2013-02-26 06:30:24 +03:00
public List<Episode> EpisodesBetweenDates(DateTime start, DateTime end)
{
var episodes = _episodeRepository.EpisodesBetweenDates(start.ToUniversalTime(), end.ToUniversalTime());
2013-05-30 07:06:24 +03:00
return episodes;
}
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);
}
public void HandleAsync(SeriesDeletedEvent message)
{
var episodes = GetEpisodeBySeries(message.Series.Id);
_episodeRepository.DeleteMany(episodes);
}
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);
episode.EpisodeFileId = 0;
if (message.Reason != DeleteMediaFileReason.Upgrade && _configService.AutoUnmonitorPreviouslyDownloadedEpisodes)
{
episode.Monitored = false;
}
UpdateEpisode(episode);
}
}
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);
}
}
}
}