New: Preserve language tags when importing subtitle files

pull/7572/head
Dominik Krivohlavek 2 years ago committed by Qstick
parent b3c3f7ddae
commit 43d77308f9

@ -74,7 +74,7 @@ namespace NzbDrone.Core.Test.Extras.Subtitles
[TestCase("Movie Title - 2022.srt", "Movie Title - 2022.srt")]
[TestCase("Movie.Title.2022.en.srt", "Movie Title - 2022.en.srt")]
[TestCase("Movie.Title.2022.english.srt", "Movie Title - 2022.en.srt")]
[TestCase("Movie Title 2022_en_sdh_forced.srt", "Movie Title - 2022.en.srt")]
[TestCase("Movie Title 2022_en_sdh_forced.srt", "Movie Title - 2022.en.sdh.forced.srt")]
[TestCase("Movie_Title_2022 en.srt", "Movie Title - 2022.en.srt")]
[TestCase(@"Subs\Movie.Title.2022\2_en.srt", "Movie Title - 2022.en.srt")]
[TestCase("sub.srt", "Movie Title - 2022.srt")]
@ -95,7 +95,7 @@ namespace NzbDrone.Core.Test.Extras.Subtitles
var files = new List<string>
{
Path.Combine(_releaseFolder, "Movie.Title.2022.en.srt").AsOsAgnostic(),
Path.Combine(_releaseFolder, "Movie.Title.2022.english.srt").AsOsAgnostic(),
Path.Combine(_releaseFolder, "Movie.Title.2022.eng.srt").AsOsAgnostic(),
Path.Combine(_releaseFolder, "Subs", "Movie_Title_2022_en_forced.srt").AsOsAgnostic(),
Path.Combine(_releaseFolder, "Subs", "Movie.Title.2022", "2_fr.srt").AsOsAgnostic()
};
@ -104,7 +104,7 @@ namespace NzbDrone.Core.Test.Extras.Subtitles
{
"Movie Title - 2022.1.en.srt",
"Movie Title - 2022.2.en.srt",
"Movie Title - 2022.3.en.srt",
"Movie Title - 2022.en.forced.srt",
"Movie Title - 2022.fr.srt",
};

@ -0,0 +1,14 @@
using FluentMigrator;
using NzbDrone.Core.Datastore.Migration.Framework;
namespace NzbDrone.Core.Datastore.Migration
{
[Migration(214)]
public class add_language_tags_to_subtitle_files : NzbDroneMigrationBase
{
protected override void MainDbUpgrade()
{
Alter.Table("SubtitleFiles").AddColumn("LanguageTags").AsString().Nullable();
}
}
}

@ -1,3 +1,4 @@
using System.Collections.Generic;
using NzbDrone.Core.Extras.Files;
using NzbDrone.Core.Languages;
@ -5,6 +6,17 @@ namespace NzbDrone.Core.Extras.Subtitles
{
public class SubtitleFile : ExtraFile
{
public SubtitleFile()
{
LanguageTags = new List<string>();
}
public Language Language { get; set; }
public string AggregateString => Language + LanguageTagsAsString + Extension;
public List<string> LanguageTags { get; set; }
private string LanguageTagsAsString => string.Join(".", LanguageTags);
}
}

