-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
209 lines (183 loc) · 4.92 KB
/
index.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
"use strict";
const fs = require('fs');
const request = require('request');
const config = require('./config/config');
const http = request.defaults({
//proxy: "http://127.0.0.1:8888", //for fiddler
jar: true,
headers: {
Accept: '*/*',
"User-Agent": 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36'
}
});
/**
* 读取股票代码配置
*/
function readStockCodeFile() {
return new Promise((resolve, reject) => {
fs.readFile(config.stockDataFile, (err, data) => {
if (err) {
reject(err);
} else {
resolve(data.toString());
}
})
})
}
/**
* 更新股票代码配置
*/
function writeStockCodeFile(data) {
return new Promise((resolve, reject) => {
fs.writeFile(config.stockDataFile, data, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
})
})
}
/**
* 访问雪球财经站点
*/
function visitStockSite() {
return new Promise((resolve, reject) => {
if (http.authed) {
return resolve();
}
http.get(config.stockAPI.site, (err, res, body) => {
if (err) {
reject(err);
} else {
http.authed = true;
resolve();
}
})
})
}
/**
* 根据股票名称/代码获取股票详情
*/
function fetchStockInfo(key) {
return visitStockSite()
.then(() => {
key = encodeURIComponent(key);
return new Promise((resolve, reject) => {
http.get(config.stockAPI.query.replace(/\{key\}/, key), (err, res, body) => {
if (err) {
reject(err);
} else {
resolve(body);
}
})
})
})
}
/**
* 根据股票代码获取股票状态信息,支持多个
*/
function fetchStockStatus(code, version) {
let isMulti = Array.isArray(code);
if (isMulti) {
code = code.join(',');
}
return visitStockSite()
.then(() => {
code = encodeURIComponent(code);
return new Promise((resolve, reject) => {
let url = version == 5 ? config.stockAPI.info_v5 : config.stockAPI.info;
http.get(url.replace(/\{code\}/, code), (err, res, body) => {
if (res.statusCode == 400) {
http.authed = false;
}
if (err) {
reject(err);
} else {
resolve(body);
}
})
})
})
}
exports.queryStockInfo = key => {
return fetchStockInfo(key)
.then(data => {
data = JSON.parse(data);
if (!data.stocks.length) {
return `无相关股票记录: ${key}`;
}
return data.stocks;
})
};
exports.queryStockStatus = code => {
code = code.toUpperCase();
return fetchStockStatus(code)
.then(data => {
data = JSON.parse(data);
if (data.error_code) {
return `无此股票代码: ${code}`
}
return Object.assign({}, data[code], {code: code});
})
};
exports.queryStockListStatus = () => {
return readStockCodeFile()
.then(data => {
let stockData = JSON.parse(data);
let codes = stockData.map(stock => stock.code);
return fetchStockStatus(codes)
.then(data => {
data = JSON.parse(data);
if (data.error_code) {
return [];
}
return Object.keys(data).map(code => Object.assign({}, data[code], {code: code}));
})
})
};
exports.queryStockListStatusV5 = () => {
return readStockCodeFile()
.then(data => {
let stockData = JSON.parse(data);
let codes = stockData.map(stock => stock.code);
return fetchStockStatus(codes, 5)
.then(data => {
data = JSON.parse(data);
if (data.error_code || !data.data || !data.data.items) {
return [];
}
return data.data.items.map(stock => Object.assign({}, stock.quote));
})
})
};
exports.addStock = code => {
return Promise.all([readStockCodeFile(), exports.queryStockInfo(code)])
.then(results => {
let stockData = JSON.parse(results[0]);
if (typeof results[1] === "string") {
return `无此股票代码: ${code}`
}
let stock = results[1].find(item => item.code.toLowerCase() === code.toLowerCase());
stockData = stockData.filter(item => item.code.toLowerCase() !== code.toLowerCase()).concat({
code: stock.code,
name: stock.name
});
return writeStockCodeFile(JSON.stringify(stockData))
.then(() => `添加股票代码 ${code} 成功`)
})
};
exports.removeStock = code => {
return readStockCodeFile()
.then(data => {
let stockData = JSON.parse(data);
let index = stockData.findIndex(item => code.toLowerCase() === item.code.toLowerCase());
if (index !== -1) {
stockData.splice(index, 1);
return writeStockCodeFile(JSON.stringify(stockData))
.then(() => `删除股票代码 ${code} 成功`)
} else {
return `列表中无此股票代码: ${code}`;
}
})
}