2013-03-07 00:20:33 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2013-11-07 04:48:51 +03:00
|
|
|
using System.Text.RegularExpressions;
|
2013-03-07 00:20:33 +03:00
|
|
|
using NLog;
|
2013-07-05 06:26:07 +03:00
|
|
|
using NzbDrone.Core.Configuration;
|
2013-03-07 00:20:33 +03:00
|
|
|
using NzbDrone.Core.MediaFiles;
|
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Organizer
|
|
|
|
{
|
|
|
|
public interface IBuildFileNames
|
|
|
|
{
|
2013-03-07 00:35:39 +03:00
|
|
|
string BuildFilename(IList<Episode> episodes, Series series, EpisodeFile episodeFile);
|
2013-08-12 03:42:11 +03:00
|
|
|
string BuildFilename(IList<Episode> episodes, Series series, EpisodeFile episodeFile, NamingConfig namingConfig);
|
2013-03-07 01:20:34 +03:00
|
|
|
string BuildFilePath(Series series, int seasonNumber, string fileName, string extension);
|
2013-03-07 00:20:33 +03:00
|
|
|
}
|
|
|
|
|
2013-04-25 07:27:49 +03:00
|
|
|
|
2013-03-07 00:20:33 +03:00
|
|
|
|
2013-04-25 07:27:49 +03:00
|
|
|
public class FileNameBuilder : IBuildFileNames
|
|
|
|
{
|
2013-07-05 06:26:07 +03:00
|
|
|
private readonly IConfigService _configService;
|
2013-04-25 07:27:49 +03:00
|
|
|
private readonly INamingConfigService _namingConfigService;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2013-11-07 04:48:51 +03:00
|
|
|
private static readonly Regex TitleRegex = new Regex(@"(?<token>\{(?:\w+)(?<separator>\s|\W|_)\w+\})",
|
|
|
|
RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
|
|
|
|
2013-11-15 07:40:56 +03:00
|
|
|
private static readonly Regex EpisodeRegex = new Regex(@"(?<episode>\{episode(?:\:0+)?})",
|
2013-11-07 04:48:51 +03:00
|
|
|
RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
|
|
|
|
2013-11-15 07:40:56 +03:00
|
|
|
private static readonly Regex SeasonRegex = new Regex(@"(?<season>\{season(?:\:0+)?})",
|
2013-11-07 04:48:51 +03:00
|
|
|
RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
|
|
|
|
2013-11-15 07:40:56 +03:00
|
|
|
private static readonly Regex SeasonEpisodePatternRegex = new Regex(@"(?<separator>(?<=}).+?)?(?<seasonEpisode>s?{season(?:\:0+)?}(?<episodeSeparator>e|x)?(?<episode>{episode(?:\:0+)?}))(?<separator>.+?(?={))?",
|
2013-11-07 04:48:51 +03:00
|
|
|
RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
|
|
|
|
2013-07-05 06:26:07 +03:00
|
|
|
public FileNameBuilder(INamingConfigService namingConfigService, IConfigService configService, Logger logger)
|
2013-03-07 00:20:33 +03:00
|
|
|
{
|
2013-04-25 07:27:49 +03:00
|
|
|
_namingConfigService = namingConfigService;
|
2013-07-05 06:26:07 +03:00
|
|
|
_configService = configService;
|
2013-04-25 07:27:49 +03:00
|
|
|
_logger = logger;
|
2013-03-07 00:20:33 +03:00
|
|
|
}
|
|
|
|
|
2013-03-07 00:35:39 +03:00
|
|
|
public string BuildFilename(IList<Episode> episodes, Series series, EpisodeFile episodeFile)
|
2013-03-07 00:20:33 +03:00
|
|
|
{
|
2013-04-25 07:27:49 +03:00
|
|
|
var nameSpec = _namingConfigService.GetConfig();
|
2013-03-07 00:20:33 +03:00
|
|
|
|
2013-08-12 03:42:11 +03:00
|
|
|
return BuildFilename(episodes, series, episodeFile, nameSpec);
|
|
|
|
}
|
|
|
|
|
2013-11-14 05:02:27 +03:00
|
|
|
public string BuildFilename(IList<Episode> episodes, Series series, EpisodeFile episodeFile, NamingConfig namingConfig)
|
2013-08-12 03:42:11 +03:00
|
|
|
{
|
2013-11-14 05:02:27 +03:00
|
|
|
if (!namingConfig.RenameEpisodes)
|
2013-03-07 00:20:33 +03:00
|
|
|
{
|
|
|
|
if (String.IsNullOrWhiteSpace(episodeFile.SceneName))
|
|
|
|
{
|
2013-03-07 01:20:34 +03:00
|
|
|
return Path.GetFileNameWithoutExtension(episodeFile.Path);
|
2013-03-07 00:20:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return episodeFile.SceneName;
|
|
|
|
}
|
|
|
|
|
2013-11-14 05:02:27 +03:00
|
|
|
if (String.IsNullOrWhiteSpace(namingConfig.StandardEpisodeFormat) && series.SeriesType == SeriesTypes.Standard)
|
2013-11-10 07:20:45 +03:00
|
|
|
{
|
|
|
|
throw new NamingFormatException("Standard episode format cannot be null");
|
|
|
|
}
|
|
|
|
|
2013-11-14 05:02:27 +03:00
|
|
|
if (String.IsNullOrWhiteSpace(namingConfig.DailyEpisodeFormat) && series.SeriesType == SeriesTypes.Daily)
|
2013-11-10 07:20:45 +03:00
|
|
|
{
|
|
|
|
throw new NamingFormatException("Daily episode format cannot be null");
|
|
|
|
}
|
|
|
|
|
2013-11-07 04:48:51 +03:00
|
|
|
var sortedEpisodes = episodes.OrderBy(e => e.EpisodeNumber).ToList();
|
2013-11-14 05:02:27 +03:00
|
|
|
var pattern = namingConfig.StandardEpisodeFormat;
|
2013-11-07 04:48:51 +03:00
|
|
|
var episodeTitles = new List<string>
|
2013-03-07 00:20:33 +03:00
|
|
|
{
|
2013-11-07 04:48:51 +03:00
|
|
|
Parser.Parser.CleanupEpisodeTitle(sortedEpisodes.First().Title)
|
|
|
|
};
|
2013-03-07 00:20:33 +03:00
|
|
|
|
2013-11-14 05:02:27 +03:00
|
|
|
var tokenValues = new Dictionary<string, string>(FilenameBuilderTokenEqualityComparer.Instance)
|
|
|
|
{
|
|
|
|
{"{Series Title}", series.Title}
|
|
|
|
};
|
2013-11-07 04:48:51 +03:00
|
|
|
|
|
|
|
if (series.SeriesType == SeriesTypes.Daily)
|
2013-03-07 00:20:33 +03:00
|
|
|
{
|
2013-11-14 05:02:27 +03:00
|
|
|
pattern = namingConfig.DailyEpisodeFormat;
|
2013-03-07 00:20:33 +03:00
|
|
|
|
2013-11-07 04:48:51 +03:00
|
|
|
if (!String.IsNullOrWhiteSpace(episodes.First().AirDate))
|
2013-03-07 00:20:33 +03:00
|
|
|
{
|
2013-11-10 07:20:45 +03:00
|
|
|
tokenValues.Add("{Air Date}", episodes.First().AirDate.Replace('-', ' '));
|
2013-03-07 00:20:33 +03:00
|
|
|
}
|
|
|
|
|
2013-11-07 04:48:51 +03:00
|
|
|
else {
|
|
|
|
tokenValues.Add("{Air Date}", "Unknown");
|
|
|
|
}
|
2013-03-07 00:20:33 +03:00
|
|
|
}
|
|
|
|
|
2013-11-07 04:48:51 +03:00
|
|
|
var seasonEpisode = SeasonEpisodePatternRegex.Match(pattern);
|
|
|
|
if (seasonEpisode.Success)
|
2013-03-07 00:20:33 +03:00
|
|
|
{
|
2013-11-07 04:48:51 +03:00
|
|
|
var episodeFormat = new EpisodeFormat
|
|
|
|
{
|
|
|
|
EpisodeSeparator = seasonEpisode.Groups["episodeSeparator"].Value,
|
|
|
|
Separator = seasonEpisode.Groups["separator"].Value,
|
|
|
|
EpisodePattern = seasonEpisode.Groups["episode"].Value,
|
|
|
|
SeasonEpisodePattern = seasonEpisode.Groups["seasonEpisode"].Value,
|
|
|
|
};
|
2013-03-07 00:20:33 +03:00
|
|
|
|
2013-11-07 04:48:51 +03:00
|
|
|
pattern = pattern.Replace(episodeFormat.SeasonEpisodePattern, "{Season Episode}");
|
|
|
|
var seasonEpisodePattern = episodeFormat.SeasonEpisodePattern;
|
2013-03-07 00:20:33 +03:00
|
|
|
|
2013-11-07 04:48:51 +03:00
|
|
|
foreach (var episode in sortedEpisodes.Skip(1))
|
|
|
|
{
|
2013-11-14 05:02:27 +03:00
|
|
|
switch ((MultiEpisodeStyle)namingConfig.MultiEpisodeStyle)
|
2013-11-07 04:48:51 +03:00
|
|
|
{
|
|
|
|
case MultiEpisodeStyle.Duplicate:
|
|
|
|
seasonEpisodePattern += episodeFormat.Separator + episodeFormat.SeasonEpisodePattern;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MultiEpisodeStyle.Repeat:
|
|
|
|
seasonEpisodePattern += episodeFormat.EpisodeSeparator + episodeFormat.EpisodePattern;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MultiEpisodeStyle.Scene:
|
|
|
|
seasonEpisodePattern += "-" + episodeFormat.EpisodeSeparator + episodeFormat.EpisodePattern;
|
|
|
|
break;
|
|
|
|
|
|
|
|
//MultiEpisodeStyle.Extend
|
|
|
|
default:
|
|
|
|
seasonEpisodePattern += "-" + episodeFormat.EpisodePattern;
|
|
|
|
break;
|
|
|
|
}
|
2013-03-07 00:20:33 +03:00
|
|
|
|
2013-11-07 04:48:51 +03:00
|
|
|
episodeTitles.Add(Parser.Parser.CleanupEpisodeTitle(episode.Title));
|
|
|
|
}
|
2013-03-07 00:20:33 +03:00
|
|
|
|
2013-11-07 04:48:51 +03:00
|
|
|
seasonEpisodePattern = ReplaceNumberTokens(seasonEpisodePattern, sortedEpisodes);
|
|
|
|
tokenValues.Add("{Season Episode}", seasonEpisodePattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
tokenValues.Add("{Episode Title}", String.Join(" + ", episodeTitles.Distinct()));
|
|
|
|
tokenValues.Add("{Quality Title}", episodeFile.Quality.ToString());
|
|
|
|
|
2013-03-07 00:20:33 +03:00
|
|
|
|
2013-11-07 04:48:51 +03:00
|
|
|
return CleanFilename(ReplaceTokens(pattern, tokenValues).Trim());
|
2013-03-07 00:20:33 +03:00
|
|
|
}
|
|
|
|
|
2013-03-07 01:20:34 +03:00
|
|
|
public string BuildFilePath(Series series, int seasonNumber, string fileName, string extension)
|
2013-03-07 00:20:33 +03:00
|
|
|
{
|
|
|
|
string path = series.Path;
|
|
|
|
if (series.SeasonFolder)
|
|
|
|
{
|
2013-10-05 08:23:10 +03:00
|
|
|
string seasonFolder;
|
|
|
|
|
|
|
|
if (seasonNumber == 0)
|
|
|
|
{
|
|
|
|
seasonFolder = "Specials";
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2013-11-14 05:02:27 +03:00
|
|
|
var tokenValues = new Dictionary<string, string>(FilenameBuilderTokenEqualityComparer.Instance);
|
2013-11-07 04:48:51 +03:00
|
|
|
tokenValues.Add("{Series Title}", series.Title);
|
|
|
|
|
|
|
|
seasonFolder = ReplaceSeasonTokens(_configService.SeasonFolderFormat, seasonNumber);
|
|
|
|
seasonFolder = ReplaceTokens(seasonFolder, tokenValues);
|
2013-10-05 08:23:10 +03:00
|
|
|
}
|
2013-03-07 00:20:33 +03:00
|
|
|
|
|
|
|
path = Path.Combine(path, seasonFolder);
|
|
|
|
}
|
|
|
|
|
2013-03-07 01:20:34 +03:00
|
|
|
return Path.Combine(path, fileName + extension);
|
2013-03-07 00:20:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public static string CleanFilename(string name)
|
|
|
|
{
|
|
|
|
string result = name;
|
|
|
|
string[] badCharacters = { "\\", "/", "<", ">", "?", "*", ":", "|", "\"" };
|
|
|
|
string[] goodCharacters = { "+", "+", "{", "}", "!", "@", "-", "#", "`" };
|
|
|
|
|
|
|
|
for (int i = 0; i < badCharacters.Length; i++)
|
|
|
|
result = result.Replace(badCharacters[i], goodCharacters[i]);
|
|
|
|
|
|
|
|
return result.Trim();
|
|
|
|
}
|
2013-03-07 01:20:34 +03:00
|
|
|
|
2013-11-07 04:48:51 +03:00
|
|
|
private string ReplaceTokens(string pattern, Dictionary<string, string> tokenValues)
|
|
|
|
{
|
|
|
|
return TitleRegex.Replace(pattern, match => ReplaceToken(match, tokenValues));
|
|
|
|
}
|
|
|
|
|
|
|
|
private string ReplaceToken(Match match, Dictionary<string, string> tokenValues)
|
|
|
|
{
|
|
|
|
var separator = match.Groups["separator"].Value;
|
|
|
|
var token = match.Groups["token"].Value;
|
2013-11-10 07:20:45 +03:00
|
|
|
var replacementText = "";
|
2013-11-07 04:48:51 +03:00
|
|
|
var patternTokenArray = token.ToCharArray();
|
2013-11-10 07:20:45 +03:00
|
|
|
if (!tokenValues.TryGetValue(token, out replacementText)) return null;
|
2013-11-07 04:48:51 +03:00
|
|
|
|
|
|
|
if (patternTokenArray.All(t => !char.IsLetter(t) || char.IsLower(t)))
|
|
|
|
{
|
|
|
|
replacementText = replacementText.ToLowerInvariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (patternTokenArray.All(t => !char.IsLetter(t) || char.IsUpper(t)))
|
|
|
|
{
|
|
|
|
replacementText = replacementText.ToUpper();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!separator.Equals(" "))
|
|
|
|
{
|
|
|
|
replacementText = replacementText.Replace(" ", separator);
|
|
|
|
}
|
|
|
|
|
|
|
|
return replacementText;
|
|
|
|
}
|
|
|
|
|
|
|
|
private string ReplaceNumberTokens(string pattern, List<Episode> episodes)
|
|
|
|
{
|
|
|
|
var episodeIndex = 0;
|
|
|
|
pattern = EpisodeRegex.Replace(pattern, match =>
|
|
|
|
{
|
|
|
|
var episode = episodes[episodeIndex].EpisodeNumber;
|
|
|
|
episodeIndex++;
|
|
|
|
|
|
|
|
return ReplaceNumberToken(match.Groups["episode"].Value, episode);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ReplaceSeasonTokens(pattern, episodes.First().SeasonNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
private string ReplaceSeasonTokens(string pattern, int seasonNumber)
|
|
|
|
{
|
|
|
|
return SeasonRegex.Replace(pattern, match => ReplaceNumberToken(match.Groups["season"].Value, seasonNumber));
|
|
|
|
}
|
|
|
|
|
|
|
|
private string ReplaceNumberToken(string token, int value)
|
|
|
|
{
|
2013-11-15 07:40:56 +03:00
|
|
|
var split = token.Trim('{', '}').Split(':');
|
|
|
|
if (split.Length == 1) return value.ToString("0");
|
2013-03-07 01:20:34 +03:00
|
|
|
|
2013-11-15 07:40:56 +03:00
|
|
|
return value.ToString(split[1]);
|
|
|
|
}
|
2013-03-07 00:20:33 +03:00
|
|
|
}
|
2013-11-07 04:48:51 +03:00
|
|
|
|
|
|
|
public enum MultiEpisodeStyle
|
|
|
|
{
|
|
|
|
Extend = 0,
|
|
|
|
Duplicate = 1,
|
|
|
|
Repeat = 2,
|
|
|
|
Scene = 3
|
|
|
|
}
|
2013-03-07 00:20:33 +03:00
|
|
|
}
|