2013-08-01 02:02:36 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using NLog;
|
2013-09-11 06:33:47 +00:00
|
|
|
|
using NzbDrone.Core.Messaging;
|
2013-09-14 06:36:07 +00:00
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2013-08-01 02:02:36 +00:00
|
|
|
|
using NzbDrone.Core.Tv.Events;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Tv
|
|
|
|
|
{
|
|
|
|
|
public interface IRefreshEpisodeService
|
|
|
|
|
{
|
|
|
|
|
void RefreshEpisodeInfo(Series series, IEnumerable<Episode> remoteEpisodes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class RefreshEpisodeService : IRefreshEpisodeService
|
|
|
|
|
{
|
|
|
|
|
private readonly IEpisodeService _episodeService;
|
2013-09-14 06:36:07 +00:00
|
|
|
|
private readonly IEventAggregator _eventAggregator;
|
2013-08-01 02:02:36 +00:00
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
2013-09-14 06:36:07 +00:00
|
|
|
|
public RefreshEpisodeService(IEpisodeService episodeService, IEventAggregator eventAggregator, Logger logger)
|
2013-08-01 02:02:36 +00:00
|
|
|
|
{
|
|
|
|
|
_episodeService = episodeService;
|
2013-09-14 06:36:07 +00:00
|
|
|
|
_eventAggregator = eventAggregator;
|
2013-08-01 02:02:36 +00:00
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RefreshEpisodeInfo(Series series, IEnumerable<Episode> remoteEpisodes)
|
|
|
|
|
{
|
2013-09-04 05:01:28 +00:00
|
|
|
|
_logger.Info("Starting episode info refresh for: {0}", series);
|
2013-08-01 02:02:36 +00:00
|
|
|
|
var successCount = 0;
|
|
|
|
|
var failCount = 0;
|
|
|
|
|
|
2013-08-29 01:43:45 +00:00
|
|
|
|
var existingEpisodes = _episodeService.GetEpisodeBySeries(series.Id);
|
2013-09-13 03:50:12 +00:00
|
|
|
|
var seasons = series.Seasons;
|
2013-08-01 02:02:36 +00:00
|
|
|
|
|
|
|
|
|
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-09-11 06:33:47 +00:00
|
|
|
|
var episodeToUpdate = existingEpisodes.FirstOrDefault(e => e.SeasonNumber == episode.SeasonNumber && e.EpisodeNumber == episode.EpisodeNumber);
|
2013-08-01 02:02:36 +00:00
|
|
|
|
|
|
|
|
|
if (episodeToUpdate != null)
|
|
|
|
|
{
|
2013-08-29 01:43:45 +00:00
|
|
|
|
existingEpisodes.Remove(episodeToUpdate);
|
2013-08-01 02:02:36 +00:00
|
|
|
|
updateList.Add(episodeToUpdate);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
episodeToUpdate = new Episode();
|
|
|
|
|
episodeToUpdate.Monitored = GetMonitoredStatus(episode, seasons);
|
|
|
|
|
newList.Add(episodeToUpdate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
episodeToUpdate.AirDateUtc = episode.AirDateUtc;
|
|
|
|
|
|
|
|
|
|
successCount++;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.FatalException(String.Format("An error has occurred while updating episode info for series {0}. {1}", series, episode), e);
|
|
|
|
|
failCount++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var allEpisodes = new List<Episode>();
|
|
|
|
|
allEpisodes.AddRange(newList);
|
|
|
|
|
allEpisodes.AddRange(updateList);
|
|
|
|
|
|
|
|
|
|
AdjustMultiEpisodeAirTime(series, allEpisodes);
|
|
|
|
|
|
2013-08-29 01:43:45 +00:00
|
|
|
|
_episodeService.DeleteMany(existingEpisodes);
|
2013-08-01 02:02:36 +00:00
|
|
|
|
_episodeService.UpdateMany(updateList);
|
|
|
|
|
_episodeService.InsertMany(newList);
|
|
|
|
|
|
|
|
|
|
if (newList.Any())
|
|
|
|
|
{
|
2013-09-14 06:36:07 +00:00
|
|
|
|
_eventAggregator.PublishEvent(new EpisodeInfoAddedEvent(newList, series));
|
2013-08-01 02:02:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (updateList.Any())
|
|
|
|
|
{
|
2013-09-14 06:36:07 +00:00
|
|
|
|
_eventAggregator.PublishEvent(new EpisodeInfoUpdatedEvent(updateList));
|
2013-08-01 02:02:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-29 01:43:45 +00:00
|
|
|
|
if (existingEpisodes.Any())
|
|
|
|
|
{
|
2013-09-14 06:36:07 +00:00
|
|
|
|
_eventAggregator.PublishEvent(new EpisodeInfoDeletedEvent(updateList));
|
2013-08-29 01:43:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-01 02:02:36 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static bool GetMonitoredStatus(Episode episode, IEnumerable<Season> seasons)
|
|
|
|
|
{
|
|
|
|
|
if (episode.EpisodeNumber == 0 && episode.SeasonNumber != 1)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var season = seasons.SingleOrDefault(c => c.SeasonNumber == episode.SeasonNumber);
|
|
|
|
|
return season == null || season.Monitored;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void AdjustMultiEpisodeAirTime(Series series, IEnumerable<Episode> allEpisodes)
|
|
|
|
|
{
|
|
|
|
|
var groups =
|
|
|
|
|
allEpisodes.Where(c => c.AirDateUtc.HasValue)
|
2013-09-11 04:23:30 +00:00
|
|
|
|
.GroupBy(e => new { e.SeasonNumber, e.AirDate })
|
2013-08-01 02:02:36 +00:00
|
|
|
|
.Where(g => g.Count() > 1)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var group in groups)
|
|
|
|
|
{
|
|
|
|
|
var episodeCount = 0;
|
|
|
|
|
foreach (var episode in @group.OrderBy(e => e.SeasonNumber).ThenBy(e => e.EpisodeNumber))
|
|
|
|
|
{
|
|
|
|
|
episode.AirDateUtc = episode.AirDateUtc.Value.AddMinutes(series.Runtime * episodeCount);
|
|
|
|
|
episodeCount++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|