Server side for custom naming is complete

This commit is contained in:
Mark McDowall 2013-11-06 17:48:51 -08:00
parent 5fd25ff3f3
commit 7b54bca3c7
14 changed files with 613 additions and 796 deletions

View File

@ -5,13 +5,9 @@ namespace NzbDrone.Api.Config
{
public class NamingConfigResource : RestResource
{
public Boolean IncludeEpisodeTitle { get; set; }
public Boolean ReplaceSpaces { get; set; }
public Boolean RenameEpisodes { get; set; }
public Int32 MultiEpisodeStyle { get; set; }
public Int32 NumberStyle { get; set; }
public String Separator { get; set; }
public Boolean IncludeQuality { get; set; }
public Boolean IncludeSeriesTitle { get; set; }
public string StandardEpisodeFormat { get; set; }
public string DailyEpisodeFormat { get; set; }
}
}

View File

@ -28,8 +28,6 @@ namespace NzbDrone.Api.Config
Get["/samples"] = x => GetExamples(this.Bind<NamingConfigResource>());
SharedValidator.RuleFor(c => c.MultiEpisodeStyle).InclusiveBetween(0, 3);
SharedValidator.RuleFor(c => c.NumberStyle).InclusiveBetween(0, 3);
SharedValidator.RuleFor(c => c.Separator).Matches(@"\s|\s\-\s|\.");
}
private void UpdateNamingConfig(NamingConfigResource resource)

View File

