Added fuzzy searching

Co-authored-by: James Hawkins <jhawki2005@gmail.com>
This commit is contained in:
Jelle van Snik 2022-02-20 15:49:21 +01:00
parent 9bec456a58
commit c763cfe0f0
2 changed files with 27 additions and 9 deletions

View File

@ -82,7 +82,7 @@ function MediaCardContent({
export function MediaCard(props: MediaCardProps) { export function MediaCard(props: MediaCardProps) {
let link = "movie"; let link = "movie";
if (props.media.mediaType === MWMediaType.MOVIE) link = "series"; if (props.media.mediaType === MWMediaType.SERIES) link = "series";
const content = <MediaCardContent {...props} />; const content = <MediaCardContent {...props} />;

View File

@ -1,6 +1,13 @@
import Fuse from "fuse.js";
import { tempScraper } from "./list/temp"; import { tempScraper } from "./list/temp";
import { theFlixScraper } from "./list/theflix"; import { theFlixScraper } from "./list/theflix";
import { MWMassProviderOutput, MWMedia, MWMediaType, MWPortableMedia, MWQuery } from "./types"; import {
MWMassProviderOutput,
MWMedia,
MWMediaType,
MWPortableMedia,
MWQuery,
} from "./types";
import { MWWrappedMediaProvider, WrapProvider } from "./wrapper"; import { MWWrappedMediaProvider, WrapProvider } from "./wrapper";
export * from "./types"; export * from "./types";
@ -21,35 +28,46 @@ export function GetProvidersForType(type: MWMediaType) {
/* /*
** Call search on all providers that matches query type ** Call search on all providers that matches query type
*/ */
export async function SearchProviders(query: MWQuery): Promise<MWMassProviderOutput> { export async function SearchProviders(
const allQueries = GetProvidersForType(query.type).map<Promise<{ media: MWMedia[], success: boolean, id: string, }>>(async (provider) => { query: MWQuery
): Promise<MWMassProviderOutput> {
const allQueries = GetProvidersForType(query.type).map<
Promise<{ media: MWMedia[]; success: boolean; id: string }>
>(async (provider) => {
try { try {
return { return {
media: await provider.searchForMedia(query), media: await provider.searchForMedia(query),
success: true, success: true,
id: provider.id, id: provider.id,
} };
} catch (err) { } catch (err) {
console.error(`Failed running provider ${provider.id}`, err, query); console.error(`Failed running provider ${provider.id}`, err, query);
return { return {
media: [], media: [],
success: false, success: false,
id: provider.id, id: provider.id,
} };
} }
}); });
const allResults = await Promise.all(allQueries); const allResults = await Promise.all(allQueries);
const providerResults = allResults.map(provider => ({ success: provider.success, id: provider.id })); const providerResults = allResults.map((provider) => ({
success: provider.success,
id: provider.id,
}));
const output = { const output = {
results: allResults.flatMap((results) => results.media), results: allResults.flatMap((results) => results.media),
providers: providerResults, providers: providerResults,
stats: { stats: {
total: providerResults.length, total: providerResults.length,
failed: providerResults.filter(v=>!v.success).length, failed: providerResults.filter((v) => !v.success).length,
succeeded: providerResults.filter(v=>v.success).length, succeeded: providerResults.filter((v) => v.success).length,
}, },
}; };
// sort results
const fuse = new Fuse(output.results, { threshold: 0.3, keys: ["title"] });
output.results = fuse.search(query.searchQuery).map((v) => v.item);
if (output.stats.total === output.stats.failed) if (output.stats.total === output.stats.failed)
throw new Error("All Scrapers failed"); throw new Error("All Scrapers failed");
return output; return output;