mirror of
https://github.com/lidarr/lidarr.git
synced 2025-07-30 03:38:26 -07:00
Set episode file modified date to local or utc air date
This commit is contained in:
parent
8478379ff4
commit
a02108922f
26 changed files with 175 additions and 202 deletions
|
@ -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())
|
||||
switch (_configService.FileDate)
|
||||
{
|
||||
_logger.ProgressDebug("{0} has no media files available to update with air dates", series.Title);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
return ChangeFileDateToLocalAirDate(episodeFile.Path, airDate, airTime);
|
||||
}
|
||||
|
||||
if (done.Any())
|
||||
case FileDateType.UtcAirDate:
|
||||
{
|
||||
_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);
|
||||
}
|
||||
var airDateUtc = episodes.First().AirDateUtc;
|
||||
|
||||
else
|
||||
{
|
||||
_logger.ProgressDebug("{0} has all its media file dates matching the date each aired", series.Title);
|
||||
if (!airDateUtc.HasValue)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return ChangeFileDateToUtcAirDate(episodeFile.Path, airDateUtc.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Execute(AirDateSeriesCommand message)
|
||||
{
|
||||
var seriesToAirDate = _seriesService.GetSeries(message.SeriesIds);
|
||||
|
||||
foreach (var series in seriesToAirDate)
|
||||
{
|
||||
var episodes = _episodeService.EpisodesWithFiles(series.Id);
|
||||
|
||||
ChangeFileDateToAirdate(episodes, series);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Handle(SeriesScannedEvent message)
|
||||
{
|
||||
if (_configService.FileDateAiredDate)
|
||||
{
|
||||
var episodes = _episodeService.EpisodesWithFiles(message.Series.Id);
|
||||
if (_configService.FileDate == FileDateType.None)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ChangeFileDateToAirdate(episodes, message.Series);
|
||||
}
|
||||
var episodes = _episodeService.EpisodesWithFiles(message.Series.Id);
|
||||
|
||||
var episodeFiles = new List<EpisodeFile>();
|
||||
var updated = new List<EpisodeFile>();
|
||||
|
||||
foreach (var group in episodes.GroupBy(e => e.EpisodeFileId))
|
||||
{
|
||||
var episodesInFile = group.Select(e => e).ToList();
|
||||
var episodeFile = episodesInFile.First().EpisodeFile;
|
||||
|
||||
episodeFiles.Add(episodeFile);
|
||||
|
||||
if (ChangeFileDate(episodeFile, message.Series, episodesInFile))
|
||||
{
|
||||
updated.Add(episodeFile);
|
||||
}
|
||||
}
|
||||
|
||||
if (updated.Any())
|
||||
{
|
||||
_logger.ProgressDebug("Changed file date for {0} files of {1} in {2}", updated.Count, episodeFiles.Count, message.Series.Title);
|
||||
}
|
||||
|
||||
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 result;
|
||||
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 false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue