From 515d57aa9130c7fba29bcdf446bfa1615212de87 Mon Sep 17 00:00:00 2001 From: lem6ns Date: Mon, 31 Jan 2022 09:28:14 -0700 Subject: [PATCH] better searching (reuse code from lookmovie) --- src/lib/scraper/xemovie.js | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/lib/scraper/xemovie.js b/src/lib/scraper/xemovie.js index f0b271d3..6f7066db 100644 --- a/src/lib/scraper/xemovie.js +++ b/src/lib/scraper/xemovie.js @@ -1,3 +1,5 @@ +import Fuse from 'fuse.js' + const BASE_URL = `${process.env.REACT_APP_CORS_PROXY_URL}https://xemovie.co`; async function findContent(searchTerm, type) { @@ -44,7 +46,34 @@ async function findContent(searchTerm, type) { break; } - return { options: results }; + const fuse = new Fuse(results, { threshold: 0.3, keys: ["title"] }); + const matchedResults = fuse + .search(searchTerm) + .map(result => result.item); + + if (matchedResults.length === 0) { + return { options: [] }; + } + + if (matchedResults.length > 1) { + const res = { options: [] }; + + matchedResults.forEach((r) => res.options.push({ + title: r.title, + slug: r.slug, + type: r.type, + year: r.year, + source: 'xemovie' + })); + + return res; + } else { + const { title, slug, type, year } = matchedResults[0]; + + return { + options: [{ title, slug, type, year, source: 'xemovie' }] + } + } } catch { return { options: [] }; }