@ -26,12 +26,12 @@ namespace NzbDrone.Core.Test.OrganizerTests
}
[Test]
[TestCase("30 Rock - S01E05 - Episode Title", 1, true, "Season %0s", @"C:\Test\30 Rock\Season 01\30 Rock - S01E05 - Episode Title.mkv")]
[TestCase("30 Rock - S01E05 - Episode Title", 1, true, "Season %s", @"C:\Test\30 Rock\Season 1\30 Rock - S01E05 - Episode Title.mkv")]
[TestCase("30 Rock - S01E05 - Episode Title", 1, false, "Season %0s", @"C:\Test\30 Rock\30 Rock - S01E05 - Episode Title.mkv")]
[TestCase("30 Rock - S01E05 - Episode Title", 1, false, "Season %s", @"C:\Test\30 Rock\30 Rock - S01E05 - Episode Title.mkv")]
[TestCase("30 Rock - S01E05 - Episode Title", 1, true, "ReallyUglySeasonFolder %s", @"C:\Test\30 Rock\ReallyUglySeasonFolder 1\30 Rock - S01E05 - Episode Title.mkv")]
[TestCase("30 Rock - S00E05 - Episode Title", 0, true, "Season %s", @"C:\Test\30 Rock\Specials\30 Rock - S00E05 - Episode Title.mkv")]
[TestCase("30 Rock - S01E05 - Episode Title", 1, true, "Season {0season}", @"C:\Test\30 Rock\Season 01\30 Rock - S01E05 - Episode Title.mkv")]
[TestCase("30 Rock - S01E05 - Episode Title", 1, true, "Season {season}", @"C:\Test\30 Rock\Season 1\30 Rock - S01E05 - Episode Title.mkv")]
[TestCase("30 Rock - S01E05 - Episode Title", 1, false, "Season {0season}", @"C:\Test\30 Rock\30 Rock - S01E05 - Episode Title.mkv")]
[TestCase("30 Rock - S01E05 - Episode Title", 1, false, "Season {season}", @"C:\Test\30 Rock\30 Rock - S01E05 - Episode Title.mkv")]
[TestCase("30 Rock - S01E05 - Episode Title", 1, true, "ReallyUglySeasonFolder {season}", @"C:\Test\30 Rock\ReallyUglySeasonFolder 1\30 Rock - S01E05 - Episode Title.mkv")]
[TestCase("30 Rock - S00E05 - Episode Title", 0, true, "Season {season}", @"C:\Test\30 Rock\Specials\30 Rock - S00E05 - Episode Title.mkv")]
public void CalculateFilePath_SeasonFolder_SingleNumber(string filename, int seasonNumber, bool useSeasonFolder, string seasonFolderFormat, string expectedPath)
{
var fakeSeries = Builder<Series>.CreateNew()

View File

@ -16,8 +16,10 @@ namespace NzbDrone.Core.Test.OrganizerTests
public class FileNameBuilderFixture : CoreTest<FileNameBuilder>
{
private Series _series;
private NamingConfig namingConfig;
private Episode _episode1;
private Episode _episode2;
private EpisodeFile _episodeFile;
private NamingConfig _namingConfig;
[SetUp]
public void Setup()
@ -28,571 +30,190 @@ namespace NzbDrone.Core.Test.OrganizerTests
.Build();
namingConfig = new NamingConfig();
namingConfig.RenameEpisodes = true;
_namingConfig = new NamingConfig();
_namingConfig.RenameEpisodes = true;
Mocker.GetMock<INamingConfigService>()
.Setup(c => c.GetConfig()).Returns(namingConfig);
.Setup(c => c.GetConfig()).Returns(_namingConfig);
}
[Test]
public void GetNewFilename_Series_Episode_Quality_S01E05_Dash()
{
namingConfig.IncludeSeriesTitle = true;
namingConfig.IncludeEpisodeTitle = true;
namingConfig.IncludeQuality = true;
namingConfig.Separator = " - ";
namingConfig.NumberStyle = 2;
namingConfig.ReplaceSpaces = false;
var episode = Builder<Episode>.CreateNew()
_episode1 = Builder<Episode>.CreateNew()
.With(e => e.Title = "City Sushi")
.With(e => e.SeasonNumber = 15)
.With(e => e.EpisodeNumber = 6)
.Build();
string result = Subject.BuildFilename(new List<Episode> { episode }, _series, new EpisodeFile { Quality = new QualityModel(Quality.HDTV720p) });
result.Should().Be("South Park - S15E06 - City Sushi [HDTV-720p]");
}
[Test]
public void GetNewFilename_Episode_Quality_1x05_Dash()
{
namingConfig.IncludeSeriesTitle = false;
namingConfig.IncludeEpisodeTitle = true;
namingConfig.IncludeQuality = true;
namingConfig.Separator = " - ";
namingConfig.NumberStyle = 0;
namingConfig.ReplaceSpaces = false;
var episode = Builder<Episode>.CreateNew()
_episode2 = Builder<Episode>.CreateNew()
.With(e => e.Title = "City Sushi")
.With(e => e.SeasonNumber = 15)
.With(e => e.EpisodeNumber = 6)
.Build();
string result = Subject.BuildFilename(new List<Episode> { episode }, _series, new EpisodeFile { Quality = new QualityModel(Quality.HDTV720p) });
result.Should().Be("15x06 - City Sushi [HDTV-720p]");
}
[Test]
public void GetNewFilename_Series_Quality_01x05_Space()
{
namingConfig.IncludeSeriesTitle = true;
namingConfig.IncludeEpisodeTitle = false;
namingConfig.IncludeQuality = true;
namingConfig.Separator = " ";
namingConfig.NumberStyle = 1;
namingConfig.ReplaceSpaces = false;
var episode = Builder<Episode>.CreateNew()
.With(e => e.Title = "City Sushi")
.With(e => e.SeasonNumber = 5)
.With(e => e.EpisodeNumber = 6)
.Build();
string result = Subject.BuildFilename(new List<Episode> { episode }, _series, new EpisodeFile { Quality = new QualityModel(Quality.HDTV720p) });
result.Should().Be("South Park 05x06 [HDTV-720p]");
}
[Test]
public void GetNewFilename_Series_s01e05_Space()
{
namingConfig.IncludeSeriesTitle = true;
namingConfig.IncludeEpisodeTitle = false;
namingConfig.IncludeQuality = false;
namingConfig.Separator = " ";
namingConfig.NumberStyle = 3;
namingConfig.ReplaceSpaces = false;
var episode = Builder<Episode>.CreateNew()
.With(e => e.Title = "City Sushi")
.With(e => e.SeasonNumber = 5)
.With(e => e.EpisodeNumber = 6)
.Build();
string result = Subject.BuildFilename(new List<Episode> { episode }, _series, new EpisodeFile { Quality = new QualityModel(Quality.HDTV720p) });
result.Should().Be("South Park s05e06");
}
[Test]
public void GetNewFilename_Series_Episode_s01e05_Periods()
{
namingConfig.IncludeSeriesTitle = true;
namingConfig.IncludeEpisodeTitle = true;
namingConfig.IncludeQuality = false;
namingConfig.Separator = " ";
namingConfig.NumberStyle = 3;
namingConfig.ReplaceSpaces = true;
var episode = Builder<Episode>.CreateNew()
.With(e => e.Title = "City Sushi")
.With(e => e.SeasonNumber = 5)
.With(e => e.EpisodeNumber = 6)
.Build();
string result = Subject.BuildFilename(new List<Episode> { episode }, _series, new EpisodeFile { Quality = new QualityModel(Quality.HDTV720p) });
result.Should().Be("South.Park.s05e06.City.Sushi");
}
[Test]
public void GetNewFilename_Series_Episode_s01e05_Dash_Periods_Quality()
{
namingConfig.IncludeSeriesTitle = true;
namingConfig.IncludeEpisodeTitle = true;
namingConfig.IncludeQuality = true;
namingConfig.Separator = " - ";
namingConfig.NumberStyle = 3;
namingConfig.ReplaceSpaces = true;
var episode = Builder<Episode>.CreateNew()
.With(e => e.Title = "City Sushi")
.With(e => e.SeasonNumber = 5)
.With(e => e.EpisodeNumber = 6)
.Build();
string result = Subject.BuildFilename(new List<Episode> { episode }, _series, new EpisodeFile { Quality = new QualityModel(Quality.HDTV720p) });
result.Should().Be("South.Park.-.s05e06.-.City.Sushi.[HDTV-720p]");
}
[Test]
public void GetNewFilename_S01E05_Dash()
{
namingConfig.IncludeSeriesTitle = false;
namingConfig.IncludeEpisodeTitle = false;
namingConfig.IncludeQuality = false;
namingConfig.Separator = " - ";
namingConfig.NumberStyle = 2;
namingConfig.ReplaceSpaces = false;
var episode = Builder<Episode>.CreateNew()
.With(e => e.Title = "City Sushi")
.With(e => e.SeasonNumber = 15)
.With(e => e.EpisodeNumber = 6)
.Build();
string result = Subject.BuildFilename(new List<Episode> { episode }, _series, new EpisodeFile { Quality = new QualityModel(Quality.HDTV720p) });
result.Should().Be("S15E06");
}
[Test]
public void GetNewFilename_multi_Series_Episode_Quality_S01E05_Scene_Dash()
{
namingConfig.IncludeSeriesTitle = true;
namingConfig.IncludeEpisodeTitle = true;
namingConfig.IncludeQuality = true;
namingConfig.Separator = " - ";
namingConfig.NumberStyle = 2;
namingConfig.ReplaceSpaces = false;
namingConfig.MultiEpisodeStyle = 3;
var episodeOne = Builder<Episode>.CreateNew()
.With(e => e.Title = "Strawberries and Cream (1)")
.With(e => e.SeasonNumber = 3)
.With(e => e.EpisodeNumber = 23)
.Build();
var episodeTwo = Builder<Episode>.CreateNew()
.With(e => e.Title = "Strawberries and Cream (2)")
.With(e => e.SeasonNumber = 3)
.With(e => e.EpisodeNumber = 24)
.Build();
string result = Subject.BuildFilename(new List<Episode> { episodeOne, episodeTwo }, new Series { Title = "The Mentalist" }, new EpisodeFile { Quality = new QualityModel(Quality.HDTV720p) });
result.Should().Be("The Mentalist - S03E23-E24 - Strawberries and Cream [HDTV-720p]");
}
[Test]
public void GetNewFilename_multi_Episode_Quality_1x05_Repeat_Dash()
{
namingConfig.IncludeSeriesTitle = false;
namingConfig.IncludeEpisodeTitle = true;
namingConfig.IncludeQuality = true;
namingConfig.Separator = " - ";
namingConfig.NumberStyle = 0;
namingConfig.ReplaceSpaces = false;
namingConfig.MultiEpisodeStyle = 2;
var episodeOne = Builder<Episode>.CreateNew()
.With(e => e.Title = "Strawberries and Cream (1)")
.With(e => e.SeasonNumber = 3)
.With(e => e.EpisodeNumber = 23)
.Build();
var episodeTwo = Builder<Episode>.CreateNew()
.With(e => e.Title = "Strawberries and Cream (2)")
.With(e => e.SeasonNumber = 3)
.With(e => e.EpisodeNumber = 24)
.Build();
string result = Subject.BuildFilename(new List<Episode> { episodeOne, episodeTwo }, new Series { Title = "The Mentalist" }, new EpisodeFile { Quality = new QualityModel(Quality.HDTV720p) });
result.Should().Be("3x23x24 - Strawberries and Cream [HDTV-720p]");
}
[Test]
public void GetNewFilename_multi_Episode_Quality_01x05_Repeat_Space()
{
namingConfig.IncludeSeriesTitle = false;
namingConfig.IncludeEpisodeTitle = true;
namingConfig.IncludeQuality = true;
namingConfig.Separator = " ";
namingConfig.NumberStyle = 0;
namingConfig.ReplaceSpaces = false;
namingConfig.MultiEpisodeStyle = 2;
var episodeOne = Builder<Episode>.CreateNew()
.With(e => e.Title = "Strawberries and Cream (1)")
.With(e => e.SeasonNumber = 3)
.With(e => e.EpisodeNumber = 23)
.Build();
var episodeTwo = Builder<Episode>.CreateNew()
.With(e => e.Title = "Strawberries and Cream (2)")
.With(e => e.SeasonNumber = 3)
.With(e => e.EpisodeNumber = 24)
.Build();
string result = Subject.BuildFilename(new List<Episode> { episodeOne, episodeTwo }, new Series { Title = "The Mentalist" }, new EpisodeFile { Quality = new QualityModel(Quality.HDTV720p) });
result.Should().Be("3x23x24 Strawberries and Cream [HDTV-720p]");
}
[Test]
public void GetNewFilename_multi_Series_Episode_s01e05_Duplicate_Period()
{
namingConfig.IncludeSeriesTitle = true;
namingConfig.IncludeEpisodeTitle = true;
namingConfig.IncludeQuality = false;
namingConfig.Separator = " ";
namingConfig.NumberStyle = 3;
namingConfig.ReplaceSpaces = true;
namingConfig.MultiEpisodeStyle = 1;
var episodeOne = Builder<Episode>.CreateNew()
.With(e => e.Title = "Strawberries and Cream (1)")
.With(e => e.SeasonNumber = 3)
.With(e => e.EpisodeNumber = 23)
.Build();
var episodeTwo = Builder<Episode>.CreateNew()
.With(e => e.Title = "Strawberries and Cream (2)")
.With(e => e.SeasonNumber = 3)
.With(e => e.EpisodeNumber = 24)
.Build();
string result = Subject.BuildFilename(new List<Episode> { episodeOne, episodeTwo }, new Series { Title = "The Mentalist" }, new EpisodeFile { Quality = new QualityModel(Quality.HDTV720p) });
result.Should().Be("The.Mentalist.s03e23.s03e24.Strawberries.and.Cream");
}
[Test]
public void GetNewFilename_multi_Series_S01E05_Extend_Dash_Period()
{
namingConfig.IncludeSeriesTitle = true;
namingConfig.IncludeEpisodeTitle = false;
namingConfig.IncludeQuality = false;
namingConfig.Separator = " - ";
namingConfig.NumberStyle = 2;
namingConfig.ReplaceSpaces = true;
namingConfig.MultiEpisodeStyle = 0;
var episodeOne = Builder<Episode>.CreateNew()
.With(e => e.Title = "Strawberries and Cream (1)")
.With(e => e.SeasonNumber = 3)
.With(e => e.EpisodeNumber = 23)
.Build();
var episodeTwo = Builder<Episode>.CreateNew()
.With(e => e.Title = "Strawberries and Cream (2)")
.With(e => e.SeasonNumber = 3)
.With(e => e.EpisodeNumber = 24)
.Build();
string result = Subject.BuildFilename(new List<Episode> { episodeOne, episodeTwo }, new Series { Title = "The Mentalist" }, new EpisodeFile { Quality = new QualityModel(Quality.HDTV720p) });
result.Should().Be("The.Mentalist.-.S03E23-24");
}
[Test]
public void GetNewFilename_multi_1x05_Repeat_Dash_Period()
{
namingConfig.IncludeSeriesTitle = false;
namingConfig.IncludeEpisodeTitle = false;
namingConfig.IncludeQuality = false;
namingConfig.Separator = " - ";
namingConfig.NumberStyle = 0;
namingConfig.ReplaceSpaces = true;
namingConfig.MultiEpisodeStyle = 2;
var episodeOne = Builder<Episode>.CreateNew()
.With(e => e.Title = "Strawberries and Cream (1)")
.With(e => e.SeasonNumber = 3)
.With(e => e.EpisodeNumber = 23)
.Build();
var episodeTwo = Builder<Episode>.CreateNew()
.With(e => e.Title = "Strawberries and Cream (2)")
.With(e => e.SeasonNumber = 3)
.With(e => e.EpisodeNumber = 24)
.Build();
string result = Subject.BuildFilename(new List<Episode> { episodeOne, episodeTwo }, new Series { Title = "The Mentalist" }, new EpisodeFile { Quality = new QualityModel(Quality.HDTV720p) });
result.Should().Be("3x23x24");
}
[Test]
public void GetNewFilename_should_append_proper_when_proper_and_append_quality_is_true()
{
namingConfig.IncludeSeriesTitle = true;
namingConfig.IncludeEpisodeTitle = true;
namingConfig.IncludeQuality = true;
namingConfig.Separator = " - ";
namingConfig.NumberStyle = 2;
namingConfig.ReplaceSpaces = false;
var episode = Builder<Episode>.CreateNew()
.With(e => e.Title = "City Sushi")
.With(e => e.SeasonNumber = 15)
.With(e => e.EpisodeNumber = 6)
.Build();
string result = Subject.BuildFilename(new List<Episode> { episode }, _series, new EpisodeFile { Quality = new QualityModel(Quality.HDTV720p, true) });
result.Should().Be("South Park - S15E06 - City Sushi [HDTV-720p] [Proper]");
}
[Test]
public void GetNewFilename_should_not_append_proper_when_not_proper_and_append_quality_is_true()
{
namingConfig.IncludeSeriesTitle = true;
namingConfig.IncludeEpisodeTitle = true;
namingConfig.IncludeQuality = true;
namingConfig.Separator = " - ";
namingConfig.NumberStyle = 2;
namingConfig.ReplaceSpaces = false;
var episode = Builder<Episode>.CreateNew()
.With(e => e.Title = "City Sushi")
.With(e => e.SeasonNumber = 15)
.With(e => e.EpisodeNumber = 6)
.Build();
string result = Subject.BuildFilename(new List<Episode> { episode }, _series, new EpisodeFile { Quality = new QualityModel(Quality.HDTV720p) });
result.Should().Be("South Park - S15E06 - City Sushi [HDTV-720p]");
}
[Test]
public void GetNewFilename_should_not_append_proper_when_proper_and_append_quality_is_false()
{
namingConfig.IncludeSeriesTitle = true;
namingConfig.IncludeEpisodeTitle = true;
namingConfig.IncludeQuality = false;
namingConfig.Separator = " - ";
namingConfig.NumberStyle = 2;
namingConfig.ReplaceSpaces = false;
var episode = Builder<Episode>.CreateNew()
.With(e => e.Title = "City Sushi")
.With(e => e.SeasonNumber = 15)
.With(e => e.EpisodeNumber = 6)
.Build();
string result = Subject.BuildFilename(new List<Episode> { episode }, _series, new EpisodeFile { Quality = new QualityModel(Quality.HDTV720p, true) });
result.Should().Be("South Park - S15E06 - City Sushi");
}
[Test]
public void GetNewFilename_should_order_multiple_episode_files_in_numerical_order()
{
namingConfig.IncludeSeriesTitle = true;
namingConfig.IncludeEpisodeTitle = true;
namingConfig.IncludeQuality = false;
namingConfig.Separator = " - ";
namingConfig.NumberStyle = 2;
namingConfig.ReplaceSpaces = false;
namingConfig.MultiEpisodeStyle = 3;
var episode = Builder<Episode>.CreateNew()
.With(e => e.Title = "Hey, Baby, What's Wrong? (1)")
.With(e => e.SeasonNumber = 6)
.With(e => e.EpisodeNumber = 6)
.Build();
var episode2 = Builder<Episode>.CreateNew()
.With(e => e.Title = "Hey, Baby, What's Wrong? (2)")
.With(e => e.SeasonNumber = 6)
.With(e => e.EpisodeNumber = 7)
.Build();
_episodeFile = new EpisodeFile { Quality = new QualityModel(Quality.HDTV720p) };
}
string result = Subject.BuildFilename(new List<Episode> { episode2, episode }, new Series { Title = "30 Rock" }, new EpisodeFile { Quality = new QualityModel(Quality.HDTV720p) });
result.Should().Be("30 Rock - S06E06-E07 - Hey, Baby, What's Wrong!");
private void GivenProper()
{
_episodeFile.Quality.Proper = true;
}
[Test]
public void GetNewFilename_Series_Episode_Quality_S01E05_Period()
public void should_replace_Series_space_Title()
{
namingConfig.IncludeSeriesTitle = true;
namingConfig.IncludeEpisodeTitle = true;
namingConfig.IncludeQuality = true;
namingConfig.Separator = ".";
namingConfig.NumberStyle = 2;
namingConfig.ReplaceSpaces = false;
_namingConfig.StandardEpisodeFormat = "{Series Title}";
var episode = Builder<Episode>.CreateNew()
.With(e => e.Title = "City Sushi")
.With(e => e.SeasonNumber = 15)
.With(e => e.EpisodeNumber = 6)
.Build();
string result = Subject.BuildFilename(new List<Episode> { episode }, _series, new EpisodeFile { Quality = new QualityModel(Quality.HDTV720p) });
result.Should().Be("South Park.S15E06.City Sushi [HDTV-720p]");
Subject.BuildFilename(new List<Episode> {_episode1}, _series, _episodeFile)
.Should().Be("South Park");
}
[Test]
public void GetNewFilename_Episode_Quality_1x05_Period()
public void should_replace_Series_underscore_Title()
{
namingConfig.IncludeSeriesTitle = false;
namingConfig.IncludeEpisodeTitle = true;
namingConfig.IncludeQuality = true;
namingConfig.Separator = "."; ;
namingConfig.NumberStyle = 0;
namingConfig.ReplaceSpaces = false;
_namingConfig.StandardEpisodeFormat = "{Series_Title}";
var episode = Builder<Episode>.CreateNew()
.With(e => e.Title = "City Sushi")
.With(e => e.SeasonNumber = 15)
.With(e => e.EpisodeNumber = 6)
.Build();
string result = Subject.BuildFilename(new List<Episode> { episode }, _series, new EpisodeFile { Quality = new QualityModel(Quality.HDTV720p) });
result.Should().Be("15x06.City Sushi [HDTV-720p]");
Subject.BuildFilename(new List<Episode> {_episode1}, _series, _episodeFile)
.Should().Be("South_Park");
}
[Test]
public void GetNewFilename_UseSceneName_when_sceneName_isNull()
public void should_replace_Series_dot_Title()
{
namingConfig.IncludeSeriesTitle = false;
namingConfig.IncludeEpisodeTitle = true;
namingConfig.IncludeQuality = true;
namingConfig.Separator = "."; ;
namingConfig.NumberStyle = 0;
namingConfig.ReplaceSpaces = false;
namingConfig.RenameEpisodes = false;
_namingConfig.StandardEpisodeFormat = "{Series.Title}";
var episode = Builder<Episode>.CreateNew()
.With(e => e.Title = "City Sushi")
.With(e => e.SeasonNumber = 15)
.With(e => e.EpisodeNumber = 6)
.Build();
var episodeFile = Builder<EpisodeFile>.CreateNew()
.With(e => e.SceneName = null)
.With(e => e.Path = @"C:\Test\TV\30 Rock - S01E01 - Test")
.Build();
string result = Subject.BuildFilename(new List<Episode> { episode }, _series, episodeFile);
result.Should().Be(Path.GetFileNameWithoutExtension(episodeFile.Path));
Subject.BuildFilename(new List<Episode> {_episode1}, _series, _episodeFile)
.Should().Be("South.Park");
}
[Test]
public void GetNewFilename_UseSceneName_when_sceneName_isNotNull()
public void should_replace_Series_dash_Title()
{
namingConfig.IncludeSeriesTitle = false;
namingConfig.IncludeEpisodeTitle = true;
namingConfig.IncludeQuality = true;
namingConfig.Separator = ".";
namingConfig.NumberStyle = 0;
namingConfig.ReplaceSpaces = false;
namingConfig.RenameEpisodes = false;
_namingConfig.StandardEpisodeFormat = "{Series-Title}";
var episode = Builder<Episode>.CreateNew()
.With(e => e.Title = "City Sushi")
.With(e => e.SeasonNumber = 15)
.With(e => e.EpisodeNumber = 6)
.Build();
Subject.BuildFilename(new List<Episode> {_episode1}, _series, _episodeFile)
.Should().Be("South-Park");
}
var episodeFile = Builder<EpisodeFile>.CreateNew()
.With(e => e.SceneName = "30.Rock.S01E01.xvid-LOL")
.With(e => e.Path = @"C:\Test\TV\30 Rock - S01E01 - Test")
.Build();
[Test]
public void should_replace_SERIES_TITLE_with_all_caps()
{
_namingConfig.StandardEpisodeFormat = "{SERIES TITLE}";
Subject.BuildFilename(new List<Episode> {_episode1}, _series, _episodeFile)
.Should().Be("SOUTH PARK");
}
string result = Subject.BuildFilename(new List<Episode> { episode }, _series, episodeFile);
[Test]
public void should_replace_series_title_with_all_lower_case()
{
_namingConfig.StandardEpisodeFormat = "{series title}";
Subject.BuildFilename(new List<Episode> {_episode1}, _series, _episodeFile)
.Should().Be("south park");
}
result.Should().Be(episodeFile.SceneName);
[Test]
public void should_replace_episode_title()
{
_namingConfig.StandardEpisodeFormat = "{Episode Title}";
Subject.BuildFilename(new List<Episode> {_episode1}, _series, _episodeFile)
.Should().Be("City Sushi");
}
[Test]
public void should_replace_season_number_with_single_digit()
{
_episode1.SeasonNumber = 1;
_namingConfig.StandardEpisodeFormat = "{season}x{episode}";
Subject.BuildFilename(new List<Episode> { _episode1 }, _series, _episodeFile)
.Should().Be("1x6");
}
[Test]
public void should_replace_0season_number_with_two_digits()
{
_episode1.SeasonNumber = 1;
_namingConfig.StandardEpisodeFormat = "{0season}x{episode}";
Subject.BuildFilename(new List<Episode> { _episode1 }, _series, _episodeFile)
.Should().Be("01x6");
}
[Test]
public void should_replace_episode_number_with_single_digit()
{
_episode1.SeasonNumber = 1;
_namingConfig.StandardEpisodeFormat = "{season}x{episode}";
Subject.BuildFilename(new List<Episode> { _episode1 }, _series, _episodeFile)
.Should().Be("1x6");
}
[Test]
public void should_replace_0episode_number_with_two_digits()
{
_episode1.SeasonNumber = 1;
_namingConfig.StandardEpisodeFormat = "{season}x{0episode}";
Subject.BuildFilename(new List<Episode> { _episode1 }, _series, _episodeFile)
.Should().Be("1x06");
}
[Test]
public void should_replace_quality_title()
{
_namingConfig.StandardEpisodeFormat = "{Quality Title}";
Subject.BuildFilename(new List<Episode> { _episode1 }, _series, _episodeFile)
.Should().Be("HDTV-720p");
}
[Test]
public void should_replace_quality_title_with_proper()
{
_namingConfig.StandardEpisodeFormat = "{Quality Title}";
_episodeFile.Quality.Proper = true;
Subject.BuildFilename(new List<Episode> { _episode1 }, _series, _episodeFile)
.Should().Be("HDTV-720p Proper");
}
[Test]
public void should_replace_all_contents_in_pattern()
{
_namingConfig.StandardEpisodeFormat = "{Series Title} - S{0season}E{0episode} - {Episode Title} [{Quality Title}]";
Subject.BuildFilename(new List<Episode> {_episode1}, _series, _episodeFile)
.Should().Be("South Park - S15E06 - City Sushi [HDTV-720p]");
}
[Test]
public void use_file_name_when_sceneName_is_null()
{
_namingConfig.RenameEpisodes = false;
_episodeFile.Path = @"C:\Test\TV\30 Rock - S01E01 - Test";
Subject.BuildFilename(new List<Episode> { _episode1 }, _series, _episodeFile)
.Should().Be(Path.GetFileNameWithoutExtension(_episodeFile.Path));
}
[Test]
public void use_file_name_when_sceneName_is_not_null()
{
_namingConfig.RenameEpisodes = false;
_episodeFile.SceneName = "30.Rock.S01E01.xvid-LOL";
_episodeFile.Path = @"C:\Test\TV\30 Rock - S01E01 - Test";
Subject.BuildFilename(new List<Episode> { _episode1 }, _series, _episodeFile)
.Should().Be("30.Rock.S01E01.xvid-LOL");
}
[Test]
public void should_only_have_one_episodeTitle_when_episode_titles_are_the_same()
{
namingConfig.IncludeSeriesTitle = true;
namingConfig.IncludeEpisodeTitle = true;
namingConfig.IncludeQuality = false;
namingConfig.Separator = " - ";
namingConfig.NumberStyle = 2;
namingConfig.ReplaceSpaces = false;
namingConfig.MultiEpisodeStyle = 3;
_namingConfig.StandardEpisodeFormat = "{Series Title} - S{0season}E{0episode} - {Episode Title}";
_namingConfig.MultiEpisodeStyle = 3;
var episode = Builder<Episode>.CreateNew()
.With(e => e.Title = "Hey, Baby, What's Wrong? (1)")
@ -607,163 +228,91 @@ namespace NzbDrone.Core.Test.OrganizerTests
.Build();
string result = Subject.BuildFilename(new List<Episode> { episode2, episode }, new Series { Title = "30 Rock" }, new EpisodeFile { Quality = new QualityModel(Quality.HDTV720p) });
result.Should().Be("30 Rock - S06E06-E07 - Hey, Baby, What's Wrong!");
Subject.BuildFilename(new List<Episode> {episode2, episode}, new Series {Title = "30 Rock"}, _episodeFile)
.Should().Be("30 Rock - S06E06-E07 - Hey, Baby, What's Wrong!");
}
[Test]
public void should_have_two_episodeTitles_when_episode_titles_are_not_the_same()
{
namingConfig.IncludeSeriesTitle = true;
namingConfig.IncludeEpisodeTitle = true;
namingConfig.IncludeQuality = false;
namingConfig.Separator = " - ";
namingConfig.NumberStyle = 2;
namingConfig.ReplaceSpaces = false;
namingConfig.MultiEpisodeStyle = 3;
_namingConfig.StandardEpisodeFormat = "{Series Title} - S{0season}E{0episode} - {Episode Title}";
_namingConfig.MultiEpisodeStyle = 3;
var episode = Builder<Episode>.CreateNew()
.With(e => e.Title = "Hello")
.With(e => e.SeasonNumber = 6)
.With(e => e.EpisodeNumber = 6)
.Build();
_episode1.Title = "Hello";
_episode2.Title = "World";
var episode2 = Builder<Episode>.CreateNew()
.With(e => e.Title = "World")
.With(e => e.SeasonNumber = 6)
.With(e => e.EpisodeNumber = 7)
.Build();
string result = Subject.BuildFilename(new List<Episode> { episode2, episode }, new Series { Title = "30 Rock" }, new EpisodeFile { Quality = new QualityModel(Quality.HDTV720p) });
result.Should().Be("30 Rock - S06E06-E07 - Hello + World");
}
[Test]
public void should_have_two_episodeTitles_when_distinct_count_is_two()
{
namingConfig.IncludeSeriesTitle = true;
namingConfig.IncludeEpisodeTitle = true;
namingConfig.IncludeQuality = false;
namingConfig.Separator = " - ";
namingConfig.NumberStyle = 2;
namingConfig.ReplaceSpaces = false;
namingConfig.MultiEpisodeStyle = 3;
var episode = Builder<Episode>.CreateNew()
.With(e => e.Title = "Hello (3)")
.With(e => e.SeasonNumber = 6)
.With(e => e.EpisodeNumber = 6)
.Build();
var episode2 = Builder<Episode>.CreateNew()
.With(e => e.Title = "Hello (2)")
.With(e => e.SeasonNumber = 6)
.With(e => e.EpisodeNumber = 7)
.Build();
var episode3 = Builder<Episode>.CreateNew()
.With(e => e.Title = "World")
.With(e => e.SeasonNumber = 6)
.With(e => e.EpisodeNumber = 8)
.Build();
string result = Subject.BuildFilename(new List<Episode> { episode, episode2, episode3 }, new Series { Title = "30 Rock" }, new EpisodeFile { Quality = new QualityModel(Quality.HDTV720p) });
result.Should().Be("30 Rock - S06E06-E07-E08 - Hello + World");
Subject.BuildFilename(new List<Episode> {_episode1, _episode2}, _series, _episodeFile)
.Should().Be("South Park - S15E06-E07 - Hello + World");
}
[Test]
public void should_use_airDate_if_series_isDaily()
{
_namingConfig.DailyEpisodeFormat = "{Series Title} - {air-date} - {Episode Title}";
namingConfig.IncludeSeriesTitle = true;
namingConfig.IncludeEpisodeTitle = true;
namingConfig.IncludeQuality = true;
namingConfig.Separator = " - ";
namingConfig.NumberStyle = 2;
namingConfig.ReplaceSpaces = false;
_series.Title = "The Daily Show with Jon Stewart";
_series.SeriesType = SeriesTypes.Daily;
var series = Builder<Series>
.CreateNew()
.With(s => s.SeriesType = SeriesTypes.Daily)
.With(s => s.Title = "The Daily Show with Jon Stewart")
.Build();
_episode1.AirDate = "2012-12-13";
_episode1.Title = "Kristen Stewart";
var episodes = Builder<Episode>
.CreateListOfSize(1)
.All()
.With(e => e.AirDate = "2012-12-13")
.With(e => e.Title = "Kristen Stewart")
.Build();
var result = Subject
.BuildFilename(episodes, series, new EpisodeFile { Quality = new QualityModel(Quality.HDTV720p) });
result.Should().Be("The Daily Show with Jon Stewart - 2012-12-13 - Kristen Stewart [HDTV-720p]");
}
[Test]
public void should_use_airDate_if_series_isDaily_no_episode_title()
{
namingConfig.IncludeSeriesTitle = true;
namingConfig.IncludeEpisodeTitle = false;
namingConfig.IncludeQuality = false;
namingConfig.Separator = " - ";
namingConfig.NumberStyle = 2;
namingConfig.ReplaceSpaces = false;
var series = Builder<Series>
.CreateNew()
.With(s => s.SeriesType = SeriesTypes.Daily)
.With(s => s.Title = "The Daily Show with Jon Stewart")
.Build();
var episodes = Builder<Episode>
.CreateListOfSize(1)
.All()
.With(e => e.AirDate = "2012-12-13")
.With(e => e.Title = "Kristen Stewart")
.Build();
var result = Subject
.BuildFilename(episodes, series, new EpisodeFile { Quality = new QualityModel(Quality.HDTV720p) });
result.Should().Be("The Daily Show with Jon Stewart - 2012-12-13");
Subject.BuildFilename(new List<Episode> { _episode1 }, _series, _episodeFile)
.Should().Be("The Daily Show with Jon Stewart - 2012-12-13 - Kristen Stewart");
}
[Test]
public void should_set_airdate_to_unknown_if_not_available()
{
_namingConfig.DailyEpisodeFormat = "{Series Title} - {Air-Date} - {Episode Title}";
namingConfig.IncludeSeriesTitle = true;
namingConfig.IncludeEpisodeTitle = true;
namingConfig.IncludeQuality = false;
namingConfig.Separator = " - ";
namingConfig.NumberStyle = 2;
namingConfig.ReplaceSpaces = false;
_series.Title = "The Daily Show with Jon Stewart";
_series.SeriesType = SeriesTypes.Daily;
var series = Builder<Series>
.CreateNew()
.With(s => s.SeriesType = SeriesTypes.Daily)
.With(s => s.Title = "The Daily Show with Jon Stewart")
.Build();
_episode1.AirDate = null;
_episode1.Title = "Kristen Stewart";
var episodes = Builder<Episode>
.CreateListOfSize(1)
.All()
.With(e => e.AirDate = null)
.With(e => e.Title = "Kristen Stewart")
.Build();
Subject.BuildFilename(new List<Episode> { _episode1 }, _series, _episodeFile)
.Should().Be("The Daily Show with Jon Stewart - Unknown - Kristen Stewart");
}
var result = Subject
.BuildFilename(episodes, series, new EpisodeFile { Quality = new QualityModel(Quality.HDTV720p) });
result.Should().Be("The Daily Show with Jon Stewart - Unknown - Kristen Stewart");
[Test]
public void should_format_extend_multi_episode_properly()
{
_namingConfig.StandardEpisodeFormat = "{Series Title} - S{0season}E{0episode} - {Episode Title}";
_namingConfig.MultiEpisodeStyle = 0;
Subject.BuildFilename(new List<Episode> {_episode1, _episode2}, _series, _episodeFile)
.Should().Be("South Park - S15E06-07 - City Sushi");
}
[Test]
public void should_format_duplicate_multi_episode_properly()
{
_namingConfig.StandardEpisodeFormat = "{Series Title} - S{0season}E{0episode} - {Episode Title}";
_namingConfig.MultiEpisodeStyle = 1;
Subject.BuildFilename(new List<Episode> { _episode1, _episode2 }, _series, _episodeFile)
.Should().Be("South Park - S15E06 - S15E07 - City Sushi");
}
[Test]
public void should_format_repeat_multi_episode_properly()
{
_namingConfig.StandardEpisodeFormat = "{Series Title} - S{0season}E{0episode} - {Episode Title}";
_namingConfig.MultiEpisodeStyle = 2;
Subject.BuildFilename(new List<Episode> { _episode1, _episode2 }, _series, _episodeFile)
.Should().Be("South Park - S15E06E07 - City Sushi");
}
[Test]
public void should_format_scene_multi_episode_properly()
{
_namingConfig.StandardEpisodeFormat = "{Series Title} - S{0season}E{0episode} - {Episode Title}";
_namingConfig.MultiEpisodeStyle = 3;
Subject.BuildFilename(new List<Episode> { _episode1, _episode2 }, _series, _episodeFile)
.Should().Be("South Park - S15E06-E07 - City Sushi");
}
}
}

