1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-04 10:02:40 +01:00

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

View File

@ -28,8 +28,6 @@ public NamingModule(INamingConfigService namingConfigService, IBuildFileNames bu
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)

View File

@ -26,12 +26,12 @@ public void Setup()
} }
[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()

View File

@ -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 @@ public void Setup()
.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);
_episode1 = Builder<Episode>.CreateNew()
}
[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()
.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)
.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) .With(e => e.EpisodeNumber = 7)
.Build(); .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) }); private void GivenProper()
{
_episodeFile.Quality.Proper = true;
result.Should().Be("30 Rock - S06E06-E07 - Hey, Baby, What's Wrong!");
} }
[Test] [Test]
public void GetNewFilename_Series_Episode_Quality_S01E05_Period() public void should_replace_Series_space_Title()
{ {
namingConfig.IncludeSeriesTitle = true; _namingConfig.StandardEpisodeFormat = "{Series Title}";
namingConfig.IncludeEpisodeTitle = true;
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("South Park");
.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] [Test]
public void GetNewFilename_Episode_Quality_1x05_Period() public void should_replace_Series_underscore_Title()
{ {
namingConfig.IncludeSeriesTitle = false; _namingConfig.StandardEpisodeFormat = "{Series_Title}";
namingConfig.IncludeEpisodeTitle = true;
namingConfig.IncludeQuality = true;
namingConfig.Separator = "."; ;
namingConfig.NumberStyle = 0;
namingConfig.ReplaceSpaces = false;
var episode = Builder<Episode>.CreateNew() Subject.BuildFilename(new List<Episode> {_episode1}, _series, _episodeFile)
.With(e => e.Title = "City Sushi") .Should().Be("South_Park");
.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] [Test]
public void GetNewFilename_UseSceneName_when_sceneName_isNull() public void should_replace_Series_dot_Title()
{ {
namingConfig.IncludeSeriesTitle = false; _namingConfig.StandardEpisodeFormat = "{Series.Title}";
namingConfig.IncludeEpisodeTitle = true;
namingConfig.IncludeQuality = true;
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("South.Park");
.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));
} }
[Test] [Test]
public void GetNewFilename_UseSceneName_when_sceneName_isNotNull() 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.RenameEpisodes = false;
var episode = Builder<Episode>.CreateNew() Subject.BuildFilename(new List<Episode> {_episode1}, _series, _episodeFile)
.With(e => e.Title = "City Sushi") .Should().Be("South-Park");
.With(e => e.SeasonNumber = 15) }
.With(e => e.EpisodeNumber = 6)
.Build();
var episodeFile = Builder<EpisodeFile>.CreateNew() [Test]
.With(e => e.SceneName = "30.Rock.S01E01.xvid-LOL") public void should_replace_SERIES_TITLE_with_all_caps()
.With(e => e.Path = @"C:\Test\TV\30 Rock - S01E01 - Test") {
.Build(); _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] [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 @@ public void should_only_have_one_episodeTitle_when_episode_titles_are_the_same()
.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() Subject.BuildFilename(new List<Episode> {_episode1, _episode2}, _series, _episodeFile)
.With(e => e.Title = "World") .Should().Be("South Park - S15E06-E07 - Hello + 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");
} }
[Test] [Test]
public void should_use_airDate_if_series_isDaily() public void should_use_airDate_if_series_isDaily()
{ {
_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 = "2012-12-13";
.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> Subject.BuildFilename(new List<Episode> { _episode1 }, _series, _episodeFile)
.CreateListOfSize(1) .Should().Be("The Daily Show with Jon Stewart - 2012-12-13 - Kristen Stewart");
.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");
} }
[Test] [Test]
public void should_set_airdate_to_unknown_if_not_available() 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 = false;
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> Subject.BuildFilename(new List<Episode> { _episode1 }, _series, _episodeFile)
.CreateListOfSize(1) .Should().Be("The Daily Show with Jon Stewart - Unknown - Kristen Stewart");
.All() }
.With(e => e.AirDate = null)
.With(e => e.Title = "Kristen Stewart")
.Build();
var result = Subject [Test]
.BuildFilename(episodes, series, new EpisodeFile { Quality = new QualityModel(Quality.HDTV720p) }); public void should_format_extend_multi_episode_properly()
result.Should().Be("The Daily Show with Jon Stewart - Unknown - Kristen Stewart"); {
_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 @@ public bool UseSeasonFolder
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); }
} }

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> <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" />

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.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 @@ public class FileNameBuilder : IBuildFileNames
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 @@ public string BuildFilename(IList<Episode> episodes, Series series, EpisodeFile
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 var seasonEpisode = SeasonEpisodePatternRegex.Match(pattern);
if (seasonEpisode.Success)
{ {
if (!String.IsNullOrEmpty(episodes.First().AirDate)) var episodeFormat = new EpisodeFormat
result += episodes.First().AirDate; {
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 += "Unknown"; var seasonEpisodePattern = episodeFormat.SeasonEpisodePattern;
}
if (nameSpec.IncludeEpisodeTitle) foreach (var episode in sortedEpisodes.Skip(1))
{ {
if (episodeNames.Distinct().Count() == 1) switch ((MultiEpisodeStyle)nameSpec.MultiEpisodeStyle)
result += nameSpec.Separator + episodeNames.First(); {
case MultiEpisodeStyle.Duplicate:
seasonEpisodePattern += episodeFormat.Separator + episodeFormat.SeasonEpisodePattern;
break;
else case MultiEpisodeStyle.Repeat:
result += nameSpec.Separator + String.Join(" + ", episodeNames.Distinct()); seasonEpisodePattern += episodeFormat.EpisodeSeparator + episodeFormat.EpisodePattern;
} break;
if (nameSpec.IncludeQuality) case MultiEpisodeStyle.Scene:
{ seasonEpisodePattern += "-" + episodeFormat.EpisodeSeparator + episodeFormat.EpisodePattern;
result += String.Format(" [{0}]", episodeFile.Quality.Quality); break;
if (episodeFile.Quality.Proper) //MultiEpisodeStyle.Extend
result += " [Proper]"; default:
} seasonEpisodePattern += "-" + episodeFormat.EpisodePattern;
break;
}
if (nameSpec.ReplaceSpaces) episodeTitles.Add(Parser.Parser.CleanupEpisodeTitle(episode.Title));
result = result.Replace(' ', '.'); }
_logger.Trace("New File Name is: [{0}]", result.Trim()); seasonEpisodePattern = ReplaceNumberTokens(seasonEpisodePattern, sortedEpisodes);
return CleanFilename(result.Trim()); tokenValues.Add("{Season Episode}", seasonEpisodePattern);
}
tokenValues.Add("{Episode Title}", String.Join(" + ", episodeTitles.Distinct()));
tokenValues.Add("{Quality Title}", episodeFile.Quality.ToString());
return CleanFilename(ReplaceTokens(pattern, tokenValues).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 @@ public string BuildFilePath(Series series, int seasonNumber, string fileName, st
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 @@ public static string CleanFilename(string name)
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",
Pattern = "%sx%0e",
EpisodeSeparator = "x"
}, private string ReplaceToken(Match match, Dictionary<string, string> tokenValues)
new EpisodeSortingType {
{ var separator = match.Groups["separator"].Value;
Id = 1, var token = match.Groups["token"].Value;
Name = "01x05", var replacementText = tokenValues[token];
Pattern = "%0sx%0e", var patternTokenArray = token.ToCharArray();
EpisodeSeparator = "x"
}, if (patternTokenArray.All(t => !char.IsLetter(t) || char.IsLower(t)))
new EpisodeSortingType {
{ replacementText = replacementText.ToLowerInvariant();
Id = 2, }
Name = "S01E05",
Pattern = "S%0sE%0e", else if (patternTokenArray.All(t => !char.IsLetter(t) || char.IsUpper(t)))
EpisodeSeparator = "E" {
}, replacementText = replacementText.ToUpper();
new EpisodeSortingType }
{
Id = 3, if (!separator.Equals(" "))
Name = "s01e05", {
Pattern = "s%0se%0e", replacementText = replacementText.Replace(" ", separator);
EpisodeSeparator = "e" }
}
}; 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> private static readonly List<EpisodeSortingType> MultiEpisodeStyles = new List<EpisodeSortingType>
{ {
@ -266,15 +292,17 @@ public static string CleanFilename(string name)
} }
}; };
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
}
} }

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 @@ public static NamingConfig Default
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; }
} }
} }

View File

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