|
|
|
@ -1,16 +1,12 @@
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Common;
|
|
|
|
|
using NzbDrone.Common.Disk;
|
|
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
|
|
|
|
using NzbDrone.Common.Instrumentation;
|
|
|
|
|
using NzbDrone.Core.Configuration;
|
|
|
|
|
using NzbDrone.Core.Instrumentation;
|
|
|
|
|
using NzbDrone.Core.MediaFiles.Commands;
|
|
|
|
|
using NzbDrone.Core.MediaFiles.Events;
|
|
|
|
|
using NzbDrone.Core.Messaging.Commands;
|
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
|
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
|
|
|
|
|
@ -18,124 +14,119 @@ namespace NzbDrone.Core.MediaFiles
|
|
|
|
|
{
|
|
|
|
|
public interface IUpdateEpisodeFileService
|
|
|
|
|
{
|
|
|
|
|
void ChangeFileDateToAirdate(EpisodeFile episodeFile, Series series);
|
|
|
|
|
void ChangeFileDateForFile(EpisodeFile episodeFile, Series series, List<Episode> episodes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class UpdateEpisodeFileService : IUpdateEpisodeFileService,
|
|
|
|
|
IExecute<AirDateSeriesCommand>,
|
|
|
|
|
IHandle<SeriesScannedEvent>
|
|
|
|
|
{
|
|
|
|
|
private readonly IDiskProvider _diskProvider;
|
|
|
|
|
private readonly IConfigService _configService;
|
|
|
|
|
private readonly ISeriesService _seriesService;
|
|
|
|
|
private readonly IEpisodeService _episodeService;
|
|
|
|
|
private readonly IEventAggregator _eventAggregator;
|
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
|
|
public UpdateEpisodeFileService(IDiskProvider diskProvider,
|
|
|
|
|
IConfigService configService,
|
|
|
|
|
ISeriesService seriesService,
|
|
|
|
|
IEpisodeService episodeService,
|
|
|
|
|
IEventAggregator eventAggregator,
|
|
|
|
|
Logger logger)
|
|
|
|
|
{
|
|
|
|
|
_diskProvider = diskProvider;
|
|
|
|
|
_configService = configService;
|
|
|
|
|
_seriesService = seriesService;
|
|
|
|
|
_episodeService = episodeService;
|
|
|
|
|
_eventAggregator = eventAggregator;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ChangeFileDateToAirdate(EpisodeFile episodeFile, Series series)
|
|
|
|
|
public void ChangeFileDateForFile(EpisodeFile episodeFile, Series series, List<Episode> episodes)
|
|
|
|
|
{
|
|
|
|
|
var episode = new Episode();
|
|
|
|
|
episode.AirDate = episodeFile.Episodes.Value.First().AirDate;
|
|
|
|
|
episode.EpisodeFile = episodeFile;
|
|
|
|
|
episode.EpisodeFileId = 1;
|
|
|
|
|
|
|
|
|
|
var episodes = new List<Episode>();
|
|
|
|
|
episodes.Add(episode);
|
|
|
|
|
|
|
|
|
|
ChangeFileDateToAirdate(episodes, series);
|
|
|
|
|
ChangeFileDate(episodeFile, series, episodes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ChangeFileDateToAirdate(List<Episode> episodes, Series series)
|
|
|
|
|
private bool ChangeFileDate(EpisodeFile episodeFile, Series series, List<Episode> episodes)
|
|
|
|
|
{
|
|
|
|
|
if (!episodes.Any())
|
|
|
|
|
{
|
|
|
|
|
_logger.ProgressDebug("{0} has no media files available to update with air dates", series.Title);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
switch (_configService.FileDate)
|
|
|
|
|
{
|
|
|
|
|
var done = new List<Episode>();
|
|
|
|
|
|
|
|
|
|
_logger.ProgressDebug("{0} ... checking {1} media file dates match air date", series.Title, episodes.Count);
|
|
|
|
|
|
|
|
|
|
foreach (var episode in episodes)
|
|
|
|
|
case FileDateType.LocalAirDate:
|
|
|
|
|
{
|
|
|
|
|
if (episode.HasFile
|
|
|
|
|
&& episode.EpisodeFile.IsLoaded
|
|
|
|
|
&& ChangeFileDate(episode.EpisodeFile.Value.Path, episode.AirDate, series.AirTime))
|
|
|
|
|
var airDate = episodes.First().AirDate;
|
|
|
|
|
var airTime = series.AirTime;
|
|
|
|
|
|
|
|
|
|
if (airDate.IsNullOrWhiteSpace() || airTime.IsNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
done.Add(episode);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (done.Any())
|
|
|
|
|
{
|
|
|
|
|
_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);
|
|
|
|
|
return ChangeFileDateToLocalAirDate(episodeFile.Path, airDate, airTime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 (_configService.FileDateAiredDate)
|
|
|
|
|
{
|
|
|
|
|
var episodes = _episodeService.EpisodesWithFiles(message.Series.Id);
|
|
|
|
|
if (updated.Any())
|
|
|
|
|
{
|
|
|
|
|
_logger.ProgressDebug("Changed file date for {0} files of {1} in {2}", updated.Count, episodeFiles.Count, message.Series.Title);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
bool result = false;
|
|
|
|
|
DateTime airDate;
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
oldDateTime = _diskProvider.GetLastFileWrite(filePath);
|
|
|
|
|
DateTime oldDateTime = _diskProvider.FileGetLastWriteUtc(filePath);
|
|
|
|
|
|
|
|
|
|
if (!DateTime.Equals(dateTime, oldDateTime))
|
|
|
|
|
if (!DateTime.Equals(airDate, oldDateTime))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_diskProvider.FileSetLastWriteTime(filePath, dateTime);
|
|
|
|
|
_diskProvider.FileSetLastAccessTime(filePath, dateTime);
|
|
|
|
|
_logger.Info("Date of file [{0}] changed from \"{1}\" to \"{2}\"", filePath, oldDateTime, dateTime);
|
|
|
|
|
result = true;
|
|
|
|
|
_diskProvider.FileSetLastWriteTime(filePath, airDate);
|
|
|
|
|
_logger.Debug("Date of file [{0}] changed from '{1}' to '{2}'", filePath, oldDateTime, airDate);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
@ -147,10 +138,33 @@ namespace NzbDrone.Core.MediaFiles
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|