From 5a67f44b48ef1c33be6d857275eff9b73c5a41c7 Mon Sep 17 00:00:00 2001 From: liixing <249568024@qq.com> Date: Thu, 18 Sep 2025 10:00:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EGET=20/api/v2/search/episodes?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=EF=BC=8C=E6=94=AF=E6=8C=81=E6=A0=B9=E6=8D=AE?= =?UTF-8?q?=E5=8A=A8=E6=BC=AB=E5=90=8D=E7=A7=B0=E5=92=8C=E9=9B=86=E6=95=B0?= =?UTF-8?q?=E8=BF=87=E6=BB=A4=E6=90=9C=E7=B4=A2=E7=BB=93=E6=9E=9C=EF=BC=8C?= =?UTF-8?q?=E5=B9=B6=E4=BC=98=E5=8C=96=E7=9B=B8=E5=85=B3=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- danmu_api/worker.js | 98 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 97 insertions(+), 1 deletion(-) diff --git a/danmu_api/worker.js b/danmu_api/worker.js index 9864d9e..6872424 100644 --- a/danmu_api/worker.js +++ b/danmu_api/worker.js @@ -2631,6 +2631,95 @@ async function matchAnime(url, req) { } } +// Extracted function for GET /api/v2/search/episodes +async function searchEpisodes(url) { + const anime = url.searchParams.get("anime"); + const episode = url.searchParams.get("episode") || ""; + + log("log", `Search episodes with anime: ${anime}, episode: ${episode}`); + + if (!anime) { + log("error", "Missing anime parameter"); + return jsonResponse( + { errorCode: 400, success: false, errorMessage: "Missing anime parameter" }, + 400 + ); + } + + // 先搜索动漫 + let searchUrl = new URL(url.href.replace("/search/episodes", `/search/anime?keyword=${anime}`)); + const searchRes = await searchAnime(searchUrl); + const searchData = await searchRes.json(); + + if (!searchData.success || !searchData.animes || searchData.animes.length === 0) { + log("log", "No anime found for the given title"); + return jsonResponse({ + errorCode: 0, + success: true, + errorMessage: "", + hasMore: false, + animes: [] + }); + } + + let resultAnimes = []; + + // 遍历所有找到的动漫,获取它们的集数信息 + for (const animeItem of searchData.animes) { + const bangumiUrl = new URL(url.href.replace("/search/episodes", `/bangumi/${animeItem.bangumiId}`)); + const bangumiRes = await getBangumi(bangumiUrl.pathname); + const bangumiData = await bangumiRes.json(); + + if (bangumiData.success && bangumiData.bangumi && bangumiData.bangumi.episodes) { + let filteredEpisodes = bangumiData.bangumi.episodes; + + // 根据 episode 参数过滤集数 + if (episode) { + if (episode === "movie") { + // 仅保留剧场版结果 + filteredEpisodes = bangumiData.bangumi.episodes.filter(ep => + animeItem.typeDescription && ( + animeItem.typeDescription.includes("电影") || + animeItem.typeDescription.includes("剧场版") || + ep.episodeTitle.toLowerCase().includes("movie") || + ep.episodeTitle.includes("剧场版") + ) + ); + } else if (/^\d+$/.test(episode)) { + // 纯数字,仅保留指定集数 + const targetEpisode = parseInt(episode); + filteredEpisodes = bangumiData.bangumi.episodes.filter(ep => + parseInt(ep.episodeNumber) === targetEpisode + ); + } + } + + // 只有当过滤后还有集数时才添加到结果中 + if (filteredEpisodes.length > 0) { + resultAnimes.push({ + animeId: animeItem.animeId, + animeTitle: animeItem.animeTitle, + type: animeItem.type, + typeDescription: animeItem.typeDescription, + episodes: filteredEpisodes.map(ep => ({ + episodeId: ep.episodeId, + episodeTitle: ep.episodeTitle + })) + }); + } + } + } + + log("log", `Found ${resultAnimes.length} animes with filtered episodes`); + + return jsonResponse({ + errorCode: 0, + success: true, + errorMessage: "", + animes: resultAnimes + }); +} + // Extracted function for GET /api/v2/bangumi/:animeId async function getBangumi(path) { const animeId = parseInt(path.split("/").pop()); @@ -2786,6 +2875,11 @@ async function handleRequest(req, env) { return searchAnime(url); } + // GET /api/v2/search/episodes + if (path === "/api/v2/search/episodes" && method === "GET") { + return searchEpisodes(url); + } + // GET /api/v2/match if (path === "/api/v2/match" && method === "POST") { return matchAnime(url, req); @@ -2815,6 +2909,8 @@ async function handleRequest(req, env) { return jsonResponse({ message: "Not found" }, 404); } + + // --- Cloudflare Workers 入口 --- export default { async fetch(request, env, ctx) { @@ -2842,5 +2938,5 @@ export async function vercelHandler(req, res) { } // 为了测试导出 handleRequest -export { handleRequest, searchAnime, matchAnime, getBangumi, getComment, fetchTencentVideo, fetchIqiyi, fetchMangoTV, +export { handleRequest, searchAnime, searchEpisodes, matchAnime, getBangumi, getComment, fetchTencentVideo, fetchIqiyi, fetchMangoTV, fetchBilibili, fetchYouku, fetchOtherServer, httpGet, httpPost }; \ No newline at end of file