From ee2b63034ef2587ddf2299697db58d4bb5edd681 Mon Sep 17 00:00:00 2001 From: mrjvs Date: Wed, 6 Sep 2023 17:40:03 +0200 Subject: [PATCH] not found reasons + json parsing --- src/fetchers/standardFetch.ts | 9 ++++++--- src/main/events.ts | 3 ++- src/main/runner.ts | 2 ++ src/providers/sources/flixhq/index.ts | 2 +- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/fetchers/standardFetch.ts b/src/fetchers/standardFetch.ts index f07b6ac..5110d0e 100644 --- a/src/fetchers/standardFetch.ts +++ b/src/fetchers/standardFetch.ts @@ -5,12 +5,11 @@ import { makeFullUrl } from '@/fetchers/common'; import { Fetcher } from '@/fetchers/types'; export function makeStandardFetcher(f: typeof fetch): Fetcher { - const normalFetch: Fetcher = (url, ops) => { + const normalFetch: Fetcher = async (url, ops) => { const fullUrl = makeFullUrl(url, ops); - const seralizedBody = serializeBody(ops.body); - return f(fullUrl, { + const res = await f(fullUrl, { method: ops.method, headers: { ...seralizedBody.headers, @@ -18,6 +17,10 @@ export function makeStandardFetcher(f: typeof fetch): Fetcher { }, body: seralizedBody.body, }); + + const isJson = res.headers.get('content-type')?.includes('application/json'); + if (isJson) return res.json(); + return res.text(); }; return normalFetch; diff --git a/src/main/events.ts b/src/main/events.ts index 5bd8e5d..485c86f 100644 --- a/src/main/events.ts +++ b/src/main/events.ts @@ -3,7 +3,8 @@ export type UpdateEventStatus = 'success' | 'failure' | 'notfound' | 'pending'; export type UpdateEvent = { percentage: number; status: UpdateEventStatus; - error?: unknown; + error?: unknown; // set when status is failure + reason?: string; // set when status is not-found }; export type InitEvent = { diff --git a/src/main/runner.ts b/src/main/runner.ts index 3a81bd2..09a6d2f 100644 --- a/src/main/runner.ts +++ b/src/main/runner.ts @@ -79,6 +79,7 @@ export async function runAllProviders(list: ProviderList, ops: ProviderRunnerOpt ops.events?.update?.({ percentage: 100, status: 'notfound', + reason: err.message, }); continue; } @@ -133,6 +134,7 @@ export async function runAllProviders(list: ProviderList, ops: ProviderRunnerOpt ops.events?.update?.({ percentage: 100, status: 'notfound', + reason: err.message, }); continue; } diff --git a/src/providers/sources/flixhq/index.ts b/src/providers/sources/flixhq/index.ts index f847502..c8559bd 100644 --- a/src/providers/sources/flixhq/index.ts +++ b/src/providers/sources/flixhq/index.ts @@ -11,7 +11,7 @@ export const flixhqScraper = makeSourcerer({ rank: 100, async scrapeMovie(ctx) { const id = await getFlixhqId(ctx, ctx.media); - if (!id) throw new NotFoundError(); + if (!id) throw new NotFoundError('no search results match'); const sources = await getFlixhqSources(ctx, id); const upcloudStream = sources.find((v) => v.embed.toLowerCase() === 'upcloud');