|
|
@ -5,7 +5,11 @@ import { |
|
|
|
} from "@movie-web/providers"; |
|
|
|
} from "@movie-web/providers"; |
|
|
|
import { RefObject, useCallback, useEffect, useRef, useState } from "react"; |
|
|
|
import { RefObject, useCallback, useEffect, useRef, useState } from "react"; |
|
|
|
|
|
|
|
|
|
|
|
import { makeProviderUrl } from "@/backend/helpers/providerApi"; |
|
|
|
import { |
|
|
|
|
|
|
|
connectServerSideEvents, |
|
|
|
|
|
|
|
getCachedMetadata, |
|
|
|
|
|
|
|
makeProviderUrl, |
|
|
|
|
|
|
|
} from "@/backend/helpers/providerApi"; |
|
|
|
import { getLoadbalancedProviderApiUrl, providers } from "@/utils/providers"; |
|
|
|
import { getLoadbalancedProviderApiUrl, providers } from "@/utils/providers"; |
|
|
|
|
|
|
|
|
|
|
|
export interface ScrapingItems { |
|
|
|
export interface ScrapingItems { |
|
|
@ -37,7 +41,7 @@ function useBaseScrape() { |
|
|
|
setSources( |
|
|
|
setSources( |
|
|
|
evt.sourceIds |
|
|
|
evt.sourceIds |
|
|
|
.map((v) => { |
|
|
|
.map((v) => { |
|
|
|
const source = providers.getMetadata(v); |
|
|
|
const source = getCachedMetadata().find((s) => s.id === v); |
|
|
|
if (!source) throw new Error("invalid source id"); |
|
|
|
if (!source) throw new Error("invalid source id"); |
|
|
|
const out: ScrapingSegment = { |
|
|
|
const out: ScrapingSegment = { |
|
|
|
name: source.name, |
|
|
|
name: source.name, |
|
|
@ -80,7 +84,9 @@ function useBaseScrape() { |
|
|
|
(evt: ScraperEvent<"discoverEmbeds">) => { |
|
|
|
(evt: ScraperEvent<"discoverEmbeds">) => { |
|
|
|
setSources((s) => { |
|
|
|
setSources((s) => { |
|
|
|
evt.embeds.forEach((v) => { |
|
|
|
evt.embeds.forEach((v) => { |
|
|
|
const source = providers.getMetadata(v.embedScraperId); |
|
|
|
const source = getCachedMetadata().find( |
|
|
|
|
|
|
|
(src) => src.id === v.embedScraperId |
|
|
|
|
|
|
|
); |
|
|
|
if (!source) throw new Error("invalid source id"); |
|
|
|
if (!source) throw new Error("invalid source id"); |
|
|
|
const out: ScrapingSegment = { |
|
|
|
const out: ScrapingSegment = { |
|
|
|
embedId: v.embedScraperId, |
|
|
|
embedId: v.embedScraperId, |
|
|
@ -149,37 +155,18 @@ export function useScrape() { |
|
|
|
const providerApiUrl = getLoadbalancedProviderApiUrl(); |
|
|
|
const providerApiUrl = getLoadbalancedProviderApiUrl(); |
|
|
|
if (providerApiUrl) { |
|
|
|
if (providerApiUrl) { |
|
|
|
startScrape(); |
|
|
|
startScrape(); |
|
|
|
const sseOutput = await new Promise<RunOutput | null>( |
|
|
|
const baseUrlMaker = makeProviderUrl(providerApiUrl); |
|
|
|
(resolve, reject) => { |
|
|
|
const conn = connectServerSideEvents<RunOutput | "">( |
|
|
|
const baseUrlMaker = makeProviderUrl(providerApiUrl); |
|
|
|
baseUrlMaker.scrapeAll(media), |
|
|
|
const scrapeEvents = new EventSource(baseUrlMaker.scrapeAll(media)); |
|
|
|
["completed", "noOutput"] |
|
|
|
scrapeEvents.addEventListener("init", (e) => { |
|
|
|
|
|
|
|
initEvent(JSON.parse(e.data)); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
scrapeEvents.addEventListener("error", (err) => { |
|
|
|
|
|
|
|
console.error("failed to use provider api", err); |
|
|
|
|
|
|
|
reject(err); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
scrapeEvents.addEventListener("start", (e) => |
|
|
|
|
|
|
|
startEvent(JSON.parse(e.data)) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
scrapeEvents.addEventListener("update", (e) => |
|
|
|
|
|
|
|
updateEvent(JSON.parse(e.data)) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
scrapeEvents.addEventListener("discoverEmbeds", (e) => |
|
|
|
|
|
|
|
discoverEmbedsEvent(JSON.parse(e.data)) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
scrapeEvents.addEventListener("completed", (e) => { |
|
|
|
|
|
|
|
scrapeEvents.close(); |
|
|
|
|
|
|
|
resolve(JSON.parse(e.data)); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
scrapeEvents.addEventListener("noOutput", () => { |
|
|
|
|
|
|
|
scrapeEvents.close(); |
|
|
|
|
|
|
|
resolve(null); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
); |
|
|
|
); |
|
|
|
return getResult(sseOutput); |
|
|
|
conn.on("init", initEvent); |
|
|
|
|
|
|
|
conn.on("start", startEvent); |
|
|
|
|
|
|
|
conn.on("update", updateEvent); |
|
|
|
|
|
|
|
conn.on("discoverEmbeds", discoverEmbedsEvent); |
|
|
|
|
|
|
|
const sseOutput = await conn.promise(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return getResult(sseOutput === "" ? null : sseOutput); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!providers) return null; |
|
|
|
if (!providers) return null; |
|
|
|