koishi-plugin-bns-rate 1.0.4 → 1.0.6
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 +28 -11
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -28,27 +28,43 @@ async function fetchRate(ctx, url) {
|
|
|
28
28
|
|
|
29
29
|
const html = typeof response === 'string' ? response : response.data
|
|
30
30
|
|
|
31
|
+
// 找到所有 "1元=XXX神石" 匹配
|
|
32
|
+
// 回收价格式: "1元=162.4神石 0.0062元/神石" (没有"1神石=")
|
|
33
|
+
// 卖家挂单格式: "1元=145.9873神石 1神石=0.0068元" (有"1神石=")
|
|
31
34
|
const forwardMatches = [...html.matchAll(FORWARD_RE)]
|
|
32
|
-
|
|
33
|
-
|
|
35
|
+
let bestSeller = null
|
|
36
|
+
for (const m of forwardMatches) {
|
|
37
|
+
const ctx = html.substring(Math.max(0, m.index - 50), m.index + 100)
|
|
38
|
+
if (ctx.includes('1神石=')) {
|
|
39
|
+
bestSeller = parseFloat(m[1])
|
|
40
|
+
break
|
|
41
|
+
}
|
|
34
42
|
}
|
|
35
43
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
44
|
+
if (bestSeller === null) {
|
|
45
|
+
throw new Error('无法解析页面汇率数据,页面结构可能已变更')
|
|
46
|
+
}
|
|
39
47
|
|
|
48
|
+
// 取对应的反向汇率
|
|
40
49
|
const reverseMatches = [...html.matchAll(REVERSE_RE)]
|
|
41
|
-
|
|
42
|
-
|
|
50
|
+
let bestReverse = null
|
|
51
|
+
for (const m of reverseMatches) {
|
|
52
|
+
const ctx = html.substring(Math.max(0, m.index - 50), m.index + 100)
|
|
53
|
+
if (!ctx.includes('/神石')) {
|
|
54
|
+
bestReverse = parseFloat(m[1])
|
|
55
|
+
break
|
|
56
|
+
}
|
|
43
57
|
}
|
|
44
58
|
|
|
45
|
-
|
|
59
|
+
if (bestReverse === null) {
|
|
60
|
+
throw new Error('无法解析页面反向汇率数据')
|
|
61
|
+
}
|
|
46
62
|
|
|
47
|
-
if (
|
|
48
|
-
throw new Error(`解析的汇率超出合理范围: forward=${
|
|
63
|
+
if (bestSeller < 100 || bestSeller > 200 || bestReverse < 0.001 || bestReverse > 0.02) {
|
|
64
|
+
throw new Error(`解析的汇率超出合理范围: forward=${bestSeller}, reverse=${bestReverse}`)
|
|
49
65
|
}
|
|
50
66
|
|
|
51
|
-
return { forward, reverse }
|
|
67
|
+
return { forward: bestSeller, reverse: bestReverse }
|
|
52
68
|
}
|
|
53
69
|
|
|
54
70
|
function formatMessage(forward, reverse, timestamp) {
|
|
@@ -56,6 +72,7 @@ function formatMessage(forward, reverse, timestamp) {
|
|
|
56
72
|
timeZone: 'Asia/Shanghai',
|
|
57
73
|
})
|
|
58
74
|
return [
|
|
75
|
+
'DD373当前时段神石比例如下:',
|
|
59
76
|
`1元 = ${forward}神石`,
|
|
60
77
|
`1神石 = ${reverse}元`,
|
|
61
78
|
`查询时间: ${time}`,
|