daxiapi-cli 2.0.1 → 2.0.2
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/commands/secid.js +8 -18
- package/lib/api.js +0 -6
- package/lib/utils.js +31 -0
- package/package.json +1 -1
package/commands/secid.js
CHANGED
|
@@ -1,30 +1,20 @@
|
|
|
1
|
-
const config = require('../lib/config');
|
|
2
|
-
const api = require('../lib/api');
|
|
3
1
|
const {handleError, createParameterError} = require('../lib/error');
|
|
4
2
|
const {output} = require('../lib/output');
|
|
3
|
+
const {getSecid} = require('../lib/utils');
|
|
5
4
|
|
|
6
|
-
module.exports = function(program) {
|
|
5
|
+
module.exports = function (program) {
|
|
7
6
|
program
|
|
8
7
|
.command('secid <code>')
|
|
9
|
-
.description(
|
|
10
|
-
|
|
8
|
+
.description(
|
|
9
|
+
'将各种股票代码格式转换为标准secid格式,支持以下格式:6位数字股票代码(000001)、sh/sz前缀(sh000001)、BK开头板块代码(BK0428)、纯数字板块代码(428)。返回标准secid格式,如1.600000(沪市)、0.000001(深市)、90.BK0428(板块)。可用于统一代码格式和K线数据查询。'
|
|
10
|
+
)
|
|
11
|
+
.action(async code => {
|
|
11
12
|
try {
|
|
12
|
-
const token = config.getToken();
|
|
13
|
-
if (!token) {
|
|
14
|
-
const error = new Error('未配置 API Token');
|
|
15
|
-
error.response = {status: 401};
|
|
16
|
-
throw error;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
13
|
if (!code) {
|
|
20
|
-
throw createParameterError(
|
|
21
|
-
'参数无效',
|
|
22
|
-
["参数 'code' 不能为空"],
|
|
23
|
-
['daxiapi secid 000001']
|
|
24
|
-
);
|
|
14
|
+
throw createParameterError('参数无效', ["参数 'code' 不能为空"], ['daxiapi secid 000001']);
|
|
25
15
|
}
|
|
26
16
|
|
|
27
|
-
const data =
|
|
17
|
+
const data = getSecid(code);
|
|
28
18
|
output(data);
|
|
29
19
|
} catch (error) {
|
|
30
20
|
handleError(error);
|
package/lib/api.js
CHANGED
|
@@ -99,11 +99,6 @@ async function getZdtPool(token, type = 'zt') {
|
|
|
99
99
|
return post(client, '/get_zdt_pool', {type});
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
-
async function getSecId(token, code) {
|
|
103
|
-
const client = createClient(token);
|
|
104
|
-
return post(client, '/get_sec_id', {code});
|
|
105
|
-
}
|
|
106
|
-
|
|
107
102
|
async function queryStockData(token, q, type = 'stock') {
|
|
108
103
|
const client = createClient(token);
|
|
109
104
|
return post(client, '/query_stock_data', {q, type});
|
|
@@ -123,6 +118,5 @@ module.exports = {
|
|
|
123
118
|
getGainianStock,
|
|
124
119
|
getKline,
|
|
125
120
|
getZdtPool,
|
|
126
|
-
getSecId,
|
|
127
121
|
queryStockData
|
|
128
122
|
};
|
package/lib/utils.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
function getSecid(code) {
|
|
2
|
+
code = String(code);
|
|
3
|
+
if (code.indexOf('.') !== -1) {
|
|
4
|
+
const parts = code.split('.');
|
|
5
|
+
const stockCode = parts[0];
|
|
6
|
+
const suffix = parts[1].toUpperCase();
|
|
7
|
+
if (suffix === 'SH') {
|
|
8
|
+
return `1.${stockCode}`;
|
|
9
|
+
} else if (suffix === 'SZ') {
|
|
10
|
+
return `0.${stockCode}`;
|
|
11
|
+
}
|
|
12
|
+
return code;
|
|
13
|
+
}
|
|
14
|
+
if (code[0] === 'B' && code[1] === 'K') {
|
|
15
|
+
return '90.' + code;
|
|
16
|
+
}
|
|
17
|
+
if (code.length < 6) {
|
|
18
|
+
return '90.BK' + ('0000' + code).slice(-4);
|
|
19
|
+
}
|
|
20
|
+
if (code[0] === '6' || code[0] === '5') {
|
|
21
|
+
return `1.${code}`;
|
|
22
|
+
}
|
|
23
|
+
if (code[0].toLowerCase() === 's') {
|
|
24
|
+
return code.replace(/^sh/i, '1.').replace(/^sz/i, '0.');
|
|
25
|
+
}
|
|
26
|
+
return `0.${code}`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
module.exports = {
|
|
30
|
+
getSecid
|
|
31
|
+
};
|