From 9f8d5dc1f6bdcc6b6fcb2ae0799495e86c3a73f2 Mon Sep 17 00:00:00 2001 From: bietiaop <1527109126@qq.com> Date: Thu, 3 Oct 2024 13:12:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=85=91=E6=8D=A2=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/code.js | 27 +++++++++++ apps/help.js | 7 +++ defSet/priority.yaml | 3 +- lib/code.js | 107 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 apps/code.js create mode 100644 lib/code.js diff --git a/apps/code.js b/apps/code.js new file mode 100644 index 0000000..ef4973b --- /dev/null +++ b/apps/code.js @@ -0,0 +1,27 @@ +import { ZZZPlugin } from '../lib/plugin.js'; +import settings from '../lib/settings.js'; +import _ from 'lodash'; +import { rulePrefix } from '../lib/common.js'; +import { getCodeMsg } from '../lib/code.js'; + +export class Note extends ZZZPlugin { + constructor() { + super({ + name: '[ZZZ-Plugin]Code', + dsc: 'zzzcode', + event: 'message', + priority: _.get(settings.getConfig('priority'), 'code', 70), + rule: [ + { + reg: `${rulePrefix}(code|兑换码)$`, + fnc: 'code', + }, + ], + }); + } + async code() { + const msg = await getCodeMsg(); + await this.reply(msg); + return false; + } +} diff --git a/apps/help.js b/apps/help.js index 93c3d95..2779cd5 100644 --- a/apps/help.js +++ b/apps/help.js @@ -187,6 +187,13 @@ const helpData = [ title: '其他', icon: 'fire', items: [ + { + title: '兑换码', + desc: '获取前瞻兑换码', + needCK: false, + needSK: false, + commands: ['code', '兑换码'], + }, { title: '绑定设备', desc: '用于尝试解决10041报错等问题(无法100%解决),需要发送设备信息,具体方法请发送%绑定设备帮助查看', diff --git a/defSet/priority.yaml b/defSet/priority.yaml index 14fc08c..4f7b44f 100644 --- a/defSet/priority.yaml +++ b/defSet/priority.yaml @@ -8,4 +8,5 @@ note: 70 # 体力 panel: 70 # 面板 update: 70 # 更新 user: 70 # 账号操作 -monthly: 70 # 菲林月历 \ No newline at end of file +monthly: 70 # 菲林月历 +code: 70 # 兑换码 \ No newline at end of file diff --git a/lib/code.js b/lib/code.js new file mode 100644 index 0000000..266b791 --- /dev/null +++ b/lib/code.js @@ -0,0 +1,107 @@ +/** + * From ZZZeroUID + */ + +import request from '../utils/request.js'; + +const TZ_OFFSET = 8 * 60 * 60 * 1000; + +async function getData(type, data = {}) { + const urls = { + activity: + 'https://bbs-api.miyoushe.com/apihub/api/home/new?gids=8&parts=1%2C3%2C4&device=OnePlus%20IN2025&cpu=placeholder&version=3', + index: 'https://api-takumi.mihoyo.com/event/miyolive/index', + code: 'https://api-takumi-static.mihoyo.com/event/miyolive/refreshCode', + }; + + try { + const headers = { 'x-rpc-act_id': data.actId || '' }; + const params = new URLSearchParams({ + version: data.version || '', + time: Math.floor(Date.now() / 1000).toString(), + }); + + let res; + if (type === 'index') { + res = await request(urls[type], { headers }); + } else if (type === 'code') { + res = await request(`${urls[type]}?${params}`, { headers }); + } else { + res = await request(urls[type]); + } + + return await res.json(); + } catch (e) { + return { error: `[${e.name}] ${type} 接口请求错误` }; + } +} + +async function getActId() { + const ret = await getData('activity'); + if (ret.error || ret.retcode !== 0) return ''; + + const keywords = ['前瞻直播']; + for (const nav of ret.data.navigator) { + const name = nav.name; + if (name && keywords.every(word => name.includes(word))) { + const matched = nav.app_path.match(/act_id=(.*?)&/); + if (matched) return matched[1]; + } + } + return ''; +} + +async function getLiveData(actId) { + const ret = await getData('index', { actId }); + if (ret.error || ret.retcode !== 0) { + return { error: ret.error || '前瞻直播数据异常' }; + } + + const liveRaw = ret.data.live; + const liveTemp = JSON.parse(ret.data.template); + const liveData = { + code_ver: liveRaw.code_ver, + title: liveRaw.title.replace('特别直播', ''), + header: liveTemp.kvDesktop, + room: liveTemp.liveConfig[0].desktop, + }; + + if (liveRaw.is_end) { + liveData.review = liveTemp.reviewUrl.args.post_id; + } else { + const now = new Date(Date.now() + TZ_OFFSET); + const start = new Date(liveRaw.start + ' GMT+0800'); + if (now < start) { + liveData.start = liveRaw.start; + } + } + return liveData; +} + +async function getCode(version, actId) { + const ret = await getData('code', { version, actId }); + if (ret.error || ret.retcode !== 0) { + return { error: ret.error || '兑换码数据异常' }; + } + + return ret.data.code_list.map(codeInfo => ({ + items: codeInfo.title.replace(/<.*?>/g, ''), + code: codeInfo.code, + })); +} + +export async function getCodeMsg() { + const actId = await getActId(); + if (!actId) return '暂无前瞻直播资讯'; + + const liveData = await getLiveData(actId); + if (liveData.error) return `获取兑换码失败:\n${liveData.error}`; + + const codeData = await getCode(liveData.code_ver, actId); + if (Array.isArray(codeData) && codeData.length > 0) { + const code = codeData[0]; + return `${liveData.title}\n${code.items}:\n${code.code}`; + } + + return '获取兑换码失败'; +}