auto-organize by date

Conflicts:
	MediaBrowser.Server.Implementations/FileOrganization/EpisodeFileOrganizer.cs
This commit is contained in:
Luke 2016-01-18 00:30:50 -05:00 committed by Luke Pulverenti
parent 0ffac65444
commit 4e567d04f9

View File

@ -74,26 +74,44 @@ namespace MediaBrowser.Server.Implementations.FileOrganization
if (!string.IsNullOrEmpty(seriesName)) if (!string.IsNullOrEmpty(seriesName))
{ {
var season = episodeInfo.SeasonNumber; var seasonNumber = episodeInfo.SeasonNumber;
result.ExtractedSeasonNumber = season; result.ExtractedSeasonNumber = seasonNumber;
if (season.HasValue)
{
// Passing in true will include a few extra regex's // Passing in true will include a few extra regex's
var episode = episodeInfo.EpisodeNumber; var episodeNumber = episodeInfo.EpisodeNumber;
result.ExtractedEpisodeNumber = episode; result.ExtractedEpisodeNumber = episodeNumber;
if (episode.HasValue) var premiereDate = episodeInfo.IsByDate ?
new DateTime(episodeInfo.Year.Value, episodeInfo.Month.Value, episodeInfo.Day.Value) :
(DateTime?)null;
if (episodeInfo.IsByDate || (seasonNumber.HasValue && episodeNumber.HasValue))
{ {
_logger.Debug("Extracted information from {0}. Series name {1}, Season {2}, Episode {3}", path, seriesName, season, episode); if (episodeInfo.IsByDate)
{
_logger.Debug("Extracted information from {0}. Series name {1}, Date {2}", path, seriesName, premiereDate.Value);
}
else
{
_logger.Debug("Extracted information from {0}. Series name {1}, Season {2}, Episode {3}", path, seriesName, seasonNumber, episodeNumber);
}
var endingEpisodeNumber = episodeInfo.EndingEpsiodeNumber; var endingEpisodeNumber = episodeInfo.EndingEpsiodeNumber;
result.ExtractedEndingEpisodeNumber = endingEpisodeNumber; result.ExtractedEndingEpisodeNumber = endingEpisodeNumber;
await OrganizeEpisode(path, seriesName, season.Value, episode.Value, endingEpisodeNumber, options, overwriteExisting, result, cancellationToken).ConfigureAwait(false); await OrganizeEpisode(path,
seriesName,
seasonNumber,
episodeNumber,
endingEpisodeNumber,
premiereDate,
options,
overwriteExisting,
result,
cancellationToken).ConfigureAwait(false);
} }
else else
{ {
@ -104,14 +122,6 @@ namespace MediaBrowser.Server.Implementations.FileOrganization
} }
} }
else else
{
var msg = string.Format("Unable to determine season number from {0}", path);
result.Status = FileSortingStatus.Failure;
result.StatusMessage = msg;
_logger.Warn(msg);
}
}
else
{ {
var msg = string.Format("Unable to determine series name from {0}", path); var msg = string.Format("Unable to determine series name from {0}", path);
result.Status = FileSortingStatus.Failure; result.Status = FileSortingStatus.Failure;
@ -141,14 +151,32 @@ namespace MediaBrowser.Server.Implementations.FileOrganization
var series = (Series)_libraryManager.GetItemById(new Guid(request.SeriesId)); var series = (Series)_libraryManager.GetItemById(new Guid(request.SeriesId));
await OrganizeEpisode(result.OriginalPath, series, request.SeasonNumber, request.EpisodeNumber, request.EndingEpisodeNumber, options, true, result, cancellationToken).ConfigureAwait(false); await OrganizeEpisode(result.OriginalPath,
series,
request.SeasonNumber,
request.EpisodeNumber,
request.EndingEpisodeNumber,
null,
options,
true,
result,
cancellationToken).ConfigureAwait(false);
await _organizationService.SaveResult(result, CancellationToken.None).ConfigureAwait(false); await _organizationService.SaveResult(result, CancellationToken.None).ConfigureAwait(false);
return result; return result;
} }
private Task OrganizeEpisode(string sourcePath, string seriesName, int seasonNumber, int episodeNumber, int? endingEpiosdeNumber, TvFileOrganizationOptions options, bool overwriteExisting, FileOrganizationResult result, CancellationToken cancellationToken) private Task OrganizeEpisode(string sourcePath,
string seriesName,
int? seasonNumber,
int? episodeNumber,
int? endingEpiosdeNumber,
DateTime? premiereDate,
TvFileOrganizationOptions options,
bool overwriteExisting,
FileOrganizationResult result,
CancellationToken cancellationToken)
{ {
var series = GetMatchingSeries(seriesName, result); var series = GetMatchingSeries(seriesName, result);
@ -161,15 +189,33 @@ namespace MediaBrowser.Server.Implementations.FileOrganization
return Task.FromResult(true); return Task.FromResult(true);
} }
return OrganizeEpisode(sourcePath, series, seasonNumber, episodeNumber, endingEpiosdeNumber, options, overwriteExisting, result, cancellationToken); return OrganizeEpisode(sourcePath,
series,
seasonNumber,
episodeNumber,
endingEpiosdeNumber,
premiereDate,
options,
overwriteExisting,
result,
cancellationToken);
} }
private async Task OrganizeEpisode(string sourcePath, Series series, int seasonNumber, int episodeNumber, int? endingEpiosdeNumber, TvFileOrganizationOptions options, bool overwriteExisting, FileOrganizationResult result, CancellationToken cancellationToken) private async Task OrganizeEpisode(string sourcePath,
Series series,
int? seasonNumber,
int? episodeNumber,
int? endingEpiosdeNumber,
DateTime? premiereDate,
TvFileOrganizationOptions options,
bool overwriteExisting,
FileOrganizationResult result,
CancellationToken cancellationToken)
{ {
_logger.Info("Sorting file {0} into series {1}", sourcePath, series.Path); _logger.Info("Sorting file {0} into series {1}", sourcePath, series.Path);
// Proceed to sort the file // Proceed to sort the file
var newPath = await GetNewPath(sourcePath, series, seasonNumber, episodeNumber, endingEpiosdeNumber, options, cancellationToken).ConfigureAwait(false); var newPath = await GetNewPath(sourcePath, series, seasonNumber, episodeNumber, endingEpiosdeNumber, premiereDate, options, cancellationToken).ConfigureAwait(false);
if (string.IsNullOrEmpty(newPath)) if (string.IsNullOrEmpty(newPath))
{ {
@ -278,8 +324,18 @@ namespace MediaBrowser.Server.Implementations.FileOrganization
} }
} }
private List<string> GetOtherDuplicatePaths(string targetPath, Series series, int seasonNumber, int episodeNumber, int? endingEpisodeNumber) private List<string> GetOtherDuplicatePaths(string targetPath,
Series series,
int? seasonNumber,
int? episodeNumber,
int? endingEpisodeNumber)
{ {
// TODO: Support date-naming?
if (!seasonNumber.HasValue || episodeNumber.HasValue)
{
return new List<string> ();
}
var episodePaths = series.GetRecursiveChildren() var episodePaths = series.GetRecursiveChildren()
.OfType<Episode>() .OfType<Episode>()
.Where(i => .Where(i =>
@ -408,7 +464,14 @@ namespace MediaBrowser.Server.Implementations.FileOrganization
/// <param name="endingEpisodeNumber">The ending episode number.</param> /// <param name="endingEpisodeNumber">The ending episode number.</param>
/// <param name="options">The options.</param> /// <param name="options">The options.</param>
/// <returns>System.String.</returns> /// <returns>System.String.</returns>
private async Task<string> GetNewPath(string sourcePath, Series series, int seasonNumber, int episodeNumber, int? endingEpisodeNumber, TvFileOrganizationOptions options, CancellationToken cancellationToken) private async Task<string> GetNewPath(string sourcePath,
Series series,
int? seasonNumber,
int? episodeNumber,
int? endingEpisodeNumber,
DateTime? premiereDate,
TvFileOrganizationOptions options,
CancellationToken cancellationToken)
{ {
var episodeInfo = new EpisodeInfo var episodeInfo = new EpisodeInfo
{ {
@ -417,7 +480,8 @@ namespace MediaBrowser.Server.Implementations.FileOrganization
MetadataCountryCode = series.GetPreferredMetadataCountryCode(), MetadataCountryCode = series.GetPreferredMetadataCountryCode(),
MetadataLanguage = series.GetPreferredMetadataLanguage(), MetadataLanguage = series.GetPreferredMetadataLanguage(),
ParentIndexNumber = seasonNumber, ParentIndexNumber = seasonNumber,
SeriesProviderIds = series.ProviderIds SeriesProviderIds = series.ProviderIds,
PremiereDate = premiereDate
}; };
var searchResults = await _providerManager.GetRemoteSearchResults<Episode, EpisodeInfo>(new RemoteSearchQuery<EpisodeInfo> var searchResults = await _providerManager.GetRemoteSearchResults<Episode, EpisodeInfo>(new RemoteSearchQuery<EpisodeInfo>
@ -428,13 +492,23 @@ namespace MediaBrowser.Server.Implementations.FileOrganization
var episode = searchResults.FirstOrDefault(); var episode = searchResults.FirstOrDefault();
string episodeName = string.Empty;
if (episode == null) if (episode == null)
{ {
_logger.Warn("No provider metadata found for {0} season {1} episode {2}", series.Name, seasonNumber, episodeNumber); var msg = string.Format("No provider metadata found for {0} season {1} episode {2}", series.Name, seasonNumber, episodeNumber);
return null; _logger.Warn(msg);
//throw new Exception(msg);
}
else
{
episodeName = episode.Name;
} }
var newPath = GetSeasonFolderPath(series, seasonNumber, options); seasonNumber = seasonNumber ?? episode.ParentIndexNumber;
episodeNumber = episodeNumber ?? episode.IndexNumber;
var newPath = GetSeasonFolderPath(series, seasonNumber.Value, options);
// MAX_PATH - trailing <NULL> charachter - drive component: 260 - 1 - 3 = 256 // MAX_PATH - trailing <NULL> charachter - drive component: 260 - 1 - 3 = 256
// Usually newPath would include the drive component, but use 256 to be sure // Usually newPath would include the drive component, but use 256 to be sure
@ -449,7 +523,7 @@ namespace MediaBrowser.Server.Implementations.FileOrganization
// Remove additional 4 chars to prevent PathTooLongException for downloaded subtitles (eg. filename.ext.eng.srt) // Remove additional 4 chars to prevent PathTooLongException for downloaded subtitles (eg. filename.ext.eng.srt)
maxFilenameLength -= 4; maxFilenameLength -= 4;
var episodeFileName = GetEpisodeFileName(sourcePath, series.Name, seasonNumber, episodeNumber, endingEpisodeNumber, episode.Name, options, maxFilenameLength); var episodeFileName = GetEpisodeFileName(sourcePath, series.Name, seasonNumber.Value, episodeNumber.Value, endingEpisodeNumber, episodeName, options, maxFilenameLength);
if (string.IsNullOrEmpty(episodeFileName)) if (string.IsNullOrEmpty(episodeFileName))
{ {
@ -505,7 +579,7 @@ namespace MediaBrowser.Server.Implementations.FileOrganization
{ {
seriesName = _fileSystem.GetValidFilename(seriesName).Trim(); seriesName = _fileSystem.GetValidFilename(seriesName).Trim();
if (episodeTitle == null) if (string.IsNullOrEmpty(episodeTitle))
{ {
episodeTitle = string.Empty; episodeTitle = string.Empty;
} }