koishi-plugin-bns-rate 1.0.8 → 1.1.0

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 +26 -26
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -4,7 +4,6 @@ const DEFAULT_URL = 'https://www.dd373.com/s-d5gqt8-0-0-0-0-0-0-0-0-0-0-0-1-0-5-
4
4
 
5
5
  const FORWARD_RE = /1元\s*=\s*(\d+\.?\d*)神石/g
6
6
 
7
-
8
7
  const Config = Schema.object({
9
8
  url: Schema.string()
10
9
  .default(DEFAULT_URL)
@@ -14,9 +13,14 @@ const Config = Schema.object({
14
13
  .min(10)
15
14
  .max(300)
16
15
  .description('Cache TTL in seconds'),
16
+ count: Schema.number()
17
+ .default(5)
18
+ .min(1)
19
+ .max(10)
20
+ .description('显示的条目数量'),
17
21
  })
18
22
 
19
- async function fetchRate(ctx, url) {
23
+ async function fetchRates(ctx, url, count) {
20
24
  const response = await ctx.http.get(url, {
21
25
  responseType: 'text',
22
26
  headers: {
@@ -32,39 +36,35 @@ async function fetchRate(ctx, url) {
32
36
  // 回收价格式: "1元=162.4神石 0.0062元/神石" (没有"1神石=")
33
37
  // 卖家挂单格式: "1元=145.9873神石 1神石=0.0068元" (有"1神石=")
34
38
  const forwardMatches = [...html.matchAll(FORWARD_RE)]
35
- let bestSeller = null
39
+ const rates = []
36
40
  for (const m of forwardMatches) {
37
41
  const ctx = html.substring(Math.max(0, m.index - 50), m.index + 100)
38
42
  if (ctx.includes('1神石=')) {
39
- bestSeller = parseFloat(m[1])
40
- break
43
+ const forward = parseFloat(m[1])
44
+ const reverse = Math.round((1 / forward) * 10000) / 10000
45
+ rates.push({ forward, reverse })
46
+ if (rates.length >= count) break
41
47
  }
42
48
  }
43
49
 
44
- if (bestSeller === null) {
50
+ if (rates.length === 0) {
45
51
  throw new Error('无法解析页面汇率数据,页面结构可能已变更')
46
52
  }
47
53
 
48
- // 反向汇率直接用正向算,更准: 1神石 = 1/forward 元
49
- const bestReverse = Math.round((1 / bestSeller) * 10000) / 10000
50
-
51
- if (bestSeller < 100 || bestSeller > 200) {
52
- throw new Error(`解析的汇率超出合理范围: forward=${bestSeller}`)
53
- }
54
-
55
- return { forward: bestSeller, reverse: bestReverse }
54
+ return rates
56
55
  }
57
56
 
58
- function formatMessage(forward, reverse, timestamp) {
57
+ function formatMessage(rates, timestamp) {
59
58
  const time = new Date(timestamp).toLocaleString('zh-CN', {
60
59
  timeZone: 'Asia/Shanghai',
61
60
  })
62
- return [
63
- 'DD373当前时段神石比例如下:',
64
- `1元 = ${forward}神石`,
65
- `1神石 = ${reverse}元`,
66
- `查询时间: ${time}`,
67
- ].join('\n')
61
+ const lines = [`DD373当前时段最低神石比例前${rates.length}条如下:`]
62
+ for (const r of rates) {
63
+ lines.push(`1元 = ${r.forward}神石 1神石 = ${r.reverse}元`)
64
+ }
65
+ lines.push(`查询时间: ${time}`)
66
+ lines.push('详情: https://www.dd373.com/s-d5gqt8.html')
67
+ return lines.join('\n')
68
68
  }
69
69
 
70
70
  function apply(ctx, config) {
@@ -77,18 +77,18 @@ function apply(ctx, config) {
77
77
  const ttl = config.cacheSeconds * 1000
78
78
 
79
79
  if (cache && Date.now() - cache.timestamp < ttl) {
80
- return formatMessage(cache.forwardRate, cache.reverseRate, cache.timestamp)
80
+ return formatMessage(cache.rates, cache.timestamp)
81
81
  }
82
82
 
83
83
  try {
84
- const { forward, reverse } = await fetchRate(ctx, config.url)
85
- cache = { forwardRate: forward, reverseRate: reverse, timestamp: Date.now() }
86
- return formatMessage(forward, reverse, cache.timestamp)
84
+ const rates = await fetchRates(ctx, config.url, config.count)
85
+ cache = { rates, timestamp: Date.now() }
86
+ return formatMessage(rates, cache.timestamp)
87
87
  } catch (e) {
88
88
  const message = e instanceof Error ? e.message : String(e)
89
89
 
90
90
  if (cache) {
91
- return formatMessage(cache.forwardRate, cache.reverseRate, cache.timestamp)
91
+ return formatMessage(cache.rates, cache.timestamp)
92
92
  + '\n[警告] 汇率数据已过期,刷新失败: ' + message
93
93
  }
94
94
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-bns-rate",
3
- "version": "1.0.8",
3
+ "version": "1.1.0",
4
4
  "description": "Fetch BNS Classic Divine Stone exchange rate from dd373.com",
5
5
  "main": "index.js",
6
6
  "license": "MIT",