koishi-plugin-bns-rate 1.2.7 → 1.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +56 -4
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
const { Schema } = require('koishi')
|
|
2
2
|
|
|
3
|
+
// 当前加载的插件版本(用于 bnsrate.diag 自检:一眼看出 Koishi 到底在跑哪个版本)
|
|
4
|
+
let PLUGIN_VERSION = '未知'
|
|
5
|
+
try {
|
|
6
|
+
PLUGIN_VERSION = require('./package.json').version
|
|
7
|
+
} catch (e) { /* 极端情况下 package.json 取不到,不影响功能 */ }
|
|
8
|
+
|
|
3
9
|
const DEFAULT_URL = 'https://www.dd373.com/s-d5gqt8-0-0-0-0-0-0-0-0-0-0-0-1-0-5-0.html'
|
|
4
10
|
const DEFAULT_BUTTON_URL = 'https://www.dd373.com/s-d5gqt8.html'
|
|
5
11
|
|
|
@@ -100,8 +106,8 @@ function looksLikeWafChallenge(html) {
|
|
|
100
106
|
return /aliyun-captcha|_waf_is_mobile|slideStyle|captcha-element/.test(s)
|
|
101
107
|
}
|
|
102
108
|
|
|
103
|
-
|
|
104
|
-
|
|
109
|
+
// 抓页面原始 HTML(含对抗 dd373 WAF 所需的请求头)
|
|
110
|
+
async function fetchHtml(ctx, url) {
|
|
105
111
|
const response = await ctx.http.get(url, {
|
|
106
112
|
responseType: 'text',
|
|
107
113
|
headers: {
|
|
@@ -113,9 +119,12 @@ async function fetchRates(ctx, url, count) {
|
|
|
113
119
|
'Referer': DEFAULT_BUTTON_URL,
|
|
114
120
|
},
|
|
115
121
|
})
|
|
122
|
+
return typeof response === 'string' ? response : response.data
|
|
123
|
+
}
|
|
116
124
|
|
|
117
|
-
|
|
118
|
-
|
|
125
|
+
// 从 HTML 里解析汇率(可能返回空数组,交给调用方判断原因)
|
|
126
|
+
function parseRates(html, count) {
|
|
127
|
+
const limit = Math.max(1, Math.min(10, Number(count) || 5))
|
|
119
128
|
// 找到所有 "1元=XXX神石" 匹配
|
|
120
129
|
// 回收价格式: "1元=162.4神石 0.0062元/神石" (没有"1神石=")
|
|
121
130
|
// 卖家挂单格式: "1元=145.9873神石 1神石=0.0068元" (有"1神石=")
|
|
@@ -132,6 +141,12 @@ async function fetchRates(ctx, url, count) {
|
|
|
132
141
|
if (rates.length >= limit) break
|
|
133
142
|
}
|
|
134
143
|
}
|
|
144
|
+
return rates
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async function fetchRates(ctx, url, count) {
|
|
148
|
+
const html = await fetchHtml(ctx, url)
|
|
149
|
+
const rates = parseRates(html, count)
|
|
135
150
|
|
|
136
151
|
if (rates.length === 0) {
|
|
137
152
|
// 区分「被反爬拦截」和「页面真的改版了」——两者的排查方向完全不同
|
|
@@ -439,6 +454,39 @@ function apply(ctx, config) {
|
|
|
439
454
|
return formatText(data.rates, data.timestamp, { stale: data.stale, error: data.error })
|
|
440
455
|
})
|
|
441
456
|
|
|
457
|
+
// 自检:把「版本 / 配置 / 真实抓取结果」原样报出来,排查环境差异(不走缓存、不依赖卡片)
|
|
458
|
+
ctx.command('bnsrate.diag', '自检:报告插件版本、配置与实际抓取结果(排查用,纯文本输出)')
|
|
459
|
+
.action(async ({ session }) => {
|
|
460
|
+
const lines = [`bnsrate 自检 · 插件 v${PLUGIN_VERSION}`]
|
|
461
|
+
lines.push(`配置: count=${config.count} cache=${config.cacheSeconds}s ratesBox=${config.ratesBox} markdown=${config.markdown}`)
|
|
462
|
+
lines.push(`URL: ${config.url}`)
|
|
463
|
+
if (session) {
|
|
464
|
+
lines.push(`平台: session=${session.platform || '(无)'} bot=${(session.bot && session.bot.platform) || '(无)'} 支持卡片=${supportsCard(session)}`)
|
|
465
|
+
} else {
|
|
466
|
+
lines.push('平台: (无 session,从控制台执行)')
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
const started = Date.now()
|
|
470
|
+
try {
|
|
471
|
+
const html = await fetchHtml(ctx, config.url)
|
|
472
|
+
const rates = parseRates(html, config.count)
|
|
473
|
+
lines.push(`抓取: ✅ 成功 · ${html.length} 字节 · ${Date.now() - started}ms`)
|
|
474
|
+
lines.push(`反爬: ${looksLikeWafChallenge(html) ? '❌ 收到阿里云 WAF 验证页' : '✅ 未检测到验证页'}`)
|
|
475
|
+
lines.push(`解析: ${rates.length ? `✅ ${rates.length} 条 · 首条 ${JSON.stringify(rates[0])}` : '❌ 0 条(页面里没有可解析的汇率)'}`)
|
|
476
|
+
if (!rates.length) {
|
|
477
|
+
const snippet = String(html).replace(/\s+/g, ' ').slice(0, 120)
|
|
478
|
+
lines.push(`页面开头: ${snippet}`)
|
|
479
|
+
}
|
|
480
|
+
} catch (e) {
|
|
481
|
+
const message = e instanceof Error ? e.message : String(e)
|
|
482
|
+
lines.push(`抓取: ❌ 失败 · ${message}`)
|
|
483
|
+
if (e && e.response) {
|
|
484
|
+
lines.push(`HTTP: ${e.response.status || '(无)'} ${e.response.statusText || ''}`)
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
return lines.join('\n')
|
|
488
|
+
})
|
|
489
|
+
|
|
442
490
|
// 调试用:只看卡片源码,不发送卡片(在控制台执行时也能看)
|
|
443
491
|
ctx.command('bnsrate.preview', '预览神石 Markdown 卡片源码(不发送卡片)')
|
|
444
492
|
.action(async () => {
|
|
@@ -485,6 +533,10 @@ module.exports = {
|
|
|
485
533
|
apply,
|
|
486
534
|
_test: {
|
|
487
535
|
fetchRates,
|
|
536
|
+
fetchHtml,
|
|
537
|
+
parseRates,
|
|
538
|
+
looksLikeWafChallenge,
|
|
539
|
+
PLUGIN_VERSION,
|
|
488
540
|
formatText,
|
|
489
541
|
buildCardMarkdown,
|
|
490
542
|
buildCardPayload,
|