Add provider-api to source selection menu

This commit is contained in:
mrjvs 2023-12-19 18:09:05 +01:00
parent 15d97dda02
commit ca2e20fdbc

View File

@ -5,6 +5,10 @@ import {
} from "@movie-web/providers"; } from "@movie-web/providers";
import { useAsyncFn } from "react-use"; import { useAsyncFn } from "react-use";
import {
connectServerSideEvents,
makeProviderUrl,
} from "@/backend/helpers/providerApi";
import { import {
scrapeSourceOutputToProviderMetric, scrapeSourceOutputToProviderMetric,
useReportProviders, useReportProviders,
@ -14,7 +18,7 @@ import { convertRunoutputToSource } from "@/components/player/utils/convertRunou
import { useOverlayRouter } from "@/hooks/useOverlayRouter"; import { useOverlayRouter } from "@/hooks/useOverlayRouter";
import { metaToScrapeMedia } from "@/stores/player/slices/source"; import { metaToScrapeMedia } from "@/stores/player/slices/source";
import { usePlayerStore } from "@/stores/player/store"; import { usePlayerStore } from "@/stores/player/store";
import { providers } from "@/utils/providers"; import { getLoadbalancedProviderApiUrl, providers } from "@/utils/providers";
export function useEmbedScraping( export function useEmbedScraping(
routerId: string, routerId: string,
@ -31,13 +35,23 @@ export function useEmbedScraping(
const { report } = useReportProviders(); const { report } = useReportProviders();
const [request, run] = useAsyncFn(async () => { const [request, run] = useAsyncFn(async () => {
const providerApiUrl = getLoadbalancedProviderApiUrl();
let result: EmbedOutput | undefined; let result: EmbedOutput | undefined;
if (!meta) return; if (!meta) return;
try { try {
if (providerApiUrl) {
const baseUrlMaker = makeProviderUrl(providerApiUrl);
const conn = connectServerSideEvents<EmbedOutput>(
baseUrlMaker.scrapeEmbed(embedId, url),
["completed"]
);
result = await conn.promise();
} else {
result = await providers.runEmbedScraper({ result = await providers.runEmbedScraper({
id: embedId, id: embedId,
url, url,
}); });
}
} catch (err) { } catch (err) {
console.error(`Failed to scrape ${embedId}`, err); console.error(`Failed to scrape ${embedId}`, err);
const notFound = err instanceof NotFoundError; const notFound = err instanceof NotFoundError;
@ -85,13 +99,23 @@ export function useSourceScraping(sourceId: string | null, routerId: string) {
const [request, run] = useAsyncFn(async () => { const [request, run] = useAsyncFn(async () => {
if (!sourceId || !meta) return null; if (!sourceId || !meta) return null;
const scrapeMedia = metaToScrapeMedia(meta); const scrapeMedia = metaToScrapeMedia(meta);
const providerApiUrl = getLoadbalancedProviderApiUrl();
let result: SourcererOutput | undefined; let result: SourcererOutput | undefined;
try { try {
if (providerApiUrl) {
const baseUrlMaker = makeProviderUrl(providerApiUrl);
const conn = connectServerSideEvents<SourcererOutput>(
baseUrlMaker.scrapeSource(sourceId, scrapeMedia),
["completed"]
);
result = await conn.promise();
} else {
result = await providers.runSourceScraper({ result = await providers.runSourceScraper({
id: sourceId, id: sourceId,
media: scrapeMedia, media: scrapeMedia,
}); });
}
} catch (err) { } catch (err) {
console.error(`Failed to scrape ${sourceId}`, err); console.error(`Failed to scrape ${sourceId}`, err);
const notFound = err instanceof NotFoundError; const notFound = err instanceof NotFoundError;
@ -120,10 +144,22 @@ export function useSourceScraping(sourceId: string | null, routerId: string) {
let embedResult: EmbedOutput | undefined; let embedResult: EmbedOutput | undefined;
if (!meta) return; if (!meta) return;
try { try {
if (providerApiUrl) {
const baseUrlMaker = makeProviderUrl(providerApiUrl);
const conn = connectServerSideEvents<EmbedOutput>(
baseUrlMaker.scrapeEmbed(
result.embeds[0].embedId,
result.embeds[0].url
),
["completed"]
);
embedResult = await conn.promise();
} else {
embedResult = await providers.runEmbedScraper({ embedResult = await providers.runEmbedScraper({
id: result.embeds[0].embedId, id: result.embeds[0].embedId,
url: result.embeds[0].url, url: result.embeds[0].url,
}); });
}
} catch (err) { } catch (err) {
console.error(`Failed to scrape ${result.embeds[0].embedId}`, err); console.error(`Failed to scrape ${result.embeds[0].embedId}`, err);
const notFound = err instanceof NotFoundError; const notFound = err instanceof NotFoundError;