1
0
wiki/Obsidian/Static/jinrishici.js
2024-09-03 13:23:28 +00:00

38 lines
1.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;