koishi-plugin-bns-rate 1.2.6 → 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 +69 -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
|
|
|
@@ -94,19 +100,31 @@ const Config = Schema.object({
|
|
|
94
100
|
|
|
95
101
|
// ===== 抓取与解析 =====
|
|
96
102
|
|
|
97
|
-
|
|
98
|
-
|
|
103
|
+
// dd373 的阿里云 WAF 挑战页特征(响应里是滑块验证组件,没有任何商品数据)
|
|
104
|
+
function looksLikeWafChallenge(html) {
|
|
105
|
+
const s = String(html || '')
|
|
106
|
+
return /aliyun-captcha|_waf_is_mobile|slideStyle|captcha-element/.test(s)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// 抓页面原始 HTML(含对抗 dd373 WAF 所需的请求头)
|
|
110
|
+
async function fetchHtml(ctx, url) {
|
|
99
111
|
const response = await ctx.http.get(url, {
|
|
100
112
|
responseType: 'text',
|
|
101
113
|
headers: {
|
|
102
114
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
|
103
115
|
'Accept': 'text/html,application/xhtml+xml',
|
|
104
116
|
'Accept-Language': 'zh-CN,zh;q=0.9',
|
|
117
|
+
// ⚠️ dd373 的阿里云 WAF 会拦截「没有 Referer」的请求,直接返回滑块验证页(约 12KB,无任何商品数据)
|
|
118
|
+
// 实测结论:只有 Referer 是必需项,Cookie 无关(连伪造的 acw_tc 都能过)。缺了它整站必然解析失败
|
|
119
|
+
'Referer': DEFAULT_BUTTON_URL,
|
|
105
120
|
},
|
|
106
121
|
})
|
|
122
|
+
return typeof response === 'string' ? response : response.data
|
|
123
|
+
}
|
|
107
124
|
|
|
108
|
-
|
|
109
|
-
|
|
125
|
+
// 从 HTML 里解析汇率(可能返回空数组,交给调用方判断原因)
|
|
126
|
+
function parseRates(html, count) {
|
|
127
|
+
const limit = Math.max(1, Math.min(10, Number(count) || 5))
|
|
110
128
|
// 找到所有 "1元=XXX神石" 匹配
|
|
111
129
|
// 回收价格式: "1元=162.4神石 0.0062元/神石" (没有"1神石=")
|
|
112
130
|
// 卖家挂单格式: "1元=145.9873神石 1神石=0.0068元" (有"1神石=")
|
|
@@ -123,8 +141,18 @@ async function fetchRates(ctx, url, count) {
|
|
|
123
141
|
if (rates.length >= limit) break
|
|
124
142
|
}
|
|
125
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)
|
|
126
150
|
|
|
127
151
|
if (rates.length === 0) {
|
|
152
|
+
// 区分「被反爬拦截」和「页面真的改版了」——两者的排查方向完全不同
|
|
153
|
+
if (looksLikeWafChallenge(html)) {
|
|
154
|
+
throw new Error('请求被 dd373 的反爬验证页拦截(阿里云 WAF,返回了滑块验证页)。通常是请求过于频繁或缺少 Referer')
|
|
155
|
+
}
|
|
128
156
|
throw new Error('无法解析页面汇率数据,页面结构可能已变更')
|
|
129
157
|
}
|
|
130
158
|
|
|
@@ -426,6 +454,39 @@ function apply(ctx, config) {
|
|
|
426
454
|
return formatText(data.rates, data.timestamp, { stale: data.stale, error: data.error })
|
|
427
455
|
})
|
|
428
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
|
+
|
|
429
490
|
// 调试用:只看卡片源码,不发送卡片(在控制台执行时也能看)
|
|
430
491
|
ctx.command('bnsrate.preview', '预览神石 Markdown 卡片源码(不发送卡片)')
|
|
431
492
|
.action(async () => {
|
|
@@ -472,6 +533,10 @@ module.exports = {
|
|
|
472
533
|
apply,
|
|
473
534
|
_test: {
|
|
474
535
|
fetchRates,
|
|
536
|
+
fetchHtml,
|
|
537
|
+
parseRates,
|
|
538
|
+
looksLikeWafChallenge,
|
|
539
|
+
PLUGIN_VERSION,
|
|
475
540
|
formatText,
|
|
476
541
|
buildCardMarkdown,
|
|
477
542
|
buildCardPayload,
|