View File

@ -151,7 +151,7 @@ namespace NzbDrone.Core.Configuration
public string SeasonFolderFormat
{
get { return GetValue("SeasonFolderFormat", "Season %s"); }
get { return GetValue("SeasonFolderFormat", "Season {season}"); }
set { SetValue("SeasonFolderFormat", value); }
}

View File

@ -0,0 +1,143 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using FluentMigrator;
using NzbDrone.Core.Datastore.Migration.Framework;
namespace NzbDrone.Core.Datastore.Migration
{
[Migration(29)]
public class add_formats_to_naming_config : NzbDroneMigrationBase
{
protected override void MainDbUpgrade()
{
Alter.Table("NamingConfig").AddColumn("StandardEpisodeFormat").AsString().Nullable();
Alter.Table("NamingConfig").AddColumn("DailyEpisodeFormat").AsString().Nullable();
Execute.WithConnection(ConvertConfig);
}
private void ConvertConfig(IDbConnection conn, IDbTransaction tran)
{
using (IDbCommand namingConfigCmd = conn.CreateCommand())
{
namingConfigCmd.Transaction = tran;
namingConfigCmd.CommandText = @"SELECT * FROM NamingConfig LIMIT 1";
using (IDataReader namingConfigReader = namingConfigCmd.ExecuteReader())
{
while (namingConfigReader.Read())
{
var separator = namingConfigReader.GetString(1);
var numberStyle = namingConfigReader.GetInt32(2);
var includeSeriesTitle = namingConfigReader.GetBoolean(3);
var includeEpisodeTitle = namingConfigReader.GetBoolean(5);
var includeQuality = namingConfigReader.GetBoolean(6);
var replaceSpaces = namingConfigReader.GetBoolean(7);
//Output settings
var seriesTitlePattern = "";
var episodeTitlePattern = "";
var dailyEpisodePattern = "{Air Date}";
var qualityFormat = " [{Quality Title}]";
if (includeSeriesTitle)
{
seriesTitlePattern = "{Series Title}" + separator;
if (replaceSpaces)
{
seriesTitlePattern = "{Series.Title}" + separator;
}
}
if (includeEpisodeTitle)
{
episodeTitlePattern = separator + "{Episode Title}";
if (replaceSpaces)
{
episodeTitlePattern = separator + "{Episode.Title}";
}
}
if (replaceSpaces)
{
dailyEpisodePattern = "{Air.Date}";
}
var standardEpisodeFormat = String.Format("{0}{1}{2}", seriesTitlePattern,
GetNumberStyle(numberStyle).Pattern,
episodeTitlePattern);
var dailyEpisodeFormat = String.Format("{0}{1}{2}", seriesTitlePattern,
dailyEpisodePattern,
episodeTitlePattern);
if (includeQuality)
{
if (replaceSpaces)
{
qualityFormat = " [{Quality.Title}]";
}
standardEpisodeFormat += qualityFormat;
dailyEpisodeFormat += qualityFormat;
}
using (IDbCommand updateCmd = conn.CreateCommand())
{
var text = String.Format("UPDATE NamingConfig " +
"SET StandardEpisodeFormat = '{0}', " +
"DailyEpisodeFormat = '{1}'",
standardEpisodeFormat,
dailyEpisodeFormat);
updateCmd.Transaction = tran;
updateCmd.CommandText = text;
updateCmd.ExecuteNonQuery();
}
}
}
}
}
private static readonly List<dynamic> NumberStyles = new List<dynamic>
{
new
{
Id = 0,
Name = "1x05",
Pattern = "{season}x{0episode}",
EpisodeSeparator = "x"
},
new
{
Id = 1,
Name = "01x05",
Pattern = "{0season}x{0episode}",
EpisodeSeparator = "x"
},
new
{
Id = 2,
Name = "S01E05",
Pattern = "S{0season}E{0episode}",
EpisodeSeparator = "E"
},
new
{
Id = 3,
Name = "s01e05",
Pattern = "s{0season}e{0episode}",
EpisodeSeparator = "e"
}
};
private static dynamic GetNumberStyle(int id)
{
return NumberStyles.Single(s => s.Id == id);
}
}
}

