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;
|
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
|
|
|
|
{
|
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-11-22 10:44:43 +03:00
|
|
|
BasicNamingConfig GetBasicNamingConfig(NamingConfig nameSpec);
|
2014-01-17 10:52:28 +03:00
|
|
|
string GetSeriesFolder(string seriesTitle);
|
2014-04-15 17:16:49 +03:00
|
|
|
string GetSeriesFolder(string seriesTitle, NamingConfig namingConfig);
|
|
|
|
string GetSeasonFolder(string seriesTitle, int seasonNumber, NamingConfig namingConfig);
|
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;
|
2013-11-21 09:10:00 +03:00
|
|
|
private readonly ICached<EpisodeFormat> _patternCache;
|
2013-04-25 07:27:49 +03:00
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2013-11-22 10:44:43 +03:00
|
|
|
private static readonly Regex TitleRegex = new Regex(@"(?<token>\{(?:\w+)(?<separator>\s|\.|-|_)\w+\})",
|
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);
|
|
|
|
|
2013-11-16 08:03:42 +03:00
|
|
|
public 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);
|
|
|
|
|
2014-05-19 22:14:41 +03:00
|
|
|
public static readonly Regex AbsoluteEpisodePatternRegex = new Regex(@"(?<separator>(?<=}).+?)?(?<absolute>{absolute(?:\:0+)?})(?<separator>.+?(?={))?",
|
|
|
|
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-01-17 21:43:15 +03:00
|
|
|
public static readonly Regex SeriesTitleRegex = new Regex(@"(?<token>\{(?:Series)(?<separator>\s|\.|-|_)Title\})",
|
|
|
|
RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
|
|
|
|
2014-05-01 23:29:09 +03:00
|
|
|
private static readonly Regex FilenameCleanupRegex = new Regex(@"\.{2,}", RegexOptions.Compiled);
|
|
|
|
|
2014-02-12 12:03:24 +03:00
|
|
|
private static readonly char[] EpisodeTitleTrimCharaters = 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-04-01 23:07:41 +03:00
|
|
|
_patternCache = cacheManager.GetCache<EpisodeFormat>(GetType());
|
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;
|
2014-05-01 23:29:09 +03:00
|
|
|
|
2013-11-07 04:48:51 +03:00
|
|
|
var episodeTitles = new List<string>
|
2013-03-07 00:20:33 +03:00
|
|
|
{
|
2014-02-12 11:59:22 +03:00
|
|
|
sortedEpisodes.First().Title.TrimEnd(EpisodeTitleTrimCharaters)
|
2013-11-07 04:48:51 +03:00
|
|
|
};
|
2013-03-07 00:20:33 +03:00
|
|
|
|
2014-01-18 14:44:36 +03:00
|
|
|
var tokenValues = new Dictionary<string, string>(FilenameBuilderTokenEqualityComparer.Instance);
|
2013-11-07 04:48:51 +03:00
|
|
|
|
2014-01-18 14:44:36 +03:00
|
|
|
tokenValues.Add("{Series Title}", series.Title);
|
|
|
|
tokenValues.Add("{Original Title}", episodeFile.SceneName);
|
2013-12-09 09:25:11 +03:00
|
|
|
tokenValues.Add("{Release Group}", episodeFile.ReleaseGroup);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-05-19 22:14:41 +03:00
|
|
|
if (series.SeriesType == SeriesTypes.Anime && episodes.All(e => e.AbsoluteEpisodeNumber > 0))
|
|
|
|
{
|
|
|
|
pattern = namingConfig.AnimeEpisodeFormat;
|
|
|
|
}
|
|
|
|
|
2013-11-22 05:29:49 +03:00
|
|
|
var episodeFormat = GetEpisodeFormat(pattern);
|
2013-03-07 00:20:33 +03:00
|
|
|
|
2013-11-21 09:10:00 +03:00
|
|
|
if (episodeFormat != null)
|
|
|
|
{
|
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
|
|
|
|
2014-02-12 11:59:22 +03:00
|
|
|
episodeTitles.Add(episode.Title.TrimEnd(EpisodeTitleTrimCharaters));
|
2013-11-07 04:48:51 +03:00
|
|
|
}
|
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);
|
2013-12-01 08:49:40 +03:00
|
|
|
}
|
|
|
|
|
2014-05-19 22:14:41 +03:00
|
|
|
//TODO: Extract to another method
|
|
|
|
var absoluteEpisodeFormat = GetAbsoluteFormat(pattern);
|
|
|
|
|
|
|
|
if (absoluteEpisodeFormat != null)
|
|
|
|
{
|
|
|
|
if (series.SeriesType != SeriesTypes.Anime)
|
|
|
|
{
|
|
|
|
pattern = pattern.Replace(absoluteEpisodeFormat.AbsoluteEpisodePattern, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pattern = pattern.Replace(absoluteEpisodeFormat.AbsoluteEpisodePattern, "{Absolute Pattern}");
|
|
|
|
var absoluteEpisodePattern = absoluteEpisodeFormat.AbsoluteEpisodePattern;
|
|
|
|
|
|
|
|
foreach (var episode in sortedEpisodes.Skip(1))
|
|
|
|
{
|
2014-07-15 09:06:06 +03:00
|
|
|
switch ((MultiEpisodeStyle)namingConfig.MultiEpisodeStyle)
|
|
|
|
{
|
|
|
|
case MultiEpisodeStyle.Duplicate:
|
|
|
|
absoluteEpisodePattern += absoluteEpisodeFormat.Separator +
|
|
|
|
absoluteEpisodeFormat.AbsoluteEpisodePattern;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MultiEpisodeStyle.Repeat:
|
|
|
|
absoluteEpisodePattern += absoluteEpisodeFormat.Separator +
|
|
|
|
absoluteEpisodeFormat.AbsoluteEpisodePattern;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MultiEpisodeStyle.Scene:
|
|
|
|
absoluteEpisodePattern += "-" + absoluteEpisodeFormat.AbsoluteEpisodePattern;
|
|
|
|
break;
|
|
|
|
|
|
|
|
//MultiEpisodeStyle.Extend
|
|
|
|
default:
|
|
|
|
absoluteEpisodePattern += "-" + absoluteEpisodeFormat.AbsoluteEpisodePattern;
|
|
|
|
break;
|
|
|
|
}
|
2014-05-19 22:14:41 +03:00
|
|
|
|
|
|
|
episodeTitles.Add(episode.Title.TrimEnd(EpisodeTitleTrimCharaters));
|
|
|
|
}
|
|
|
|
|
|
|
|
absoluteEpisodePattern = ReplaceAbsoluteNumberTokens(absoluteEpisodePattern, sortedEpisodes);
|
|
|
|
tokenValues.Add("{Absolute Pattern}", absoluteEpisodePattern);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-01 08:49:40 +03:00
|
|
|
tokenValues.Add("{Episode Title}", GetEpisodeTitle(episodeTitles));
|
2014-01-18 14:44:36 +03:00
|
|
|
tokenValues.Add("{Quality Title}", GetQualityTitle(episodeFile.Quality));
|
2014-05-01 23:29:09 +03:00
|
|
|
|
|
|
|
var filename = ReplaceTokens(pattern, tokenValues).Trim();
|
|
|
|
filename = FilenameCleanupRegex.Replace(filename, match => match.Captures[0].Value[0].ToString() );
|
|
|
|
|
|
|
|
return CleanFilename(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
|
|
|
{
|
|
|
|
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-04-15 17:16:49 +03:00
|
|
|
seasonFolder = GetSeasonFolder(series.Title, seasonNumber, nameSpec);
|
2013-10-05 08:23:10 +03:00
|
|
|
}
|
2013-03-07 00:20:33 +03:00
|
|
|
|
2014-01-24 19:23:03 +03:00
|
|
|
seasonFolder = CleanFilename(seasonFolder);
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
var episodeFormat = GetEpisodeFormat(nameSpec.StandardEpisodeFormat);
|
|
|
|
|
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-01-17 10:52:28 +03:00
|
|
|
public string GetSeriesFolder(string seriesTitle)
|
2014-04-15 17:16:49 +03:00
|
|
|
{
|
|
|
|
var namingConfig = _namingConfigService.GetConfig();
|
|
|
|
|
|
|
|
return GetSeriesFolder(seriesTitle, namingConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
public string GetSeriesFolder(string seriesTitle, NamingConfig namingConfig)
|
2014-01-17 10:52:28 +03:00
|
|
|
{
|
|
|
|
seriesTitle = CleanFilename(seriesTitle);
|
|
|
|
|
|
|
|
var tokenValues = new Dictionary<string, string>(FilenameBuilderTokenEqualityComparer.Instance);
|
|
|
|
tokenValues.Add("{Series Title}", seriesTitle);
|
|
|
|
|
2014-04-15 17:16:49 +03:00
|
|
|
return ReplaceTokens(namingConfig.SeriesFolderFormat, tokenValues);
|
|
|
|
}
|
|
|
|
|
|
|
|
public string GetSeasonFolder(string seriesTitle, int seasonNumber, NamingConfig namingConfig)
|
|
|
|
{
|
|
|
|
var tokenValues = new Dictionary<string, string>(FilenameBuilderTokenEqualityComparer.Instance);
|
|
|
|
tokenValues.Add("{Series Title}", seriesTitle);
|
|
|
|
|
|
|
|
var seasonFolder = ReplaceSeasonTokens(namingConfig.SeasonFolderFormat, seasonNumber);
|
|
|
|
return ReplaceTokens(seasonFolder, tokenValues);
|
2014-01-17 10:52:28 +03:00
|
|
|
}
|
|
|
|
|
2013-03-07 00:20:33 +03:00
|
|
|
public static string CleanFilename(string name)
|
|
|
|
{
|
|
|
|
string result = name;
|
|
|
|
string[] badCharacters = { "\\", "/", "<", ">", "?", "*", ":", "|", "\"" };
|
2014-02-22 22:30:44 +03:00
|
|
|
string[] goodCharacters = { "+", "+", "", "", "!", "-", "-", "", "" };
|
2013-03-07 00:20:33 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
2013-11-15 11:53:12 +03:00
|
|
|
if (patternTokenArray.All(t => !Char.IsLetter(t) || Char.IsLower(t)))
|
2013-11-07 04:48:51 +03:00
|
|
|
{
|
|
|
|
replacementText = replacementText.ToLowerInvariant();
|
|
|
|
}
|
|
|
|
|
2013-11-15 11:53:12 +03:00
|
|
|
else if (patternTokenArray.All(t => !Char.IsLetter(t) || Char.IsUpper(t)))
|
2013-11-07 04:48:51 +03:00
|
|
|
{
|
|
|
|
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 =>
|
|
|
|
{
|
2014-05-19 22:14:41 +03:00
|
|
|
var episode = episodes[episodeIndex];
|
2013-11-07 04:48:51 +03:00
|
|
|
episodeIndex++;
|
|
|
|
|
2014-05-19 22:14:41 +03:00
|
|
|
return ReplaceNumberToken(match.Groups["episode"].Value, episode.EpisodeNumber);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ReplaceSeasonTokens(pattern, episodes.First().SeasonNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
private string ReplaceAbsoluteNumberTokens(string pattern, List<Episode> episodes)
|
|
|
|
{
|
|
|
|
var episodeIndex = 0;
|
|
|
|
pattern = AbsoluteEpisodeRegex.Replace(pattern, match =>
|
|
|
|
{
|
|
|
|
var episode = episodes[episodeIndex];
|
|
|
|
episodeIndex++;
|
|
|
|
|
|
|
|
//TODO: We need to handle this null check somewhere, I think earlier is better...
|
|
|
|
return ReplaceNumberToken(match.Groups["absolute"].Value, episode.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
|
|
|
|
2013-11-22 05:29:49 +03:00
|
|
|
private EpisodeFormat GetEpisodeFormat(string pattern)
|
2013-11-21 09:10:00 +03:00
|
|
|
{
|
|
|
|
return _patternCache.Get(pattern, () =>
|
|
|
|
{
|
|
|
|
var match = SeasonEpisodePatternRegex.Match(pattern);
|
|
|
|
|
|
|
|
if (match.Success)
|
|
|
|
{
|
|
|
|
return new EpisodeFormat
|
|
|
|
{
|
|
|
|
EpisodeSeparator = match.Groups["episodeSeparator"].Value,
|
|
|
|
Separator = match.Groups["separator"].Value,
|
|
|
|
EpisodePattern = match.Groups["episode"].Value,
|
|
|
|
SeasonEpisodePattern = match.Groups["seasonEpisode"].Value,
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
}
|
2013-12-01 08:49:40 +03:00
|
|
|
|
2014-05-19 22:14:41 +03:00
|
|
|
private AbsoluteEpisodeFormat GetAbsoluteFormat(string pattern)
|
|
|
|
{
|
|
|
|
var match = AbsoluteEpisodePatternRegex.Match(pattern);
|
|
|
|
|
|
|
|
if (match.Success)
|
|
|
|
{
|
|
|
|
return new AbsoluteEpisodeFormat
|
|
|
|
{
|
|
|
|
Separator = match.Groups["separator"].Value,
|
|
|
|
AbsoluteEpisodePattern = match.Groups["absolute"].Value
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-12-01 08:49:40 +03:00
|
|
|
private string GetEpisodeTitle(List<string> episodeTitles)
|
|
|
|
{
|
|
|
|
if (episodeTitles.Count == 1)
|
|
|
|
{
|
|
|
|
return episodeTitles.First();
|
|
|
|
}
|
|
|
|
|
|
|
|
return String.Join(" + ", episodeTitles.Select(Parser.Parser.CleanupEpisodeTitle).Distinct());
|
|
|
|
}
|
2014-01-18 14:44:36 +03:00
|
|
|
|
|
|
|
private string GetQualityTitle(QualityModel quality)
|
|
|
|
{
|
|
|
|
if (quality.Proper)
|
|
|
|
return _qualityDefinitionService.Get(quality.Quality).Title + " Proper";
|
|
|
|
else
|
|
|
|
return _qualityDefinitionService.Get(quality.Quality).Title;
|
|
|
|
}
|
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
|
|
|
}
|