From 02150482df7b47064e9f2fa03be010ba0271634f Mon Sep 17 00:00:00 2001 From: iPromKnight Date: Sun, 10 Mar 2024 15:14:17 +0000 Subject: [PATCH] reduce cpu cycles in parsing in producer --- .../Crawlers/Dmm/DebridMediaManagerCrawler.cs | 32 +++++++++++-------- .../ParseTorrentTitle/IParsingService.cs | 1 + .../ParseTorrentTitle/ITorrentTitleParser.cs | 1 + .../ParseTorrentTitle/ParsingService.cs | 2 ++ .../ParseTorrentTitle/TorrentTitleParser.cs | 2 +- 5 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/producer/Features/Crawlers/Dmm/DebridMediaManagerCrawler.cs b/src/producer/Features/Crawlers/Dmm/DebridMediaManagerCrawler.cs index 1b35c55..ef6eb93 100644 --- a/src/producer/Features/Crawlers/Dmm/DebridMediaManagerCrawler.cs +++ b/src/producer/Features/Crawlers/Dmm/DebridMediaManagerCrawler.cs @@ -101,13 +101,15 @@ public partial class DebridMediaManagerCrawler( return null; } - var parsedTorrent = parsingService.Parse(filenameElement.GetString()); + var torrentTitle = filenameElement.GetString(); - if (parsedTorrent.IsInvalid) + if (torrentTitle.IsNullOrEmpty()) { return null; } + var torrentType = parsingService.GetTypeByName(torrentTitle); + var torrent = new Torrent { Source = Source, @@ -117,48 +119,52 @@ public partial class DebridMediaManagerCrawler( Leechers = 0, }; - return parsedTorrent.Type switch + return torrentType switch { - TorrentType.Movie => HandleMovieType(torrent, parsedTorrent), - TorrentType.Tv => HandleTvType(torrent, parsedTorrent), + TorrentType.Movie => HandleMovieType(torrent, torrentTitle), + TorrentType.Tv => HandleTvType(torrent, torrentTitle), _ => null, }; } - private Torrent HandleMovieType(Torrent torrent, ParsedFilename parsedTorrent) + private Torrent HandleMovieType(Torrent torrent, string title) { - if (parsedTorrent.Movie.ReleaseTitle.IsNullOrEmpty()) + if (title.IsNullOrEmpty()) { return null; } - if (!parsingService.HasNoBannedTerms(parsedTorrent.Movie.ReleaseTitle)) + if (!parsingService.HasNoBannedTerms(title)) { + LogBannedTermsFound(torrent); return null; } torrent.Category = "movies"; - torrent.Name = parsedTorrent.Movie.ReleaseTitle; + torrent.Name = title; return torrent; } - private Torrent HandleTvType(Torrent torrent, ParsedFilename parsedTorrent) + private Torrent HandleTvType(Torrent torrent, string title) { - if (parsedTorrent.Show.ReleaseTitle.IsNullOrEmpty()) + if (title.IsNullOrEmpty()) { return null; } - if (!parsingService.HasNoBannedTerms(parsedTorrent.Show.ReleaseTitle)) + if (!parsingService.HasNoBannedTerms(title)) { + LogBannedTermsFound(torrent); return null; } torrent.Category = "tv"; - torrent.Name = parsedTorrent.Show.ReleaseTitle; + torrent.Name = title; return torrent; } + private void LogBannedTermsFound(Torrent torrent) => logger.LogWarning("Banned terms found in torrent title for ingested infoHash: {InfoHash}. Skipping", torrent.InfoHash); + private async Task InsertTorrentsForPage(JsonDocument json) { var torrents = json.RootElement.EnumerateArray() diff --git a/src/producer/Features/ParseTorrentTitle/IParsingService.cs b/src/producer/Features/ParseTorrentTitle/IParsingService.cs index 115e6c2..9aed341 100644 --- a/src/producer/Features/ParseTorrentTitle/IParsingService.cs +++ b/src/producer/Features/ParseTorrentTitle/IParsingService.cs @@ -2,6 +2,7 @@ namespace Producer.Features.ParseTorrentTitle; public interface IParsingService { + TorrentType GetTypeByName(string name); ParsedFilename Parse(string name); string Naked(string title); List GrabYears(string str); diff --git a/src/producer/Features/ParseTorrentTitle/ITorrentTitleParser.cs b/src/producer/Features/ParseTorrentTitle/ITorrentTitleParser.cs index 0b0dcce..12b02ba 100644 --- a/src/producer/Features/ParseTorrentTitle/ITorrentTitleParser.cs +++ b/src/producer/Features/ParseTorrentTitle/ITorrentTitleParser.cs @@ -3,4 +3,5 @@ namespace Producer.Features.ParseTorrentTitle; public interface ITorrentTitleParser { ParsedFilename Parse(string name); + TorrentType GetTypeByName(string name); } diff --git a/src/producer/Features/ParseTorrentTitle/ParsingService.cs b/src/producer/Features/ParseTorrentTitle/ParsingService.cs index 26dbaa9..c375765 100644 --- a/src/producer/Features/ParseTorrentTitle/ParsingService.cs +++ b/src/producer/Features/ParseTorrentTitle/ParsingService.cs @@ -347,4 +347,6 @@ public partial class ParsingService(IWordCollections wordCollections, ITorrentTi } public ParsedFilename Parse(string name) => torrentTitleParser.Parse(name); + + public TorrentType GetTypeByName(string name) => torrentTitleParser.GetTypeByName(name); } diff --git a/src/producer/Features/ParseTorrentTitle/TorrentTitleParser.cs b/src/producer/Features/ParseTorrentTitle/TorrentTitleParser.cs index e4e51ce..53ed4b7 100644 --- a/src/producer/Features/ParseTorrentTitle/TorrentTitleParser.cs +++ b/src/producer/Features/ParseTorrentTitle/TorrentTitleParser.cs @@ -117,7 +117,7 @@ public partial class TorrentTitleParser : ITorrentTitleParser }; } - private static TorrentType GetTypeByName(string name) + public TorrentType GetTypeByName(string name) { var tvRegexes = new[] {