diff --git a/public/script/fetchinfo.js b/public/script/fetchinfo.js index 2ae824c..2d79d67 100644 --- a/public/script/fetchinfo.js +++ b/public/script/fetchinfo.js @@ -30,15 +30,15 @@ function formatSize(bytes) { */ async function fetchVideoInfo(url) { try { - const res = await fetch("/info", { - method: "POST", - headers: { "Content-Type": "application/x-www-form-urlencoded" }, - body: new URLSearchParams({ url }), + const res = await fetch(`/info?url=${encodeURIComponent(url)}`, { + method: "GET", + headers: {"Accept": "application/json"} }); - if (!res.ok) return { error: `An Error occured when fetching info` }; - const data = await res.json(); + + if (!res.ok) return {error: `An Error occured when fetching info`}; + if (!data) return { error: "Data is Missing" }; return data; diff --git a/server/controller/info.controller.js b/server/controller/info.controller.js index d8078ac..38826fa 100644 --- a/server/controller/info.controller.js +++ b/server/controller/info.controller.js @@ -18,7 +18,9 @@ const { isValidUrl } = require("../helpers/validation.helpers"); */ async function infoController(req, res) { - const url = req.body.url || req.query.url; // Supports POST and GET requests + // Previously, i support both POST and GET element, but it's kinda weird. So now it only support GET. + const url = req.query.url; + const encodedUrl = encodeURIComponent(url) /** * Basic validation: @@ -30,9 +32,10 @@ async function infoController(req, res) { return res.status(400).send("Invalid URL Or Missing"); } - logger.info(`Info request received. URL: ${url}`); + logger.info(`Info request received. RAW URL: ${url}`); + logger.info(`Info request received. ENCODED: ${encodedUrl}`); - // Lightweight heuristic to detect playlist URLs + // Lightweight heuristic to detect playlist URLs. It works for Youtube. const isPlaylistUrl = url.includes("&list") || url.includes("?list"); logger.info( @@ -86,6 +89,16 @@ async function infoController(req, res) { */ if (data._type === "playlist") { + // If the playlist got only one video, show it as a video. + if (data.entries && data.entries.length === 1) { + + const singleVideo = parseVideo(data.entries[0]); + + logger.info(`Playlist with 1 item converted to video: ${singleVideo.title}`); + + return res.json(singleVideo); + } + const playlist = parsePlaylist(data); logger.info( diff --git a/server/routes/info.route.js b/server/routes/info.route.js index f7465fe..8e7de75 100644 --- a/server/routes/info.route.js +++ b/server/routes/info.route.js @@ -2,6 +2,6 @@ const express = require("express"); const router = express.Router(); const { infoController } = require("../controller/info.controller"); -router.post("/", infoController); +router.get("/", infoController); module.exports = router; \ No newline at end of file