-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
83 lines (65 loc) · 1.93 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
const crypto = require('crypto');
const url = require('url');
const querystring = require('query-string');
const got = require('got');
const secret = 'XXX'; // 秘钥
const publicKey = 'XXX'; // 公钥
// 心知 V4 接口签名
function signature(urlString, paramsObj) {
if (!urlString) {
return;
}
const obj = url.parse(urlString, true, true);
const params = Object.assign({}, paramsObj, obj.query);
let result = querystring.stringify(params, { encode: false });
let encodeResult = querystring.stringify(params, { encode: true });
const sig = crypto
.createHmac('sha1', secret)
.update(result, 'utf8')
.digest('base64');
result += `&sig=${encodeURIComponent(sig)}`;
encodeResult += `&sig=${encodeURIComponent(sig)}`;
console.log('浏览器访问链接: ', `${obj.protocol}//${obj.host}${obj.pathname}?${encodeResult}`);
return result;
}
async function precipMinutely() {
const ts = Math.round(Date.now() / 1000);
const ttl = 600;
const latitude = 29.5617;
const longitude = 120.0962;
let url = 'https://api.seniverse.com/v4?fields=precip_minutely';
const query = signature(url, {
ttl, ts,
public_key: publicKey,
locations: `${latitude}:${longitude}`,
});
const rsp = await got.get(url, {
responseType: 'json',
searchParams: query
}).catch(err => {
console.log(err);
});
console.log('url', url);
console.log('query', query);
console.log('success:', JSON.stringify(rsp.body));
}
async function typhoonList() {
const ts = Math.round(Date.now() / 1000);
const ttl = 600;
let url = 'https://api.seniverse.com/v4?fields=typhoon_list';
const query = signature(url, {
ttl, ts,
public_key: publicKey,
year: 2021,
active: 1,
});
const rsp = await got.get(url, {
responseType: 'json',
searchParams: query
}).catch(err => {
console.log(err);
});
console.log('success:', JSON.stringify(rsp.body));
}
precipMinutely();
// typhoonList();