|
|
@ -1,16 +1,12 @@
|
|
|
|
using System;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
using NLog;
|
|
|
|
using NLog;
|
|
|
|
|
|
|
|
using NzbDrone.Common;
|
|
|
|
using NzbDrone.Common.Disk;
|
|
|
|
using NzbDrone.Common.Disk;
|
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
|
|
|
|
|
|
|
using NzbDrone.Common.Instrumentation;
|
|
|
|
|
|
|
|
using NzbDrone.Core.Configuration;
|
|
|
|
using NzbDrone.Core.Configuration;
|
|
|
|
using NzbDrone.Core.Instrumentation;
|
|
|
|
using NzbDrone.Core.Instrumentation;
|
|
|
|
using NzbDrone.Core.MediaFiles.Commands;
|
|
|
|
|
|
|
|
using NzbDrone.Core.MediaFiles.Events;
|
|
|
|
using NzbDrone.Core.MediaFiles.Events;
|
|
|
|
using NzbDrone.Core.Messaging.Commands;
|
|
|
|
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
|
|
|
|
|
|
|
@ -18,124 +14,119 @@ namespace NzbDrone.Core.MediaFiles
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public interface IUpdateEpisodeFileService
|
|
|
|
public interface IUpdateEpisodeFileService
|
|
|
|
{
|
|
|
|
{
|
|
|
|
void ChangeFileDateToAirdate(EpisodeFile episodeFile, Series series);
|
|
|
|
void ChangeFileDateForFile(EpisodeFile episodeFile, Series series, List<Episode> episodes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class UpdateEpisodeFileService : IUpdateEpisodeFileService,
|
|
|
|
public class UpdateEpisodeFileService : IUpdateEpisodeFileService,
|
|
|
|
IExecute<AirDateSeriesCommand>,
|
|
|
|
|
|
|
|
IHandle<SeriesScannedEvent>
|
|
|
|
IHandle<SeriesScannedEvent>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
private readonly IDiskProvider _diskProvider;
|
|
|
|
private readonly IDiskProvider _diskProvider;
|
|
|
|
private readonly IConfigService _configService;
|
|
|
|
private readonly IConfigService _configService;
|
|
|
|
private readonly ISeriesService _seriesService;
|
|
|
|
|
|
|
|
private readonly IEpisodeService _episodeService;
|
|
|
|
private readonly IEpisodeService _episodeService;
|
|
|
|
private readonly IEventAggregator _eventAggregator;
|
|
|
|
|
|
|
|
private readonly Logger _logger;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
|
|
|
|
public UpdateEpisodeFileService(IDiskProvider diskProvider,
|
|
|
|
public UpdateEpisodeFileService(IDiskProvider diskProvider,
|
|
|
|
IConfigService configService,
|
|
|
|
IConfigService configService,
|
|
|
|
ISeriesService seriesService,
|
|
|
|
|
|
|
|
IEpisodeService episodeService,
|
|
|
|
IEpisodeService episodeService,
|
|
|
|
IEventAggregator eventAggregator,
|
|
|
|
|
|
|
|
Logger logger)
|
|
|
|
Logger logger)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_diskProvider = diskProvider;
|
|
|
|
_diskProvider = diskProvider;
|
|
|
|
_configService = configService;
|
|
|
|
_configService = configService;
|
|
|
|
_seriesService = seriesService;
|
|
|
|
|
|
|
|
_episodeService = episodeService;
|
|
|
|
_episodeService = episodeService;
|
|
|
|
_eventAggregator = eventAggregator;
|
|
|
|
|
|
|
|
_logger = logger;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void ChangeFileDateToAirdate(EpisodeFile episodeFile, Series series)
|
|
|
|
public void ChangeFileDateForFile(EpisodeFile episodeFile, Series series, List<Episode> episodes)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var episode = new Episode();
|
|
|
|
ChangeFileDate(episodeFile, series, episodes);
|
|
|
|
episode.AirDate = episodeFile.Episodes.Value.First().AirDate;
|
|
|
|
|
|
|
|
episode.EpisodeFile = episodeFile;
|
|
|
|
|
|
|
|
episode.EpisodeFileId = 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var episodes = new List<Episode>();
|
|
|
|
|
|
|
|
episodes.Add(episode);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ChangeFileDateToAirdate(episodes, series);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ChangeFileDateToAirdate(List<Episode> episodes, Series series)
|
|
|
|
private bool ChangeFileDate(EpisodeFile episodeFile, Series series, List<Episode> episodes)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (!episodes.Any())
|
|
|
|
switch (_configService.FileDate)
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.ProgressDebug("{0} has no media files available to update with air dates", series.Title);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var done = new List<Episode>();
|
|
|
|
case FileDateType.LocalAirDate:
|
|
|
|
|
|
|
|
|
|
|
|
_logger.ProgressDebug("{0} ... checking {1} media file dates match air date", series.Title, episodes.Count);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var episode in episodes)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (episode.HasFile
|
|
|
|
var airDate = episodes.First().AirDate;
|
|
|
|
&& episode.EpisodeFile.IsLoaded
|
|
|
|
var airTime = series.AirTime;
|
|
|
|
&& ChangeFileDate(episode.EpisodeFile.Value.Path, episode.AirDate, series.AirTime))
|
|
|
|
|
|
|
|
|
|
|
|
if (airDate.IsNullOrWhiteSpace() || airTime.IsNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
{
|
|
|
|
done.Add(episode);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (done.Any())
|
|
|
|
return ChangeFileDateToLocalAirDate(episodeFile.Path, airDate, airTime);
|
|
|
|
{
|
|
|
|
|
|
|
|
_eventAggregator.PublishEvent(new SeriesAirDatedEvent(series));
|
|
|
|
|
|
|
|
_logger.ProgressDebug("{0} had {1} of {2} media file dates changed to the date and time the episode aired", series.Title, done.Count, episodes.Count);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
case FileDateType.UtcAirDate:
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_logger.ProgressDebug("{0} has all its media file dates matching the date each aired", series.Title);
|
|
|
|
var airDateUtc = episodes.First().AirDateUtc;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!airDateUtc.HasValue)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ChangeFileDateToUtcAirDate(episodeFile.Path, airDateUtc.Value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Execute(AirDateSeriesCommand message)
|
|
|
|
public void Handle(SeriesScannedEvent message)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var seriesToAirDate = _seriesService.GetSeries(message.SeriesIds);
|
|
|
|
if (_configService.FileDate == FileDateType.None)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var episodes = _episodeService.EpisodesWithFiles(message.Series.Id);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var episodeFiles = new List<EpisodeFile>();
|
|
|
|
|
|
|
|
var updated = new List<EpisodeFile>();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var series in seriesToAirDate)
|
|
|
|
foreach (var group in episodes.GroupBy(e => e.EpisodeFileId))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var episodes = _episodeService.EpisodesWithFiles(series.Id);
|
|
|
|
var episodesInFile = group.Select(e => e).ToList();
|
|
|
|
|
|
|
|
var episodeFile = episodesInFile.First().EpisodeFile;
|
|
|
|
|
|
|
|
|
|
|
|
ChangeFileDateToAirdate(episodes, series);
|
|
|
|
episodeFiles.Add(episodeFile);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (ChangeFileDate(episodeFile, message.Series, episodesInFile))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
updated.Add(episodeFile);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Handle(SeriesScannedEvent message)
|
|
|
|
if (updated.Any())
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (_configService.FileDateAiredDate)
|
|
|
|
_logger.ProgressDebug("Changed file date for {0} files of {1} in {2}", updated.Count, episodeFiles.Count, message.Series.Title);
|
|
|
|
{
|
|
|
|
}
|
|
|
|
var episodes = _episodeService.EpisodesWithFiles(message.Series.Id);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ChangeFileDateToAirdate(episodes, message.Series);
|
|
|
|
else
|
|
|
|
}
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.ProgressDebug("No file dates changed for {0}", message.Series.Title);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private bool ChangeFileDate(String filePath, String fileDate, String fileTime)
|
|
|
|
private bool ChangeFileDateToLocalAirDate(string filePath, string fileDate, string fileTime)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
DateTime dateTime, oldDateTime;
|
|
|
|
DateTime airDate;
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (DateTime.TryParse(fileDate + ' ' + fileTime, out dateTime))
|
|
|
|
if (DateTime.TryParse(fileDate + ' ' + fileTime, out airDate))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// avoiding false +ve checks and set date skewing by not using UTC (Windows)
|
|
|
|
// avoiding false +ve checks and set date skewing by not using UTC (Windows)
|
|
|
|
oldDateTime = _diskProvider.GetLastFileWrite(filePath);
|
|
|
|
DateTime oldDateTime = _diskProvider.FileGetLastWriteUtc(filePath);
|
|
|
|
|
|
|
|
|
|
|
|
if (!DateTime.Equals(dateTime, oldDateTime))
|
|
|
|
if (!DateTime.Equals(airDate, oldDateTime))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
try
|
|
|
|
try
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_diskProvider.FileSetLastWriteTime(filePath, dateTime);
|
|
|
|
_diskProvider.FileSetLastWriteTime(filePath, airDate);
|
|
|
|
_diskProvider.FileSetLastAccessTime(filePath, dateTime);
|
|
|
|
_logger.Debug("Date of file [{0}] changed from '{1}' to '{2}'", filePath, oldDateTime, airDate);
|
|
|
|
_logger.Info("Date of file [{0}] changed from \"{1}\" to \"{2}\"", filePath, oldDateTime, dateTime);
|
|
|
|
|
|
|
|
result = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
catch (Exception ex)
|
|
|
@ -147,10 +138,33 @@ namespace NzbDrone.Core.MediaFiles
|
|
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_logger.Warn("Could not create valid date to set [{0}]", filePath);
|
|
|
|
_logger.Debug("Could not create valid date to change file [{0}]", filePath);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private bool ChangeFileDateToUtcAirDate(string filePath, DateTime airDateUtc)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
DateTime oldLastWrite = _diskProvider.FileGetLastWriteUtc(filePath);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!DateTime.Equals(airDateUtc, oldLastWrite))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_diskProvider.FileSetLastWriteTime(filePath, airDateUtc);
|
|
|
|
|
|
|
|
_logger.Debug("Date of file [{0}] changed from '{1}' to '{2}'", filePath, oldLastWrite, airDateUtc);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.WarnException("Unable to set date of file [" + filePath + "]", ex);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|