2013-10-03 01:01:32 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using NLog;
|
2014-12-02 06:26:25 +00:00
|
|
|
|
using NzbDrone.Common.Extensions;
|
2013-10-03 01:01:32 +00:00
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
|
|
|
|
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;
|
|
|
|
|
private readonly IEventAggregator _eventAggregator;
|
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
2015-01-04 01:02:45 +00:00
|
|
|
|
public RefreshEpisodeService(IEpisodeService episodeService, IEventAggregator eventAggregator, Logger logger)
|
2013-10-03 01:01:32 +00:00
|
|
|
|
{
|
|
|
|
|
_episodeService = episodeService;
|
|
|
|
|
_eventAggregator = eventAggregator;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RefreshEpisodeInfo(Series series, IEnumerable<Episode> remoteEpisodes)
|
|
|
|
|
{
|
|
|
|
|
_logger.Info("Starting episode info refresh for: {0}", series);
|
|
|
|
|
var successCount = 0;
|
|
|
|
|
var failCount = 0;
|
|
|
|
|
|
|
|
|
|
var existingEpisodes = _episodeService.GetEpisodeBySeries(series.Id);
|
|
|
|
|
var seasons = series.Seasons;
|
|
|
|
|
|
|
|
|
|
var updateList = new List<Episode>();
|
|
|
|
|
var newList = new List<Episode>();
|
|
|
|
|
var dupeFreeRemoteEpisodes = remoteEpisodes.DistinctBy(m => new { m.SeasonNumber, m.EpisodeNumber }).ToList();
|
|
|
|
|
|
2014-07-11 02:23:57 +00:00
|
|
|
|
if (series.SeriesType == SeriesTypes.Anime)
|
|
|
|
|
{
|
|
|
|
|
dupeFreeRemoteEpisodes = MapAbsoluteEpisodeNumbers(series, dupeFreeRemoteEpisodes);
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-07 12:42:46 +00:00
|
|
|
|
foreach (var episode in OrderEpisodes(series, dupeFreeRemoteEpisodes))
|
2013-10-03 01:01:32 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-07-11 02:23:57 +00:00
|
|
|
|
var episodeToUpdate = GetEpisodeToUpdate(series, episode, existingEpisodes);
|
2013-10-03 01:01:32 +00:00
|
|
|
|
|
|
|
|
|
if (episodeToUpdate != null)
|
|
|
|
|
{
|
|
|
|
|
existingEpisodes.Remove(episodeToUpdate);
|
|
|
|
|
updateList.Add(episodeToUpdate);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
episodeToUpdate = new Episode();
|
|
|
|
|
episodeToUpdate.Monitored = GetMonitoredStatus(episode, seasons);
|
|
|
|
|
newList.Add(episodeToUpdate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
episodeToUpdate.SeriesId = series.Id;
|
|
|
|
|
episodeToUpdate.EpisodeNumber = episode.EpisodeNumber;
|
|
|
|
|
episodeToUpdate.SeasonNumber = episode.SeasonNumber;
|
2014-07-11 02:23:57 +00:00
|
|
|
|
episodeToUpdate.AbsoluteEpisodeNumber = episode.AbsoluteEpisodeNumber;
|
2015-01-10 19:31:05 +00:00
|
|
|
|
episodeToUpdate.Title = episode.Title ?? "TBA";
|
2013-10-03 01:01:32 +00:00
|
|
|
|
episodeToUpdate.Overview = episode.Overview;
|
|
|
|
|
episodeToUpdate.AirDate = episode.AirDate;
|
|
|
|
|
episodeToUpdate.AirDateUtc = episode.AirDateUtc;
|
2014-01-22 05:22:09 +00:00
|
|
|
|
episodeToUpdate.Ratings = episode.Ratings;
|
|
|
|
|
episodeToUpdate.Images = episode.Images;
|
2013-10-03 01:01:32 +00:00
|
|
|
|
|
|
|
|
|
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);
|
2014-08-07 12:42:46 +00:00
|
|
|
|
AdjustDirectToDvdAirDate(series, allEpisodes);
|
2013-10-03 01:01:32 +00:00
|
|
|
|
|
|
|
|
|
_episodeService.DeleteMany(existingEpisodes);
|
|
|
|
|
_episodeService.UpdateMany(updateList);
|
|
|
|
|
_episodeService.InsertMany(newList);
|
|
|
|
|
|
2014-11-27 07:08:17 +00:00
|
|
|
|
_eventAggregator.PublishEvent(new EpisodeInfoRefreshedEvent(series, newList, updateList));
|
2013-10-03 01:01:32 +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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-10 20:00:00 +00:00
|
|
|
|
private bool GetMonitoredStatus(Episode episode, IEnumerable<Season> seasons)
|
2013-10-03 01:01:32 +00:00
|
|
|
|
{
|
|
|
|
|
if (episode.EpisodeNumber == 0 && episode.SeasonNumber != 1)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var season = seasons.SingleOrDefault(c => c.SeasonNumber == episode.SeasonNumber);
|
|
|
|
|
return season == null || season.Monitored;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-10 20:00:00 +00:00
|
|
|
|
private void AdjustMultiEpisodeAirTime(Series series, IEnumerable<Episode> allEpisodes)
|
2013-10-03 01:01:32 +00:00
|
|
|
|
{
|
2015-01-10 20:00:00 +00:00
|
|
|
|
if (series.Network == "Netflix")
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Not adjusting episode air times for Netflix series {0}", series.Title);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var groups = allEpisodes.Where(c => c.AirDateUtc.HasValue)
|
|
|
|
|
.GroupBy(e => new {e.SeasonNumber, e.AirDate})
|
|
|
|
|
.Where(g => g.Count() > 1)
|
|
|
|
|
.ToList();
|
2013-10-03 01:01:32 +00:00
|
|
|
|
|
|
|
|
|
foreach (var group in groups)
|
|
|
|
|
{
|
|
|
|
|
var episodeCount = 0;
|
2015-01-10 20:00:00 +00:00
|
|
|
|
|
2014-02-01 22:26:49 +00:00
|
|
|
|
foreach (var episode in group.OrderBy(e => e.SeasonNumber).ThenBy(e => e.EpisodeNumber))
|
2013-10-03 01:01:32 +00:00
|
|
|
|
{
|
|
|
|
|
episode.AirDateUtc = episode.AirDateUtc.Value.AddMinutes(series.Runtime * episodeCount);
|
|
|
|
|
episodeCount++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-02-01 22:26:49 +00:00
|
|
|
|
|
2015-01-10 20:00:00 +00:00
|
|
|
|
private void AdjustDirectToDvdAirDate(Series series, IEnumerable<Episode> allEpisodes)
|
2014-08-07 12:42:46 +00:00
|
|
|
|
{
|
|
|
|
|
if (series.Status == SeriesStatusType.Ended && allEpisodes.All(v => !v.AirDateUtc.HasValue) && series.FirstAired.HasValue)
|
|
|
|
|
{
|
|
|
|
|
foreach (var episode in allEpisodes)
|
|
|
|
|
{
|
|
|
|
|
episode.AirDateUtc = series.FirstAired;
|
|
|
|
|
episode.AirDate = series.FirstAired.Value.ToString("yyyy-MM-dd");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-11 02:23:57 +00:00
|
|
|
|
private List<Episode> MapAbsoluteEpisodeNumbers(Series series, List<Episode> traktEpisodes)
|
2014-02-01 22:26:49 +00:00
|
|
|
|
{
|
2014-10-14 22:19:35 +00:00
|
|
|
|
//Return all episodes with no abs number, but distinct for those with abs number
|
|
|
|
|
return traktEpisodes.Where(e => e.AbsoluteEpisodeNumber.HasValue)
|
|
|
|
|
.DistinctBy(e => e.AbsoluteEpisodeNumber.Value)
|
|
|
|
|
.Concat(traktEpisodes.Where(e => !e.AbsoluteEpisodeNumber.HasValue))
|
2014-07-14 02:11:28 +00:00
|
|
|
|
.ToList();
|
2014-07-11 02:23:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-11 03:38:48 +00:00
|
|
|
|
private Episode GetEpisodeToUpdate(Series series, Episode episode, List<Episode> existingEpisodes)
|
2014-07-11 02:23:57 +00:00
|
|
|
|
{
|
|
|
|
|
if (series.SeriesType == SeriesTypes.Anime)
|
|
|
|
|
{
|
2014-10-14 22:19:35 +00:00
|
|
|
|
if (episode.AbsoluteEpisodeNumber.HasValue)
|
2014-07-11 02:23:57 +00:00
|
|
|
|
{
|
2014-07-11 03:38:48 +00:00
|
|
|
|
var matchingEpisode = existingEpisodes.FirstOrDefault(e => e.AbsoluteEpisodeNumber == episode.AbsoluteEpisodeNumber);
|
|
|
|
|
|
|
|
|
|
if (matchingEpisode != null) return matchingEpisode;
|
2014-07-11 02:23:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return existingEpisodes.FirstOrDefault(e => e.SeasonNumber == episode.SeasonNumber && e.EpisodeNumber == episode.EpisodeNumber);
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-07 12:42:46 +00:00
|
|
|
|
private IEnumerable<Episode> OrderEpisodes(Series series, List<Episode> episodes)
|
2014-07-11 02:23:57 +00:00
|
|
|
|
{
|
|
|
|
|
if (series.SeriesType == SeriesTypes.Anime)
|
|
|
|
|
{
|
2014-10-14 22:19:35 +00:00
|
|
|
|
var withAbs = episodes.Where(e => e.AbsoluteEpisodeNumber.HasValue)
|
2014-07-11 02:23:57 +00:00
|
|
|
|
.OrderBy(e => e.AbsoluteEpisodeNumber);
|
|
|
|
|
|
2014-10-14 22:19:35 +00:00
|
|
|
|
var withoutAbs = episodes.Where(e => !e.AbsoluteEpisodeNumber.HasValue)
|
2014-07-11 02:23:57 +00:00
|
|
|
|
.OrderBy(e => e.SeasonNumber)
|
|
|
|
|
.ThenBy(e => e.EpisodeNumber);
|
|
|
|
|
|
|
|
|
|
return withAbs.Concat(withoutAbs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return episodes.OrderBy(e => e.SeasonNumber).ThenBy(e => e.EpisodeNumber);
|
2014-02-01 22:26:49 +00:00
|
|
|
|
}
|
2013-10-03 01:01:32 +00:00
|
|
|
|
}
|
2013-08-01 02:02:36 +00:00
|
|
|
|
}
|