mirror of
https://github.com/Radarr/Radarr.git
synced 2026-04-24 22:35:49 -04:00
New: Parse subtitle titles
(cherry picked from commit 69f99373e56a2fca49a2be645e6640624cf12339) Includes: * Fixed: Subtitle title migration when original title is null (cherry picked from commit 8921c5d7a079c58b0c74713355bf82846abc43ab) * Fixed: Migrating subtitle files with unexpectedly large number at end (cherry picked from commit f95dd00b51e61a96a0e6c094ec922c8f5cbc5334) * Fixed: Parsing of subtitle languages separated by dash (cherry picked from commit cb72e752f9e24e5691292d86ac1f46c8fa35a844) * Fix incorrect subtitle copy regex (cherry picked from commit 2ef46e5b902b42cdbafbd80b3154a5d22b090f29)
This commit is contained in:
@@ -7,6 +7,7 @@ using NLog;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Common.Instrumentation;
|
||||
using NzbDrone.Core.Languages;
|
||||
using NzbDrone.Core.Parser.Model;
|
||||
|
||||
namespace NzbDrone.Core.Parser
|
||||
{
|
||||
@@ -45,7 +46,11 @@ namespace NzbDrone.Core.Parser
|
||||
private static readonly Regex GermanDualLanguageRegex = new (@"(?<!WEB[-_. ]?)\bDL\b", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||
private static readonly Regex GermanMultiLanguageRegex = new (@"\bML\b", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||
|
||||
private static readonly Regex SubtitleLanguageRegex = new Regex(".+?[-_. ](?<iso_code>[a-z]{2,3})([-_. ](?<tags>full|forced|foreign|default|cc|psdh|sdh))*$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||
private static readonly Regex SubtitleLanguageRegex = new Regex(".+?([-_. ](?<tags>forced|foreign|default|cc|psdh|sdh))*[-_. ](?<iso_code>[a-z]{2,3})([-_. ](?<tags>forced|foreign|default|cc|psdh|sdh))*$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||
|
||||
private static readonly Regex SubtitleLanguageTitleRegex = new Regex(@".+?(\.((?<tags1>forced|foreign|default|cc|psdh|sdh)|(?<iso_code>[a-z]{2,3})))*[-_. ](?<title>[^.]*)(\.((?<tags2>forced|foreign|default|cc|psdh|sdh)|(?<iso_code>[a-z]{2,3})))*$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||
|
||||
private static readonly Regex SubtitleTitleRegex = new Regex(@"^((?<title>.+) - )?(?<copy>(?<!\d+)\d{1,3}(?!\d+))$", RegexOptions.Compiled);
|
||||
|
||||
public static List<Language> ParseLanguages(string title)
|
||||
{
|
||||
@@ -430,14 +435,80 @@ namespace NzbDrone.Core.Parser
|
||||
}
|
||||
}
|
||||
|
||||
Logger.Debug("Unable to parse langauge from subtitle file: {0}", fileName);
|
||||
Logger.Debug("Unable to parse language from subtitle file: {0}", fileName);
|
||||
}
|
||||
catch (Exception)
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Debug("Failed parsing langauge from subtitle file: {0}", fileName);
|
||||
Logger.Debug(ex, "Failed parsing language from subtitle file: {0}", fileName);
|
||||
}
|
||||
|
||||
return Language.Unknown;
|
||||
}
|
||||
|
||||
public static SubtitleTitleInfo ParseBasicSubtitle(string fileName)
|
||||
{
|
||||
return new SubtitleTitleInfo
|
||||
{
|
||||
TitleFirst = false,
|
||||
LanguageTags = ParseLanguageTags(fileName),
|
||||
Language = ParseSubtitleLanguage(fileName)
|
||||
};
|
||||
}
|
||||
|
||||
public static SubtitleTitleInfo ParseSubtitleLanguageInformation(string fileName)
|
||||
{
|
||||
var simpleFilename = Path.GetFileNameWithoutExtension(fileName);
|
||||
var matchTitle = SubtitleLanguageTitleRegex.Match(simpleFilename);
|
||||
|
||||
if (!matchTitle.Groups["title"].Success || (matchTitle.Groups["iso_code"].Captures.Count is var languageCodeNumber && languageCodeNumber != 1))
|
||||
{
|
||||
Logger.Debug("Could not parse a title from subtitle file: {0}. Falling back to parsing without title.", fileName);
|
||||
|
||||
return ParseBasicSubtitle(fileName);
|
||||
}
|
||||
|
||||
var isoCode = matchTitle.Groups["iso_code"].Value;
|
||||
var isoLanguage = IsoLanguages.Find(isoCode.ToLower());
|
||||
|
||||
var language = isoLanguage?.Language ?? Language.Unknown;
|
||||
|
||||
var languageTags = matchTitle.Groups["tags1"].Captures
|
||||
.Union(matchTitle.Groups["tags2"].Captures)
|
||||
.Cast<Capture>()
|
||||
.Where(tag => !tag.Value.Empty())
|
||||
.Select(tag => tag.Value.ToLower());
|
||||
var rawTitle = matchTitle.Groups["title"].Value;
|
||||
|
||||
var subtitleTitleInfo = new SubtitleTitleInfo
|
||||
{
|
||||
TitleFirst = matchTitle.Groups["tags1"].Captures.Empty(),
|
||||
LanguageTags = languageTags.ToList(),
|
||||
RawTitle = rawTitle,
|
||||
Language = language
|
||||
};
|
||||
|
||||
UpdateTitleAndCopyFromTitle(subtitleTitleInfo);
|
||||
|
||||
return subtitleTitleInfo;
|
||||
}
|
||||
|
||||
public static void UpdateTitleAndCopyFromTitle(SubtitleTitleInfo subtitleTitleInfo)
|
||||
{
|
||||
if (subtitleTitleInfo.RawTitle is null)
|
||||
{
|
||||
subtitleTitleInfo.Title = null;
|
||||
subtitleTitleInfo.Copy = 0;
|
||||
}
|
||||
else if (SubtitleTitleRegex.Match(subtitleTitleInfo.RawTitle) is var match && match.Success)
|
||||
{
|
||||
subtitleTitleInfo.Title = match.Groups["title"].Success ? match.Groups["title"].ToString() : null;
|
||||
subtitleTitleInfo.Copy = int.Parse(match.Groups["copy"].ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
subtitleTitleInfo.Title = subtitleTitleInfo.RawTitle;
|
||||
subtitleTitleInfo.Copy = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user