-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathjinrishici.js
37 lines (31 loc) · 1.12 KB
/
jinrishici.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
const https = require('https');
/**
* 获取今日诗词并格式化输出
* @returns {Promise<string>} 返回格式化后的诗词
*/
async function getTodayPoetry() {
return new Promise((resolve, reject) => {
const url = 'https://v2.jinrishici.com/one.json';
https.get(url, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
const result = JSON.parse(data);
if (result.status === "success") {
const poetryContent = result.data.content;
const author = result.data.origin.author;
const dynasty = result.data.origin.dynasty;
const formattedPoetry = `${poetryContent}——${author}(${dynasty})`;
resolve(formattedPoetry);
} else {
reject("Failed to fetch poetry");
}
});
}).on("error", (err) => {
reject("Error: " + err.message);
});
});
}
module.exports = getTodayPoetry;