-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathservice.js
230 lines (204 loc) · 5.22 KB
/
service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// http://fund.eastmoney.com/js/fundcode_search.js
// http://fund.eastmoney.com/js/jjjz_gs.js
// http://fundgz.1234567.com.cn/js/001186.js
const cheerio = require('cheerio')
const axios = require('axios')
const { getCenter } = require('./util')
const fundcodeSearch = require('./fundcode-search')
// const jjjzGs = require('./jjjz-gs')
const fundcodeNameMap = new Map(fundcodeSearch.map(item => [item[2], item]))
const fundcodeNameList = fundcodeSearch.map(item => item[2])
// cache
const fundcodeCodeMap = new Map()
const chicangMap = new Map()
const USER_AGENT =
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36'
async function getStockCodesNew(code) {
try {
let { data } = await axios({
url: `http://fund.eastmoney.com/pingzhongdata/${code}.js`,
timeout: 5000,
headers: {
'user-agent': USER_AGENT
}
})
if (!data) {
return ''
}
return JSON.parse(
getCenter(data, 'var stockCodesNew =', ';/*基金持仓债券代码')
).join(',')
} catch (e) {
console.error('[fund]', e)
return ''
}
}
async function getChiCangNew(code) {
try {
let { data } = await axios({
url: `https://push2.eastmoney.com/api/qt/ulist.np/get?cb=jQuery18309362619702541979_1613979058115&fltt=2&invt=2&&fields=f3,f12,f14&secids=${await getStockCodesNew(
code
)}`,
timeout: 5000,
headers: {
'user-agent': USER_AGENT
}
})
if (!data) {
return []
}
return JSON.parse(
getCenter(data, 'jQuery18309362619702541979_1613979058115(', ');')
).data.diff
} catch (e) {
console.error('[fund]', e)
return []
}
}
async function getChiCangHtml(code) {
try {
let { data } = await axios({
url: `http://fund.eastmoney.com/${code}.html`,
timeout: 5000,
headers: {
'user-agent': USER_AGENT
}
})
if (!data) {
return []
}
const $ = cheerio.load(data)
const list = []
$('#position_shares table')
.find('tr')
.each((index, item) => {
const temp = []
$(item)
.find('td:lt(3)')
.each((index2, item2) => {
temp.push($(item2).text().trim().replace(/%|\s+/g, ''))
})
list.push(temp)
})
return list.slice(1)
} catch (e) {
console.error('[fund]', e)
return []
}
}
async function getChiCang(code) {
try {
const cache = chicangMap.get(code)
if (cache && Date.now() - cache.time < 1000 * 60) {
return cache.data
}
let [htmlData, newData] = await Promise.all([
getChiCangHtml(code),
getChiCangNew(code)
])
if (!htmlData.length || !newData.length) {
return ''
}
htmlData = [
'股票名称 持仓占比 涨跌幅',
...htmlData.map(
(item, index) =>
`${item[0]}(${newData[index].f12}) ${item[1]} ${newData[index].f3}`
)
]
const data = '\n\n' + htmlData.join('\n')
chicangMap.set(code, { time: Date.now(), data })
// const $ = cheerio.load(data)
// data = '\n\n' + getChiCangTable($, '#position_shares table')
// chicang_map.set(code, { time: Date.now(), data })
return data
} catch (e) {
console.error('[fund]', e)
return ''
}
}
async function getDetail(code) {
try {
const cache = fundcodeCodeMap.get(code)
if (cache && Date.now() - cache.time < 1000 * 60) {
return cache.data
}
let { data } = await axios({
url: `http://fundgz.1234567.com.cn/js/${code}.js`,
timeout: 5000,
headers: {
'user-agent': USER_AGENT
}
})
data = getCenter(data, 'jsonpgz(', ');')
if (!data) {
return null
}
data = JSON.parse(data)
if (new Date() - new Date(data.jzrq) > 1000 * 60 * 60 * 24 * 14) {
return null
}
fundcodeCodeMap.set(code, { time: Date.now(), data })
return data
} catch (e) {
console.error('[fund]', e)
return null
}
}
async function findFund(codeOrName) {
codeOrName = codeOrName.toUpperCase()
if (!codeOrName) {
return [
'[指令]',
'基金/JJ 代码/名称',
'',
'[示例]',
'基金 161725',
'JJ 白酒'
].join('\n')
}
const list = (/^\d{6}$/.test(codeOrName)
? [await getDetail(codeOrName)]
: await Promise.all(
fundcodeNameList
.filter(name => name.includes(codeOrName))
.map(name => fundcodeNameMap.get(name)[0])
.slice(0, 9)
.map(code => getDetail(code))
)
)
.filter(res => res)
.sort((a, b) => b.gszzl - a.gszzl)
const foot =
list.length === 9
? '\n(为了避免刷屏, 最多查询 9 条, 建议搜索词精确一些)'
: ''
let chichang = ''
if (list.length === 1) {
chichang = await getChiCang(list[0].fundcode)
}
// ${res.gszzl > 0 ? '📈' : res.gszzl === 0 ? '🔨' : '📉'}
return (
list
.map(
res =>
`${res.fundcode} ${res.gszzl > 0 ? '+' : ''}${res.gszzl} ${res.name}`
)
.join('\n') +
foot +
chichang || '未找到该赌场'
)
}
async function getFund(message) {
return [
{
type: 'text',
data: {
text: await findFund(message)
}
}
]
}
module.exports = {
getFund
}