View File

@ -0,0 +1,52 @@
using System;
using System.Data;
using FluentMigrator;
using NzbDrone.Core.Datastore.Migration.Framework;
namespace NzbDrone.Core.Datastore.Migration
{
[Migration(30)]
public class update_series_folder_format : NzbDroneMigrationBase
{
protected override void MainDbUpgrade()
{
Execute.WithConnection(ConvertConfig);
}
private void ConvertConfig(IDbConnection conn, IDbTransaction tran)
{
using (IDbCommand namingConfigCmd = conn.CreateCommand())
{
namingConfigCmd.Transaction = tran;
namingConfigCmd.CommandText = @"SELECT * FROM Config WHERE [Key] = 'SeasonFolderFormat'";
using (IDataReader namingConfigReader = namingConfigCmd.ExecuteReader())
{
while (namingConfigReader.Read())
{
var value = namingConfigReader.GetString(2);
value = value.Replace("%sn", "{Series Title}")
.Replace("%s.n", "{Series.Title}")
.Replace("%s", "{season}")
.Replace("%0s", "{0season}")
.Replace("%e", "{episode}")
.Replace("%0e", "{0episode}");
using (IDbCommand updateCmd = conn.CreateCommand())
{
var text = String.Format("UPDATE Config " +
"SET [VALUE] = '{0}'" +
"WHERE [Key] = 'SeasonFolderFormat'",
value);
updateCmd.Transaction = tran;
updateCmd.CommandText = text;
updateCmd.ExecuteNonQuery();
}
}
}
}
}
}
}

