-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
only use runtimeConfig inside of the a nuxt function
- Loading branch information
Showing
3 changed files
with
142 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
}) | ||
} | ||
}) | ||
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", | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; | ||
} | ||
}) | ||
|
||
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", | ||
}); | ||
} | ||
}); |