Move to using aggregator

This commit is contained in:
Jendrik Weise 2023-10-14 13:03:25 +02:00
parent 578d6e8408
commit eca1bb6b63
14 changed files with 111 additions and 58 deletions

View File

@ -454,10 +454,11 @@ namespace NzbDrone.Core.Test.ParserTests
}) })
}; };
var (languageTags, title, language) = LanguageParser.ParseSubtitleLanguageInformation(postTitle, episode); var subtitleTitleInfo = LanguageParser.ParseSubtitleLanguageInformation(postTitle, episode);
languageTags.Should().BeEquivalentTo(expectedTags);
title.Should().BeEquivalentTo(expectedTitle); subtitleTitleInfo.LanguageTags.Should().BeEquivalentTo(expectedTags);
language.Should().BeEquivalentTo((Language)expectedLanguage); subtitleTitleInfo.Title.Should().BeEquivalentTo(expectedTitle);
subtitleTitleInfo.Language.Should().BeEquivalentTo((Language)expectedLanguage);
} }
[TestCase("Name (2020) - S01E20 - [AAC 2.0].default.forced.ass", "Name (2020)/Season 1/Name (2020) - S01E20 - [AAC 2.0].mkv")] [TestCase("Name (2020) - S01E20 - [AAC 2.0].default.forced.ass", "Name (2020)/Season 1/Name (2020) - S01E20 - [AAC 2.0].mkv")]
@ -474,7 +475,10 @@ namespace NzbDrone.Core.Test.ParserTests
RelativePath = episodeFilePath RelativePath = episodeFilePath
}) })
}; };
LanguageParser.ParseSubtitleLanguageInformation(postTitle, episode).Should().BeNull(); var subtitleTitleInfo = LanguageParser.ParseSubtitleLanguageInformation(postTitle, episode);
subtitleTitleInfo.Language.Should().BeNull();
subtitleTitleInfo.LanguageTags.Should().BeEmpty();
subtitleTitleInfo.RawTitle.Should().BeNull();
} }
} }
} }

View File

@ -41,15 +41,15 @@ namespace NzbDrone.Core.Datastore.Migration
EpisodeFile = new LazyLoaded<EpisodeFile>(episodeFile) EpisodeFile = new LazyLoaded<EpisodeFile>(episodeFile)
}; };
var subtitleTitleCopyInfo = LanguageParser.CopyFromTitle(LanguageParser.ParseSubtitleLanguageInformation(relativePath, episode)?.Title); var subtitleTitleInfo = LanguageParser.ParseSubtitleLanguageInformation(relativePath, episode);
if (subtitleTitleCopyInfo.Copy != 0) if (subtitleTitleInfo.Copy != 0)
{ {
updatedTitles.Add(new updatedTitles.Add(new
{ {
Id = id, Id = id,
Title = subtitleTitleCopyInfo.Title, Title = subtitleTitleInfo.Title,
Copy = subtitleTitleCopyInfo.Copy Copy = subtitleTitleInfo.Copy
}); });
} }
} }

View File

