From b3c4ad5e15e9b5e8be72359cfdd48d7f8e6c9db5 Mon Sep 17 00:00:00 2001 From: Jelle van Snik Date: Sun, 6 Feb 2022 21:42:39 +0100 Subject: [PATCH] add scraper types --- craco.config.js | 2 -- paths.json | 8 ++++++++ src/App.tsx | 18 +++++++++++++++++- src/scrapers/index.ts | 17 +++++++++++++++-- src/scrapers/list/theflix/index.ts | 16 ++++++++++++---- src/scrapers/{scrapers.ts => types.ts} | 6 ++++-- tsconfig.json | 1 + 7 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 paths.json rename src/scrapers/{scrapers.ts => types.ts} (67%) diff --git a/craco.config.js b/craco.config.js index c9520063..aec99bf0 100644 --- a/craco.config.js +++ b/craco.config.js @@ -4,8 +4,6 @@ module.exports = { webpack: { alias: { "@": path.resolve(__dirname, "src/"), - "@components": path.resolve(__dirname, "src/components"), - "@scrapers": path.resolve(__dirname, "src/scrapers"), }, }, }; diff --git a/paths.json b/paths.json new file mode 100644 index 00000000..6d6d796d --- /dev/null +++ b/paths.json @@ -0,0 +1,8 @@ +{ + "compilerOptions": { + "baseUrl": "./src", + "paths": { + "@/*": [ "*"] + } + } +} \ No newline at end of file diff --git a/src/App.tsx b/src/App.tsx index a0a14ac1..80bff536 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,8 +1,24 @@ +import { GetProviderFromId, SearchProviders, MWMedia, MWMediaType } from '@/scrapers'; +import { useState } from 'react'; import './index.css'; function App() { + const [results, setResults] = useState([]); + + async function runSearch() { + const results = await SearchProviders({ type: MWMediaType.MOVIE, searchQuery: "abc" }); + setResults(results); + } + return ( -

Hello world

+ <> +

Search

+ +

Search results

+ {results.map(v=>( +

{v.title} ({GetProviderFromId(v.providerId)?.displayName})

+ ))} + ); } diff --git a/src/scrapers/index.ts b/src/scrapers/index.ts index d458898b..9cb69408 100644 --- a/src/scrapers/index.ts +++ b/src/scrapers/index.ts @@ -1,6 +1,19 @@ import { theFlixScraper } from "./list/theflix"; -import { MWMediaProvider } from "./scrapers"; +import { MWMediaProvider, MWQuery } from "./types"; +export * from "./types"; -export const mediaProviders: MWMediaProvider[] = [ +const mediaProvidersUnchecked: MWMediaProvider[] = [ theFlixScraper ] +export const mediaProviders: MWMediaProvider[] = mediaProvidersUnchecked.filter(v=>v.enabled); + +export async function SearchProviders(query: MWQuery) { + const allQueries = mediaProviders.map(provider => provider.searchForMedia(query)); + const allResults = await Promise.all(allQueries); + + return allResults.flatMap(results => results); +} + +export function GetProviderFromId(id: string) { + return mediaProviders.find(v=>v.id===id); +} diff --git a/src/scrapers/list/theflix/index.ts b/src/scrapers/list/theflix/index.ts index c903bfd8..834e4daa 100644 --- a/src/scrapers/list/theflix/index.ts +++ b/src/scrapers/list/theflix/index.ts @@ -1,15 +1,23 @@ -import { MWMedia, MWMediaProvider, MWMediaType, MWPortableMedia, MWQuery } from "../../scrapers"; +import { MWMedia, MWMediaProvider, MWMediaType, MWPortableMedia, MWQuery } from "@/scrapers/types"; export const theFlixScraper: MWMediaProvider = { id: "theflix", + enabled: true, type: MWMediaType.MOVIE, - getMediaFromPortable(media: MWPortableMedia): MWMedia { + displayName: "TheFlix", + + async getMediaFromPortable(media: MWPortableMedia): Promise { return { ...media, title: "title here" } }, - searchForMedia(query: MWQuery): MWMedia[] { - return []; + + async searchForMedia(query: MWQuery): Promise { + return [{ + mediaId: "a", + providerId: this.id, + title: "testing", + }]; }, } diff --git a/src/scrapers/scrapers.ts b/src/scrapers/types.ts similarity index 67% rename from src/scrapers/scrapers.ts rename to src/scrapers/types.ts index 533e865b..04d3c4ad 100644 --- a/src/scrapers/scrapers.ts +++ b/src/scrapers/types.ts @@ -19,8 +19,10 @@ export interface MWQuery { export interface MWMediaProvider { id: string, // id of provider, must be unique + enabled: boolean, type: MWMediaType, + displayName: string, - getMediaFromPortable(media: MWPortableMedia): MWMedia, - searchForMedia(query: MWQuery): MWMedia[], + getMediaFromPortable(media: MWPortableMedia): Promise, + searchForMedia(query: MWQuery): Promise, } diff --git a/tsconfig.json b/tsconfig.json index 16fff78a..15ea6d2f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,4 +1,5 @@ { + "extends": "./paths.json", "compilerOptions": { "target": "es5", "lib": [