From 223a6e13c949a2e8ecba93485d5b10f08fe2c34a Mon Sep 17 00:00:00 2001 From: shanmite Date: Mon, 22 Aug 2022 20:58:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=8E=B7=E5=8F=96=E6=8E=A8=E8=8D=90412(?= =?UTF-8?q?#182)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/pkg.yml | 2 -- lib/net/api.bili.js | 1 + lib/net/bili.js | 43 ++++++++++++++++++++++++--------------- package.json | 1 + test/api.test.js | 8 ++++++++ test/index.js | 12 +++++++++++ 6 files changed, 49 insertions(+), 18 deletions(-) create mode 100644 test/api.test.js create mode 100644 test/index.js diff --git a/.github/workflows/pkg.yml b/.github/workflows/pkg.yml index 63cbc99..0f064aa 100644 --- a/.github/workflows/pkg.yml +++ b/.github/workflows/pkg.yml @@ -2,8 +2,6 @@ name: "Package Node.js project into an executable" on: push: - branches: - - main paths: - "lib/**" - "*.js" diff --git a/lib/net/api.bili.js b/lib/net/api.bili.js index 9f7e1a1..88da5dd 100644 --- a/lib/net/api.bili.js +++ b/lib/net/api.bili.js @@ -30,6 +30,7 @@ module.exports = Object.freeze({ SPACE_MYINFO: 'https://api.bilibili.com/x/space/myinfo', TAG_INFO: 'https://api.bilibili.com/x/tag/info', TOP_RCMD: "https://api.bilibili.com/x/web-interface/index/top/rcmd", + TOP_FEED_RCMD: "https://api.bilibili.com/x/web-interface/index/top/feed/rcmd", TOPIC_SVR_TOPIC_HISTORY: 'https://api.vc.bilibili.com/topic_svr/v1/topic_svr/topic_history', TOPIC_SVR_TOPIC_NEW: 'https://api.vc.bilibili.com/topic_svr/v1/topic_svr/topic_new', WEB_INTERFACE_CARD: 'https://api.bilibili.com/x/web-interface/card', diff --git a/lib/net/bili.js b/lib/net/bili.js index ef2d367..2dfaa13 100644 --- a/lib/net/bili.js +++ b/lib/net/bili.js @@ -888,27 +888,38 @@ const bili_client = { return true } }, + _getTopRcmd: new Line('获取推荐', [ + () => new Promise((resolve) => { + send({ + url: API.TOP_RCMD, + method: 'GET', + headers: { + "accept": 'application/json, text/plain, */*', + "cookie": GlobalVar.get("cookie") + ";buvid3=1" + }, + success: res => resolve(res.body), + failure: err => resolve(err) + }) + }), + () => get({ + url: API.TOP_FEED_RCMD + }) + ], responseText => { + const res = strToJson(responseText); + if (res.code === 0) { + return [false, res.data.item.map(it => { + return [it.owner.mid, it.id]; + }), "成功"]; + } else { + return [true, [], `获取推荐失败\n${responseText}`]; + } + }), /** * 获取推荐 * @returns {Promise>} */ async getTopRcmd() { - const - responseText = await get( - { - url: API.TOP_RCMD, - } - ), - res = strToJson(responseText); - if (res.code === 0) { - log.info('获取推荐', `成功`); - return res.data.item.map(it => { - return [it.owner.mid, it.id]; - }); - } else { - log.error('获取推荐', `获取推荐失败\n${responseText}`); - return []; - } + return this._getTopRcmd.run() }, /** * 分享视频 diff --git a/package.json b/package.json index e6da2a3..e6f6610 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "main": "main.js", "scripts": { "start": "node main.js start", + "test": "node test/index.js", "clear": "node main.js clear", "check": "node main.js check", "update": "node main.js update", diff --git a/test/api.test.js b/test/api.test.js new file mode 100644 index 0000000..caefdd4 --- /dev/null +++ b/test/api.test.js @@ -0,0 +1,8 @@ +const assert = require('assert'); +const bili_client = require("../lib/net/bili"); + +(async () => { + assert(await bili_client.getMyinfo()) + assert.equal((await bili_client.getTopRcmd()).length, 10) + console.log("api.test ... ok!"); +})() \ No newline at end of file diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..70a4460 --- /dev/null +++ b/test/index.js @@ -0,0 +1,12 @@ +const env = require("../lib/data/env"); +const global_var = require("../lib/data/global_var"); +const { log } = require('../lib/utils'); +const fs = require('fs'); + +log._level = 1 +env.init() +global_var.init(process.env["COOKIE"], 1) + +fs.readdirSync(module.path) + .filter(file => file.endsWith(".test.js")) + .forEach(file => require(`${module.path}/${file}`)) \ No newline at end of file