From c79cb7405fb69ed48fc16f31bf7966dd24238ece Mon Sep 17 00:00:00 2001 From: mirsella Date: Wed, 20 Nov 2024 00:22:52 +0100 Subject: [PATCH] only use runtimeConfig inside of the a nuxt function see https://github.com/primefaces/primevue/issues/6712 --- server/api/movies/[id]/index.get.ts | 113 ++++++++++++++++++---------- server/api/movies/genres.get.ts | 37 +++++---- server/api/movies/search.get.ts | 101 +++++++++++++------------ 3 files changed, 142 insertions(+), 109 deletions(-) diff --git a/server/api/movies/[id]/index.get.ts b/server/api/movies/[id]/index.get.ts index 15b4a62..bd0b666 100644 --- a/server/api/movies/[id]/index.get.ts +++ b/server/api/movies/[id]/index.get.ts @@ -1,96 +1,129 @@ import { XMLParser } from "fast-xml-parser"; -const BASE_URL = 'https://api.themoviedb.org/3/'; -const config = useRuntimeConfig(); +const BASE_URL = "https://api.themoviedb.org/3/"; export default defineEventHandler(async (event) => { + const config = useRuntimeConfig(); try { if (!event.context.params?.id) { throw createError({ statusCode: 400, - statusMessage: 'id parameter is required' - }) + statusMessage: "id parameter is required", + }); } - const id = event.context.params?.id + const id = event.context.params?.id; //Fetch movie infos - const response = await fetch(`${BASE_URL}find/${id}` - + `?external_source=imdb_id` - + `&api_key=${config.tmdbApiKey}`); + const response = await fetch( + `${BASE_URL}find/${id}` + + `?external_source=imdb_id` + + `&api_key=${config.tmdbApiKey}`, + ); const resData = await response.json(); if (!resData.movie_results || resData.movie_results.length === 0) { throw createError({ statusCode: 404, - statusMessage: `No movie found with IMDb ID: ${id}` - }) + statusMessage: `No movie found with IMDb ID: ${id}`, + }); } //Fetch movie credits - const resCredits = await fetch(`${BASE_URL}movie/${resData.movie_results[0].id}/credits` - + `?api_key=${config.tmdbApiKey}`); + const resCredits = await fetch( + `${BASE_URL}movie/${resData.movie_results[0].id}/credits` + + `?api_key=${config.tmdbApiKey}`, + ); const credits = await resCredits.json(); //Fetch movie details - const resDetails = await fetch(`${BASE_URL}movie/${resData.movie_results[0].id}` - + `?api_key=${config.tmdbApiKey}`); + const resDetails = await fetch( + `${BASE_URL}movie/${resData.movie_results[0].id}` + + `?api_key=${config.tmdbApiKey}`, + ); const details = await resDetails.json(); const movie_infos = { ...resData.movie_results[0], tagline: details.tagline, runtime: details.runtime, - cast: credits.cast.filter((a: any) => (a.known_for_department === "Acting" && a.order <= 10)), + cast: credits.cast.filter( + (a: any) => a.known_for_department === "Acting" && a.order <= 10, + ), director: credits.crew.filter((a: any) => a.job === "Director"), writing: [ ...credits.crew.filter((a: any) => a.job === "Screenplay"), credits.crew.filter((a: any) => a.job === "Novel")[0], ], - producers: credits.crew.filter((a: any) => a.job === "Executive Producer"), - production_companies: details.production_companies - } - + producers: credits.crew.filter( + (a: any) => a.job === "Executive Producer", + ), + production_companies: details.production_companies, + }; //Fetch torrents from title - const resTorrents = await fetch("http://localhost:9117/api/v2.0/indexers/all/results/torznab/api" - + `?apikey=${config.jackettApiKey}` - + `&t=movie&q=${encodeURI(movie_infos?.title)}` - + `&year=${movie_infos.release_date.slice(0, 4)}` - + `&cat=2000`) + const resTorrents = await fetch( + "http://localhost:9117/api/v2.0/indexers/all/results/torznab/api" + + `?apikey=${config.jackettApiKey}` + + `&t=movie&q=${encodeURI(movie_infos?.title)}` + + `&year=${movie_infos.release_date.slice(0, 4)}` + + `&cat=2000`, + ); if (!resTorrents.ok) { throw createError({ statusCode: resTorrents.status, - statusMessage: `Jackett API error: ${resTorrents.statusText}` - }) + statusMessage: `Jackett API error: ${resTorrents.statusText}`, + }); } - const torrents = new XMLParser({ ignoreAttributes: false }).parse(await resTorrents.text()) - + const torrents = new XMLParser({ ignoreAttributes: false }).parse( + await resTorrents.text(), + ); //Filter,sort,format torrents results movie_infos.torrents = torrents.rss.channel.item - .filter((a: any) => a["guid"].split(':')[0] === "magnet" - && parseInt(a["torznab:attr"].find((a: any) => a["@_name"] === "seeders")?.["@_value"]) > 5 - && a["torznab:attr"].find((a: any) => a["@_name"] === "imdbid")?.["@_value"] === id) + .filter( + (a: any) => + a["guid"].split(":")[0] === "magnet" && + parseInt( + a["torznab:attr"].find((a: any) => a["@_name"] === "seeders")?.[ + "@_value" + ], + ) > 5 && + a["torznab:attr"].find((a: any) => a["@_name"] === "imdbid")?.[ + "@_value" + ] === id, + ) .sort( (a: any, b: any) => - parseInt(b["torznab:attr"].find((a: any) => a["@_name"] === "seeders")?.["@_value"]) - - parseInt(a["torznab:attr"].find((a: any) => a["@_name"] === "seeders")?.["@_value"])) + parseInt( + b["torznab:attr"].find((a: any) => a["@_name"] === "seeders")?.[ + "@_value" + ], + ) - + parseInt( + a["torznab:attr"].find((a: any) => a["@_name"] === "seeders")?.[ + "@_value" + ], + ), + ) .slice(0, 5) .map((t: any) => ({ magnet: t["guid"], - seeders: parseInt(t["torznab:attr"].find((a: any) => a["@_name"] === "seeders")?.["@_value"]), + seeders: parseInt( + t["torznab:attr"].find((a: any) => a["@_name"] === "seeders")?.[ + "@_value" + ], + ), description: t["title"], - size: t["size"] - }) - ); + size: t["size"], + })); - return movie_infos + return movie_infos; // TODO: add available subtiltes ? number of comments ? } catch (error) { - console.error('Error in movie/id search:', error) + console.error("Error in movie/id search:", error); return error; } -}) +}); diff --git a/server/api/movies/genres.get.ts b/server/api/movies/genres.get.ts index 147528a..14edba0 100644 --- a/server/api/movies/genres.get.ts +++ b/server/api/movies/genres.get.ts @@ -1,22 +1,21 @@ -const BASE_URL = 'https://api.themoviedb.org/3/'; -const config = useRuntimeConfig(); +const BASE_URL = "https://api.themoviedb.org/3/"; export default defineEventHandler(async (event) => { - try { - // Fetch setup data - const response = await fetch( - `${BASE_URL}genre/movie/list?api_key=${config.tmdbApiKey}` - ) - const data = await response.json() + const config = useRuntimeConfig(); + try { + // Fetch setup data + const response = await fetch( + `${BASE_URL}genre/movie/list?api_key=${config.tmdbApiKey}`, + ); + const data = await response.json(); - return data; - - } catch (error) { - console.error('Error in genre fetching:', error) - return createError({ - statusCode: 500, - statusMessage: 'Internal Server Error', - message: 'Failed to process movie genres request' - }) - } -}) \ No newline at end of file + return data; + } catch (error) { + console.error("Error in genre fetching:", error); + return createError({ + statusCode: 500, + statusMessage: "Internal Server Error", + message: "Failed to process movie genres request", + }); + } +}); diff --git a/server/api/movies/search.get.ts b/server/api/movies/search.get.ts index c66847b..4fc1efa 100644 --- a/server/api/movies/search.get.ts +++ b/server/api/movies/search.get.ts @@ -1,53 +1,54 @@ -const BASE_URL = 'https://api.themoviedb.org/3/'; -const config = useRuntimeConfig(); +const BASE_URL = "https://api.themoviedb.org/3/"; export default defineEventHandler(async (event) => { - try { - // Get all query parameters - const query = getQuery(event) - - // Validate query parameters - const searchParams = { - page: Number(query.page) || 1, - title: (query.title as string) || "", - } - - let popularOrSearch = 'search/movie' - - if (!searchParams.title) { - popularOrSearch = 'movie/popular' - }; - - - const response = await fetch(`${BASE_URL}${popularOrSearch}` - + `?api_key=${config.tmdbApiKey}` - + `&query=${searchParams.title}` - + `&include_adult=false` - + `&page=${searchParams.page}`); - const data = await response.json(); - - - // Add imdb_id - if (data.results) { - for (const element of data.results) { - const resImdbID = await fetch(`${BASE_URL}movie/${element.id}/external_ids` - + `?api_key=${config.tmdbApiKey}`); - const external_ids = await resImdbID.json() - element.imdb_id = external_ids.imdb_id - }; - } - - // Remove if imdb_id not found - data.results = data.results.filter((movie: any) => movie.imdb_id !== null); - - return data; - - } catch (error) { - console.error('Error in movie search:', error) - return createError({ - statusCode: 500, - statusMessage: 'Internal Server Error', - message: 'Failed to process movie search request' - }) + const config = useRuntimeConfig(); + try { + // Get all query parameters + const query = getQuery(event); + + // Validate query parameters + const searchParams = { + page: Number(query.page) || 1, + title: (query.title as string) || "", + }; + + let popularOrSearch = "search/movie"; + + if (!searchParams.title) { + popularOrSearch = "movie/popular"; } -}) \ No newline at end of file + + const response = await fetch( + `${BASE_URL}${popularOrSearch}` + + `?api_key=${config.tmdbApiKey}` + + `&query=${searchParams.title}` + + `&include_adult=false` + + `&page=${searchParams.page}`, + ); + const data = await response.json(); + + // Add imdb_id + if (data.results) { + for (const element of data.results) { + const resImdbID = await fetch( + `${BASE_URL}movie/${element.id}/external_ids` + + `?api_key=${config.tmdbApiKey}`, + ); + const external_ids = await resImdbID.json(); + element.imdb_id = external_ids.imdb_id; + } + } + + // Remove if imdb_id not found + data.results = data.results.filter((movie: any) => movie.imdb_id !== null); + + return data; + } catch (error) { + console.error("Error in movie search:", error); + return createError({ + statusCode: 500, + statusMessage: "Internal Server Error", + message: "Failed to process movie search request", + }); + } +});