From 4d4626806d4faf9f1ac20879a33dcb276e815f56 Mon Sep 17 00:00:00 2001 From: mrjvs Date: Sat, 18 Feb 2023 14:00:38 +0100 Subject: [PATCH] fuzzy matching for title Co-authored-by: Jip Frijlink --- src/backend/providers/flixhq.ts | 4 ++-- src/backend/providers/superstream/index.ts | 4 ++-- src/utils/titleMatch.ts | 7 +++++++ 3 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 src/utils/titleMatch.ts diff --git a/src/backend/providers/flixhq.ts b/src/backend/providers/flixhq.ts index 493a1c3b..c8a89400 100644 --- a/src/backend/providers/flixhq.ts +++ b/src/backend/providers/flixhq.ts @@ -1,3 +1,4 @@ +import { compareTitle } from "@/utils/titleMatch"; import { proxiedFetch } from "../helpers/fetch"; import { registerProvider } from "../helpers/register"; import { MWStreamQuality, MWStreamType } from "../helpers/streams"; @@ -19,9 +20,8 @@ registerProvider({ baseURL: flixHqBase, } ); - // TODO fuzzy match or normalize title before comparison const foundItem = searchResults.results.find((v: any) => { - return v.title === media.meta.title && v.releaseDate === media.meta.year; + return compareTitle(v.title, media.meta.title) && v.releaseDate === media.meta.year; }); if (!foundItem) throw new Error("No watchable item found"); const flixId = foundItem.id; diff --git a/src/backend/providers/superstream/index.ts b/src/backend/providers/superstream/index.ts index c0861452..0f20fb2d 100644 --- a/src/backend/providers/superstream/index.ts +++ b/src/backend/providers/superstream/index.ts @@ -10,6 +10,7 @@ import { MWStreamQuality, MWStreamType, } from "@/backend/helpers/streams"; +import { compareTitle } from "@/utils/titleMatch"; const nanoid = customAlphabet("0123456789abcdef", 32); @@ -128,10 +129,9 @@ registerProvider({ const searchRes = (await get(searchQuery, true)).data; progress(33); - // TODO: add fuzzy search and normalise strings before matching const superstreamEntry = searchRes.find( (res: any) => - res.title === media.meta.title && res.year === Number(media.meta.year) + compareTitle(res.title, media.meta.title) && res.year === Number(media.meta.year) ); if (!superstreamEntry) throw new Error("No entry found on SuperStream"); diff --git a/src/utils/titleMatch.ts b/src/utils/titleMatch.ts new file mode 100644 index 00000000..cb69c790 --- /dev/null +++ b/src/utils/titleMatch.ts @@ -0,0 +1,7 @@ +function normalizeTitle(title: string): string { + return title.trim().toLowerCase().replace(/[\'\"\:]/g, "").replace(/[^a-zA-Z0-9]+/g, "_"); +} + +export function compareTitle(a: string, b: string): boolean { + return normalizeTitle(a) === normalizeTitle(b); +}