View File

@ -0,0 +1,23 @@
using FluentMigrator;
using NzbDrone.Core.Datastore.Migration.Framework;
namespace NzbDrone.Core.Datastore.Migration
{
[Migration(31)]
public class delete_old_naming_config_columns : NzbDroneMigrationBase
{
protected override void MainDbUpgrade()
{
SqLiteAlter.DropColumns("NamingConfig",
new[]
{
"Separator",
"NumberStyle",
"IncludeSeriesTitle",
"IncludeEpisodeTitle",
"IncludeQuality",
"ReplaceSpaces"
});
}
}
}

View File

@ -185,6 +185,9 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="Datastore\Migration\028_add_blacklist_table.cs" />
<Compile Include="Datastore\Migration\029_add_formats_to_naming_config.cs" />
<Compile Include="Datastore\Migration\030_update_series_folder_format.cs" />
<Compile Include="Datastore\Migration\031_delete_old_naming_config_columns.cs" />
<Compile Include="Datastore\Migration\Framework\MigrationContext.cs" />
<Compile Include="Datastore\Migration\Framework\MigrationController.cs" />
<Compile Include="Datastore\Migration\Framework\MigrationExtension.cs" />
@ -321,6 +324,8 @@
<Compile Include="Notifications\Xbmc\Model\VersionResult.cs" />
<Compile Include="Notifications\Xbmc\Model\XbmcJsonResult.cs" />
<Compile Include="Notifications\Xbmc\Model\XbmcVersion.cs" />
<Compile Include="Organizer\EpisodeFormat.cs" />
<Compile Include="Organizer\FilenameBuilderTokenEqualityComparer.cs" />
<Compile Include="Parser\InvalidDateException.cs" />
<Compile Include="Parser\Model\SeriesTitleInfo.cs" />
<Compile Include="ProgressMessaging\CommandUpdatedEvent.cs" />

