Skip to content

Commit

Permalink
back to fuse from ufuzzy
Browse files Browse the repository at this point in the history
  • Loading branch information
akesher committed Sep 18, 2023
1 parent 9f6beee commit bf98d93
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 29 deletions.
11 changes: 0 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
29 changes: 12 additions & 17 deletions src/torrent.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -32,8 +32,6 @@ export enum SnatchError {
UNKNOWN_ERROR = "UNKNOWN_ERROR",
}

const uf = new uFuzzy();

export async function parseTorrentFromFilename(
filename: string
): Promise<Metafile> {
Expand Down Expand Up @@ -208,41 +206,38 @@ export async function getTorrentByFuzzyName(
name: string
): Promise<null | Metafile> {
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(
Expand Down

0 comments on commit bf98d93

Please sign in to comment.