这段代码是一个简单的天气预报应用程序。它使用和风天气 API 获取特定经纬度坐标位置的未来两小时降雨情况,并通过控制台输出相应的信息。
如果降雨概率大于 0,程序将调用 fetchData 函数,该函数将使用自定义的 API(通过 URL)向指定接收器发送有关降雨情况的信息。在此过程中,information 参数被传递给 fetchData 函数作为数据的一部分。
const axios = require('axios');
const location = '000.00,000.00'; // 请替换为您所在的经纬度坐标先经度后纬度
const key = '697ceb4d6d5641a893ec6******'; //请替换为您的和风天气 API Key
const url = `https://devapi.qweather.com/v7/minutely/5m?location=${location}&key=${key}`;
axios.get(url)
.then((response) => {
const data = response.data;
if (data.summary === "未来两小时无降水") {
console.log("现在不需要带伞" + data.summary + "现在无需推送");
} else {
console.log("建议带上雨伞" + data.summary + "现在赶紧推送" );
const information = data.summary;
fetchData(information); // 将 information 作为参数传递给 fetchData 函数
}
})
.catch((error) => {
console.error(error);
});
//这里使用bark推送 url和用户id自行替换
async function fetchData(information) { // 将 information 作为参数接收
const title = '下雨啦';
const config = {
method: 'get',
url: `https://url/用户id/${title}/${information}?icon=https://cdn.pixabay.com/photo/2016/03/31/18/14/icon-1294224_640.png`,
headers: {}
};
try {
const response = await axios(config);
console.log(JSON.stringify(response.data));
} catch (error) {
console.log(error);
}
}
0 */30 * * * *每30分钟运行一次