View File

@ -0,0 +1,12 @@
using System;
namespace NzbDrone.Core.Organizer
{
public class EpisodeFormat
{
public String Separator { get; set; }
public String EpisodePattern { get; set; }
public String EpisodeSeparator { get; set; }
public String SeasonEpisodePattern { get; set; }
}
}

View File

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using NLog;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Datastore;
@ -57,6 +58,18 @@ namespace NzbDrone.Core.Organizer
private readonly INamingConfigService _namingConfigService;
private readonly Logger _logger;
private static readonly Regex TitleRegex = new Regex(@"(?<token>\{(?:\w+)(?<separator>\s|\W|_)\w+\})",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex EpisodeRegex = new Regex(@"(?<episode>\{0*(?:episode)})",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex SeasonRegex = new Regex(@"(?<season>\{0*(?:season)})",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex SeasonEpisodePatternRegex = new Regex(@"(?<separator>(?<=}).+?)?(?<seasonEpisode>s?{0?season}(?<episodeSeparator>e|x)?(?<episode>{0?episode}))(?<separator>.+?(?={))?",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
public FileNameBuilder(INamingConfigService namingConfigService, IConfigService configService, Logger logger)
{
_namingConfigService = namingConfigService;
@ -83,86 +96,78 @@ namespace NzbDrone.Core.Organizer
return episodeFile.SceneName;
}
var sortedEpisodes = episodes.OrderBy(e => e.EpisodeNumber);
var numberStyle = GetNumberStyle(nameSpec.NumberStyle);
var episodeNames = new List<string>
var sortedEpisodes = episodes.OrderBy(e => e.EpisodeNumber).ToList();
var pattern = nameSpec.StandardEpisodeFormat;
var episodeTitles = new List<string>
{
Parser.Parser.CleanupEpisodeTitle(sortedEpisodes.First().Title)
};
var result = String.Empty;
var tokenValues = new Dictionary<string, string>(new FilenameBuilderTokenEqualityComparer());
tokenValues.Add("{Series Title}", series.Title);
if (nameSpec.IncludeSeriesTitle)
if (series.SeriesType == SeriesTypes.Daily)
{
result += series.Title + nameSpec.Separator;
pattern = nameSpec.DailyEpisodeFormat;
if (!String.IsNullOrWhiteSpace(episodes.First().AirDate))
{
tokenValues.Add("{Air Date}", episodes.First().AirDate);
}
if (series.SeriesType == SeriesTypes.Standard)
{
result += numberStyle.Pattern.Replace("%0e",
String.Format("{0:00}", sortedEpisodes.First().EpisodeNumber));
else {
tokenValues.Add("{Air Date}", "Unknown");
}
}
if (episodes.Count > 1)
var seasonEpisode = SeasonEpisodePatternRegex.Match(pattern);
if (seasonEpisode.Success)
{
var multiEpisodeStyle =
GetMultiEpisodeStyle(nameSpec.MultiEpisodeStyle);
var episodeFormat = new EpisodeFormat
{
EpisodeSeparator = seasonEpisode.Groups["episodeSeparator"].Value,
Separator = seasonEpisode.Groups["separator"].Value,
EpisodePattern = seasonEpisode.Groups["episode"].Value,
SeasonEpisodePattern = seasonEpisode.Groups["seasonEpisode"].Value,
};
pattern = pattern.Replace(episodeFormat.SeasonEpisodePattern, "{Season Episode}");
var seasonEpisodePattern = episodeFormat.SeasonEpisodePattern;
foreach (var episode in sortedEpisodes.Skip(1))
{
if (multiEpisodeStyle.Name == "Duplicate")
switch ((MultiEpisodeStyle)nameSpec.MultiEpisodeStyle)
{
result += nameSpec.Separator + numberStyle.Pattern;
}
else
{
result += multiEpisodeStyle.Pattern;
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;
}
result = result.Replace("%0e", String.Format("{0:00}", episode.EpisodeNumber));
episodeNames.Add(Parser.Parser.CleanupEpisodeTitle(episode.Title));
}
episodeTitles.Add(Parser.Parser.CleanupEpisodeTitle(episode.Title));
}
result = result
.Replace("%s", String.Format("{0}", episodes.First().SeasonNumber))
.Replace("%0s", String.Format("{0:00}", episodes.First().SeasonNumber))
.Replace("%x", numberStyle.EpisodeSeparator)
.Replace("%p", nameSpec.Separator);
seasonEpisodePattern = ReplaceNumberTokens(seasonEpisodePattern, sortedEpisodes);
tokenValues.Add("{Season Episode}", seasonEpisodePattern);
}
else
{
if (!String.IsNullOrEmpty(episodes.First().AirDate))
result += episodes.First().AirDate;
tokenValues.Add("{Episode Title}", String.Join(" + ", episodeTitles.Distinct()));
tokenValues.Add("{Quality Title}", episodeFile.Quality.ToString());
else
result += "Unknown";
}
if (nameSpec.IncludeEpisodeTitle)
{
if (episodeNames.Distinct().Count() == 1)
result += nameSpec.Separator + episodeNames.First();
else
result += nameSpec.Separator + String.Join(" + ", episodeNames.Distinct());
}
if (nameSpec.IncludeQuality)
{
result += String.Format(" [{0}]", episodeFile.Quality.Quality);
if (episodeFile.Quality.Proper)
result += " [Proper]";
}
if (nameSpec.ReplaceSpaces)
result = result.Replace(' ', '.');
_logger.Trace("New File Name is: [{0}]", result.Trim());
return CleanFilename(result.Trim());
return CleanFilename(ReplaceTokens(pattern, tokenValues).Trim());
}
public string BuildFilePath(Series series, int seasonNumber, string fileName, string extension)
@ -179,12 +184,11 @@ namespace NzbDrone.Core.Organizer
else
{
seasonFolder = _configService.SeasonFolderFormat
.Replace("%sn", series.Title)
.Replace("%s.n", series.Title.Replace(' ', '.'))
.Replace("%s_n", series.Title.Replace(' ', '_'))
.Replace("%0s", seasonNumber.ToString("00"))
.Replace("%s", seasonNumber.ToString());
var tokenValues = new Dictionary<string, string>(new FilenameBuilderTokenEqualityComparer());
tokenValues.Add("{Series Title}", series.Title);
seasonFolder = ReplaceSeasonTokens(_configService.SeasonFolderFormat, seasonNumber);
seasonFolder = ReplaceTokens(seasonFolder, tokenValues);
}
path = Path.Combine(path, seasonFolder);
@ -205,38 +209,60 @@ namespace NzbDrone.Core.Organizer
return result.Trim();
}
private static readonly List<EpisodeSortingType> NumberStyles = new List<EpisodeSortingType>
private string ReplaceTokens(string pattern, Dictionary<string, string> tokenValues)
{
new EpisodeSortingType
{
Id = 0,
Name = "1x05",
Pattern = "%sx%0e",
EpisodeSeparator = "x"
},
new EpisodeSortingType
{
Id = 1,
Name = "01x05",
Pattern = "%0sx%0e",
EpisodeSeparator = "x"
},
new EpisodeSortingType
{
Id = 2,
Name = "S01E05",
Pattern = "S%0sE%0e",
EpisodeSeparator = "E"
},
new EpisodeSortingType
{
Id = 3,
Name = "s01e05",
Pattern = "s%0se%0e",
EpisodeSeparator = "e"
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;
var replacementText = tokenValues[token];
var patternTokenArray = token.ToCharArray();
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)
{
var zeroCount = token.Count(z => z == '0');
return value.ToString().PadLeft(zeroCount + 1, '0');
}
};
private static readonly List<EpisodeSortingType> MultiEpisodeStyles = new List<EpisodeSortingType>
{
@ -266,15 +292,17 @@ namespace NzbDrone.Core.Organizer
}
};
private static EpisodeSortingType GetNumberStyle(int id)
{
return NumberStyles.Single(s => s.Id == id);
}
private static EpisodeSortingType GetMultiEpisodeStyle(int id)
{
return MultiEpisodeStyles.Single(s => s.Id == id);
}
}
public enum MultiEpisodeStyle
{
Extend = 0,
Duplicate = 1,
Repeat = 2,
Scene = 3
}
}