@ -72,7 +72,7 @@ namespace NzbDrone.Core.Extras.Subtitles
foreach (var movieFile in movieFiles)
{
var groupedExtraFilesForMovieFile = subtitleFiles.Where(m => m.MovieFileId == movieFile.Id)
.GroupBy(s => s.Language + s.Extension).ToList();
.GroupBy(s => s.AggregateString).ToList();
foreach (var group in groupedExtraFilesForMovieFile)
{
@ -81,7 +81,7 @@ namespace NzbDrone.Core.Extras.Subtitles
foreach (var subtitleFile in group)
{
var suffix = GetSuffix(subtitleFile.Language, copy, groupCount > 1);
var suffix = GetSuffix(subtitleFile.Language, copy, subtitleFile.LanguageTags, groupCount > 1);
movedFiles.AddIfNotNull(MoveFile(movie, movieFile, subtitleFile, suffix));
copy++;
@ -116,7 +116,7 @@ namespace NzbDrone.Core.Extras.Subtitles
try
{
// Filename match
if (Path.GetFileNameWithoutExtension(file).StartsWith(sourceFileName, StringComparison.InvariantCultureIgnoreCase))
if (Path.GetFileNameWithoutExtension(file).StartsWithIgnoreCase(sourceFileName))
{
matchingFiles.Add(file);
continue;
@ -175,16 +175,24 @@ namespace NzbDrone.Core.Extras.Subtitles
}
}
var subtitleFiles = new List<Tuple<string, Language, string>>();
var subtitleFiles = new List<SubtitleFile>();
foreach (string file in matchingFiles)
{
var language = LanguageParser.ParseSubtitleLanguage(file);
var extension = Path.GetExtension(file);
subtitleFiles.Add(new Tuple<string, Language, string>(file, language, extension));
var languageTags = LanguageParser.ParseLanguageTags(file);
var subFile = new SubtitleFile
{
Language = language,
Extension = extension
};
subFile.LanguageTags = languageTags.ToList();
subFile.RelativePath = PathExtensions.GetRelativePath(sourceFolder, file);
subtitleFiles.Add(subFile);
}
var groupedSubtitleFiles = subtitleFiles.GroupBy(s => s.Item2 + s.Item3).ToList();
var groupedSubtitleFiles = subtitleFiles.GroupBy(s => s.AggregateString).ToList();
foreach (var group in groupedSubtitleFiles)
{
@ -193,14 +201,15 @@ namespace NzbDrone.Core.Extras.Subtitles
foreach (var file in group)
{
var path = Path.Combine(sourceFolder, file.RelativePath);
var language = file.Language;
var extension = file.Extension;
var suffix = GetSuffix(language, copy, file.LanguageTags, groupCount > 1);
try
{
var path = file.Item1;
var language = file.Item2;
var extension = file.Item3;
var suffix = GetSuffix(language, copy, groupCount > 1);
var subtitleFile = ImportFile(localMovie.Movie, movieFile, path, isReadOnly, extension, suffix);
subtitleFile.Language = language;
subtitleFile.LanguageTags = file.LanguageTags;
_mediaFileAttributeService.SetFilePermissions(path);
_subtitleFileService.Upsert(subtitleFile);
@ -211,7 +220,7 @@ namespace NzbDrone.Core.Extras.Subtitles
}
catch (Exception ex)
{
_logger.Warn(ex, "Failed to import subtitle file: {0}", file.Item1);
_logger.Warn(ex, "Failed to import subtitle file: {0}", path);
}
}
}
@ -219,7 +228,7 @@ namespace NzbDrone.Core.Extras.Subtitles
return importedFiles;
}
private string GetSuffix(Language language, int copy, bool multipleCopies = false)
private string GetSuffix(Language language, int copy, List<string> languageTags, bool multipleCopies = false)
{
var suffixBuilder = new StringBuilder();
@ -235,6 +244,12 @@ namespace NzbDrone.Core.Extras.Subtitles
suffixBuilder.Append(IsoLanguages.Get(language).TwoLetterCode);
}
if (languageTags.Any())
{
suffixBuilder.Append(".");
suffixBuilder.Append(string.Join(".", languageTags));
}
return suffixBuilder.ToString();
}
}

@ -4,6 +4,7 @@ using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using NLog;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Instrumentation;
using NzbDrone.Core.Languages;
@ -304,6 +305,25 @@ namespace NzbDrone.Core.Parser
return languages.DistinctBy(l => (int)l).ToList();
}
public static IEnumerable<string> ParseLanguageTags(string fileName)
{
try
{
var simpleFilename = Path.GetFileNameWithoutExtension(fileName);
var match = SubtitleLanguageRegex.Match(simpleFilename);
var languageTags = match.Groups["tags"].Captures.Cast<Capture>()
.Where(tag => !tag.Value.Empty())
.Select(tag => tag.Value.ToLower());
return languageTags;
}
catch (Exception ex)
{
Logger.Debug(ex, "Failed parsing language tags from subtitle file: {0}", fileName);
}
return Enumerable.Empty<string>();
}
public static Language ParseSubtitleLanguage(string fileName)
{
try

Loading…
Cancel
Save