| const { fetchTmdbData } = require('../handlers/mainHandle'); |
| const { processVideo } = require('../utils/ts'); |
| const { getCachedLink } = require('../utils/cache'); |
|
|
| |
| |
| |
| |
| const tmdbEndpointMap = { |
| '/movie/popular': 'movie/popular', |
| '/tv/popular': 'tv/popular', |
| '/trending/movie/day': 'trending/movie/day', |
| '/trending/tv/day': 'trending/tv/day', |
| '/trending/all/day': 'trending/all/day', |
| '/trending/all/week': 'trending/all/week', |
| '/trending/movie/week': 'trending/movie/week', |
| '/trending/tv/week': 'trending/tv/week', |
| '/genre/movie/list': 'genre/movie/list', |
| '/discover/movie': 'discover/movie', |
| '/discover/tv': 'discover/tv', |
| '/movie/upcoming': 'movie/upcoming', |
| '/movie/now_playing': 'movie/now_playing', |
| '/tv': 'tv', |
| '/movie': 'movie', |
| '/search/multi': 'search/multi', |
| }; |
|
|
| |
|
|
| async function getTmdbContent(req, res) { |
| try { |
| |
| const basePath = req.route.path.replace(/\/:tmdbid.*$/, ''); |
| let endpoint = tmdbEndpointMap[basePath]; |
|
|
| if (!endpoint) { |
| return res.status(404).json({ error: "Ruta no mapeada en el servidor." }); |
| } |
|
|
| |
| const { tmdbid, seasonid } = req.params; |
| |
| if (tmdbid) { |
| endpoint += `/${tmdbid}`; |
| if (seasonid) { |
| endpoint += `/season/${seasonid}`; |
| } |
| } |
|
|
| |
| const data = await fetchTmdbData(endpoint, req.query); |
|
|
| if (!data) { |
| return res.status(404).json({ error: "No se encontraron resultados en TMDB." }); |
| } |
|
|
| res.setHeader('Access-Control-Allow-Origin', '*'); |
| return res.json(data); |
|
|
| } catch (error) { |
| console.error(`[TMDB Controller Error]: ${error.message}`); |
| return res.status(500).json({ |
| error: "Error al procesar la solicitud con TMDB.", |
| details: error.message |
| }); |
| } |
| } |
|
|
| async function getCacheStream(req, res) { |
| const { locker, id } = req.params; |
| const cacheKey = `${locker}-get${id}`; |
| |
| const cachedItem = getCachedLink(cacheKey); |
|
|
| if (!cachedItem || !cachedItem.result) { |
| return res.status(404).json({ error: `Contenido no encontrado o expirado para el ID: ${id}` }); |
| } |
|
|
| try { |
| const processedLink = await processVideo(cachedItem.result); |
| return res.json({ link: processedLink }); |
| } catch (error) { |
| console.error(`[Stream Error]: ${error.message}`); |
| return res.status(500).json({ error: 'Error al procesar el enlace de video.' }); |
| } |
| } |
|
|
| module.exports = { |
| getCacheStream, |
| getTmdbContent |
| }; |