You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
3.7 KiB
100 lines
3.7 KiB
12 years ago
|
using System;
|
||
13 years ago
|
using System.Collections.Generic;
|
||
13 years ago
|
using System.Linq;
|
||
13 years ago
|
using NLog;
|
||
12 years ago
|
using NzbDrone.Common.Eventing;
|
||
|
using NzbDrone.Core.Download;
|
||
12 years ago
|
using NzbDrone.Core.MediaFiles;
|
||
13 years ago
|
using NzbDrone.Core.Model.Notification;
|
||
13 years ago
|
using NzbDrone.Core.Providers;
|
||
12 years ago
|
using NzbDrone.Core.Tv;
|
||
13 years ago
|
|
||
12 years ago
|
namespace NzbDrone.Core.Jobs.Implementations
|
||
13 years ago
|
{
|
||
|
public class RenameSeasonJob : IJob
|
||
|
{
|
||
12 years ago
|
private readonly IMediaFileService _mediaFileService;
|
||
13 years ago
|
private readonly DiskScanProvider _diskScanProvider;
|
||
12 years ago
|
private readonly ISeriesRepository _seriesRepository;
|
||
12 years ago
|
private readonly IEventAggregator _eventAggregator;
|
||
13 years ago
|
|
||
13 years ago
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
||
13 years ago
|
|
||
12 years ago
|
public RenameSeasonJob(IMediaFileService mediaFileService, DiskScanProvider diskScanProvider, ISeriesService seriesService,
|
||
12 years ago
|
ISeriesRepository seriesRepository, IEventAggregator eventAggregator)
|
||
13 years ago
|
{
|
||
12 years ago
|
_mediaFileService = mediaFileService;
|
||
13 years ago
|
_diskScanProvider = diskScanProvider;
|
||
12 years ago
|
_seriesRepository = seriesRepository;
|
||
12 years ago
|
_eventAggregator = eventAggregator;
|
||
13 years ago
|
}
|
||
|
|
||
|
public string Name
|
||
|
{
|
||
|
get { return "Rename Season"; }
|
||
|
}
|
||
|
|
||
13 years ago
|
public TimeSpan DefaultInterval
|
||
13 years ago
|
{
|
||
13 years ago
|
get { return TimeSpan.FromTicks(0); }
|
||
13 years ago
|
}
|
||
|
|
||
12 years ago
|
public void Start(ProgressNotification notification, dynamic options)
|
||
13 years ago
|
{
|
||
12 years ago
|
if (options == null || options.SeriesId <= 0)
|
||
|
throw new ArgumentException("options");
|
||
13 years ago
|
|
||
12 years ago
|
if (options.SeasonNumber < 0)
|
||
|
throw new ArgumentException("options.SeasonNumber");
|
||
13 years ago
|
|
||
12 years ago
|
var series = _seriesRepository.Get((int)options.SeriesId);
|
||
13 years ago
|
|
||
12 years ago
|
notification.CurrentMessage = String.Format("Renaming episodes for {0} Season {1}", series.Title, options.SeasonNumber);
|
||
13 years ago
|
|
||
12 years ago
|
logger.Debug("Getting episodes from database for series: {0} and season: {1}", options.SeriesId, options.SeasonNumber);
|
||
12 years ago
|
IList<EpisodeFile> episodeFiles = _mediaFileService.GetFilesBySeason((int)options.SeriesId, (int)options.SeasonNumber);
|
||
13 years ago
|
|
||
13 years ago
|
if (episodeFiles == null || !episodeFiles.Any())
|
||
13 years ago
|
{
|
||
12 years ago
|
logger.Warn("No episodes in database found for series: {0} and season: {1}.", options.SeriesId, options.SeasonNumber);
|
||
13 years ago
|
return;
|
||
|
}
|
||
|
|
||
13 years ago
|
var newEpisodeFiles = new List<EpisodeFile>();
|
||
|
var oldEpisodeFiles = new List<EpisodeFile>();
|
||
|
|
||
13 years ago
|
foreach (var episodeFile in episodeFiles)
|
||
|
{
|
||
13 years ago
|
try
|
||
|
{
|
||
13 years ago
|
var oldFile = new EpisodeFile(episodeFile);
|
||
13 years ago
|
var newFile = _diskScanProvider.MoveEpisodeFile(episodeFile);
|
||
|
|
||
|
if (newFile != null)
|
||
|
{
|
||
|
newEpisodeFiles.Add(newFile);
|
||
13 years ago
|
oldEpisodeFiles.Add(oldFile);
|
||
13 years ago
|
}
|
||
13 years ago
|
}
|
||
13 years ago
|
|
||
|
catch (Exception e)
|
||
13 years ago
|
{
|
||
13 years ago
|
logger.WarnException("An error has occurred while renaming file", e);
|
||
13 years ago
|
}
|
||
13 years ago
|
}
|
||
|
|
||
12 years ago
|
if (!oldEpisodeFiles.Any())
|
||
12 years ago
|
{
|
||
|
logger.Trace("No episodes were renamed for: {0} Season {1}, no changes were made", series.Title,
|
||
|
options.SeasonNumber);
|
||
|
notification.CurrentMessage = String.Format("Rename completed for: {0} Season {1}, no changes were made", series.Title, options.SeasonNumber);
|
||
|
return;
|
||
|
}
|
||
|
|
||
13 years ago
|
//Start AfterRename
|
||
12 years ago
|
_eventAggregator.Publish(new SeriesRenamedEvent(series));
|
||
13 years ago
|
|
||
12 years ago
|
notification.CurrentMessage = String.Format("Rename completed for {0} Season {1}", series.Title, options.SeasonNumber);
|
||
13 years ago
|
}
|
||
|
}
|
||
|
}
|