From 6b0971170eb598de3164d1d92d0d7c115ac6a47f Mon Sep 17 00:00:00 2001 From: shanmiteko Date: Thu, 4 Nov 2021 22:46:26 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=8E=B7=E5=8F=96=E5=92=8C=E5=82=A8?= =?UTF-8?q?=E5=AD=98=E6=95=B4=E7=90=86=E5=A5=BD=E7=9A=84=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E4=BF=A1=E6=81=AF(#64)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 具体要求见config.example.js --- lib/core/monitor.js | 28 ++++++++++++++++++++++++++-- lib/core/searcher.js | 36 ++++++++++++++++++++++++++++++++++++ lib/data/config.js | 28 ++++++++++++++++++++++++++++ lib/data/global_var.js | 5 +++-- lib/net/http.js | 3 ++- my_config.example.js | 28 ++++++++++++++++++++++++++++ 6 files changed, 123 insertions(+), 5 deletions(-) diff --git a/lib/core/monitor.js b/lib/core/monitor.js index 3564b02..9c53a4c 100644 --- a/lib/core/monitor.js +++ b/lib/core/monitor.js @@ -1,4 +1,5 @@ const { log, hasEnv, shuffle, getRandomOne, delay, try_for_each } = require('../utils'); +const { send } = require('../net/http'); const bili = require('../net/bili'); const { sendNotify } = require('../helper/notify'); const event_bus = require('../helper/event_bus'); @@ -24,6 +25,7 @@ class Monitor extends Searcher { ['UIDs', this.getLotteryInfoByUID.bind(this)], ['TAGs', this.getLotteryInfoByTag.bind(this)], ['Articles', this.getLotteryInfoByArticle.bind(this)], + ['APIs', this.getLotteryInfoByAPI.bind(this)] ]); } /** @@ -172,7 +174,7 @@ class Monitor extends Searcher { async filterLotteryInfo() { const { lottery_param, LotteryInfoMap, attentionList } = this; /** - * @type {import("./searcher").LotteryInfo} + * @type {import("./searcher").LotteryInfo[]} */ let protoLotteryInfo = await LotteryInfoMap.get(lottery_param[0])(lottery_param[1]); @@ -186,7 +188,7 @@ class Monitor extends Searcher { /** 所有抽奖信息 */ let alllotteryinfo = []; const - { key_words, model, chatmodel, max_create_time, is_imitator, only_followed, at_users, blockword, blacklist } = config, + { set_lottery_info_url, key_words, model, chatmodel, max_create_time, is_imitator, only_followed, at_users, blockword, blacklist } = config, now_ts = Date.now() / 1000; /** @@ -203,6 +205,28 @@ class Monitor extends Searcher { return true }); + if (lottery_param[0] !== "APIs" && set_lottery_info_url) { + log.info("上传抽奖信息", "开始") + await new Promise((resolve) => { + send({ + url: set_lottery_info_url, + method: "POST", + headers: { + "content-type": "application/json" + }, + contents: protoLotteryInfo, + success: ({ body }) => { + log.info("发送获取到的动态数据", body) + resolve() + }, + failure: err => { + log.error("发送获取到的动态数据", err) + resolve() + } + }) + }) + } + /* 检查动态是否满足要求 */ await try_for_each(protoLotteryInfo, async function ({ lottery_info_type, is_liked, diff --git a/lib/core/searcher.js b/lib/core/searcher.js index 0060713..2b5ee2c 100644 --- a/lib/core/searcher.js +++ b/lib/core/searcher.js @@ -1,5 +1,6 @@ const utils = require('../utils'); const bili = require('../net/bili'); +const { send } = require("../net/http"); const config = require("../data/config"); const { log } = utils @@ -406,6 +407,41 @@ class Searcher { return fomatdata } + + /** + * 从特定格式的api响应数据中获取抽奖信息 + * @param {string} api + * @returns {Promise} + */ + getLotteryInfoByAPI(api) { + return new Promise((resolve) => { + if (api) { + log.info('获取动态', `开始获取链接(${api})中的抽奖信息`) + send({ + url: api, + config: { + redirect: true + }, + method: 'GET', + success: ({ body }) => { + if (body.err_msg) { + log.error("从API响应数据中获取抽奖信息", body.err_msg) + resolve(null) + } else { + resolve(JSON.parse(body).lottery_info) + } + }, + failure: err => { + log.error("从API响应数据中获取抽奖信息", err) + resolve(null) + } + }) + } else { + log.warn('获取动态', `链接为空`) + resolve(null) + } + }); + } } diff --git a/lib/data/config.js b/lib/data/config.js index 90b5688..b63f891 100644 --- a/lib/data/config.js +++ b/lib/data/config.js @@ -16,6 +16,34 @@ const config = { */ TAGs: [], + /** + * 从API接口中获取抽奖信息 + * @typedef {object} LotteryInfo + * @property {string} lottery_info_type + * @property {number} create_time + * @property {boolean} is_liked + * @property {number[]} uids `[uid,ouid]` + * @property {string} uname + * @property {Array<{}>} ctrl + * @property {string} dyid + * @property {string} rid + * @property {string} des + * @property {number} type + * @property {boolean} hasOfficialLottery 是否官方 + * @typedef RespondBody + * @property {string} err_msg 错误信息 + * @property {LotteryInfo[]} lottery_info + * API传回数据类型 {RespondBody} + * 获取抽奖信息的链接字符串 + */ + APIs: [], + + /** + * API发送数据类型 {LotteryInfo[]} + * 上传抽奖信息的链接字符串 + */ + set_lottery_info_url: "", + /** * 动态中的关键词(表示须同时满足以下条件) * 符合js正则表达式的字符串 diff --git a/lib/data/global_var.js b/lib/data/global_var.js index f7f6d1f..a5e332d 100644 --- a/lib/data/global_var.js +++ b/lib/data/global_var.js @@ -28,11 +28,12 @@ let global_var = { this.set(key_map.get(_item[0]), _item[1]); }); - const { UIDs = [], TAGs = [], Articles = [] } = config; + const { UIDs = [], TAGs = [], Articles = [], APIs = [] } = config; this.set('Lottery', [ ...UIDs.map(it => ['UIDs', it]), ...TAGs.map(it => ['TAGs', it]), - ...Articles.map(it => ['Articles', it]) + ...Articles.map(it => ['Articles', it]), + ...APIs.map(it => ['APIs', it]) ]); this.set('remoteconfig', await getRemoteConfig()); } diff --git a/lib/net/http.js b/lib/net/http.js index 1dd2a0b..ae2d2a4 100644 --- a/lib/net/http.js +++ b/lib/net/http.js @@ -69,7 +69,8 @@ function send(detail) { let options = { timeout, method: method.toUpperCase(), - host: thisURL.host, + hostname: thisURL.hostname, + port: thisURL.port, path: thisURL.pathname + thisURL.search + thisURL.hash, headers, }; diff --git a/my_config.example.js b/my_config.example.js index 702b02f..92e8033 100644 --- a/my_config.example.js +++ b/my_config.example.js @@ -28,6 +28,34 @@ module.exports = Object.freeze({ '抽奖', ], + /** + * 从API接口中获取抽奖信息 + * @typedef {object} LotteryInfo + * @property {string} lottery_info_type + * @property {number} create_time + * @property {boolean} is_liked + * @property {number[]} uids `[uid,ouid]` + * @property {string} uname + * @property {Array<{}>} ctrl + * @property {string} dyid + * @property {string} rid + * @property {string} des + * @property {number} type + * @property {boolean} hasOfficialLottery 是否官方 + * @typedef RespondBody + * @property {string} err_msg 错误信息 + * @property {LotteryInfo[]} lottery_info + * API传回数据类型 {RespondBody} + * 获取抽奖信息的链接字符串 + */ + APIs: [], + + /** + * API发送数据类型 {LotteryInfo[]} + * 上传抽奖信息的链接字符串 + */ + set_lottery_info_url: "", + /** * 动态中的关键词(表示须同时满足以下条件) * 符合js正则表达式的字符串