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;
|
2014-05-19 22:14:41 +03:00
|
|
|
using NzbDrone.Common;
|
2013-11-21 09:10:00 +03:00
|
|
|
using NzbDrone.Common.Cache;
|
2014-07-24 02:43:54 +03:00
|
|
|
using NzbDrone.Common.EnsureThat;
|
2013-03-07 00:20:33 +03:00
|
|
|
using NzbDrone.Core.MediaFiles;
|
2014-01-18 14:44:36 +03:00
|
|
|
using NzbDrone.Core.Qualities;
|
2013-03-07 00:20:33 +03:00
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Organizer
|
|
|
|
{
|
|
|
|
public interface IBuildFileNames
|
|
|
|
{
|
2014-07-29 00:14:34 +03:00
|
|
|
string BuildFileName(List<Episode> episodes, Series series, EpisodeFile episodeFile, NamingConfig namingConfig = null);
|
|
|
|
string BuildFilePath(Series series, Int32 seasonNumber, String fileName, String extension);
|
2013-11-22 10:44:43 +03:00
|
|
|
BasicNamingConfig GetBasicNamingConfig(NamingConfig nameSpec);
|
2014-07-29 00:14:34 +03:00
|
|
|
string GetSeriesFolder(Series series, NamingConfig namingConfig = null);
|
|
|
|
string GetSeasonFolder(Series series, Int32 seasonNumber, NamingConfig namingConfig = null);
|
2013-03-07 00:20:33 +03:00
|
|
|
}
|
|
|
|
|
2013-04-25 07:27:49 +03:00
|
|
|
public class FileNameBuilder : IBuildFileNames
|
|
|
|
{
|
|
|
|
private readonly INamingConfigService _namingConfigService;
|
2014-01-18 14:44:36 +03:00
|
|
|
private readonly IQualityDefinitionService _qualityDefinitionService;
|
2014-09-05 23:05:52 +03:00
|
|
|
private readonly ICached<EpisodeFormat[]> _episodeFormatCache;
|
|
|
|
private readonly ICached<AbsoluteEpisodeFormat[]> _absoluteEpisodeFormatCache;
|
2013-04-25 07:27:49 +03:00
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2014-07-29 00:14:34 +03:00
|
|
|
private static readonly Regex TitleRegex = new Regex(@"\{(?<prefix>[- ._]*)(?<token>(?:[a-z0-9]+)(?:(?<separator>[- ._]+)(?:[a-z0-9]+))?)(?::(?<customFormat>[a-z0-9]+))?(?<suffix>[- ._]*)\}",
|
2013-11-07 04:48:51 +03:00
|
|
|
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);
|
|
|
|
|
2014-05-19 22:14:41 +03:00
|
|
|
private static readonly Regex AbsoluteEpisodeRegex = new Regex(@"(?<absolute>\{absolute(?:\:0+)?})",
|
|
|
|
RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
|
|
|
|
2014-10-27 16:38:53 +02:00
|
|
|
public static readonly Regex SeasonEpisodePatternRegex = new Regex(@"(?<separator>(?<=})[- ._]+?)?(?<seasonEpisode>s?{season(?:\:0+)?}(?<episodeSeparator>[- ._]?[ex])(?<episode>{episode(?:\:0+)?}))(?<separator>[- ._]+?(?={))?",
|
2013-11-07 04:48:51 +03:00
|
|
|
RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
|
|
|
|
2014-10-21 17:14:51 +03:00
|
|
|
public static readonly Regex AbsoluteEpisodePatternRegex = new Regex(@"(?<separator>(?<=})[- ._]+?)?(?<absolute>{absolute(?:\:0+)?})(?<separator>[- ._]+?(?={))?",
|
2014-05-19 22:14:41 +03:00
|
|
|
RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
|
|
|
|
2013-11-15 11:53:12 +03:00
|
|
|
public static readonly Regex AirDateRegex = new Regex(@"\{Air(\s|\W|_)Date\}", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
|
|
|
|
2014-10-21 17:14:51 +03:00
|
|
|
public static readonly Regex SeriesTitleRegex = new Regex(@"(?<token>\{(?:Series)(?<separator>[- ._])(Clean)?Title\})",
|
2014-01-17 21:43:15 +03:00
|
|
|
RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
|
|
|
|
2014-10-26 08:43:18 +02:00
|
|
|
private static readonly Regex OriginalTitleRegex = new Regex(@"(\^{original[- ._]title\}$)",
|
|
|
|
RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
|
|
|
|
2014-07-29 00:14:34 +03:00
|
|
|
private static readonly Regex FileNameCleanupRegex = new Regex(@"\.{2,}", RegexOptions.Compiled);
|
2014-05-01 23:29:09 +03:00
|
|
|
|
2014-07-29 00:14:34 +03:00
|
|
|
private static readonly char[] EpisodeTitleTrimCharacters = new[] { ' ', '.', '?' };
|
2014-02-12 11:59:22 +03:00
|
|
|
|
2013-11-21 09:10:00 +03:00
|
|
|
public FileNameBuilder(INamingConfigService namingConfigService,
|
2014-01-18 14:44:36 +03:00
|
|
|
IQualityDefinitionService qualityDefinitionService,
|
2014-04-01 23:07:41 +03:00
|
|
|
ICacheManager cacheManager,
|
2013-11-21 09:10:00 +03:00
|
|
|
Logger logger)
|
2013-03-07 00:20:33 +03:00
|
|
|
{
|
2013-04-25 07:27:49 +03:00
|
|
|
_namingConfigService = namingConfigService;
|
2014-01-18 14:44:36 +03:00
|
|
|
_qualityDefinitionService = qualityDefinitionService;
|
2014-09-05 23:05:52 +03:00
|
|
|
_episodeFormatCache = cacheManager.GetCache<EpisodeFormat[]>(GetType(), "episodeFormat");
|
|
|
|
_absoluteEpisodeFormatCache = cacheManager.GetCache<AbsoluteEpisodeFormat[]>(GetType(), "absoluteEpisodeFormat");
|
2013-04-25 07:27:49 +03:00
|
|
|
_logger = logger;
|
2013-03-07 00:20:33 +03:00
|
|
|
}
|
|
|
|
|
2014-07-29 00:14:34 +03:00
|
|
|
public string BuildFileName(List<Episode> episodes, Series series, EpisodeFile episodeFile, NamingConfig namingConfig = null)
|
2013-03-07 00:20:33 +03:00
|
|
|
{
|
2014-07-29 00:14:34 +03:00
|
|
|
if (namingConfig == null)
|
|
|
|
{
|
|
|
|
namingConfig = _namingConfigService.GetConfig();
|
|
|
|
}
|
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
|
|
|
{
|
2014-10-26 08:43:18 +02:00
|
|
|
return GetOriginalTitle(episodeFile);
|
2013-03-07 00:20:33 +03:00
|
|
|
}
|
|
|
|
|
2014-07-29 00:14:34 +03:00
|
|
|
if (namingConfig.StandardEpisodeFormat.IsNullOrWhiteSpace() && series.SeriesType == SeriesTypes.Standard)
|
2013-11-10 07:20:45 +03:00
|
|
|
{
|
2014-10-26 08:43:18 +02:00
|
|
|
throw new NamingFormatException("Standard episode format cannot be empty");
|
2013-11-10 07:20:45 +03:00
|
|
|
}
|
|
|
|
|
2014-07-29 00:14:34 +03:00
|
|
|
if (namingConfig.DailyEpisodeFormat.IsNullOrWhiteSpace() && series.SeriesType == SeriesTypes.Daily)
|
2013-11-10 07:20:45 +03:00
|
|
|
{
|
2014-10-26 08:43:18 +02:00
|
|
|
throw new NamingFormatException("Daily episode format cannot be empty");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (namingConfig.AnimeEpisodeFormat.IsNullOrWhiteSpace() && series.SeriesType == SeriesTypes.Anime)
|
|
|
|
{
|
|
|
|
throw new NamingFormatException("Anime episode format cannot be empty");
|
2013-11-10 07:20:45 +03:00
|
|
|
}
|
|
|
|
|
2013-11-14 05:02:27 +03:00
|
|
|
var pattern = namingConfig.StandardEpisodeFormat;
|
2014-07-29 00:14:34 +03:00
|
|
|
var tokenHandlers = new Dictionary<String, Func<TokenMatch, String>>(FileNameBuilderTokenEqualityComparer.Instance);
|
|
|
|
|
|
|
|
episodes = episodes.OrderBy(e => e.SeasonNumber).ThenBy(e => e.EpisodeNumber).ToList();
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-10-15 01:19:35 +03:00
|
|
|
if (series.SeriesType == SeriesTypes.Anime && episodes.All(e => e.AbsoluteEpisodeNumber.HasValue))
|
2014-05-19 22:14:41 +03:00
|
|
|
{
|
|
|
|
pattern = namingConfig.AnimeEpisodeFormat;
|
|
|
|
}
|
|
|
|
|
2014-09-05 23:05:52 +03:00
|
|
|
pattern = AddSeasonEpisodeNumberingTokens(pattern, tokenHandlers, episodes, namingConfig);
|
|
|
|
pattern = AddAbsoluteNumberingTokens(pattern, tokenHandlers, series, episodes, namingConfig);
|
2014-05-19 22:14:41 +03:00
|
|
|
|
2014-09-05 23:05:52 +03:00
|
|
|
AddSeriesTokens(tokenHandlers, series);
|
|
|
|
AddEpisodeTokens(tokenHandlers, episodes);
|
2014-09-01 05:28:42 +03:00
|
|
|
AddEpisodeFileTokens(tokenHandlers, series, episodeFile);
|
2014-09-05 23:05:52 +03:00
|
|
|
AddMediaInfoTokens(tokenHandlers, episodeFile);
|
|
|
|
|
2014-10-26 08:43:18 +02:00
|
|
|
var fileName = ReplaceTokens(pattern, tokenHandlers).Trim();
|
|
|
|
fileName = FileNameCleanupRegex.Replace(fileName, match => match.Captures[0].Value[0].ToString());
|
2014-05-01 23:29:09 +03:00
|
|
|
|
2014-10-26 08:43:18 +02:00
|
|
|
return fileName;
|
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
|
|
|
{
|
2014-07-24 02:43:54 +03:00
|
|
|
Ensure.That(extension, () => extension).IsNotNullOrWhiteSpace();
|
|
|
|
|
2013-03-07 00:20:33 +03:00
|
|
|
string path = series.Path;
|
2014-01-17 10:52:28 +03:00
|
|
|
|
2013-03-07 00:20:33 +03:00
|
|
|
if (series.SeasonFolder)
|
|
|
|
{
|
2013-10-05 08:23:10 +03:00
|
|
|
string seasonFolder;
|
|
|
|
|
|
|
|
if (seasonNumber == 0)
|
|
|
|
{
|
|
|
|
seasonFolder = "Specials";
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2013-11-21 08:34:38 +03:00
|
|
|
var nameSpec = _namingConfigService.GetConfig();
|
2014-07-29 00:14:34 +03:00
|
|
|
seasonFolder = GetSeasonFolder(series, seasonNumber, nameSpec);
|
2013-10-05 08:23:10 +03:00
|
|
|
}
|
2013-03-07 00:20:33 +03:00
|
|
|
|
2014-07-29 00:14:34 +03:00
|
|
|
seasonFolder = CleanFileName(seasonFolder);
|
2014-01-24 19:23:03 +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
|
|
|
}
|
|
|
|
|
2013-11-22 10:44:43 +03:00
|
|
|
public BasicNamingConfig GetBasicNamingConfig(NamingConfig nameSpec)
|
|
|
|
{
|
2014-09-05 23:05:52 +03:00
|
|
|
var episodeFormat = GetEpisodeFormat(nameSpec.StandardEpisodeFormat).LastOrDefault();
|
2013-11-22 10:44:43 +03:00
|
|
|
|
2013-11-22 19:59:03 +03:00
|
|
|
if (episodeFormat == null)
|
|
|
|
{
|
|
|
|
return new BasicNamingConfig();
|
|
|
|
}
|
|
|
|
|
2013-11-22 10:44:43 +03:00
|
|
|
var basicNamingConfig = new BasicNamingConfig
|
|
|
|
{
|
|
|
|
Separator = episodeFormat.Separator,
|
|
|
|
NumberStyle = episodeFormat.SeasonEpisodePattern
|
|
|
|
};
|
|
|
|
|
|
|
|
var titleTokens = TitleRegex.Matches(nameSpec.StandardEpisodeFormat);
|
|
|
|
|
|
|
|
foreach (Match match in titleTokens)
|
|
|
|
{
|
|
|
|
var separator = match.Groups["separator"].Value;
|
|
|
|
var token = match.Groups["token"].Value;
|
|
|
|
|
|
|
|
if (!separator.Equals(" "))
|
|
|
|
{
|
|
|
|
basicNamingConfig.ReplaceSpaces = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (token.StartsWith("{Series", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
{
|
|
|
|
basicNamingConfig.IncludeSeriesTitle = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (token.StartsWith("{Episode", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
{
|
|
|
|
basicNamingConfig.IncludeEpisodeTitle = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (token.StartsWith("{Quality", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
{
|
|
|
|
basicNamingConfig.IncludeQuality = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return basicNamingConfig;
|
|
|
|
}
|
|
|
|
|
2014-07-29 00:14:34 +03:00
|
|
|
public string GetSeriesFolder(Series series, NamingConfig namingConfig = null)
|
2014-04-15 17:16:49 +03:00
|
|
|
{
|
2014-07-29 00:14:34 +03:00
|
|
|
if (namingConfig == null)
|
|
|
|
{
|
|
|
|
namingConfig = _namingConfigService.GetConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
var tokenHandlers = new Dictionary<string, Func<TokenMatch, String>>(FileNameBuilderTokenEqualityComparer.Instance);
|
|
|
|
|
|
|
|
AddSeriesTokens(tokenHandlers, series);
|
2014-04-15 17:16:49 +03:00
|
|
|
|
2014-09-03 03:15:03 +03:00
|
|
|
return CleanFolderName(ReplaceTokens(namingConfig.SeriesFolderFormat, tokenHandlers));
|
2014-04-15 17:16:49 +03:00
|
|
|
}
|
|
|
|
|
2014-07-29 00:14:34 +03:00
|
|
|
public string GetSeasonFolder(Series series, Int32 seasonNumber, NamingConfig namingConfig = null)
|
2014-01-17 10:52:28 +03:00
|
|
|
{
|
2014-07-29 00:14:34 +03:00
|
|
|
if (namingConfig == null)
|
|
|
|
{
|
|
|
|
namingConfig = _namingConfigService.GetConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
var tokenHandlers = new Dictionary<string, Func<TokenMatch, String>>(FileNameBuilderTokenEqualityComparer.Instance);
|
2014-01-17 10:52:28 +03:00
|
|
|
|
2014-07-29 00:14:34 +03:00
|
|
|
AddSeriesTokens(tokenHandlers, series);
|
|
|
|
AddSeasonTokens(tokenHandlers, seasonNumber);
|
|
|
|
|
2014-09-03 03:15:03 +03:00
|
|
|
return CleanFolderName(ReplaceTokens(namingConfig.SeasonFolderFormat, tokenHandlers));
|
2014-04-15 17:16:49 +03:00
|
|
|
}
|
|
|
|
|
2014-07-29 00:14:34 +03:00
|
|
|
public static string CleanTitle(string name)
|
2014-04-15 17:16:49 +03:00
|
|
|
{
|
2014-07-29 00:14:34 +03:00
|
|
|
string[] dropCharacters = { ":", ".", "(", ")" };
|
|
|
|
|
|
|
|
string result = name;
|
|
|
|
|
|
|
|
for (int i = 0; i < dropCharacters.Length; i++)
|
|
|
|
{
|
|
|
|
result = result.Replace(dropCharacters[i], "");
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2014-01-17 10:52:28 +03:00
|
|
|
}
|
|
|
|
|
2014-07-29 00:14:34 +03:00
|
|
|
public static string CleanFileName(string name)
|
2013-03-07 00:20:33 +03:00
|
|
|
{
|
|
|
|
string result = name;
|
|
|
|
string[] badCharacters = { "\\", "/", "<", ">", "?", "*", ":", "|", "\"" };
|
2014-07-29 00:14:34 +03:00
|
|
|
string[] goodCharacters = { "+", "+", "", "", "!", "-", "-", "", "" };
|
2013-03-07 00:20:33 +03:00
|
|
|
|
|
|
|
for (int i = 0; i < badCharacters.Length; i++)
|
2014-07-29 00:14:34 +03:00
|
|
|
{
|
2013-03-07 00:20:33 +03:00
|
|
|
result = result.Replace(badCharacters[i], goodCharacters[i]);
|
2014-07-29 00:14:34 +03:00
|
|
|
}
|
2013-03-07 00:20:33 +03:00
|
|
|
|
|
|
|
return result.Trim();
|
|
|
|
}
|
2013-03-07 01:20:34 +03:00
|
|
|
|
2014-09-03 03:15:03 +03:00
|
|
|
public static string CleanFolderName(string name)
|
|
|
|
{
|
|
|
|
name = FileNameCleanupRegex.Replace(name, match => match.Captures[0].Value[0].ToString());
|
|
|
|
return name.Trim(' ', '.');
|
|
|
|
}
|
|
|
|
|
2014-07-29 00:14:34 +03:00
|
|
|
private void AddSeriesTokens(Dictionary<String, Func<TokenMatch, String>> tokenHandlers, Series series)
|
2014-04-10 20:58:50 +03:00
|
|
|
{
|
2014-07-29 00:14:34 +03:00
|
|
|
tokenHandlers["{Series Title}"] = m => series.Title;
|
|
|
|
tokenHandlers["{Series CleanTitle}"] = m => CleanTitle(series.Title);
|
2014-04-10 20:58:50 +03:00
|
|
|
}
|
|
|
|
|
2014-09-05 23:05:52 +03:00
|
|
|
private String AddSeasonEpisodeNumberingTokens(String pattern, Dictionary<String, Func<TokenMatch, String>> tokenHandlers, List<Episode> episodes, NamingConfig namingConfig)
|
|
|
|
{
|
|
|
|
var episodeFormats = GetEpisodeFormat(pattern).DistinctBy(v => v.SeasonEpisodePattern).ToList();
|
|
|
|
|
|
|
|
int index = 1;
|
|
|
|
foreach (var episodeFormat in episodeFormats)
|
|
|
|
{
|
|
|
|
var seasonEpisodePattern = episodeFormat.SeasonEpisodePattern;
|
2014-11-17 07:31:53 +02:00
|
|
|
string formatPattern;
|
2014-09-05 23:05:52 +03:00
|
|
|
|
2014-11-17 07:31:53 +02:00
|
|
|
switch ((MultiEpisodeStyle)namingConfig.MultiEpisodeStyle)
|
2014-09-05 23:05:52 +03:00
|
|
|
{
|
2014-11-17 07:31:53 +02:00
|
|
|
case MultiEpisodeStyle.Duplicate:
|
|
|
|
formatPattern = episodeFormat.Separator + episodeFormat.SeasonEpisodePattern;
|
|
|
|
seasonEpisodePattern = FormatNumberTokens(seasonEpisodePattern, formatPattern, episodes);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MultiEpisodeStyle.Repeat:
|
|
|
|
formatPattern = episodeFormat.EpisodeSeparator + episodeFormat.EpisodePattern;
|
|
|
|
seasonEpisodePattern = FormatNumberTokens(seasonEpisodePattern, formatPattern, episodes);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MultiEpisodeStyle.Scene:
|
|
|
|
formatPattern = "-" + episodeFormat.EpisodeSeparator + episodeFormat.EpisodePattern;
|
|
|
|
seasonEpisodePattern = FormatNumberTokens(seasonEpisodePattern, formatPattern, episodes);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MultiEpisodeStyle.Range:
|
|
|
|
formatPattern = "-" + episodeFormat.EpisodePattern;
|
|
|
|
var eps = new List<Episode> { episodes.First() };
|
|
|
|
|
|
|
|
if (episodes.Count > 1) eps.Add(episodes.Last());
|
|
|
|
|
|
|
|
seasonEpisodePattern = FormatNumberTokens(seasonEpisodePattern, formatPattern, eps);
|
|
|
|
break;
|
|
|
|
|
|
|
|
//MultiEpisodeStyle.Extend
|
|
|
|
default:
|
|
|
|
formatPattern = "-" + episodeFormat.EpisodePattern;
|
|
|
|
seasonEpisodePattern = FormatNumberTokens(seasonEpisodePattern, formatPattern, episodes);
|
|
|
|
break;
|
2014-09-05 23:05:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var token = String.Format("{{Season Episode{0}}}", index++);
|
|
|
|
pattern = pattern.Replace(episodeFormat.SeasonEpisodePattern, token);
|
|
|
|
tokenHandlers[token] = m => seasonEpisodePattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
AddSeasonTokens(tokenHandlers, episodes.First().SeasonNumber);
|
|
|
|
|
|
|
|
if (episodes.Count > 1)
|
|
|
|
{
|
|
|
|
tokenHandlers["{Episode}"] = m => episodes.First().EpisodeNumber.ToString(m.CustomFormat) + "-" + episodes.Last().EpisodeNumber.ToString(m.CustomFormat);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tokenHandlers["{Episode}"] = m => episodes.First().EpisodeNumber.ToString(m.CustomFormat);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
private String AddAbsoluteNumberingTokens(String pattern, Dictionary<String, Func<TokenMatch, String>> tokenHandlers, Series series, List<Episode> episodes, NamingConfig namingConfig)
|
|
|
|
{
|
|
|
|
var absoluteEpisodeFormats = GetAbsoluteFormat(pattern).DistinctBy(v => v.AbsoluteEpisodePattern).ToList();
|
|
|
|
|
|
|
|
int index = 1;
|
|
|
|
foreach (var absoluteEpisodeFormat in absoluteEpisodeFormats)
|
|
|
|
{
|
|
|
|
if (series.SeriesType != SeriesTypes.Anime)
|
|
|
|
{
|
|
|
|
pattern = pattern.Replace(absoluteEpisodeFormat.AbsoluteEpisodePattern, "");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var absoluteEpisodePattern = absoluteEpisodeFormat.AbsoluteEpisodePattern;
|
2014-11-17 07:31:53 +02:00
|
|
|
string formatPattern;
|
2014-09-05 23:05:52 +03:00
|
|
|
|
2014-11-17 07:31:53 +02:00
|
|
|
switch ((MultiEpisodeStyle) namingConfig.MultiEpisodeStyle)
|
2014-09-05 23:05:52 +03:00
|
|
|
{
|
|
|
|
|
2014-11-17 07:31:53 +02:00
|
|
|
case MultiEpisodeStyle.Duplicate:
|
|
|
|
formatPattern = absoluteEpisodeFormat.Separator + absoluteEpisodeFormat.AbsoluteEpisodePattern;
|
|
|
|
absoluteEpisodePattern = FormatAbsoluteNumberTokens(absoluteEpisodePattern, formatPattern, episodes);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MultiEpisodeStyle.Repeat:
|
|
|
|
var repeatSeparator = absoluteEpisodeFormat.Separator.Trim().IsNullOrWhiteSpace() ? " " : absoluteEpisodeFormat.Separator.Trim();
|
|
|
|
|
|
|
|
formatPattern = repeatSeparator + absoluteEpisodeFormat.AbsoluteEpisodePattern;
|
|
|
|
absoluteEpisodePattern = FormatAbsoluteNumberTokens(absoluteEpisodePattern, formatPattern, episodes);
|
|
|
|
break;
|
2014-09-05 23:05:52 +03:00
|
|
|
|
2014-11-17 07:31:53 +02:00
|
|
|
case MultiEpisodeStyle.Scene:
|
|
|
|
formatPattern = "-" + absoluteEpisodeFormat.AbsoluteEpisodePattern;
|
|
|
|
absoluteEpisodePattern = FormatAbsoluteNumberTokens(absoluteEpisodePattern, formatPattern, episodes);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MultiEpisodeStyle.Range:
|
|
|
|
formatPattern = "-" + absoluteEpisodeFormat.AbsoluteEpisodePattern;
|
|
|
|
var eps = new List<Episode> {episodes.First()};
|
|
|
|
|
|
|
|
if (episodes.Count > 1) eps.Add(episodes.Last());
|
|
|
|
|
|
|
|
absoluteEpisodePattern = FormatAbsoluteNumberTokens(absoluteEpisodePattern, formatPattern, eps);
|
|
|
|
break;
|
2014-09-05 23:05:52 +03:00
|
|
|
|
|
|
|
//MultiEpisodeStyle.Extend
|
2014-11-17 07:31:53 +02:00
|
|
|
default:
|
|
|
|
formatPattern = "-" + absoluteEpisodeFormat.AbsoluteEpisodePattern;
|
|
|
|
absoluteEpisodePattern = FormatAbsoluteNumberTokens(absoluteEpisodePattern, formatPattern, episodes);
|
|
|
|
break;
|
2014-09-05 23:05:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var token = String.Format("{{Absolute Pattern{0}}}", index++);
|
|
|
|
pattern = pattern.Replace(absoluteEpisodeFormat.AbsoluteEpisodePattern, token);
|
|
|
|
tokenHandlers[token] = m => absoluteEpisodePattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pattern;
|
|
|
|
}
|
|
|
|
|
2014-07-29 00:14:34 +03:00
|
|
|
private void AddSeasonTokens(Dictionary<String, Func<TokenMatch, String>> tokenHandlers, Int32 seasonNumber)
|
2014-04-10 20:58:50 +03:00
|
|
|
{
|
2014-09-05 23:05:52 +03:00
|
|
|
tokenHandlers["{Season}"] = m => seasonNumber.ToString(m.CustomFormat);
|
2014-07-29 00:14:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private void AddEpisodeTokens(Dictionary<String, Func<TokenMatch, String>> tokenHandlers, List<Episode> episodes)
|
|
|
|
{
|
|
|
|
if (!episodes.First().AirDate.IsNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
tokenHandlers["{Air Date}"] = m => episodes.First().AirDate.Replace('-', ' ');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tokenHandlers["{Air Date}"] = m => "Unknown";
|
|
|
|
}
|
|
|
|
|
|
|
|
tokenHandlers["{Episode Title}"] = m => GetEpisodeTitle(episodes);
|
2014-04-10 20:58:50 +03:00
|
|
|
}
|
|
|
|
|
2014-09-01 05:28:42 +03:00
|
|
|
private void AddEpisodeFileTokens(Dictionary<String, Func<TokenMatch, String>> tokenHandlers, Series series, EpisodeFile episodeFile)
|
2013-11-07 04:48:51 +03:00
|
|
|
{
|
2014-10-26 08:43:18 +02:00
|
|
|
tokenHandlers["{Original Title}"] = m => GetOriginalTitle(episodeFile);
|
2014-09-02 02:37:59 +03:00
|
|
|
tokenHandlers["{Release Group}"] = m => episodeFile.ReleaseGroup ?? "DRONE";
|
2014-09-01 05:28:42 +03:00
|
|
|
tokenHandlers["{Quality Title}"] = m => GetQualityTitle(series, episodeFile.Quality);
|
2013-11-07 04:48:51 +03:00
|
|
|
}
|
|
|
|
|
2014-07-29 00:14:34 +03:00
|
|
|
private void AddMediaInfoTokens(Dictionary<String, Func<TokenMatch, String>> tokenHandlers, EpisodeFile episodeFile)
|
2013-11-07 04:48:51 +03:00
|
|
|
{
|
2014-07-29 00:14:34 +03:00
|
|
|
if (episodeFile.MediaInfo == null) return;
|
2013-11-07 04:48:51 +03:00
|
|
|
|
2014-07-29 00:14:34 +03:00
|
|
|
String mediaInfoVideo;
|
|
|
|
switch (episodeFile.MediaInfo.VideoCodec)
|
2013-11-07 04:48:51 +03:00
|
|
|
{
|
2014-07-29 00:14:34 +03:00
|
|
|
case "AVC":
|
|
|
|
// TODO: What to do if the original SceneName is hashed?
|
|
|
|
if (!episodeFile.SceneName.IsNullOrWhiteSpace() && Path.GetFileNameWithoutExtension(episodeFile.SceneName).Contains("h264"))
|
|
|
|
{
|
|
|
|
mediaInfoVideo = "h264";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mediaInfoVideo = "x264";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
mediaInfoVideo = episodeFile.MediaInfo.VideoCodec;
|
|
|
|
break;
|
2013-11-07 04:48:51 +03:00
|
|
|
}
|
|
|
|
|
2014-07-29 00:14:34 +03:00
|
|
|
String mediaInfoAudio;
|
|
|
|
switch (episodeFile.MediaInfo.AudioFormat)
|
|
|
|
{
|
|
|
|
case "AC-3":
|
|
|
|
mediaInfoAudio = "AC3";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "MPEG Audio":
|
|
|
|
if (episodeFile.MediaInfo.AudioProfile == "Layer 3")
|
|
|
|
{
|
|
|
|
mediaInfoAudio = "MP3";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mediaInfoAudio = episodeFile.MediaInfo.AudioFormat;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "DTS":
|
|
|
|
mediaInfoAudio = episodeFile.MediaInfo.AudioFormat;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
mediaInfoAudio = episodeFile.MediaInfo.AudioFormat;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
var mediaInfoAudioLanguages = GetLanguagesToken(episodeFile.MediaInfo.AudioLanguages);
|
|
|
|
if (!mediaInfoAudioLanguages.IsNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
mediaInfoAudioLanguages = String.Format("[{0}]", mediaInfoAudioLanguages);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mediaInfoAudioLanguages == "[EN]")
|
|
|
|
{
|
|
|
|
mediaInfoAudioLanguages = String.Empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
var mediaInfoSubtitleLanguages = GetLanguagesToken(episodeFile.MediaInfo.Subtitles);
|
|
|
|
if (!mediaInfoSubtitleLanguages.IsNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
mediaInfoSubtitleLanguages = String.Format("[{0}]", mediaInfoSubtitleLanguages);
|
|
|
|
}
|
|
|
|
|
|
|
|
tokenHandlers["{MediaInfo Video}"] = m => mediaInfoVideo;
|
|
|
|
tokenHandlers["{MediaInfo Audio}"] = m => mediaInfoAudio;
|
|
|
|
|
|
|
|
tokenHandlers["{MediaInfo Simple}"] = m => String.Format("{0} {1}", mediaInfoVideo, mediaInfoAudio);
|
|
|
|
|
|
|
|
tokenHandlers["{MediaInfo Full}"] = m => String.Format("{0} {1}{2} {3}", mediaInfoVideo, mediaInfoAudio, mediaInfoAudioLanguages, mediaInfoSubtitleLanguages);
|
|
|
|
}
|
|
|
|
|
|
|
|
private string GetLanguagesToken(String mediaInfoLanguages)
|
|
|
|
{
|
|
|
|
List<string> tokens = new List<string>();
|
|
|
|
foreach (var item in mediaInfoLanguages.Split('/'))
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrWhiteSpace(item))
|
|
|
|
tokens.Add(item.Trim());
|
|
|
|
}
|
|
|
|
|
|
|
|
var cultures = System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.NeutralCultures);
|
|
|
|
for (int i = 0; i < tokens.Count; i++)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var cultureInfo = cultures.FirstOrDefault(p => p.EnglishName == tokens[i]);
|
|
|
|
|
|
|
|
if (cultureInfo != null)
|
|
|
|
tokens[i] = cultureInfo.TwoLetterISOLanguageName.ToUpper();
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return string.Join("+", tokens.Distinct());
|
|
|
|
}
|
|
|
|
|
|
|
|
private string ReplaceTokens(String pattern, Dictionary<String, Func<TokenMatch, String>> tokenHandlers)
|
|
|
|
{
|
|
|
|
return TitleRegex.Replace(pattern, match => ReplaceToken(match, tokenHandlers));
|
|
|
|
}
|
|
|
|
|
|
|
|
private string ReplaceToken(Match match, Dictionary<String, Func<TokenMatch, String>> tokenHandlers)
|
|
|
|
{
|
|
|
|
var tokenMatch = new TokenMatch
|
|
|
|
{
|
|
|
|
RegexMatch = match,
|
|
|
|
Prefix = match.Groups["prefix"].Value,
|
|
|
|
Separator = match.Groups["separator"].Value,
|
|
|
|
Suffix = match.Groups["suffix"].Value,
|
|
|
|
Token = match.Groups["token"].Value,
|
|
|
|
CustomFormat = match.Groups["customFormat"].Value
|
|
|
|
};
|
|
|
|
|
|
|
|
if (tokenMatch.CustomFormat.IsNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
tokenMatch.CustomFormat = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var tokenHandler = tokenHandlers.GetValueOrDefault(tokenMatch.Token, m => String.Empty);
|
|
|
|
|
|
|
|
var replacementText = tokenHandler(tokenMatch).Trim();
|
|
|
|
|
|
|
|
if (tokenMatch.Token.All(t => !Char.IsLetter(t) || Char.IsLower(t)))
|
|
|
|
{
|
|
|
|
replacementText = replacementText.ToLower();
|
|
|
|
}
|
|
|
|
else if (tokenMatch.Token.All(t => !Char.IsLetter(t) || Char.IsUpper(t)))
|
2013-11-07 04:48:51 +03:00
|
|
|
{
|
|
|
|
replacementText = replacementText.ToUpper();
|
|
|
|
}
|
|
|
|
|
2014-07-29 00:14:34 +03:00
|
|
|
if (!tokenMatch.Separator.IsNullOrWhiteSpace())
|
2013-11-07 04:48:51 +03:00
|
|
|
{
|
2014-07-29 00:14:34 +03:00
|
|
|
replacementText = replacementText.Replace(" ", tokenMatch.Separator);
|
|
|
|
}
|
|
|
|
|
|
|
|
replacementText = CleanFileName(replacementText);
|
|
|
|
|
|
|
|
if (!replacementText.IsNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
replacementText = tokenMatch.Prefix + replacementText + tokenMatch.Suffix;
|
2013-11-07 04:48:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return replacementText;
|
|
|
|
}
|
|
|
|
|
2014-11-17 07:31:53 +02:00
|
|
|
private string FormatNumberTokens(string basePattern, string formatPattern, List<Episode> episodes)
|
2013-11-07 04:48:51 +03:00
|
|
|
{
|
2014-11-17 07:31:53 +02:00
|
|
|
var pattern = String.Empty;
|
|
|
|
|
|
|
|
for (int i = 0; i < episodes.Count; i++)
|
2013-11-07 04:48:51 +03:00
|
|
|
{
|
2014-11-17 07:31:53 +02:00
|
|
|
var patternToReplace = i == 0 ? basePattern : formatPattern;
|
2013-11-07 04:48:51 +03:00
|
|
|
|
2014-11-17 07:31:53 +02:00
|
|
|
pattern += EpisodeRegex.Replace(patternToReplace, match => ReplaceNumberToken(match.Groups["episode"].Value, episodes[i].EpisodeNumber));
|
|
|
|
}
|
2014-05-19 22:14:41 +03:00
|
|
|
|
|
|
|
return ReplaceSeasonTokens(pattern, episodes.First().SeasonNumber);
|
|
|
|
}
|
|
|
|
|
2014-11-17 07:31:53 +02:00
|
|
|
private string FormatAbsoluteNumberTokens(string basePattern, string formatPattern, List<Episode> episodes)
|
2014-05-19 22:14:41 +03:00
|
|
|
{
|
2014-11-17 07:31:53 +02:00
|
|
|
var pattern = String.Empty;
|
|
|
|
|
|
|
|
for (int i = 0; i < episodes.Count; i++)
|
2014-05-19 22:14:41 +03:00
|
|
|
{
|
2014-11-17 07:31:53 +02:00
|
|
|
var patternToReplace = i == 0 ? basePattern : formatPattern;
|
2014-05-19 22:14:41 +03:00
|
|
|
|
2014-11-17 07:31:53 +02:00
|
|
|
pattern += AbsoluteEpisodeRegex.Replace(patternToReplace, match => ReplaceNumberToken(match.Groups["absolute"].Value, episodes[i].AbsoluteEpisodeNumber.Value));
|
|
|
|
}
|
2013-11-07 04:48:51 +03:00
|
|
|
|
|
|
|
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-11-21 09:10:00 +03:00
|
|
|
|
2014-09-05 23:05:52 +03:00
|
|
|
private EpisodeFormat[] GetEpisodeFormat(string pattern)
|
2013-11-21 09:10:00 +03:00
|
|
|
{
|
2014-09-05 23:05:52 +03:00
|
|
|
return _episodeFormatCache.Get(pattern, () => SeasonEpisodePatternRegex.Matches(pattern).OfType<Match>()
|
|
|
|
.Select(match => new EpisodeFormat
|
2013-11-21 09:10:00 +03:00
|
|
|
{
|
2014-09-05 23:05:52 +03:00
|
|
|
EpisodeSeparator = match.Groups["episodeSeparator"].Value,
|
|
|
|
Separator = match.Groups["separator"].Value,
|
|
|
|
EpisodePattern = match.Groups["episode"].Value,
|
|
|
|
SeasonEpisodePattern = match.Groups["seasonEpisode"].Value,
|
|
|
|
}).ToArray());
|
2013-11-21 09:10:00 +03:00
|
|
|
}
|
2013-12-01 08:49:40 +03:00
|
|
|
|
2014-09-05 23:05:52 +03:00
|
|
|
private AbsoluteEpisodeFormat[] GetAbsoluteFormat(string pattern)
|
2014-05-19 22:14:41 +03:00
|
|
|
{
|
2014-09-05 23:05:52 +03:00
|
|
|
return _absoluteEpisodeFormatCache.Get(pattern, () => AbsoluteEpisodePatternRegex.Matches(pattern).OfType<Match>()
|
|
|
|
.Select(match => new AbsoluteEpisodeFormat
|
|
|
|
{
|
2014-11-02 08:58:32 +02:00
|
|
|
Separator = match.Groups["separator"].Value.IsNotNullOrWhiteSpace() ? match.Groups["separator"].Value : "-",
|
2014-09-05 23:05:52 +03:00
|
|
|
AbsoluteEpisodePattern = match.Groups["absolute"].Value
|
|
|
|
}).ToArray());
|
2014-05-19 22:14:41 +03:00
|
|
|
}
|
|
|
|
|
2014-07-29 00:14:34 +03:00
|
|
|
private String GetEpisodeTitle(List<Episode> episodes)
|
2013-12-01 08:49:40 +03:00
|
|
|
{
|
2014-07-29 00:14:34 +03:00
|
|
|
if (episodes.Count == 1)
|
2013-12-01 08:49:40 +03:00
|
|
|
{
|
2014-07-29 00:14:34 +03:00
|
|
|
return episodes.First().Title.TrimEnd(EpisodeTitleTrimCharacters);
|
2013-12-01 08:49:40 +03:00
|
|
|
}
|
|
|
|
|
2014-07-29 00:14:34 +03:00
|
|
|
var titles = episodes
|
|
|
|
.Select(c => c.Title.TrimEnd(EpisodeTitleTrimCharacters))
|
|
|
|
.Select(Parser.Parser.CleanupEpisodeTitle)
|
|
|
|
.Distinct();
|
|
|
|
|
|
|
|
return String.Join(" + ", titles);
|
2013-12-01 08:49:40 +03:00
|
|
|
}
|
2014-01-18 14:44:36 +03:00
|
|
|
|
2014-09-01 05:28:42 +03:00
|
|
|
private String GetQualityTitle(Series series, QualityModel quality)
|
2014-01-18 14:44:36 +03:00
|
|
|
{
|
2014-09-01 05:28:42 +03:00
|
|
|
var qualitySuffix = String.Empty;
|
|
|
|
|
|
|
|
if (quality.Revision.Version > 1)
|
|
|
|
{
|
|
|
|
if (series.SeriesType == SeriesTypes.Anime)
|
|
|
|
{
|
|
|
|
qualitySuffix = " v" + quality.Revision.Version;
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qualitySuffix = " Proper";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return _qualityDefinitionService.Get(quality.Quality).Title + qualitySuffix;
|
2014-01-18 14:44:36 +03:00
|
|
|
}
|
2014-10-26 08:43:18 +02:00
|
|
|
|
|
|
|
private String GetOriginalTitle(EpisodeFile episodeFile)
|
|
|
|
{
|
|
|
|
if (episodeFile.SceneName.IsNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
if (episodeFile.RelativePath.IsNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
return Path.GetFileNameWithoutExtension(episodeFile.Path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Path.GetFileNameWithoutExtension(episodeFile.RelativePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return episodeFile.SceneName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal sealed class TokenMatch
|
|
|
|
{
|
|
|
|
public Match RegexMatch { get; set; }
|
|
|
|
public String Prefix { get; set; }
|
|
|
|
public String Separator { get; set; }
|
|
|
|
public String Suffix { get; set; }
|
|
|
|
public String Token { get; set; }
|
|
|
|
public String CustomFormat { get; set; }
|
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,
|
2014-11-17 07:31:53 +02:00
|
|
|
Scene = 3,
|
|
|
|
Range = 4
|
2013-11-07 04:48:51 +03:00
|
|
|
}
|
2013-03-07 00:20:33 +03:00
|
|
|
}
|