Lidarr/NzbDrone.Core/DataAugmentation/Xem/XemService.cs

151 lines
4.9 KiB
C#
Raw Normal View History

2012-10-17 05:00:28 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using NLog;
using NzbDrone.Common.Cache;
using NzbDrone.Core.Lifecycle;
using NzbDrone.Core.Messaging.Commands;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Tv;
2013-07-23 05:50:32 +00:00
using NzbDrone.Core.Tv.Events;
2012-10-17 05:00:28 +00:00
2013-09-19 01:09:26 +00:00
namespace NzbDrone.Core.DataAugmentation.Xem
2012-10-17 05:00:28 +00:00
{
2013-09-17 06:14:47 +00:00
public class XemService : IExecute<UpdateXemMappingsCommand>, IHandle<SeriesUpdatedEvent>, IHandleAsync<ApplicationStartedEvent>
2012-10-17 05:00:28 +00:00
{
private readonly IEpisodeService _episodeService;
2013-09-17 06:14:47 +00:00
private readonly IXemProxy _xemProxy;
2013-07-23 05:50:32 +00:00
private readonly ISeriesService _seriesService;
2013-09-17 06:14:47 +00:00
private readonly Logger _logger;
private readonly ICached<bool> _cache;
2012-10-17 05:00:28 +00:00
2013-09-17 06:14:47 +00:00
public XemService(IEpisodeService episodeService,
IXemProxy xemProxy,
ISeriesService seriesService, ICacheManger cacheManger, Logger logger)
2012-10-17 05:00:28 +00:00
{
2013-07-23 05:50:32 +00:00
if (seriesService == null) throw new ArgumentNullException("seriesService");
_episodeService = episodeService;
2013-09-17 06:14:47 +00:00
_xemProxy = xemProxy;
2013-07-23 05:50:32 +00:00
_seriesService = seriesService;
2013-09-17 06:14:47 +00:00
_logger = logger;
_logger = logger;
_cache = cacheManger.GetCache<bool>(GetType());
2012-10-17 05:00:28 +00:00
}
2013-09-17 06:14:47 +00:00
public void Execute(UpdateXemMappingsCommand message)
{
UpdateMappings();
}
public void Handle(SeriesUpdatedEvent message)
{
UpdateMappings(message.Series);
}
public void HandleAsync(ApplicationStartedEvent message)
{
GetXemSeriesIds();
}
private void UpdateMappings()
2012-10-17 05:00:28 +00:00
{
_logger.Trace("Starting scene numbering update");
2012-10-17 05:00:28 +00:00
try
{
var ids = GetXemSeriesIds();
2013-07-23 05:50:32 +00:00
var series = _seriesService.GetAllSeries();
var wantedSeries = series.Where(s => ids.Contains(s.TvdbId)).ToList();
2012-10-17 05:00:28 +00:00
2013-07-05 05:17:25 +00:00
foreach (var ser in wantedSeries)
2012-10-17 05:00:28 +00:00
{
PerformUpdate(ser);
2012-10-17 05:00:28 +00:00
}
_logger.Trace("Completed scene numbering update");
}
2013-07-05 05:17:25 +00:00
catch (Exception ex)
2012-10-17 05:00:28 +00:00
{
_logger.WarnException("Error updating Scene Mappings", ex);
throw;
}
}
2013-09-17 06:14:47 +00:00
private void UpdateMappings(Series series)
{
if (!_cache.Find(series.TvdbId.ToString()))
{
_logger.Trace("Scene numbering is not available for {0} [{1}]", series.Title, series.TvdbId);
return;
}
PerformUpdate(series);
}
2013-09-17 06:14:47 +00:00
private void PerformUpdate(Series series)
{
2013-07-05 05:17:25 +00:00
_logger.Trace("Updating scene numbering mapping for: {0}", series);
try
{
var episodesToUpdate = new List<Episode>();
2013-09-17 06:14:47 +00:00
var mappings = _xemProxy.GetSceneTvdbMappings(series.TvdbId);
2013-09-17 06:14:47 +00:00
if (!mappings.Any())
{
2013-09-17 06:14:47 +00:00
_logger.Trace("Mappings for: {0} are empty, skipping", series);
_cache.Remove(series.TvdbId.ToString());
return;
}
var episodes = _episodeService.GetEpisodeBySeries(series.Id);
foreach (var mapping in mappings)
{
2013-07-05 05:17:25 +00:00
_logger.Trace("Setting scene numbering mappings for {0} S{1:00}E{2:00}", series, mapping.Tvdb.Season, mapping.Tvdb.Episode);
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;
}
episode.AbsoluteEpisodeNumber = mapping.Scene.Absolute;
episode.SceneSeasonNumber = mapping.Scene.Season;
episode.SceneEpisodeNumber = mapping.Scene.Episode;
episodesToUpdate.Add(episode);
}
2013-07-05 05:17:25 +00:00
_logger.Trace("Committing scene numbering mappings to database for: {0}", series);
_episodeService.UpdateEpisodes(episodesToUpdate);
2013-07-05 05:17:25 +00:00
_logger.Trace("Setting UseSceneMapping for {0}", series);
series.UseSceneNumbering = true;
2013-07-23 05:50:32 +00:00
_seriesService.UpdateSeries(series);
}
catch (Exception ex)
{
_logger.ErrorException("Error updating scene numbering mappings for: " + series, ex);
}
}
2013-05-07 00:39:33 +00:00
private List<int> GetXemSeriesIds()
{
_cache.Clear();
2013-09-17 06:14:47 +00:00
var ids = _xemProxy.GetXemSeriesIds();
foreach (var id in ids)
{
_cache.Set(id.ToString(), true);
}
return ids;
}
2012-10-17 05:00:28 +00:00
}
}