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.
Files changed (2) hide show
  1. package/index.js +28 -11
  2. 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
- if (forwardMatches.length === 0) {
33
- throw new Error('无法解析页面汇率数据,页面结构可能已变更')
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
- // 平台回收价区域已不再使用 '1元=X神石' 格式
38
- const forward = parseFloat(forwardMatches[0][1])
44
+ if (bestSeller === null) {
45
+ throw new Error('无法解析页面汇率数据,页面结构可能已变更')
46
+ }
39
47
 
48
+ // 取对应的反向汇率
40
49
  const reverseMatches = [...html.matchAll(REVERSE_RE)]
41
- if (reverseMatches.length === 0) {
42
- throw new Error('无法解析页面反向汇率数据')
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
- const reverse = parseFloat(reverseMatches[0][1])
59
+ if (bestReverse === null) {
60
+ throw new Error('无法解析页面反向汇率数据')
61
+ }
46
62
 
47
- if (forward < 100 || forward > 200 || reverse < 0.001 || reverse > 0.02) {
48
- throw new Error(`解析的汇率超出合理范围: forward=${forward}, reverse=${reverse}`)
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}`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-bns-rate",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "Fetch BNS Classic Divine Stone exchange rate from dd373.com",
5
5
  "main": "index.js",
6
6
  "license": "MIT",