Server side for custom naming is complete

pull/2/head
Mark McDowall 11 years ago
parent 5fd25ff3f3
commit 7b54bca3c7

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

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

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

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

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

@ -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);
}
}
}

@ -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();
}
}
}
}
}
}
}

@ -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"
});
}
}
}

@ -185,6 +185,9 @@
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Compile Include="Datastore\Migration\028_add_blacklist_table.cs" /> <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\MigrationContext.cs" />
<Compile Include="Datastore\Migration\Framework\MigrationController.cs" /> <Compile Include="Datastore\Migration\Framework\MigrationController.cs" />
<Compile Include="Datastore\Migration\Framework\MigrationExtension.cs" /> <Compile Include="Datastore\Migration\Framework\MigrationExtension.cs" />
@ -321,6 +324,8 @@
<Compile Include="Notifications\Xbmc\Model\VersionResult.cs" /> <Compile Include="Notifications\Xbmc\Model\VersionResult.cs" />
<Compile Include="Notifications\Xbmc\Model\XbmcJsonResult.cs" /> <Compile Include="Notifications\Xbmc\Model\XbmcJsonResult.cs" />
<Compile Include="Notifications\Xbmc\Model\XbmcVersion.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\InvalidDateException.cs" />
<Compile Include="Parser\Model\SeriesTitleInfo.cs" /> <Compile Include="Parser\Model\SeriesTitleInfo.cs" />
<Compile Include="ProgressMessaging\CommandUpdatedEvent.cs" /> <Compile Include="ProgressMessaging\CommandUpdatedEvent.cs" />

@ -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; }
}
}

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

@ -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();
}
}
}

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

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

Loading…
Cancel
Save