2013-10-03 04:01:32 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common;
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2014-05-19 22:14:41 +03:00
|
|
|
using NzbDrone.Core.MetadataSource.Tvdb;
|
2013-10-03 04:01:32 +03: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;
|
2014-05-19 22:14:41 +03:00
|
|
|
private readonly ITvdbProxy _tvdbProxy;
|
2013-10-03 04:01:32 +03:00
|
|
|
private readonly IEventAggregator _eventAggregator;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2014-05-19 22:14:41 +03:00
|
|
|
public RefreshEpisodeService(IEpisodeService episodeService, ITvdbProxy tvdbProxy, IEventAggregator eventAggregator, Logger logger)
|
2013-10-03 04:01:32 +03:00
|
|
|
{
|
|
|
|
_episodeService = episodeService;
|
2014-05-19 22:14:41 +03:00
|
|
|
_tvdbProxy = tvdbProxy;
|
2013-10-03 04:01:32 +03:00
|
|
|
_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 05:23:57 +03:00
|
|
|
if (series.SeriesType == SeriesTypes.Anime)
|
|
|
|
{
|
|
|
|
dupeFreeRemoteEpisodes = MapAbsoluteEpisodeNumbers(series, dupeFreeRemoteEpisodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var episode in OrderEpsiodes(series, dupeFreeRemoteEpisodes))
|
2013-10-03 04:01:32 +03:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2014-07-11 05:23:57 +03:00
|
|
|
var episodeToUpdate = GetEpisodeToUpdate(series, episode, existingEpisodes);
|
2013-10-03 04:01:32 +03: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 05:23:57 +03:00
|
|
|
episodeToUpdate.AbsoluteEpisodeNumber = episode.AbsoluteEpisodeNumber;
|
2013-10-03 04:01:32 +03:00
|
|
|
episodeToUpdate.Title = episode.Title;
|
|
|
|
episodeToUpdate.Overview = episode.Overview;
|
|
|
|
episodeToUpdate.AirDate = episode.AirDate;
|
|
|
|
episodeToUpdate.AirDateUtc = episode.AirDateUtc;
|
2014-01-22 08:22:09 +03:00
|
|
|
episodeToUpdate.Ratings = episode.Ratings;
|
|
|
|
episodeToUpdate.Images = episode.Images;
|
2013-10-03 04:01:32 +03: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);
|
|
|
|
|
|
|
|
_episodeService.DeleteMany(existingEpisodes);
|
|
|
|
_episodeService.UpdateMany(updateList);
|
|
|
|
_episodeService.InsertMany(newList);
|
|
|
|
|
|
|
|
if (newList.Any())
|
|
|
|
{
|
|
|
|
_eventAggregator.PublishEvent(new EpisodeInfoAddedEvent(newList, series));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updateList.Any())
|
|
|
|
{
|
|
|
|
_eventAggregator.PublishEvent(new EpisodeInfoUpdatedEvent(updateList));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (existingEpisodes.Any())
|
|
|
|
{
|
|
|
|
_eventAggregator.PublishEvent(new EpisodeInfoDeletedEvent(updateList));
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
.GroupBy(e => new { e.SeasonNumber, e.AirDate })
|
|
|
|
.Where(g => g.Count() > 1)
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
foreach (var group in groups)
|
|
|
|
{
|
|
|
|
var episodeCount = 0;
|
2014-02-02 01:26:49 +03:00
|
|
|
foreach (var episode in group.OrderBy(e => e.SeasonNumber).ThenBy(e => e.EpisodeNumber))
|
2013-10-03 04:01:32 +03:00
|
|
|
{
|
|
|
|
episode.AirDateUtc = episode.AirDateUtc.Value.AddMinutes(series.Runtime * episodeCount);
|
|
|
|
episodeCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-02 01:26:49 +03:00
|
|
|
|
2014-07-11 05:23:57 +03:00
|
|
|
private List<Episode> MapAbsoluteEpisodeNumbers(Series series, List<Episode> traktEpisodes)
|
2014-02-02 01:26:49 +03:00
|
|
|
{
|
2014-05-19 22:14:41 +03:00
|
|
|
var tvdbEpisodes = _tvdbProxy.GetEpisodeInfo(series.TvdbId);
|
|
|
|
|
2014-07-11 05:23:57 +03:00
|
|
|
foreach (var episode in traktEpisodes)
|
2014-02-02 01:26:49 +03:00
|
|
|
{
|
2014-05-19 22:14:41 +03:00
|
|
|
//I'd use single, but then I'd have to trust the tvdb data... and I don't
|
|
|
|
var tvdbEpisode = tvdbEpisodes.FirstOrDefault(e => e.SeasonNumber == episode.SeasonNumber &&
|
|
|
|
e.EpisodeNumber == episode.EpisodeNumber);
|
|
|
|
|
|
|
|
if (tvdbEpisode == null)
|
|
|
|
{
|
|
|
|
_logger.Debug("Cannot find matching episode from the tvdb: {0}x{1:00}", episode.SeasonNumber, episode.EpisodeNumber);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
episode.AbsoluteEpisodeNumber = tvdbEpisode.AbsoluteEpisodeNumber;
|
2014-02-02 01:26:49 +03:00
|
|
|
}
|
2014-07-11 05:23:57 +03:00
|
|
|
|
|
|
|
return traktEpisodes.DistinctBy(e => e.AbsoluteEpisodeNumber).ToList();
|
|
|
|
}
|
|
|
|
|
|
|
|
private Episode GetEpisodeToUpdate(Series series, Episode episode, IEnumerable<Episode> existingEpisodes)
|
|
|
|
{
|
|
|
|
if (series.SeriesType == SeriesTypes.Anime)
|
|
|
|
{
|
|
|
|
if (episode.AbsoluteEpisodeNumber > 0)
|
|
|
|
{
|
|
|
|
return existingEpisodes.FirstOrDefault(e => e.AbsoluteEpisodeNumber == episode.AbsoluteEpisodeNumber);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return existingEpisodes.FirstOrDefault(e => e.SeasonNumber == episode.SeasonNumber && e.EpisodeNumber == episode.EpisodeNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
private IEnumerable<Episode> OrderEpsiodes(Series series, List<Episode> episodes)
|
|
|
|
{
|
|
|
|
if (series.SeriesType == SeriesTypes.Anime)
|
|
|
|
{
|
|
|
|
var withAbs = episodes.Where(e => e.AbsoluteEpisodeNumber > 0)
|
|
|
|
.OrderBy(e => e.AbsoluteEpisodeNumber);
|
|
|
|
|
|
|
|
var withoutAbs = episodes.Where(e => e.AbsoluteEpisodeNumber == 0)
|
|
|
|
.OrderBy(e => e.SeasonNumber)
|
|
|
|
.ThenBy(e => e.EpisodeNumber);
|
|
|
|
|
|
|
|
return withAbs.Concat(withoutAbs);
|
|
|
|
}
|
|
|
|
|
|
|
|
return episodes.OrderBy(e => e.SeasonNumber).ThenBy(e => e.EpisodeNumber);
|
2014-02-02 01:26:49 +03:00
|
|
|
}
|
2013-10-03 04:01:32 +03:00
|
|
|
}
|
2013-08-01 05:02:36 +03:00
|
|
|
}
|