2012-10-17 07:00:28 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using NLog;
|
2013-02-19 07:01:03 +01:00
|
|
|
|
using NzbDrone.Core.Tv;
|
2012-10-17 07:00:28 +02:00
|
|
|
|
using NzbDrone.Core.Repository;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
|
{
|
|
|
|
|
public class XemProvider
|
|
|
|
|
{
|
2013-02-20 03:05:15 +01:00
|
|
|
|
private readonly ISeriesService _seriesService;
|
2013-02-22 01:47:09 +01:00
|
|
|
|
private readonly IEpisodeService _episodeService;
|
2012-10-17 07:00:28 +02:00
|
|
|
|
private readonly XemCommunicationProvider _xemCommunicationProvider;
|
2013-02-19 07:56:02 +01:00
|
|
|
|
private readonly ISeriesRepository _seriesRepository;
|
2012-10-17 07:00:28 +02:00
|
|
|
|
|
|
|
|
|
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
2013-02-22 01:47:09 +01:00
|
|
|
|
public XemProvider(ISeriesService seriesService, IEpisodeService episodeService,
|
2013-02-19 07:56:02 +01:00
|
|
|
|
XemCommunicationProvider xemCommunicationProvider,ISeriesRepository seriesRepository)
|
2012-10-17 07:00:28 +02:00
|
|
|
|
{
|
2013-02-20 03:05:15 +01:00
|
|
|
|
_seriesService = seriesService;
|
|
|
|
|
_episodeService = episodeService;
|
2012-10-17 07:00:28 +02:00
|
|
|
|
_xemCommunicationProvider = xemCommunicationProvider;
|
2013-02-19 07:56:02 +01:00
|
|
|
|
_seriesRepository = seriesRepository;
|
2012-10-17 07:00:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public XemProvider()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void UpdateMappings()
|
|
|
|
|
{
|
|
|
|
|
_logger.Trace("Starting scene numbering update");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var ids = _xemCommunicationProvider.GetXemSeriesIds();
|
2013-02-19 07:56:02 +01:00
|
|
|
|
var series = _seriesRepository.All();
|
2012-10-18 08:49:53 +02:00
|
|
|
|
var wantedSeries = series.Where(s => ids.Contains(s.SeriesId)).ToList();
|
2012-10-17 07:00:28 +02:00
|
|
|
|
|
|
|
|
|
foreach(var ser in wantedSeries)
|
|
|
|
|
{
|
2012-10-18 06:15:42 +02:00
|
|
|
|
PerformUpdate(ser);
|
2012-10-17 07:00:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.Trace("Completed scene numbering update");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.WarnException("Error updating Scene Mappings", ex);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-10-18 06:15:42 +02:00
|
|
|
|
|
|
|
|
|
public virtual void UpdateMappings(int seriesId)
|
|
|
|
|
{
|
2012-11-14 21:31:44 +01:00
|
|
|
|
var xemIds = _xemCommunicationProvider.GetXemSeriesIds();
|
|
|
|
|
|
|
|
|
|
if (!xemIds.Contains(seriesId))
|
|
|
|
|
{
|
|
|
|
|
_logger.Trace("Xem doesn't have a mapping for this series: {0}", seriesId);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-19 07:56:02 +01:00
|
|
|
|
var series = _seriesRepository.Get(seriesId);
|
2012-10-18 06:15:42 +02:00
|
|
|
|
|
|
|
|
|
if (series == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.Trace("Series could not be found: {0}", seriesId);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PerformUpdate(series);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void PerformUpdate(Series series)
|
|
|
|
|
{
|
|
|
|
|
_logger.Trace("Updating scene numbering mapping for: {0}", series.Title);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var episodesToUpdate = new List<Episode>();
|
|
|
|
|
var mappings = _xemCommunicationProvider.GetSceneTvdbMappings(series.SeriesId);
|
|
|
|
|
|
|
|
|
|
if (mappings == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.Trace("Mappings for: {0} are null, skipping", series.Title);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-24 00:08:22 +01:00
|
|
|
|
var episodes = _episodeService.GetEpisodeBySeries(series.OID);
|
2012-10-18 08:49:53 +02:00
|
|
|
|
|
2012-10-18 06:15:42 +02:00
|
|
|
|
foreach (var mapping in mappings)
|
|
|
|
|
{
|
|
|
|
|
_logger.Trace("Setting scene numbering mappings for {0} S{1:00}E{2:00}", series.Title, mapping.Tvdb.Season, mapping.Tvdb.Episode);
|
|
|
|
|
|
2012-10-18 08:49:53 +02: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 06:15:42 +02:00
|
|
|
|
episode.AbsoluteEpisodeNumber = mapping.Scene.Absolute;
|
|
|
|
|
episode.SceneSeasonNumber = mapping.Scene.Season;
|
|
|
|
|
episode.SceneEpisodeNumber = mapping.Scene.Episode;
|
|
|
|
|
episodesToUpdate.Add(episode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.Trace("Committing scene numbering mappings to database for: {0}", series.Title);
|
2013-02-20 03:05:15 +01:00
|
|
|
|
_episodeService.UpdateEpisodes(episodesToUpdate);
|
2012-10-18 08:49:53 +02:00
|
|
|
|
|
|
|
|
|
_logger.Trace("Setting UseSceneMapping for {0}", series.Title);
|
|
|
|
|
series.UseSceneNumbering = true;
|
2013-02-19 07:56:02 +01:00
|
|
|
|
_seriesRepository.Update(series);
|
2012-10-18 06:15:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.WarnException("Error updating scene numbering mappings for: " + series, ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-10-17 07:00:28 +02:00
|
|
|
|
}
|
|
|
|
|
}
|