2013-06-02 19:29:00 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Common.Messaging;
|
2013-07-19 01:52:31 +00:00
|
|
|
|
using NzbDrone.Core.DataAugmentation.DailySeries;
|
2013-06-02 19:29:00 +00:00
|
|
|
|
using NzbDrone.Core.MetadataSource;
|
2013-06-03 05:39:42 +00:00
|
|
|
|
using NzbDrone.Core.Tv.Commands;
|
2013-06-02 19:29:00 +00:00
|
|
|
|
using NzbDrone.Core.Tv.Events;
|
2013-06-03 05:51:16 +00:00
|
|
|
|
using NzbDrone.Common;
|
2013-06-02 19:29:00 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Tv
|
|
|
|
|
{
|
|
|
|
|
public class RefreshSeriesService : IExecute<RefreshSeriesCommand>, IHandleAsync<SeriesAddedEvent>
|
|
|
|
|
{
|
|
|
|
|
private readonly IProvideSeriesInfo _seriesInfo;
|
|
|
|
|
private readonly ISeriesService _seriesService;
|
|
|
|
|
private readonly IEpisodeService _episodeService;
|
|
|
|
|
private readonly ISeasonRepository _seasonRepository;
|
|
|
|
|
private readonly IMessageAggregator _messageAggregator;
|
2013-07-19 01:52:31 +00:00
|
|
|
|
private readonly IDailySeriesService _dailySeriesService;
|
2013-06-02 19:29:00 +00:00
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
|
|
public RefreshSeriesService(IProvideSeriesInfo seriesInfo, ISeriesService seriesService, IEpisodeService episodeService,
|
2013-07-19 01:52:31 +00:00
|
|
|
|
ISeasonRepository seasonRepository, IMessageAggregator messageAggregator, IDailySeriesService dailySeriesService, Logger logger)
|
2013-06-02 19:29:00 +00:00
|
|
|
|
{
|
|
|
|
|
_seriesInfo = seriesInfo;
|
|
|
|
|
_seriesService = seriesService;
|
|
|
|
|
_episodeService = episodeService;
|
|
|
|
|
_seasonRepository = seasonRepository;
|
|
|
|
|
_messageAggregator = messageAggregator;
|
2013-07-19 01:52:31 +00:00
|
|
|
|
_dailySeriesService = dailySeriesService;
|
2013-06-02 19:29:00 +00:00
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Execute(RefreshSeriesCommand message)
|
|
|
|
|
{
|
|
|
|
|
if (message.SeriesId.HasValue)
|
|
|
|
|
{
|
2013-06-03 05:39:42 +00:00
|
|
|
|
var series = _seriesService.GetSeries(message.SeriesId.Value);
|
|
|
|
|
RefreshSeriesInfo(series);
|
2013-06-02 19:29:00 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-06-03 05:39:42 +00:00
|
|
|
|
var allSeries = _seriesService.GetAllSeries().OrderBy(c => c.LastInfoSync).ToList();
|
2013-06-02 19:29:00 +00:00
|
|
|
|
|
2013-06-03 05:39:42 +00:00
|
|
|
|
foreach (var series in allSeries)
|
2013-06-02 19:29:00 +00:00
|
|
|
|
{
|
2013-06-03 05:51:16 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
RefreshSeriesInfo(series);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2013-07-05 05:17:25 +00:00
|
|
|
|
_logger.ErrorException("Couldn't refresh info for {0}".Inject(series), e);
|
2013-06-03 05:51:16 +00:00
|
|
|
|
}
|
2013-06-02 19:29:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void HandleAsync(SeriesAddedEvent message)
|
|
|
|
|
{
|
2013-06-03 05:39:42 +00:00
|
|
|
|
RefreshSeriesInfo(message.Series);
|
2013-06-02 19:29:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-03 05:39:42 +00:00
|
|
|
|
private void RefreshSeriesInfo(Series series)
|
2013-06-02 19:29:00 +00:00
|
|
|
|
{
|
|
|
|
|
var tuple = _seriesInfo.GetSeriesInfo(series.TvdbId);
|
|
|
|
|
|
|
|
|
|
var seriesInfo = tuple.Item1;
|
|
|
|
|
|
|
|
|
|
series.Title = seriesInfo.Title;
|
|
|
|
|
series.AirTime = seriesInfo.AirTime;
|
|
|
|
|
series.Overview = seriesInfo.Overview;
|
|
|
|
|
series.Status = seriesInfo.Status;
|
2013-06-08 19:14:52 +00:00
|
|
|
|
series.CleanTitle = Parser.Parser.CleanSeriesTitle(seriesInfo.Title);
|
2013-06-03 05:39:42 +00:00
|
|
|
|
series.LastInfoSync = DateTime.UtcNow;
|
2013-06-02 19:29:00 +00:00
|
|
|
|
series.Runtime = seriesInfo.Runtime;
|
|
|
|
|
series.Images = seriesInfo.Images;
|
|
|
|
|
series.Network = seriesInfo.Network;
|
|
|
|
|
series.FirstAired = seriesInfo.FirstAired;
|
2013-07-19 01:52:31 +00:00
|
|
|
|
|
|
|
|
|
if (_dailySeriesService.IsDailySeries(series.TvdbId))
|
|
|
|
|
{
|
|
|
|
|
series.SeriesType = SeriesTypes.Daily;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-02 19:29:00 +00:00
|
|
|
|
_seriesService.UpdateSeries(series);
|
|
|
|
|
|
2013-06-08 00:37:31 +00:00
|
|
|
|
RefreshEpisodeInfo(series, tuple.Item2);
|
2013-06-02 19:29:00 +00:00
|
|
|
|
|
|
|
|
|
_messageAggregator.PublishEvent(new SeriesUpdatedEvent(series));
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-03 05:39:42 +00:00
|
|
|
|
private void RefreshEpisodeInfo(Series series, IEnumerable<Episode> remoteEpisodes)
|
2013-06-02 19:29:00 +00:00
|
|
|
|
{
|
2013-07-05 05:17:25 +00:00
|
|
|
|
_logger.Info("Starting series info refresh for: {0}", series);
|
2013-06-02 19:29:00 +00:00
|
|
|
|
var successCount = 0;
|
|
|
|
|
var failCount = 0;
|
|
|
|
|
|
|
|
|
|
var seriesEpisodes = _episodeService.GetEpisodeBySeries(series.Id);
|
|
|
|
|
var seasons = _seasonRepository.GetSeasonBySeries(series.Id);
|
|
|
|
|
|
|
|
|
|
var updateList = new List<Episode>();
|
|
|
|
|
var newList = new List<Episode>();
|
|
|
|
|
|
|
|
|
|
foreach (var episode in remoteEpisodes.OrderBy(e => e.SeasonNumber).ThenBy(e => e.EpisodeNumber))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-07-17 00:53:33 +00:00
|
|
|
|
var episodeToUpdate = seriesEpisodes.SingleOrDefault(e => e.TvDbEpisodeId == episode.TvDbEpisodeId) ??
|
|
|
|
|
seriesEpisodes.SingleOrDefault(e => e.SeasonNumber == episode.SeasonNumber && e.EpisodeNumber == episode.EpisodeNumber);
|
2013-06-02 19:29:00 +00:00
|
|
|
|
|
|
|
|
|
if (episodeToUpdate == null)
|
|
|
|
|
{
|
|
|
|
|
episodeToUpdate = new Episode();
|
|
|
|
|
newList.Add(episodeToUpdate);
|
|
|
|
|
|
2013-07-12 02:49:02 +00:00
|
|
|
|
//If it is Episode Zero or Season zero ignore it
|
|
|
|
|
if ((episode.EpisodeNumber == 0 && episode.SeasonNumber != 1) || episode.SeasonNumber == 0)
|
2013-06-02 19:29:00 +00:00
|
|
|
|
{
|
2013-07-09 01:22:02 +00:00
|
|
|
|
episodeToUpdate.Monitored = false;
|
2013-06-02 19:29:00 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var season = seasons.FirstOrDefault(c => c.SeasonNumber == episode.SeasonNumber);
|
2013-07-17 00:53:33 +00:00
|
|
|
|
episodeToUpdate.Monitored = season == null || season.Monitored;
|
2013-06-02 19:29:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
updateList.Add(episodeToUpdate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((episodeToUpdate.EpisodeNumber != episode.EpisodeNumber ||
|
|
|
|
|
episodeToUpdate.SeasonNumber != episode.SeasonNumber) &&
|
|
|
|
|
episodeToUpdate.EpisodeFileId > 0)
|
|
|
|
|
{
|
2013-06-03 03:44:31 +00:00
|
|
|
|
_logger.Debug("Un-linking episode file because the episode number has changed");
|
2013-06-02 19:29:00 +00:00
|
|
|
|
episodeToUpdate.EpisodeFileId = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
episodeToUpdate.SeriesId = series.Id;
|
|
|
|
|
episodeToUpdate.TvDbEpisodeId = episode.TvDbEpisodeId;
|
|
|
|
|
episodeToUpdate.EpisodeNumber = episode.EpisodeNumber;
|
|
|
|
|
episodeToUpdate.SeasonNumber = episode.SeasonNumber;
|
|
|
|
|
episodeToUpdate.Title = episode.Title;
|
|
|
|
|
episodeToUpdate.Overview = episode.Overview;
|
|
|
|
|
episodeToUpdate.AirDate = episode.AirDate;
|
2013-07-24 05:28:06 +00:00
|
|
|
|
episodeToUpdate.AirDateUtc = episode.AirDateUtc;
|
2013-06-02 19:29:00 +00:00
|
|
|
|
|
|
|
|
|
successCount++;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2013-07-17 00:53:33 +00:00
|
|
|
|
_logger.FatalException(String.Format("An error has occurred while updating episode info for series {0}. {1}", series, episode), e);
|
2013-06-02 19:29:00 +00:00
|
|
|
|
failCount++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var allEpisodes = new List<Episode>();
|
|
|
|
|
allEpisodes.AddRange(newList);
|
|
|
|
|
allEpisodes.AddRange(updateList);
|
|
|
|
|
|
2013-07-26 03:25:24 +00:00
|
|
|
|
var groups = allEpisodes.Where(c=>c.AirDateUtc.HasValue).GroupBy(e => new { e.SeriesId, e.AirDate }).Where(g => g.Count() > 1).ToList();
|
2013-06-02 19:29:00 +00:00
|
|
|
|
|
|
|
|
|
foreach (var group in groups)
|
|
|
|
|
{
|
|
|
|
|
int episodeCount = 0;
|
|
|
|
|
foreach (var episode in group.OrderBy(e => e.SeasonNumber).ThenBy(e => e.EpisodeNumber))
|
|
|
|
|
{
|
2013-07-26 03:25:24 +00:00
|
|
|
|
episode.AirDateUtc = episode.AirDateUtc.Value.AddMinutes(series.Runtime * episodeCount);
|
2013-06-02 19:29:00 +00:00
|
|
|
|
episodeCount++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_episodeService.UpdateMany(updateList);
|
2013-07-05 22:54:42 +00:00
|
|
|
|
_episodeService.InsertMany(newList);
|
2013-06-02 19:29:00 +00:00
|
|
|
|
|
|
|
|
|
if (newList.Any())
|
|
|
|
|
{
|
|
|
|
|
_messageAggregator.PublishEvent(new EpisodeInfoAddedEvent(newList, series));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (updateList.Any())
|
|
|
|
|
{
|
|
|
|
|
_messageAggregator.PublishEvent(new EpisodeInfoUpdatedEvent(updateList));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (failCount != 0)
|
|
|
|
|
{
|
|
|
|
|
_logger.Info("Finished episode refresh for series: {0}. Successful: {1} - Failed: {2} ",
|
|
|
|
|
series.Title, successCount, failCount);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-07-05 05:17:25 +00:00
|
|
|
|
_logger.Info("Finished episode refresh for series: {0}.", series);
|
2013-06-02 19:29:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//DeleteEpisodesNotAvailableAnymore(series, remoteEpisodes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* private void DeleteEpisodesNotAvailableAnymore(Series series, IEnumerable<Episode> onlineEpisodes)
|
|
|
|
|
{
|
|
|
|
|
//Todo: This will not work as currently implemented - what are we trying to do here?
|
2013-07-12 03:38:25 +00:00
|
|
|
|
//Todo: We were trying to remove episodes that were once on tvdb but were removed, for whatever reason, instead of polluting our DB with them.
|
2013-06-02 19:29:00 +00:00
|
|
|
|
return;
|
|
|
|
|
_logger.Trace("Starting deletion of episodes that no longer exist in TVDB: {0}", series.Title.WithDefault(series.Id));
|
|
|
|
|
foreach (var episode in onlineEpisodes)
|
|
|
|
|
{
|
|
|
|
|
_episodeRepository.Delete(episode.Id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.Trace("Deleted episodes that no longer exist in TVDB for {0}", series.Id);
|
|
|
|
|
}*/
|
|
|
|
|
}
|
|
|
|
|
}
|