2013-06-02 22:29:00 +03:00
|
|
|
using System;
|
2013-09-10 08:22:38 +03:00
|
|
|
using System.Collections.Generic;
|
2013-08-20 18:35:39 +03:00
|
|
|
using System.IO;
|
2013-06-02 22:29:00 +03:00
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
2013-09-04 08:01:28 +03:00
|
|
|
using NzbDrone.Common.Instrumentation;
|
2013-06-02 22:29:00 +03:00
|
|
|
using NzbDrone.Common.Messaging;
|
2013-07-19 04:52:31 +03:00
|
|
|
using NzbDrone.Core.DataAugmentation.DailySeries;
|
2013-06-02 22:29:00 +03:00
|
|
|
using NzbDrone.Core.MetadataSource;
|
2013-06-03 08:39:42 +03:00
|
|
|
using NzbDrone.Core.Tv.Commands;
|
2013-06-02 22:29:00 +03:00
|
|
|
using NzbDrone.Core.Tv.Events;
|
2013-06-03 08:51:16 +03:00
|
|
|
using NzbDrone.Common;
|
2013-06-02 22:29:00 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Tv
|
|
|
|
{
|
|
|
|
public class RefreshSeriesService : IExecute<RefreshSeriesCommand>, IHandleAsync<SeriesAddedEvent>
|
|
|
|
{
|
|
|
|
private readonly IProvideSeriesInfo _seriesInfo;
|
|
|
|
private readonly ISeriesService _seriesService;
|
2013-08-01 05:02:36 +03:00
|
|
|
private readonly IRefreshEpisodeService _refreshEpisodeService;
|
2013-06-02 22:29:00 +03:00
|
|
|
private readonly IMessageAggregator _messageAggregator;
|
2013-07-19 04:52:31 +03:00
|
|
|
private readonly IDailySeriesService _dailySeriesService;
|
2013-06-02 22:29:00 +03:00
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2013-08-01 05:02:36 +03:00
|
|
|
public RefreshSeriesService(IProvideSeriesInfo seriesInfo, ISeriesService seriesService, IRefreshEpisodeService refreshEpisodeService, IMessageAggregator messageAggregator, IDailySeriesService dailySeriesService, Logger logger)
|
2013-06-02 22:29:00 +03:00
|
|
|
{
|
|
|
|
_seriesInfo = seriesInfo;
|
|
|
|
_seriesService = seriesService;
|
2013-08-01 05:02:36 +03:00
|
|
|
_refreshEpisodeService = refreshEpisodeService;
|
2013-06-02 22:29:00 +03:00
|
|
|
_messageAggregator = messageAggregator;
|
2013-07-19 04:52:31 +03:00
|
|
|
_dailySeriesService = dailySeriesService;
|
2013-06-02 22:29:00 +03:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2013-06-03 08:39:42 +03:00
|
|
|
private void RefreshSeriesInfo(Series series)
|
2013-06-02 22:29:00 +03:00
|
|
|
{
|
2013-09-04 08:01:28 +03:00
|
|
|
_logger.Progress("Starting Series Refresh for {0}", series.Title);
|
2013-06-02 22:29:00 +03:00
|
|
|
var tuple = _seriesInfo.GetSeriesInfo(series.TvdbId);
|
|
|
|
|
|
|
|
var seriesInfo = tuple.Item1;
|
|
|
|
|
|
|
|
series.Title = seriesInfo.Title;
|
|
|
|
series.AirTime = seriesInfo.AirTime;
|
|
|
|
series.Overview = seriesInfo.Overview;
|
|
|
|
series.Status = seriesInfo.Status;
|
2013-08-01 05:02:36 +03:00
|
|
|
series.CleanTitle = seriesInfo.CleanTitle;
|
2013-06-03 08:39:42 +03:00
|
|
|
series.LastInfoSync = DateTime.UtcNow;
|
2013-06-02 22:29:00 +03:00
|
|
|
series.Runtime = seriesInfo.Runtime;
|
|
|
|
series.Images = seriesInfo.Images;
|
|
|
|
series.Network = seriesInfo.Network;
|
|
|
|
series.FirstAired = seriesInfo.FirstAired;
|
2013-07-19 04:52:31 +03:00
|
|
|
|
|
|
|
if (_dailySeriesService.IsDailySeries(series.TvdbId))
|
|
|
|
{
|
|
|
|
series.SeriesType = SeriesTypes.Daily;
|
|
|
|
}
|
|
|
|
|
2013-08-20 18:35:39 +03:00
|
|
|
try
|
|
|
|
{
|
|
|
|
series.Path = new DirectoryInfo(series.Path).FullName;
|
|
|
|
series.Path = series.Path.GetActualCasing();
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
_logger.WarnException("Couldn't update series path for " + series.Path, e);
|
|
|
|
}
|
|
|
|
|
2013-09-10 08:22:38 +03:00
|
|
|
series.Seasons = UpdateSeasons(series, seriesInfo);
|
|
|
|
|
2013-06-02 22:29:00 +03:00
|
|
|
_seriesService.UpdateSeries(series);
|
2013-08-01 05:02:36 +03:00
|
|
|
_refreshEpisodeService.RefreshEpisodeInfo(series, tuple.Item2);
|
2013-06-02 22:29:00 +03:00
|
|
|
|
2013-09-04 08:01:28 +03:00
|
|
|
_logger.Complete("Finished series refresh for {0}", series.Title);
|
2013-06-02 22:29:00 +03:00
|
|
|
_messageAggregator.PublishEvent(new SeriesUpdatedEvent(series));
|
|
|
|
}
|
2013-09-04 08:01:28 +03:00
|
|
|
|
2013-09-10 08:22:38 +03:00
|
|
|
private List<Season> UpdateSeasons(Series series, Series seriesInfo)
|
|
|
|
{
|
|
|
|
foreach (var season in seriesInfo.Seasons)
|
|
|
|
{
|
|
|
|
var existingSeason = series.Seasons.SingleOrDefault(s => s.SeasonNumber == season.SeasonNumber);
|
|
|
|
|
|
|
|
if (existingSeason != null)
|
|
|
|
{
|
|
|
|
season.Monitored = existingSeason.Monitored;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return seriesInfo.Seasons;
|
|
|
|
}
|
|
|
|
|
2013-09-04 08:01:28 +03:00
|
|
|
public void Execute(RefreshSeriesCommand message)
|
|
|
|
{
|
|
|
|
if (message.SeriesId.HasValue)
|
|
|
|
{
|
|
|
|
var series = _seriesService.GetSeries(message.SeriesId.Value);
|
|
|
|
RefreshSeriesInfo(series);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var allSeries = _seriesService.GetAllSeries().OrderBy(c => c.LastInfoSync).ToList();
|
|
|
|
|
|
|
|
foreach (var series in allSeries)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
RefreshSeriesInfo(series);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
_logger.ErrorException("Couldn't refresh info for {0}".Inject(series), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void HandleAsync(SeriesAddedEvent message)
|
|
|
|
{
|
|
|
|
RefreshSeriesInfo(message.Series);
|
|
|
|
}
|
2013-06-02 22:29:00 +03:00
|
|
|
}
|
|
|
|
}
|