2012-10-17 08:00:28 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
2013-08-26 23:05:13 +03:00
|
|
|
using NzbDrone.Common.Cache;
|
2013-08-31 04:42:30 +03:00
|
|
|
using NzbDrone.Common.Instrumentation;
|
2013-08-26 23:05:13 +03:00
|
|
|
using NzbDrone.Core.Lifecycle;
|
2013-09-11 09:33:47 +03:00
|
|
|
using NzbDrone.Core.Messaging;
|
2013-02-19 09:01:03 +03:00
|
|
|
using NzbDrone.Core.Tv;
|
2013-07-23 08:50:32 +03:00
|
|
|
using NzbDrone.Core.Tv.Events;
|
2012-10-17 08:00:28 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
{
|
2013-07-23 08:50:32 +03:00
|
|
|
public interface IXemProvider
|
|
|
|
{
|
|
|
|
void UpdateMappings();
|
|
|
|
void UpdateMappings(int seriesId);
|
2013-08-26 23:05:13 +03:00
|
|
|
void UpdateMappings(Series series);
|
2013-07-23 08:50:32 +03:00
|
|
|
void PerformUpdate(Series series);
|
|
|
|
}
|
|
|
|
|
2013-08-26 23:05:13 +03:00
|
|
|
public class XemProvider : IXemProvider, IExecute<UpdateXemMappingsCommand>, IHandle<SeriesUpdatedEvent>, IHandleAsync<ApplicationStartedEvent>
|
2012-10-17 08:00:28 +03:00
|
|
|
{
|
2013-02-22 03:47:09 +03:00
|
|
|
private readonly IEpisodeService _episodeService;
|
2013-08-21 04:32:49 +03:00
|
|
|
private readonly IXemCommunicationProvider _xemCommunicationProvider;
|
2013-07-23 08:50:32 +03:00
|
|
|
private readonly ISeriesService _seriesService;
|
2013-08-26 23:05:13 +03:00
|
|
|
private readonly ICached<bool> _cache;
|
2012-10-17 08:00:28 +03:00
|
|
|
|
2013-08-31 04:42:30 +03:00
|
|
|
private static readonly Logger _logger = NzbDroneLogger.GetLogger();
|
2012-10-17 08:00:28 +03:00
|
|
|
|
2013-08-26 23:05:13 +03:00
|
|
|
public XemProvider(IEpisodeService episodeService,
|
|
|
|
IXemCommunicationProvider xemCommunicationProvider,
|
|
|
|
ISeriesService seriesService, ICacheManger cacheManger)
|
2012-10-17 08:00:28 +03:00
|
|
|
{
|
2013-07-23 08:50:32 +03:00
|
|
|
if (seriesService == null) throw new ArgumentNullException("seriesService");
|
2013-02-20 05:05:15 +03:00
|
|
|
_episodeService = episodeService;
|
2012-10-17 08:00:28 +03:00
|
|
|
_xemCommunicationProvider = xemCommunicationProvider;
|
2013-07-23 08:50:32 +03:00
|
|
|
_seriesService = seriesService;
|
2013-08-26 23:05:13 +03:00
|
|
|
_cache = cacheManger.GetCache<bool>(GetType());
|
2012-10-17 08:00:28 +03:00
|
|
|
}
|
|
|
|
|
2013-07-23 08:50:32 +03:00
|
|
|
public void UpdateMappings()
|
2012-10-17 08:00:28 +03:00
|
|
|
{
|
|
|
|
_logger.Trace("Starting scene numbering update");
|
2013-08-26 23:05:13 +03:00
|
|
|
|
2012-10-17 08:00:28 +03:00
|
|
|
try
|
|
|
|
{
|
2013-08-26 23:05:13 +03:00
|
|
|
var ids = GetXemSeriesIds();
|
2013-07-23 08:50:32 +03:00
|
|
|
var series = _seriesService.GetAllSeries();
|
|
|
|
var wantedSeries = series.Where(s => ids.Contains(s.TvdbId)).ToList();
|
2012-10-17 08:00:28 +03:00
|
|
|
|
2013-07-05 08:17:25 +03:00
|
|
|
foreach (var ser in wantedSeries)
|
2012-10-17 08:00:28 +03:00
|
|
|
{
|
2012-10-18 07:15:42 +03:00
|
|
|
PerformUpdate(ser);
|
2012-10-17 08:00:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_logger.Trace("Completed scene numbering update");
|
|
|
|
}
|
|
|
|
|
2013-07-05 08:17:25 +03:00
|
|
|
catch (Exception ex)
|
2012-10-17 08:00:28 +03:00
|
|
|
{
|
|
|
|
_logger.WarnException("Error updating Scene Mappings", ex);
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
2012-10-18 07:15:42 +03:00
|
|
|
|
2013-07-23 08:50:32 +03:00
|
|
|
public void UpdateMappings(int seriesId)
|
2012-10-18 07:15:42 +03:00
|
|
|
{
|
2013-07-23 08:50:32 +03:00
|
|
|
var series = _seriesService.GetSeries(seriesId);
|
2012-11-14 23:31:44 +03:00
|
|
|
|
2013-07-23 08:50:32 +03:00
|
|
|
if (series == null)
|
2012-11-14 23:31:44 +03:00
|
|
|
{
|
2013-07-23 08:50:32 +03:00
|
|
|
_logger.Trace("Series could not be found: {0}", seriesId);
|
2012-11-14 23:31:44 +03:00
|
|
|
return;
|
|
|
|
}
|
2013-08-26 23:05:13 +03:00
|
|
|
|
|
|
|
UpdateMappings(series);
|
|
|
|
}
|
2012-11-14 23:31:44 +03:00
|
|
|
|
2013-08-26 23:05:13 +03:00
|
|
|
public void UpdateMappings(Series series)
|
|
|
|
{
|
|
|
|
if (!_cache.Find(series.TvdbId.ToString()))
|
2012-10-18 07:15:42 +03:00
|
|
|
{
|
2013-08-26 23:05:13 +03:00
|
|
|
_logger.Trace("Scene numbering is not available for {0} [{1}]", series.Title, series.TvdbId);
|
2012-10-18 07:15:42 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PerformUpdate(series);
|
|
|
|
}
|
|
|
|
|
2013-07-23 08:50:32 +03:00
|
|
|
public void PerformUpdate(Series series)
|
2012-10-18 07:15:42 +03:00
|
|
|
{
|
2013-07-05 08:17:25 +03:00
|
|
|
_logger.Trace("Updating scene numbering mapping for: {0}", series);
|
2012-10-18 07:15:42 +03:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var episodesToUpdate = new List<Episode>();
|
2013-07-23 08:50:32 +03:00
|
|
|
var mappings = _xemCommunicationProvider.GetSceneTvdbMappings(series.TvdbId);
|
2012-10-18 07:15:42 +03:00
|
|
|
|
|
|
|
if (mappings == null)
|
|
|
|
{
|
2013-07-05 08:17:25 +03:00
|
|
|
_logger.Trace("Mappings for: {0} are null, skipping", series);
|
2012-10-18 07:15:42 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-26 06:58:57 +03:00
|
|
|
var episodes = _episodeService.GetEpisodeBySeries(series.Id);
|
2012-10-18 09:49:53 +03:00
|
|
|
|
2012-10-18 07:15:42 +03:00
|
|
|
foreach (var mapping in mappings)
|
|
|
|
{
|
2013-07-05 08:17:25 +03:00
|
|
|
_logger.Trace("Setting scene numbering mappings for {0} S{1:00}E{2:00}", series, mapping.Tvdb.Season, mapping.Tvdb.Episode);
|
2012-10-18 07:15:42 +03:00
|
|
|
|
2012-10-18 09:49:53 +03:00
|
|
|
var episode = episodes.SingleOrDefault(e => e.SeasonNumber == mapping.Tvdb.Season && e.EpisodeNumber == mapping.Tvdb.Episode);
|
|
|
|
|
|
|
|
if (episode == null)
|
|
|
|
{
|
|
|
|
_logger.Trace("Information hasn't been added to TheTVDB yet, skipping.");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-10-18 07:15:42 +03:00
|
|
|
episode.AbsoluteEpisodeNumber = mapping.Scene.Absolute;
|
|
|
|
episode.SceneSeasonNumber = mapping.Scene.Season;
|
|
|
|
episode.SceneEpisodeNumber = mapping.Scene.Episode;
|
|
|
|
episodesToUpdate.Add(episode);
|
|
|
|
}
|
|
|
|
|
2013-07-05 08:17:25 +03:00
|
|
|
_logger.Trace("Committing scene numbering mappings to database for: {0}", series);
|
2013-02-20 05:05:15 +03:00
|
|
|
_episodeService.UpdateEpisodes(episodesToUpdate);
|
2012-10-18 09:49:53 +03:00
|
|
|
|
2013-07-05 08:17:25 +03:00
|
|
|
_logger.Trace("Setting UseSceneMapping for {0}", series);
|
2012-10-18 09:49:53 +03:00
|
|
|
series.UseSceneNumbering = true;
|
2013-07-23 08:50:32 +03:00
|
|
|
_seriesService.UpdateSeries(series);
|
2012-10-18 07:15:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2013-08-28 09:51:42 +03:00
|
|
|
_logger.ErrorException("Error updating scene numbering mappings for: " + series, ex);
|
2012-10-18 07:15:42 +03:00
|
|
|
}
|
|
|
|
}
|
2013-05-07 03:39:33 +03:00
|
|
|
|
2013-08-26 23:05:13 +03:00
|
|
|
private List<int> GetXemSeriesIds()
|
|
|
|
{
|
|
|
|
_cache.Clear();
|
|
|
|
|
|
|
|
var ids = _xemCommunicationProvider.GetXemSeriesIds();
|
|
|
|
|
|
|
|
foreach (var id in ids)
|
|
|
|
{
|
|
|
|
_cache.Set(id.ToString(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ids;
|
|
|
|
}
|
|
|
|
|
2013-07-23 08:50:32 +03:00
|
|
|
public void Execute(UpdateXemMappingsCommand message)
|
|
|
|
{
|
|
|
|
if (message.SeriesId.HasValue)
|
|
|
|
{
|
|
|
|
UpdateMappings(message.SeriesId.Value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
UpdateMappings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Handle(SeriesUpdatedEvent message)
|
2013-05-07 03:39:33 +03:00
|
|
|
{
|
2013-08-26 23:05:13 +03:00
|
|
|
UpdateMappings(message.Series);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void HandleAsync(ApplicationStartedEvent message)
|
|
|
|
{
|
|
|
|
GetXemSeriesIds();
|
2013-05-07 03:39:33 +03:00
|
|
|
}
|
2012-10-17 08:00:28 +03:00
|
|
|
}
|
|
|
|
}
|