@ -5,7 +5,6 @@ using NLog;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Core.Extras.Files; using NzbDrone.Core.Extras.Files;
using NzbDrone.Core.MediaFiles.EpisodeImport.Aggregation; using NzbDrone.Core.MediaFiles.EpisodeImport.Aggregation;
using NzbDrone.Core.Parser;
using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Tv; using NzbDrone.Core.Tv;
@ -73,25 +72,17 @@ namespace NzbDrone.Core.Extras.Subtitles
var firstEpisode = localEpisode.Episodes.First(); var firstEpisode = localEpisode.Episodes.First();
var subtitleTitleInfo = LanguageParser.ParseSubtitleLanguageInformation(possibleSubtitleFile, firstEpisode);
var languageTags = subtitleTitleInfo?.LanguageTags ?? LanguageParser.ParseLanguageTags(possibleSubtitleFile);
var title = subtitleTitleInfo?.Title;
var language = subtitleTitleInfo?.Language ?? LanguageParser.ParseSubtitleLanguage(possibleSubtitleFile);
var subtitleTitleCopyInfo = LanguageParser.CopyFromTitle(title);
var subtitleFile = new SubtitleFile var subtitleFile = new SubtitleFile
{ {
SeriesId = series.Id, SeriesId = series.Id,
SeasonNumber = localEpisode.SeasonNumber, SeasonNumber = localEpisode.SeasonNumber,
EpisodeFileId = firstEpisode.EpisodeFileId, EpisodeFileId = firstEpisode.EpisodeFileId,
RelativePath = series.Path.GetRelativePath(possibleSubtitleFile), RelativePath = series.Path.GetRelativePath(possibleSubtitleFile),
Language = language, Language = localEpisode.SubtitleInfo.Language,
LanguageTags = languageTags, LanguageTags = localEpisode.SubtitleInfo.LanguageTags,
Title = subtitleTitleCopyInfo.Title, Title = localEpisode.SubtitleInfo.Title,
Extension = extension, Extension = extension,
Copy = subtitleTitleCopyInfo.Copy Copy = localEpisode.SubtitleInfo.Copy
}; };
subtitleFiles.Add(subtitleFile); subtitleFiles.Add(subtitleFile);

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using NLog; using NLog;
using NzbDrone.Common.Disk; using NzbDrone.Common.Disk;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
@ -30,7 +31,7 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport.Aggregation
IConfigService configService, IConfigService configService,
Logger logger) Logger logger)
{ {
_augmenters = augmenters; _augmenters = augmenters.OrderBy(a => a.Order).ToList();
_diskProvider = diskProvider; _diskProvider = diskProvider;
_videoFileInfoReader = videoFileInfoReader; _videoFileInfoReader = videoFileInfoReader;
_configService = configService; _configService = configService;

View File

@ -10,6 +10,8 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport.Aggregation.Aggregators
{ {
public class AggregateEpisodes : IAggregateLocalEpisode public class AggregateEpisodes : IAggregateLocalEpisode
{ {
public int Order => 1;
private readonly IParsingService _parsingService; private readonly IParsingService _parsingService;
public AggregateEpisodes(IParsingService parsingService) public AggregateEpisodes(IParsingService parsingService)

View File

@ -10,6 +10,8 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport.Aggregation.Aggregators
{ {
public class AggregateLanguage : IAggregateLocalEpisode public class AggregateLanguage : IAggregateLocalEpisode
{ {
public int Order => 2;
private readonly List<IAugmentLanguage> _augmentLanguages; private readonly List<IAugmentLanguage> _augmentLanguages;
private readonly Logger _logger; private readonly Logger _logger;

View File

@ -10,6 +10,8 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport.Aggregation.Aggregators
{ {
public class AggregateQuality : IAggregateLocalEpisode public class AggregateQuality : IAggregateLocalEpisode
{ {
public int Order => 3;
private readonly List<IAugmentQuality> _augmentQualities; private readonly List<IAugmentQuality> _augmentQualities;
private readonly Logger _logger; private readonly Logger _logger;

View File

@ -6,6 +6,8 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport.Aggregation.Aggregators
{ {
public class AggregateReleaseGroup : IAggregateLocalEpisode public class AggregateReleaseGroup : IAggregateLocalEpisode
{ {
public int Order => 5;
public LocalEpisode Aggregate(LocalEpisode localEpisode, DownloadClientItem downloadClientItem) public LocalEpisode Aggregate(LocalEpisode localEpisode, DownloadClientItem downloadClientItem)
{ {
// Prefer ReleaseGroup from DownloadClient/Folder if they're not a season pack // Prefer ReleaseGroup from DownloadClient/Folder if they're not a season pack

View File

@ -8,6 +8,8 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport.Aggregation.Aggregators
{ {
public class AggregateReleaseInfo : IAggregateLocalEpisode public class AggregateReleaseInfo : IAggregateLocalEpisode
{ {
public int Order => 4;
private readonly IHistoryService _historyService; private readonly IHistoryService _historyService;
public AggregateReleaseInfo(IHistoryService historyService) public AggregateReleaseInfo(IHistoryService historyService)

View File

@ -0,0 +1,42 @@
using System.IO;
using System.Linq;
using NLog;
using NzbDrone.Core.Download;
using NzbDrone.Core.Extras.Subtitles;
using NzbDrone.Core.Parser;
using NzbDrone.Core.Parser.Model;
namespace NzbDrone.Core.MediaFiles.EpisodeImport.Aggregation.Aggregators
{
public class AggregateSubtitleInfo : IAggregateLocalEpisode
{
public int Order => 6;
private readonly Logger _logger;
public AggregateSubtitleInfo(Logger logger)
{
_logger = logger;
}
public LocalEpisode Aggregate(LocalEpisode localEpisode, DownloadClientItem downloadClientItem)
{
var path = localEpisode.Path;
var isSubtitleFile = SubtitleFileExtensions.Extensions.Contains(Path.GetExtension(path));
if (!isSubtitleFile)
{
return localEpisode;
}
var firstEpisode = localEpisode.Episodes.First();
var subtitleTitleInfo = LanguageParser.ParseSubtitleLanguageInformation(path, firstEpisode);
subtitleTitleInfo.LanguageTags ??= LanguageParser.ParseLanguageTags(path);
subtitleTitleInfo.Language ??= LanguageParser.ParseSubtitleLanguage(path);
localEpisode.SubtitleInfo = subtitleTitleInfo;
return localEpisode;
}
}
}

View File

@ -5,6 +5,7 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport.Aggregation.Aggregators
{ {
public interface IAggregateLocalEpisode public interface IAggregateLocalEpisode
{ {
int Order { get; }
LocalEpisode Aggregate(LocalEpisode localEpisode, DownloadClientItem downloadClientItem); LocalEpisode Aggregate(LocalEpisode localEpisode, DownloadClientItem downloadClientItem);
} }
} }

View File

@ -34,8 +34,6 @@ namespace NzbDrone.Core.Parser
private static readonly Regex SubtitleLanguageTitleRegex = new Regex(".+?(\\.((?<tags1>full|forced|foreign|default|cc|psdh|sdh)|(?<iso_code>[a-z]{2,3})))*\\.(?<title>[^.]*)(\\.((?<tags2>full|forced|foreign|default|cc|psdh|sdh)|(?<iso_code>[a-z]{2,3})))*$", RegexOptions.Compiled | RegexOptions.IgnoreCase); private static readonly Regex SubtitleLanguageTitleRegex = new Regex(".+?(\\.((?<tags1>full|forced|foreign|default|cc|psdh|sdh)|(?<iso_code>[a-z]{2,3})))*\\.(?<title>[^.]*)(\\.((?<tags2>full|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+)", RegexOptions.Compiled);
public static List<Language> ParseLanguages(string title) public static List<Language> ParseLanguages(string title)
{ {
foreach (var regex in CleanSeriesTitleRegex) foreach (var regex in CleanSeriesTitleRegex)
@ -262,7 +260,7 @@ namespace NzbDrone.Core.Parser
if (matchTitle.Groups["iso_code"].Captures.Count is var languageCodeNumber && languageCodeNumber != 1) if (matchTitle.Groups["iso_code"].Captures.Count is var languageCodeNumber && languageCodeNumber != 1)
{ {
return null; return new SubtitleTitleInfo();
} }
var isoCode = matchTitle.Groups["iso_code"].Value; var isoCode = matchTitle.Groups["iso_code"].Value;
@ -285,38 +283,18 @@ namespace NzbDrone.Core.Parser
if (episodeFileTitle.Contains(title, StringComparison.OrdinalIgnoreCase) || originalEpisodeFileTitle.Contains(title, StringComparison.OrdinalIgnoreCase)) if (episodeFileTitle.Contains(title, StringComparison.OrdinalIgnoreCase) || originalEpisodeFileTitle.Contains(title, StringComparison.OrdinalIgnoreCase))
{ {
return null; return new SubtitleTitleInfo();
} }
} }
return new SubtitleTitleInfo return new SubtitleTitleInfo
{ {
LanguageTags = languageTags.ToList(), LanguageTags = languageTags.ToList(),
Title = title, RawTitle = title,
Language = language Language = language
}; };
} }
public static SubtitleTitleCopyInfo CopyFromTitle(string title)
{
if (title is null)
{
return new SubtitleTitleCopyInfo(0, title);
}
var match = SubtitleTitleRegex.Match(title);
if (match.Success)
{
var copy = match.Groups["copy"].ToString();
var newTitle = match.Groups["title"].Success ? match.Groups["title"].ToString() : null;
return new SubtitleTitleCopyInfo(int.Parse(copy), newTitle);
}
return new SubtitleTitleCopyInfo(0, title);
}
public static List<string> ParseLanguageTags(string fileName) public static List<string> ParseLanguageTags(string fileName)
{ {
try try

View File

@ -44,6 +44,7 @@ namespace NzbDrone.Core.Parser.Model
public bool FileRenamedAfterScriptImport { get; set; } public bool FileRenamedAfterScriptImport { get; set; }
public bool ShouldImportExtras { get; set; } public bool ShouldImportExtras { get; set; }
public List<string> PossibleExtraFiles { get; set; } public List<string> PossibleExtraFiles { get; set; }
public SubtitleTitleInfo SubtitleInfo { get; set; }
public int SeasonNumber public int SeasonNumber
{ {

View File

@ -1,24 +1,49 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.RegularExpressions;
using NzbDrone.Core.Languages; using NzbDrone.Core.Languages;
namespace NzbDrone.Core.Parser.Model namespace NzbDrone.Core.Parser.Model
{ {
public class SubtitleTitleInfo public class SubtitleTitleInfo
{ {
private static readonly Regex SubtitleTitleRegex = new Regex("((?<title>.+) - )?(?<copy>\\d+)", RegexOptions.Compiled);
public List<string> LanguageTags { get; set; } public List<string> LanguageTags { get; set; }
public string Title { get; set; } public string RawTitle { get; set; }
public string Title {
get {
if (RawTitle is null)
{
return null;
}
var match = SubtitleTitleRegex.Match(RawTitle);
if (match.Success)
{
return match.Groups["title"].Success ? match.Groups["title"].ToString() : null;
}
return RawTitle;
}
}
public Language Language { get; set; } public Language Language { get; set; }
} public int Copy {
get {
if (RawTitle is null)
{
return 0;
}
public class SubtitleTitleCopyInfo var match = SubtitleTitleRegex.Match(RawTitle);
{
public int Copy { get; set; }
public string Title { get; set; }
public SubtitleTitleCopyInfo(int copy, string title) if (match.Success)
{ {
Copy = copy; return int.Parse(match.Groups["copy"].ToString());
Title = title; }
return 0;
}
} }
} }
} }