From c27f08738acda9bfffb65a1a8006cd0ca00d3c24 Mon Sep 17 00:00:00 2001 From: jpogs <49833162+jpogs@users.noreply.github.com> Date: Sun, 1 Sep 2019 23:53:40 +0800 Subject: [PATCH] Fixed: .srt files in subfolders are not being imported (#3647) * Fixed #1958 - extra file module will search for any subfolder and filename - fixed language parser to match RARBG format - Add .srt subs according to level of details such that higher detailed sub gets loaded to media player first * Fixed Code Factor issue on SubtitleService.cs:104 * Fixed: issues on unit test for TV subs; added test cases for RARBG movie subs * Updated RARBG parser so it won't match movie title format * Cleaned up code for review * Update SubtitleService.cs --- .../ParserTests/LanguageParserFixture.cs | 4 ++++ src/NzbDrone.Core/Extras/ExtraService.cs | 6 ++---- .../Extras/Subtitles/SubtitleService.cs | 19 +++++++++++++++---- src/NzbDrone.Core/Parser/LanguageParser.cs | 13 +++++++++---- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/NzbDrone.Core.Test/ParserTests/LanguageParserFixture.cs b/src/NzbDrone.Core.Test/ParserTests/LanguageParserFixture.cs index eec79002b..1267fd9ed 100644 --- a/src/NzbDrone.Core.Test/ParserTests/LanguageParserFixture.cs +++ b/src/NzbDrone.Core.Test/ParserTests/LanguageParserFixture.cs @@ -70,6 +70,10 @@ namespace NzbDrone.Core.Test.ParserTests [TestCase("2 Broke Girls - S01E01 - Pilot.sub", Language.Unknown)] [TestCase("2 Broke Girls - S01E01 - Pilot.eng.forced.sub", Language.English)] [TestCase("2 Broke Girls - S01E01 - Pilot-eng-forced.sub", Language.English)] + [TestCase("2_Eng.srt", Language.English)] + [TestCase("3_English.srt", Language.English)] + [TestCase("Title.2000.1080p.BluRay.H264.AAC-RARBG.idx", Language.Unknown)] + [TestCase("Title.2000.1080p.BluRay.H264.AAC-RARBG.sub", Language.Unknown)] public void should_parse_subtitle_language(string fileName, Language language) { var result = LanguageParser.ParseSubtitleLanguage(fileName); diff --git a/src/NzbDrone.Core/Extras/ExtraService.cs b/src/NzbDrone.Core/Extras/ExtraService.cs index aedb233dd..59d007071 100644 --- a/src/NzbDrone.Core/Extras/ExtraService.cs +++ b/src/NzbDrone.Core/Extras/ExtraService.cs @@ -66,15 +66,13 @@ namespace NzbDrone.Core.Extras var sourcePath = localMovie.Path; var sourceFolder = _diskProvider.GetParentFolder(sourcePath); var sourceFileName = Path.GetFileNameWithoutExtension(sourcePath); - var files = _diskProvider.GetFiles(sourceFolder, SearchOption.TopDirectoryOnly); + var files = _diskProvider.GetFiles(sourceFolder, SearchOption.AllDirectories).OrderByDescending(d => d).ToArray(); var wantedExtensions = _configService.ExtraFileExtensions.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) .Select(e => e.Trim(' ', '.')) .ToList(); - var matchingFilenames = files.Where(f => Path.GetFileNameWithoutExtension(f).StartsWith(sourceFileName, StringComparison.InvariantCultureIgnoreCase)); - - foreach (var matchingFilename in matchingFilenames) + foreach (var matchingFilename in files) { var matchingExtension = wantedExtensions.FirstOrDefault(e => matchingFilename.EndsWith(e)); diff --git a/src/NzbDrone.Core/Extras/Subtitles/SubtitleService.cs b/src/NzbDrone.Core/Extras/Subtitles/SubtitleService.cs index d37a1d226..a0915c7e9 100644 --- a/src/NzbDrone.Core/Extras/Subtitles/SubtitleService.cs +++ b/src/NzbDrone.Core/Extras/Subtitles/SubtitleService.cs @@ -88,11 +88,22 @@ namespace NzbDrone.Core.Extras.Subtitles if (SubtitleFileExtensions.Extensions.Contains(Path.GetExtension(path))) { var language = LanguageParser.ParseSubtitleLanguage(path); - var suffix = GetSuffix(language, 1, false); - var subtitleFile = ImportFile(movie, movieFile, path, readOnly, extension, suffix); - subtitleFile.Language = language; + var subtitleFiles = _subtitleFileService.GetFilesByMovie(movie.Id); + var existingSrtSubs = subtitleFiles.Where(m => m.MovieFileId == movieFile.Id) + .Where(m => m.Language == language) + .Where(m => m.Extension == extension); + + var suffix = GetSuffix(language, existingSrtSubs.Count() + 1, extension.EqualsIgnoreCase(".srt")); + var subtitleFile = new SubtitleFile(); + + if ((extension.EqualsIgnoreCase(".srt") && language != Language.Unknown) || + !extension.EqualsIgnoreCase(".srt")) + { + subtitleFile = ImportFile(movie, movieFile, path, readOnly, extension, suffix); + subtitleFile.Language = language; - _subtitleFileService.Upsert(subtitleFile); + _subtitleFileService.Upsert(subtitleFile); + } return subtitleFile; } diff --git a/src/NzbDrone.Core/Parser/LanguageParser.cs b/src/NzbDrone.Core/Parser/LanguageParser.cs index 68af72c2f..c3f1b7224 100644 --- a/src/NzbDrone.Core/Parser/LanguageParser.cs +++ b/src/NzbDrone.Core/Parser/LanguageParser.cs @@ -15,9 +15,8 @@ namespace NzbDrone.Core.Parser private static readonly Regex LanguageRegex = new Regex(@"(?:\W|_|^)(?\b(?:ita|italian)\b)|(?german\b|videomann)|(?flemish)|(?greek)|(?(?:\W|_)(?:FR|VOSTFR|VO|VFF|VFQ|VF2|TRUEFRENCH)(?:\W|_))|(?\brus\b)|(?nl\W?subs?)|(?\b(?:HUNDUB|HUN)\b)|(?\bHebDub\b)|(?\b(?:CZ|SK)\b)|(?\bukr\b)", RegexOptions.IgnoreCase | RegexOptions.Compiled); - private static readonly Regex SubtitleLanguageRegex = new Regex(".+?[-_. ](?[a-z]{2,3})(?:[-_. ]forced)?$", RegexOptions.Compiled | RegexOptions.IgnoreCase); - + private static readonly Regex RarbgSubtitleLanguageRegex = new Regex("^[0-9]{1,2}_(?[A-Za-z]{2,3}).*$", RegexOptions.Compiled | RegexOptions.IgnoreCase); public static List ParseLanguages(string title) { var lowerTitle = title.ToLower(); @@ -153,13 +152,19 @@ namespace NzbDrone.Core.Parser var simpleFilename = Path.GetFileNameWithoutExtension(fileName); var languageMatch = SubtitleLanguageRegex.Match(simpleFilename); + + if (!languageMatch.Success) + { + languageMatch = RarbgSubtitleLanguageRegex.Match(simpleFilename); + } if (languageMatch.Success) { var isoCode = languageMatch.Groups["iso_code"].Value; - var isoLanguage = IsoLanguages.Find(isoCode); + var isoLanguage = IsoLanguages.Find(isoCode.ToLower()); - return isoLanguage?.Language ?? Language.Unknown; + Logger.Debug("Parsed language: {0}", isoLanguage?.Language ?? Language.Unknown); + return isoLanguage?.Language ?? Language.Unknown; } #if !LIBRARY Logger.Debug("Unable to parse langauge from subtitle file: {0}", fileName);