View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace NzbDrone.Core.Organizer
{
public class FilenameBuilderTokenEqualityComparer : IEqualityComparer<String>
{
private static readonly Regex SimpleTokenRegex = new Regex(@"\s|_|\W", RegexOptions.Compiled | RegexOptions.IgnoreCase);
public bool Equals(String s1, String s2)
{
return SimplifyToken(s1).Equals(SimplifyToken(s2));
}
public int GetHashCode(String str)
{
return SimplifyToken(str).GetHashCode();
}
private string SimplifyToken(string token)
{
return SimpleTokenRegex.Replace(token, String.Empty).ToLower();
}
}
}

View File

@ -11,31 +11,16 @@ namespace NzbDrone.Core.Organizer
return new NamingConfig
{
RenameEpisodes = true,
Separator = " - ",
NumberStyle = 0,
IncludeSeriesTitle = true,
MultiEpisodeStyle = 0,
IncludeEpisodeTitle = true,
IncludeQuality = true,
ReplaceSpaces = false
StandardEpisodeFormat = "{Series Title} - {season}x{0episode} - {Episode Title} {Quality Title}",
DailyEpisodeFormat = "{Series Title} - {Air Date} - {Episode Title} {Quality Title}"
};
}
}
public bool RenameEpisodes { get; set; }
public string Separator { get; set; }
public int NumberStyle { get; set; }
public bool IncludeSeriesTitle { get; set; }
public bool IncludeEpisodeTitle { get; set; }
public bool IncludeQuality { get; set; }
public int MultiEpisodeStyle { get; set; }
public bool ReplaceSpaces { get; set; }
public string StandardEpisodeFormat { get; set; }
public string DailyEpisodeFormat { get; set; }
}
}

View File

@ -85,7 +85,7 @@ namespace NzbDrone.Core.Tv
string result = Quality.ToString();
if (Proper)
{
result += " [proper]";
result += " Proper";
}
return result;