From 5eb83e38699a2a158291bc3e4993a301fde1ebf7 Mon Sep 17 00:00:00 2001 From: zakary Date: Sun, 7 Jan 2024 23:23:42 -0600 Subject: [PATCH] feat(decide): release group match prior to snatch (#577) adds to decide.ts the condition that release groups match before snatching the torrent for comparison # notes - risky matchMode will not require both groups to be present to snatch - safe matchMode will require both groups match - groups are case-insensitive --------- Co-authored-by: Michael Goodnow --- src/constants.ts | 2 ++ src/decide.ts | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index 100a97951..52d294a3e 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -12,6 +12,7 @@ export const SEASON_REGEX = /^(?.+?)[_.\s-]+(?<season>S\d+)(?:[_.\s-]*?(?<seasonmax>S?\d+))?(?=[_.\s](?!E\d+))/i; export const MOVIE_REGEX = /^(?<title>.+?)[_.\s][[(]?(?<year>\d{4})[)\]]?(?![pi])/i; +export const RELEASE_GROUP_REGEX = /(?<=-)[\w ]+(?=(?:\.\w{1,5})?$)/i; export const VIDEO_EXTENSIONS = [".mkv", ".mp4", ".avi"]; @@ -46,6 +47,7 @@ export enum Decision { RATE_LIMITED = "RATE_LIMITED", INFO_HASH_ALREADY_EXISTS = "INFO_HASH_ALREADY_EXISTS", FILE_TREE_MISMATCH = "FILE_TREE_MISMATCH", + RELEASE_GROUP_MISMATCH = "RELEASE_GROUP_MISMATCH", } export enum MatchMode { diff --git a/src/decide.ts b/src/decide.ts index ac6779b02..a163cac1b 100644 --- a/src/decide.ts +++ b/src/decide.ts @@ -1,7 +1,12 @@ import { existsSync, statSync, writeFileSync } from "fs"; import path from "path"; import { appDir } from "./configuration.js"; -import { Decision, MatchMode, TORRENT_CACHE_FOLDER } from "./constants.js"; +import { + Decision, + MatchMode, + RELEASE_GROUP_REGEX, + TORRENT_CACHE_FOLDER, +} from "./constants.js"; import { db } from "./db.js"; import { Label, logger } from "./logger.js"; import { Metafile } from "./parseTorrent.js"; @@ -30,6 +35,8 @@ const createReasonLogger = } let reason; switch (decision) { + case Decision.MATCH_SIZE_ONLY: + return; case Decision.MATCH: return; case Decision.SIZE_MISMATCH: @@ -50,6 +57,9 @@ const createReasonLogger = case Decision.FILE_TREE_MISMATCH: reason = "it has a different file tree"; break; + case Decision.RELEASE_GROUP_MISMATCH: + reason = "it has a different release group"; + break; default: reason = decision; break; @@ -97,8 +107,24 @@ function sizeDoesMatch(resultSize, searchee) { return resultSize >= lowerBound && resultSize <= upperBound; } +function releaseGroupDoesMatch( + searcheeName: string, + candidateName: string, + matchMode: MatchMode +) { + const searcheeMatch = searcheeName.match(RELEASE_GROUP_REGEX); + const candidateMatch = candidateName.match(RELEASE_GROUP_REGEX); + + // if we are unsure, pass in risky mode but fail in safe mode + if (!searcheeMatch || !candidateMatch) { + return matchMode === MatchMode.RISKY; + } + + return searcheeMatch[0].toLowerCase() === candidateMatch[0].toLowerCase(); +} + async function assessCandidateHelper( - { link, size }: Candidate, + { link, size, name }: Candidate, searchee: Searchee, hashesToExclude: string[] ): Promise<ResultAssessment> { @@ -110,6 +136,9 @@ async function assessCandidateHelper( if (!link) return { decision: Decision.NO_DOWNLOAD_LINK }; + if (!releaseGroupDoesMatch(searchee.name, name, matchMode)) + return { decision: Decision.RELEASE_GROUP_MISMATCH }; + const result = await parseTorrentFromURL(link); if (result.isErr()) {