From cc9e3033232c2329136940d217ab4599c3908749 Mon Sep 17 00:00:00 2001 From: huangxd- <649924757@qq.com> Date: Sun, 19 Oct 2025 14:19:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=90=84=E6=BA=90=E5=86=85an?= =?UTF-8?q?ime=E6=A0=B9=E6=8D=AE=E5=B9=B4=E4=BB=BD=E9=99=8D=E5=BA=8F?= =?UTF-8?q?=E6=8E=92=E5=BA=8F=20(#64)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- danmu_api/worker.js | 72 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 65 insertions(+), 7 deletions(-) diff --git a/danmu_api/worker.js b/danmu_api/worker.js index cfccc76..79edca3 100644 --- a/danmu_api/worker.js +++ b/danmu_api/worker.js @@ -9,7 +9,7 @@ if (typeof process !== "undefined" && process.env && !process.env.CLOUDFLARE_ENV // 全局状态(Cloudflare 和 Vercel 都可能重用实例) // ⚠️ 不是持久化存储,每次冷启动会丢失 -const VERSION = "1.4.0"; +const VERSION = "1.4.1"; let animes = []; let episodeIds = []; let episodeNum = 10001; // 全局变量,用于自增 ID @@ -4498,7 +4498,43 @@ function matchSeason(anime, queryTitle, season) { } } +// 提取年份的辅助函数 +function extractYear(animeTitle) { + const match = animeTitle.match(/\((\d{4})\)/); + return match ? parseInt(match[1]) : null; +} + +// 按年份降序排序并添加到curAnimes +function sortAndPushAnimesByYear(processedAnimes, curAnimes) { + processedAnimes + .filter(anime => anime !== null) + .sort((a, b) => { + const yearA = extractYear(a.animeTitle); + const yearB = extractYear(b.animeTitle); + + // 如果都有年份,按年份降序排列 + if (yearA !== null && yearA !== undefined && yearB !== null && yearB !== undefined) { + return yearB - yearA; + } + // 如果只有a有年份,a排在前面 + if ((yearA !== null && yearA !== undefined) && (yearB === null || yearB === undefined)) { + return -1; + } + // 如果只有b有年份,b排在前面 + if ((yearA === null || yearA === undefined) && (yearB !== null && yearB !== undefined)) { + return 1; + } + // 如果都没有年份,保持原顺序 + return 0; + }) + .forEach(anime => { + curAnimes.push(anime); + }); +} + async function handleVodAnimes(animesVod, curAnimes, key) { + const tmpAnimes = []; + const processVodAnimes = await Promise.all(animesVod.map(async (anime) => { let vodPlayFromList = anime.vod_play_from.split("$$$"); vodPlayFromList = vodPlayFromList.map(item => { @@ -4542,16 +4578,20 @@ async function handleVodAnimes(animesVod, curAnimes, key) { isFavorited: true, }; - curAnimes.push(transformedAnime); + tmpAnimes.push(transformedAnime); addAnime({...transformedAnime, links: links}); if (animes.length > MAX_ANIMES) removeEarliestAnime(); } })); + sortAndPushAnimesByYear(tmpAnimes, curAnimes); + return processVodAnimes; } async function handle360Animes(animes360, curAnimes) { + const tmpAnimes = []; + const process360Animes = await Promise.all(animes360.map(async (anime) => { let links = []; if (anime.cat_name === "电影") { @@ -4606,16 +4646,20 @@ async function handle360Animes(animes360, curAnimes) { isFavorited: true, }; - curAnimes.push(transformedAnime); + tmpAnimes.push(transformedAnime); addAnime({...transformedAnime, links: links}); if (animes.length > MAX_ANIMES) removeEarliestAnime(); } })); + sortAndPushAnimesByYear(tmpAnimes, curAnimes); + return process360Animes; } async function handleRenrenAnimes(animesRenren, queryTitle, curAnimes) { + const tmpAnimes = []; + // 使用 map 和 async 时需要返回 Promise 数组,并等待所有 Promise 完成 const processRenrenAnimes = await Promise.all(animesRenren .filter(s => s.title.includes(queryTitle)) @@ -4644,7 +4688,7 @@ async function handleRenrenAnimes(animesRenren, queryTitle, curAnimes) { isFavorited: true, }; - curAnimes.push(transformedAnime); + tmpAnimes.push(transformedAnime); addAnime({...transformedAnime, links: links}); @@ -4653,6 +4697,8 @@ async function handleRenrenAnimes(animesRenren, queryTitle, curAnimes) { }) ); + sortAndPushAnimesByYear(tmpAnimes, curAnimes); + return processRenrenAnimes; } @@ -4663,6 +4709,8 @@ async function handleHanjutvAnimes(animesHanjutv, queryTitle, curAnimes) { return cateMap[key] || "其他"; } + const tmpAnimes = []; + // 使用 map 和 async 时需要返回 Promise 数组,并等待所有 Promise 完成 const processHanjutvAnimes = await Promise.all(animesHanjutv .filter(s => s.name.includes(queryTitle)) @@ -4693,7 +4741,7 @@ async function handleHanjutvAnimes(animesHanjutv, queryTitle, curAnimes) { isFavorited: true, }; - curAnimes.push(transformedAnime); + tmpAnimes.push(transformedAnime); addAnime({...transformedAnime, links: links}); @@ -4702,10 +4750,14 @@ async function handleHanjutvAnimes(animesHanjutv, queryTitle, curAnimes) { }) ); + sortAndPushAnimesByYear(tmpAnimes, curAnimes); + return processHanjutvAnimes; } async function handleBahamutAnimes(animesBahamut, queryTitle, curAnimes) { + const tmpAnimes = []; + // 使用 map 和 async 时需要返回 Promise 数组,并等待所有 Promise 完成 const processBahamutAnimes = await Promise.all(animesBahamut .filter(s => s.title.includes(queryTitle)) @@ -4737,7 +4789,7 @@ async function handleBahamutAnimes(animesBahamut, queryTitle, curAnimes) { isFavorited: true, }; - curAnimes.push(transformedAnime); + tmpAnimes.push(transformedAnime); addAnime({...transformedAnime, links: links}); @@ -4746,10 +4798,14 @@ async function handleBahamutAnimes(animesBahamut, queryTitle, curAnimes) { }) ); + sortAndPushAnimesByYear(tmpAnimes, curAnimes); + return processBahamutAnimes; } async function handleTencentAnimes(animesTencent, queryTitle, curAnimes) { + const tmpAnimes = []; + // 使用 map 和 async 时需要返回 Promise 数组,并等待所有 Promise 完成 const processTencentAnimes = await Promise.all(animesTencent .filter(s => s.title.includes(queryTitle)) @@ -4785,7 +4841,7 @@ async function handleTencentAnimes(animesTencent, queryTitle, curAnimes) { isFavorited: true, }; - curAnimes.push(transformedAnime); + tmpAnimes.push(transformedAnime); addAnime({...transformedAnime, links: links}); @@ -4794,6 +4850,8 @@ async function handleTencentAnimes(animesTencent, queryTitle, curAnimes) { }) ); + sortAndPushAnimesByYear(tmpAnimes, curAnimes); + return processTencentAnimes; }