From bf98d930dc9a289ff4d10f6d1e299d4b0d0a0a89 Mon Sep 17 00:00:00 2001 From: akesher <=> Date: Sun, 17 Sep 2023 23:45:16 -0700 Subject: [PATCH] back to fuse from ufuzzy --- package-lock.json | 11 ----------- package.json | 2 +- src/torrent.ts | 29 ++++++++++++----------------- 3 files changed, 13 insertions(+), 29 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7dec8c3da..de5a7c63c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,6 @@ "version": "5.4.3", "license": "Apache-2.0", "dependencies": { - "@leeoniya/ufuzzy": "^1.0.9", "bencode": "^2.0.1", "better-sqlite3": "^8.0.1", "chalk": "^5.0.0", @@ -114,11 +113,6 @@ "integrity": "sha512-ZnQMnLV4e7hDlUvw8H+U8ASL02SS2Gn6+9Ac3wGGLIe7+je2AeAOxPY+izIPJDfFDb7eDjev0Us8MO1iFRN8hA==", "dev": true }, - "node_modules/@leeoniya/ufuzzy": { - "version": "1.0.9", - "resolved": "https://registry.npmjs.org/@leeoniya/ufuzzy/-/ufuzzy-1.0.9.tgz", - "integrity": "sha512-enIZWDtquUssceZDnq8wTLiljImZt1kQBWDz2jJIMXDb+fgz5+0N8ez8EJgA1ooSpRRXrLg4C65BzmB+Fif0mQ==" - }, "node_modules/@nodelib/fs.scandir": { "version": "2.1.5", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", @@ -3245,11 +3239,6 @@ "integrity": "sha512-ZnQMnLV4e7hDlUvw8H+U8ASL02SS2Gn6+9Ac3wGGLIe7+je2AeAOxPY+izIPJDfFDb7eDjev0Us8MO1iFRN8hA==", "dev": true }, - "@leeoniya/ufuzzy": { - "version": "1.0.9", - "resolved": "https://registry.npmjs.org/@leeoniya/ufuzzy/-/ufuzzy-1.0.9.tgz", - "integrity": "sha512-enIZWDtquUssceZDnq8wTLiljImZt1kQBWDz2jJIMXDb+fgz5+0N8ez8EJgA1ooSpRRXrLg4C65BzmB+Fif0mQ==" - }, "@nodelib/fs.scandir": { "version": "2.1.5", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", diff --git a/package.json b/package.json index 2f5734c46..0b1979483 100644 --- a/package.json +++ b/package.json @@ -25,13 +25,13 @@ "author": "Michael Goodnow", "license": "Apache-2.0", "dependencies": { - "@leeoniya/ufuzzy": "^1.0.9", "bencode": "^2.0.1", "better-sqlite3": "^8.0.1", "chalk": "^5.0.0", "commander": "^8.3.0", "fetch-blob": "^3.1.4", "formdata-polyfill": "^4.0.10", + "fuse.js": "^6.6.2", "knex": "^2.4.2", "lodash-es": "^4.17.21", "ms": "^2.1.3", diff --git a/src/torrent.ts b/src/torrent.ts index d53b04ce9..66e629502 100644 --- a/src/torrent.ts +++ b/src/torrent.ts @@ -1,4 +1,4 @@ -import uFuzzy from "@leeoniya/ufuzzy"; +import Fuse from "fuse.js"; import fs, { promises as fsPromises } from "fs"; import fetch, { Response } from "node-fetch"; import path, { join } from "path"; @@ -32,8 +32,6 @@ export enum SnatchError { UNKNOWN_ERROR = "UNKNOWN_ERROR", } -const uf = new uFuzzy(); - export async function parseTorrentFromFilename( filename: string ): Promise { @@ -208,41 +206,38 @@ export async function getTorrentByFuzzyName( name: string ): Promise { const allNames = await db("torrent").select("name", "file_path"); - const fullMatch = reformatTitleForSearching(name).replace( /[^a-z0-9]/gi, "" - ); + ).toLowerCase(); // Attempt to filter torrents in DB to match incoming torrent before fuzzy check let filteredNames = []; - if (fullMatch) { filteredNames = allNames.filter((dbName) => { const dbMatch = reformatTitleForSearching(dbName.name).replace( /[^a-z0-9]/gi, "" - ); + ).toLowerCase(); if (!dbMatch) return false; return fullMatch === dbMatch; }); - } else { - filteredNames = allNames; } // If none match, proceed with fuzzy name check on all names. + filteredNames = filteredNames.length > 0 ? filteredNames : allNames; - const haystack = (filteredNames).map( - (filteredName) => filteredName.name - ); - - let [idxs, info, order] = uf.search(haystack, name, false, 1e3); + const potentialMatches = new Fuse(filteredNames, { + keys: ["name"], + distance: 6, + threshold: 0.6, + }).search(name); // Valid matches exist - if (order.length === 0) return null; + if (potentialMatches.length === 0) return null; - const [firstMatch] = filteredNames[info.idx[order[0]]]; - return parseTorrentFromFilename(firstMatch.file_path); + const firstMatch = potentialMatches[0]; + return parseTorrentFromFilename(firstMatch.item.file_path); } export async function getTorrentByCriteria(