diff --git a/routes/api/base.mjs b/routes/api/base.mjs index 2130a62..2a472fc 100644 --- a/routes/api/base.mjs +++ b/routes/api/base.mjs @@ -1,4 +1,5 @@ import { filesize } from "filesize"; +import pLimit from 'p-limit'; export default class BaseSource { // 构造函数,初始化通用配置 @@ -53,14 +54,14 @@ export default class BaseSource { async work(url) { const promises = await this.resolve(url); if (!this.error_msg) { - console.log(this.name, "API lens:", promises.length); this.memory(); //显示内存使用量 // 并发请求弹幕数据,等待所有请求完成。 let datas = (await Promise.allSettled(promises)) .filter(x => x.status === "fulfilled") - .map(x => x.value.data); + .map(x => x.value.data || x.value.body); //同时兼容 axios 和 got 的返回值结构 this.memory(); //显示内存使用量 - this.content = await this.parse(datas); + console.log(this.name, "API lens:", promises.length,"API fulfilled:", datas.length, "比例:", ((datas.length / promises.length) * 100).toFixed(2) + "%"); + this.content = await this.parse(datas); } return { title: this.title, diff --git a/routes/api/mgtv.mjs b/routes/api/mgtv.mjs index 8e78f93..09cb173 100644 --- a/routes/api/mgtv.mjs +++ b/routes/api/mgtv.mjs @@ -1,5 +1,5 @@ import urlmodule from "url"; -import axios from "axios"; +import got from "got"; import BaseSource from "./base.mjs"; @@ -21,19 +21,28 @@ export default class MgtvSource extends BaseSource { const path = q.pathname.split("/"); const cid = path.slice(-2)[0]; const vid = path.slice(-1)[0].split(".")[0]; - const res = await axios.get(api_video_info, {params: {cid, vid}}); - if (res.data.code !== 200) { - this.error_msg = this.name + " API: " + api_video_info + "请求失败,错误信息: " + res.data.msg; + const res = await got(api_video_info,{ + searchParams: {cid, vid}, + responseType: 'json' + }) + if (res.body.code !== 200) { + this.error_msg = this.name + " API: " + api_video_info + "请求失败,错误信息: " + res.body.msg; return null; } - this.title = res.data.data.info.videoName; - const time = res.data.data.info.time; + this.title = res.body.data.info.videoName; + const time = res.body.data.info.time; const step = 60 * 1000; const end_time = this.time_to_second(time) * 1000; let promises = []; for (let i = 0; i < end_time; i += step) { - promises.push(axios({method: "get", url: api_danmaku, params: {vid, cid, time: i}})); + promises.push( + got(api_danmaku, { + searchParams: { vid, cid, time: i }, + timeout: 10000, + responseType: 'json' + }) + ); } return promises; } diff --git a/routes/danmaku.mjs b/routes/danmaku.mjs index f406737..d3a5847 100644 --- a/routes/danmaku.mjs +++ b/routes/danmaku.mjs @@ -76,7 +76,7 @@ async function resolve(req, res) { res.redirect(ret.url); } else { console.log("标题:", ret.title, "弹幕数量:", ret.content.length); - res.set('Cache-Control', 'public, max-age=86400'); // 缓存一天 + // res.set('Cache-Control', 'public, max-age=86400'); // 缓存一天 res.render("danmaku-xml", { contents: ret.content }); } }