Properly map events and data to providers api

This commit is contained in:
mrjvs 2023-12-18 22:24:34 +01:00
parent 8af4256d95
commit f55b39d0fa
3 changed files with 53 additions and 11 deletions

View File

@ -150,15 +150,56 @@ export function useScrape() {
startScrape(); startScrape();
const sseOutput = await new Promise<RunOutput | null>( const sseOutput = await new Promise<RunOutput | null>(
(resolve, reject) => { (resolve, reject) => {
const scrapeEvents = new EventSource(providerApiUrl); const finalUrl = new URL(`${providerApiUrl}/scrape`);
scrapeEvents.addEventListener("error", (err) => reject(err)); finalUrl.searchParams.append("type", media.type);
scrapeEvents.addEventListener("init", (e) => initEvent(e.data)); finalUrl.searchParams.append(
scrapeEvents.addEventListener("start", (e) => startEvent(e.data)); "releaseYear",
scrapeEvents.addEventListener("update", (e) => updateEvent(e.data)); media.releaseYear.toString()
scrapeEvents.addEventListener("discoverEmbeds", (e) =>
discoverEmbedsEvent(e.data)
); );
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); return getResult(sseOutput);

View File

@ -22,7 +22,7 @@ function makeLoadbalancedList(getter: () => string[]) {
}; };
} }
const getLoadbalancedProxyUrl = makeLoadbalancedList(getProxyUrls); export const getLoadbalancedProxyUrl = makeLoadbalancedList(getProxyUrls);
export const getLoadbalancedProviderApiUrl = export const getLoadbalancedProviderApiUrl =
makeLoadbalancedList(getProviderApiUrls); makeLoadbalancedList(getProviderApiUrls);

View File

@ -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<string, string> { function parseParams(input: string): Record<string, string> {
const entriesParams = input 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 (!match || !match[2]) return;
if (!canParseUrl(match[2])) return; if (!canParseUrl(match[2])) return;
const params = parseParams(match[1]); const params = parseParams(match[1]);
@ -61,6 +61,7 @@ export function getParsedUrls() {
}); });
}); });
console.log(urls, output);
return output; return output;
} }