From f55b39d0fa7309b72a6646092a7e86eaee836a56 Mon Sep 17 00:00:00 2001 From: mrjvs Date: Mon, 18 Dec 2023 22:24:34 +0100 Subject: [PATCH] Properly map events and data to providers api --- src/hooks/useProviderScrape.tsx | 57 ++++++++++++++++++++++++++++----- src/utils/providers.ts | 2 +- src/utils/proxyUrls.ts | 5 +-- 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/src/hooks/useProviderScrape.tsx b/src/hooks/useProviderScrape.tsx index d1120996..80ab63a4 100644 --- a/src/hooks/useProviderScrape.tsx +++ b/src/hooks/useProviderScrape.tsx @@ -150,15 +150,56 @@ export function useScrape() { startScrape(); const sseOutput = await new Promise( (resolve, reject) => { - const scrapeEvents = new EventSource(providerApiUrl); - scrapeEvents.addEventListener("error", (err) => reject(err)); - scrapeEvents.addEventListener("init", (e) => initEvent(e.data)); - scrapeEvents.addEventListener("start", (e) => startEvent(e.data)); - scrapeEvents.addEventListener("update", (e) => updateEvent(e.data)); - scrapeEvents.addEventListener("discoverEmbeds", (e) => - discoverEmbedsEvent(e.data) + const finalUrl = new URL(`${providerApiUrl}/scrape`); + finalUrl.searchParams.append("type", media.type); + finalUrl.searchParams.append( + "releaseYear", + media.releaseYear.toString() ); - scrapeEvents.addEventListener("finish", (e) => resolve(e.data)); + finalUrl.searchParams.append("title", media.title); + finalUrl.searchParams.append("tmdbId", media.tmdbId); + if (media.imdbId) + finalUrl.searchParams.append("imdbId", media.imdbId); + if (media.type === "show") { + finalUrl.searchParams.append( + "episodeNumber", + media.episode.number.toString() + ); + finalUrl.searchParams.append( + "episodeTmdbId", + media.episode.tmdbId + ); + finalUrl.searchParams.append( + "seasonNumber", + media.season.number.toString() + ); + finalUrl.searchParams.append("seasonTmdbId", media.season.tmdbId); + } + const scrapeEvents = new EventSource(finalUrl.toString()); + 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); diff --git a/src/utils/providers.ts b/src/utils/providers.ts index 35ebe80d..73c3849e 100644 --- a/src/utils/providers.ts +++ b/src/utils/providers.ts @@ -22,7 +22,7 @@ function makeLoadbalancedList(getter: () => string[]) { }; } -const getLoadbalancedProxyUrl = makeLoadbalancedList(getProxyUrls); +export const getLoadbalancedProxyUrl = makeLoadbalancedList(getProxyUrls); export const getLoadbalancedProviderApiUrl = makeLoadbalancedList(getProviderApiUrls); diff --git a/src/utils/proxyUrls.ts b/src/utils/proxyUrls.ts index c967a01c..645c8329 100644 --- a/src/utils/proxyUrls.ts +++ b/src/utils/proxyUrls.ts @@ -24,7 +24,7 @@ function isParsedUrlType(type: string): type is ParsedUrlType { } /** - * Turn a string like "a=b,c=d,d=e" into a dictionary object + * Turn a string like "a=b;c=d;d=e" into a dictionary object */ function parseParams(input: string): Record { const entriesParams = input @@ -48,7 +48,7 @@ export function getParsedUrls() { } } - const match = url.match(/^|([^|])+|(.*)$/g); + const match = /^\|([^|]+)\|(.*)$/g.exec(url); if (!match || !match[2]) return; if (!canParseUrl(match[2])) return; const params = parseParams(match[1]); @@ -61,6 +61,7 @@ export function getParsedUrls() { }); }); + console.log(urls, output); return output; }